Browse Source

Implemented CSHARP-270. Added more information to a few of the error messages in exceptions thrown by BsonClassMapSerializer.

pull/63/head
rstam 14 years ago
parent
commit
8e9d342487
  1. 11
      Bson/Serialization/BsonClassMapSerializer.cs
  2. 1
      BsonUnitTests/BsonUnitTests.csproj
  3. 75
      BsonUnitTests/Jira/CSharp270Tests.cs

11
Bson/Serialization/BsonClassMapSerializer.cs

@ -108,7 +108,7 @@ namespace MongoDB.Bson.Serialization {
var classMap = BsonClassMap.LookupClassMap(actualType);
if (classMap.IsAnonymous) {
throw new InvalidOperationException("Anonymous class cannot be deserialized.");
throw new InvalidOperationException("An anonymous class cannot be deserialized.");
}
var obj = classMap.CreateInstance();
@ -132,7 +132,7 @@ namespace MongoDB.Bson.Serialization {
} else if (classMap.IgnoreExtraElements) {
bsonReader.SkipValue();
} else {
string message = string.Format("Unexpected element '{0}'.", elementName);
var message = string.Format("Element '{0}' does not match any field or property of class {1}.", elementName, classMap.ClassType.FullName);
throw new FileFormatException(message);
}
}
@ -141,7 +141,8 @@ namespace MongoDB.Bson.Serialization {
foreach (var memberMap in missingElementMemberMaps) {
if (memberMap.IsRequired) {
var message = string.Format("Required element '{0}' is missing.", memberMap.ElementName);
var fieldOrProperty = (memberMap.MemberInfo.MemberType == MemberTypes.Field) ? "field" : "property";
var message = string.Format("Required element '{0}' for {1} '{2}' of class {3} is missing.", memberMap.ElementName, fieldOrProperty, memberMap.MemberName, classMap.ClassType.FullName);
throw new FileFormatException(message);
}
@ -257,7 +258,7 @@ namespace MongoDB.Bson.Serialization {
) {
var documentType = document.GetType();
if (documentType.IsValueType) {
var message = string.Format("SetDocumentId cannot be used with value type '{0}'.", documentType.FullName);
var message = string.Format("SetDocumentId cannot be used with value type {0}.", documentType.FullName);
throw new BsonSerializationException(message);
}
@ -266,7 +267,7 @@ namespace MongoDB.Bson.Serialization {
if (idMemberMap != null) {
idMemberMap.Setter(document, id);
} else {
var message = string.Format("Class {0} has no Id member.", document.GetType());
var message = string.Format("Class {0} has no Id member.", document.GetType().FullName);
throw new InvalidOperationException(message);
}
}

1
BsonUnitTests/BsonUnitTests.csproj

@ -125,6 +125,7 @@
<Compile Include="Jira\CSharp239Tests.cs" />
<Compile Include="Jira\CSharp260Tests.cs" />
<Compile Include="Jira\CSharp261Tests.cs" />
<Compile Include="Jira\CSharp270Tests.cs" />
<Compile Include="Jira\CSharp70Tests.cs" />
<Compile Include="Jira\CSharp71Tests.cs" />
<Compile Include="Jira\CSharp74Tests.cs" />

75
BsonUnitTests/Jira/CSharp270Tests.cs

@ -0,0 +1,75 @@
/* 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;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using NUnit.Framework;
using MongoDB.Bson;
using MongoDB.Bson.IO;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
namespace MongoDB.BsonUnitTests.Jira.CSharp270 {
public class C {
public ObjectId Id;
[BsonRequired]
[BsonElement("field")]
public int Field;
[BsonRequired]
[BsonElement("property")]
public int Property { get; set; }
}
[TestFixture]
public class CSharp270Tests {
[Test]
public void TestBogusElement() {
var document = new BsonDocument("bogus", 0);
var message = "Element 'bogus' does not match any field or property of class MongoDB.BsonUnitTests.Jira.CSharp270.C.";
var ex = Assert.Throws<FileFormatException>(() => { BsonSerializer.Deserialize<C>(document); });
Assert.AreEqual(message, ex.Message);
}
[Test]
public void TestMissingElementForField() {
var document = new BsonDocument {
{ "_id", ObjectId.GenerateNewId() },
{ "property", 0 }
};
var message = "Required element 'field' for field 'Field' of class MongoDB.BsonUnitTests.Jira.CSharp270.C is missing.";
var ex = Assert.Throws<FileFormatException>(() => { BsonSerializer.Deserialize<C>(document); });
Assert.AreEqual(message, ex.Message);
}
[Test]
public void TestMissingElementForProperty() {
var document = new BsonDocument {
{ "_id", ObjectId.GenerateNewId() },
{ "field", 0 }
};
var message = "Required element 'property' for property 'Property' of class MongoDB.BsonUnitTests.Jira.CSharp270.C is missing.";
var ex = Assert.Throws<FileFormatException>(() => { BsonSerializer.Deserialize<C>(document); });
Assert.AreEqual(message, ex.Message);
}
}
}
Loading…
Cancel
Save