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
rstam 15 years ago
parent
commit
ecb411f155
  1. 19
      Driver/GridFS/MongoGridFSFileInfo.cs
  2. 1
      DriverOnlineTests/DriverOnlineTests.csproj
  3. 86
      DriverOnlineTests/Jira/CSharp163Tests.cs

19
Driver/GridFS/MongoGridFSFileInfo.cs

@ -330,9 +330,10 @@ namespace MongoDB.Driver.GridFS {
md5 = null; md5 = null;
uploadDate = default(DateTime); uploadDate = default(DateTime);
} else { } else {
if (fileInfo.Contains("aliases")) {
var aliasesValue = fileInfo["aliases", null];
if (aliasesValue != null && !aliasesValue.IsBsonNull) {
var list = new List<string>(); var list = new List<string>();
foreach (var alias in fileInfo["aliases"].AsBsonArray) {
foreach (var alias in aliasesValue.AsBsonArray) {
list.Add(alias.AsString); list.Add(alias.AsString);
} }
aliases = list.ToArray(); aliases = list.ToArray();
@ -340,12 +341,22 @@ namespace MongoDB.Driver.GridFS {
aliases = null; aliases = null;
} }
chunkSize = fileInfo["chunkSize"].ToInt32(); chunkSize = fileInfo["chunkSize"].ToInt32();
contentType = (string) fileInfo["contentType", null];
var contentTypeValue = fileInfo["contentType", null];
if (contentTypeValue != null && !contentTypeValue.IsBsonNull) {
contentType = contentTypeValue.AsString;
} else {
contentType = null;
}
exists = true; exists = true;
id = fileInfo["_id"]; id = fileInfo["_id"];
length = fileInfo["length"].ToInt32(); length = fileInfo["length"].ToInt32();
md5 = (string) fileInfo["md5", null]; md5 = (string) fileInfo["md5", null];
metadata = (BsonDocument) fileInfo["metadata", null];
var metadataValue = fileInfo["metadata", null];
if (metadataValue != null && !metadataValue.IsBsonNull) {
metadata = metadataValue.AsBsonDocument;
} else {
metadata = null;
}
name = fileInfo["filename"].AsString; name = fileInfo["filename"].AsString;
uploadDate = fileInfo["uploadDate"].AsDateTime; uploadDate = fileInfo["uploadDate"].AsDateTime;
} }

1
DriverOnlineTests/DriverOnlineTests.csproj

@ -92,6 +92,7 @@
<Compile Include="Jira\CSharp112Tests.cs" /> <Compile Include="Jira\CSharp112Tests.cs" />
<Compile Include="Jira\CSharp130Tests.cs" /> <Compile Include="Jira\CSharp130Tests.cs" />
<Compile Include="Jira\CSharp134Tests.cs" /> <Compile Include="Jira\CSharp134Tests.cs" />
<Compile Include="Jira\CSharp163Tests.cs" />
<Compile Include="Jira\CSharp77Tests.cs" /> <Compile Include="Jira\CSharp77Tests.cs" />
<Compile Include="Jira\CSharp92Tests.cs" /> <Compile Include="Jira\CSharp92Tests.cs" />
<Compile Include="Jira\CSharp93Tests.cs" /> <Compile Include="Jira\CSharp93Tests.cs" />

86
DriverOnlineTests/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);
}
}
}
Loading…
Cancel
Save