Browse Source

Implemented CSHARP-307. Insert, InsertBatch and Save now throw a more appropriate exception when passed a null document.

pull/66/merge
rstam 14 years ago
parent
commit
f1554daa79
  1. 21
      Driver/Core/MongoCollection.cs
  2. 1
      DriverOnlineTests/DriverOnlineTests.csproj
  3. 68
      DriverOnlineTests/Jira/CSharp307Tests.cs

21
Driver/Core/MongoCollection.cs

@ -879,6 +879,9 @@ namespace MongoDB.Driver {
object document,
MongoInsertOptions options
) {
if (document == null) {
throw new ArgumentNullException("document");
}
var results = InsertBatch(nominalType, new object[] { document }, options);
return (results == null) ? null : results.Single();
}
@ -908,6 +911,9 @@ namespace MongoDB.Driver {
public virtual IEnumerable<SafeModeResult> InsertBatch<TNominalType>(
IEnumerable<TNominalType> documents
) {
if (documents == null) {
throw new ArgumentNullException("documents");
}
return InsertBatch(typeof(TNominalType), documents.Cast<object>());
}
@ -922,6 +928,9 @@ namespace MongoDB.Driver {
IEnumerable<TNominalType> documents,
MongoInsertOptions options
) {
if (documents == null) {
throw new ArgumentNullException("documents");
}
return InsertBatch(typeof(TNominalType), documents.Cast<object>(), options);
}
@ -936,6 +945,9 @@ namespace MongoDB.Driver {
IEnumerable<TNominalType> documents,
SafeMode safeMode
) {
if (documents == null) {
throw new ArgumentNullException("documents");
}
return InsertBatch(typeof(TNominalType), documents.Cast<object>(), safeMode);
}
@ -980,6 +992,9 @@ namespace MongoDB.Driver {
IEnumerable documents,
MongoInsertOptions options
) {
if (documents == null) {
throw new ArgumentNullException("documents");
}
var connection = server.AcquireConnection(database, false); // not slaveOk
try {
var safeMode = options.SafeMode;
@ -990,6 +1005,9 @@ namespace MongoDB.Driver {
message.WriteToBuffer(); // must be called before AddDocument
foreach (var document in documents) {
if (document == null) {
throw new ArgumentException("Batch contains one or more null documents.");
}
if (settings.AssignIdOnInsert) {
var serializer = BsonSerializer.LookupSerializer(document.GetType());
object id;
@ -1268,6 +1286,9 @@ namespace MongoDB.Driver {
object document,
MongoInsertOptions options
) {
if (document == null) {
throw new ArgumentNullException("document");
}
var serializer = BsonSerializer.LookupSerializer(document.GetType());
object id;
Type idNominalType;

1
DriverOnlineTests/DriverOnlineTests.csproj

@ -110,6 +110,7 @@
<Compile Include="Jira\CSharp269Tests.cs" />
<Compile Include="Jira\CSharp281Tests.cs" />
<Compile Include="Jira\CSharp282Tests.cs" />
<Compile Include="Jira\CSharp307Tests.cs" />
<Compile Include="Jira\CSharp77Tests.cs" />
<Compile Include="Jira\CSharp92Tests.cs" />
<Compile Include="Jira\CSharp93Tests.cs" />

68
DriverOnlineTests/Jira/CSharp307Tests.cs

@ -0,0 +1,68 @@
/* 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.Linq;
using System.Text;
using NUnit.Framework;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
namespace MongoDB.DriverOnlineTests.Jira {
[TestFixture]
public class CSharp307Tests {
private MongoServer server;
private MongoDatabase database;
private MongoCollection<BsonDocument> collection;
[TestFixtureSetUp]
public void TestFixtureSetup() {
server = MongoServer.Create("mongodb://localhost/?safe=true");
database = server["onlinetests"];
collection = database["testcollection"];
collection.Drop();
}
[Test]
public void TestInsertNullDocument() {
BsonDocument document = null;
var ex = Assert.Catch<ArgumentNullException>(() => collection.Insert(document));
Assert.AreEqual("document", ex.ParamName);
}
[Test]
public void TestInsertNullBatch() {
BsonDocument[] batch = null;
var ex = Assert.Catch<ArgumentNullException>(() => collection.InsertBatch(batch));
Assert.AreEqual("documents", ex.ParamName);
}
[Test]
public void TestInsertBatchWithNullDocument() {
BsonDocument[] batch = new BsonDocument[] { null };
Assert.Throws<ArgumentException>(() => collection.InsertBatch(batch), "Batch contains one or more null documents.");
}
[Test]
public void TestSaveNullDocument() {
BsonDocument document = null;
var ex = Assert.Catch<ArgumentNullException>(() => collection.Save(document));
Assert.AreEqual("document", ex.ParamName);
}
}
}
Loading…
Cancel
Save