From 8e9d342487815c7b9f685083ce971237ab868a1f Mon Sep 17 00:00:00 2001 From: rstam Date: Wed, 20 Jul 2011 22:38:39 -0400 Subject: [PATCH] Implemented CSHARP-270. Added more information to a few of the error messages in exceptions thrown by BsonClassMapSerializer. --- Bson/Serialization/BsonClassMapSerializer.cs | 11 +-- BsonUnitTests/BsonUnitTests.csproj | 1 + BsonUnitTests/Jira/CSharp270Tests.cs | 75 ++++++++++++++++++++ 3 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 BsonUnitTests/Jira/CSharp270Tests.cs diff --git a/Bson/Serialization/BsonClassMapSerializer.cs b/Bson/Serialization/BsonClassMapSerializer.cs index e639bdcfb2..0bedd63234 100755 --- a/Bson/Serialization/BsonClassMapSerializer.cs +++ b/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); } } diff --git a/BsonUnitTests/BsonUnitTests.csproj b/BsonUnitTests/BsonUnitTests.csproj index 4d519a2faa..fd1670af66 100644 --- a/BsonUnitTests/BsonUnitTests.csproj +++ b/BsonUnitTests/BsonUnitTests.csproj @@ -125,6 +125,7 @@ + diff --git a/BsonUnitTests/Jira/CSharp270Tests.cs b/BsonUnitTests/Jira/CSharp270Tests.cs new file mode 100644 index 0000000000..c513b8e6f0 --- /dev/null +++ b/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(() => { BsonSerializer.Deserialize(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(() => { BsonSerializer.Deserialize(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(() => { BsonSerializer.Deserialize(document); }); + Assert.AreEqual(message, ex.Message); + } + } +}