Browse Source
Fixed CSHARP-163. Allow BSON null values for aliases, contentType and metadata fields for GridFS files to interoperate with other drivers.
pull/41/head
Fixed CSHARP-163. Allow BSON null values for aliases, contentType and metadata fields for GridFS files to interoperate with other drivers.
pull/41/head

3 changed files with 102 additions and 4 deletions
-
19Driver/GridFS/MongoGridFSFileInfo.cs
-
1DriverOnlineTests/DriverOnlineTests.csproj
-
86DriverOnlineTests/Jira/CSharp163Tests.cs
@ -0,0 +1,86 @@ |
|||||
|
/* Copyright 2010-2011 10gen Inc. |
||||
|
* |
||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
|
* you may not use this file except in compliance with the License. |
||||
|
* You may obtain a copy of the License at |
||||
|
* |
||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
* |
||||
|
* Unless required by applicable law or agreed to in writing, software |
||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
|
* See the License for the specific language governing permissions and |
||||
|
* limitations under the License. |
||||
|
*/ |
||||
|
|
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.IO; |
||||
|
using System.Linq; |
||||
|
using System.Runtime.Serialization; |
||||
|
using System.Text; |
||||
|
using NUnit.Framework; |
||||
|
|
||||
|
using MongoDB.Bson; |
||||
|
using MongoDB.Bson.DefaultSerializer; |
||||
|
using MongoDB.Bson.Serialization; |
||||
|
using MongoDB.Driver; |
||||
|
using MongoDB.Driver.Builders; |
||||
|
|
||||
|
namespace MongoDB.DriverOnlineTests.Jira.CSharp163 { |
||||
|
[TestFixture] |
||||
|
public class CSharp163Tests { |
||||
|
private MongoServer server; |
||||
|
private MongoDatabase database; |
||||
|
|
||||
|
[TestFixtureSetUp] |
||||
|
public void TestFixtureSetup() { |
||||
|
server = MongoServer.Create("mongodb://localhost/?safe=true"); |
||||
|
database = server["onlinetests"]; |
||||
|
} |
||||
|
|
||||
|
[Test] |
||||
|
public void TestNullAliasesAndContentType() { |
||||
|
database.GridFS.Files.RemoveAll(); |
||||
|
database.GridFS.Chunks.RemoveAll(); |
||||
|
|
||||
|
var text = "Hello World"; |
||||
|
var bytes = Encoding.UTF8.GetBytes(text); |
||||
|
var stream = new MemoryStream(bytes); |
||||
|
var fileInfo = database.GridFS.Upload(stream, "Hello World.txt"); |
||||
|
Assert.IsNull(fileInfo.Aliases); |
||||
|
Assert.IsNull(fileInfo.ContentType); |
||||
|
|
||||
|
var query = Query.EQ("_id", fileInfo.Id); |
||||
|
var files = database.GridFS.Files.FindOne(query); |
||||
|
Assert.IsFalse(files.Contains("aliases")); |
||||
|
Assert.IsFalse(files.Contains("contentType")); |
||||
|
Assert.IsFalse(files.Contains("metadata")); |
||||
|
|
||||
|
// simulate null values as stored by other drivers
|
||||
|
var update = Update |
||||
|
.Set("aliases", BsonNull.Value) |
||||
|
.Set("contentType", BsonNull.Value) |
||||
|
.Set("metadata", BsonNull.Value); |
||||
|
database.GridFS.Files.Update(query, update); |
||||
|
|
||||
|
var fileInfo2 = database.GridFS.FindOne(query); |
||||
|
Assert.IsNull(fileInfo2.Aliases); |
||||
|
Assert.IsNull(fileInfo2.ContentType); |
||||
|
Assert.IsNull(fileInfo2.Metadata); |
||||
|
|
||||
|
// test that non-null values still work
|
||||
|
var aliases = new[] { "a", "b", "c" }; |
||||
|
var contentType = "text/plain"; |
||||
|
var metadata = new BsonDocument { { "x", 1 }, { "y", 2 } }; |
||||
|
database.GridFS.SetAliases(fileInfo, aliases); |
||||
|
database.GridFS.SetContentType(fileInfo, contentType); |
||||
|
database.GridFS.SetMetadata(fileInfo, metadata); |
||||
|
|
||||
|
var fileInfo3 = database.GridFS.FindOne(query); |
||||
|
Assert.IsTrue(aliases.SequenceEqual(fileInfo3.Aliases)); |
||||
|
Assert.AreEqual(contentType, fileInfo3.ContentType); |
||||
|
Assert.AreEqual(metadata, fileInfo3.Metadata); |
||||
|
} |
||||
|
} |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue