Browse Source

Implemented BsonDefaultValue, BsonDiscriminator, BsonElement, BsonId, BsonIgnore, BsonIgnoreIfNull, BsonRequired and BsonUseCompactRepresentation attributes and modified AutoMap to handle them (still need to modify BsonClassMapSerializer).

pull/1/head
rstam 15 years ago
parent
commit
f8ce7f6ad0
  1. 7
      BsonLibrary/BsonLibrary.csproj
  2. 48
      BsonLibrary/Serialization/Attributes/BsonDefaultValueAttribute.cs
  3. 48
      BsonLibrary/Serialization/Attributes/BsonDiscriminatorAttribute.cs
  4. 48
      BsonLibrary/Serialization/Attributes/BsonElementAttribute.cs
  5. 25
      BsonLibrary/Serialization/Attributes/BsonIdAttribute.cs
  6. 25
      BsonLibrary/Serialization/Attributes/BsonIgnoreAttribute.cs
  7. 25
      BsonLibrary/Serialization/Attributes/BsonIgnoreIfNullAttribute.cs
  8. 25
      BsonLibrary/Serialization/Attributes/BsonRequiredAttribute.cs
  9. 22
      BsonLibrary/Serialization/Attributes/BsonUseCompactRepresentationAttribute.cs
  10. 109
      BsonLibrary/Serialization/BsonClassMap.cs
  11. 44
      BsonLibrary/Serialization/BsonPropertyMap.cs
  12. 174
      BsonLibraryUnitTests/Attributes/BsonAttributeTests.cs
  13. 1
      BsonLibraryUnitTests/BsonLibraryUnitTests.csproj

7
BsonLibrary/BsonLibrary.csproj

@ -55,6 +55,13 @@
<Compile Include="Exceptions\BsonInternalException.cs" />
<Compile Include="Exceptions\BsonSerializationException.cs" />
<Compile Include="ObjectModel\IConvertibleToBsonDocument.cs" />
<Compile Include="Serialization\Attributes\BsonDefaultValueAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonDiscriminatorAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonElementAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonIdAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonIgnoreAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonIgnoreIfNullAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonRequiredAttribute.cs" />
<Compile Include="Serialization\Attributes\BsonUseCompactRepresentationAttribute.cs" />
<Compile Include="Serialization\BsonClassMap.cs" />
<Compile Include="Serialization\BsonClassMapSerializer.cs" />

48
BsonLibrary/Serialization/Attributes/BsonDefaultValueAttribute.cs

@ -0,0 +1,48 @@
/* Copyright 2010 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;
namespace MongoDB.BsonLibrary.Serialization {
[AttributeUsage(AttributeTargets.Property)]
public class BsonDefaultValueAttribute : Attribute {
#region private fields
private object defaultValue;
private bool serializeDefaultValue = true;
#endregion
#region constructors
public BsonDefaultValueAttribute(
object defaultValue
) {
this.defaultValue = defaultValue;
}
#endregion
#region public properties
public object DefaultValue {
get { return defaultValue; }
}
public bool SerializeDefaultValue {
get { return serializeDefaultValue; }
set { serializeDefaultValue = value; }
}
#endregion
}
}

48
BsonLibrary/Serialization/Attributes/BsonDiscriminatorAttribute.cs

@ -0,0 +1,48 @@
/* Copyright 2010 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;
namespace MongoDB.BsonLibrary.Serialization {
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)]
public class BsonDiscriminatorAttribute : Attribute {
#region private fields
private string discriminator;
private bool required;
#endregion
#region constructors
public BsonDiscriminatorAttribute(
string discriminator
) {
this.discriminator = discriminator;
}
#endregion
#region public properties
public string Discriminator {
get { return discriminator; }
}
public bool Required {
get { return required; }
set { required = value; }
}
#endregion
}
}

48
BsonLibrary/Serialization/Attributes/BsonElementAttribute.cs

@ -0,0 +1,48 @@
/* Copyright 2010 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;
namespace MongoDB.BsonLibrary.Serialization {
[AttributeUsage(AttributeTargets.Property)]
public class BsonElementAttribute : Attribute {
#region private fields
private string elementName;
private int order = int.MaxValue;
#endregion
#region constructors
public BsonElementAttribute(
string elementName
) {
this.elementName = elementName;
}
#endregion
#region public properties
public string ElementName {
get { return elementName; }
}
public int Order {
get { return order; }
set { order = value; }
}
#endregion
}
}

25
BsonLibrary/Serialization/Attributes/BsonIdAttribute.cs

@ -0,0 +1,25 @@
/* Copyright 2010 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;
namespace MongoDB.BsonLibrary.Serialization {
[AttributeUsage(AttributeTargets.Property)]
public class BsonIdAttribute : Attribute {
}
}

25
BsonLibrary/Serialization/Attributes/BsonIgnoreAttribute.cs

@ -0,0 +1,25 @@
/* Copyright 2010 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;
namespace MongoDB.BsonLibrary.Serialization {
[AttributeUsage(AttributeTargets.Property)]
public class BsonIgnoreAttribute : Attribute {
}
}

25
BsonLibrary/Serialization/Attributes/BsonIgnoreIfNullAttribute.cs

@ -0,0 +1,25 @@
/* Copyright 2010 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;
namespace MongoDB.BsonLibrary.Serialization {
[AttributeUsage(AttributeTargets.Property)]
public class BsonIgnoreIfNullAttribute : Attribute {
}
}

25
BsonLibrary/Serialization/Attributes/BsonRequiredAttribute.cs

@ -0,0 +1,25 @@
/* Copyright 2010 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;
namespace MongoDB.BsonLibrary.Serialization {
[AttributeUsage(AttributeTargets.Property)]
public class BsonRequiredAttribute : Attribute {
}
}

22
BsonLibrary/Serialization/Attributes/BsonUseCompactRepresentationAttribute.cs

@ -19,7 +19,27 @@ using System.Linq;
using System.Text;
namespace MongoDB.BsonLibrary.Serialization {
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property)]
public class BsonUseCompactRepresentationAttribute : Attribute {
#region private fields
private bool useCompactRepresentation;
#endregion
#region constructors
public BsonUseCompactRepresentationAttribute() : this(true) {
}
public BsonUseCompactRepresentationAttribute(
bool useCompactRepresentation
) {
this.useCompactRepresentation = useCompactRepresentation;
}
#endregion
#region public properties
public bool UseCompactRepresentation {
get { return useCompactRepresentation; }
}
#endregion
}
}

109
BsonLibrary/Serialization/BsonClassMap.cs

@ -38,15 +38,16 @@ namespace MongoDB.BsonLibrary.Serialization {
protected BsonClassMap baseClassMap; // null for class object and interfaces
protected Type classType;
protected string discriminator;
protected bool discriminatorIsRequired;
protected bool isAnonymous;
protected string collectionName;
protected BsonPropertyMap idPropertyMap;
protected List<BsonPropertyMap> propertyMaps = new List<BsonPropertyMap>();
protected bool useCompactRepresentation;
#endregion
#region static constructor
static BsonClassMap() {
RegisterPropertySerializers();
AutoRegisterPropertySerializers();
}
#endregion
@ -76,12 +77,12 @@ namespace MongoDB.BsonLibrary.Serialization {
get { return discriminator; }
}
public bool IsAnonymous {
get { return isAnonymous; }
public bool DiscriminatorIsRequired {
get { return discriminatorIsRequired; }
}
public string CollectionName {
get { return collectionName; }
public bool IsAnonymous {
get { return isAnonymous; }
}
public BsonPropertyMap IdPropertyMap {
@ -109,6 +110,10 @@ namespace MongoDB.BsonLibrary.Serialization {
}
}
}
public bool UseCompactRepresentation {
get { return useCompactRepresentation; }
}
#endregion
#region public static methods
@ -294,7 +299,7 @@ namespace MongoDB.BsonLibrary.Serialization {
#region private static methods
// automatically register all property serializers found in the BsonLibrary
private static void RegisterPropertySerializers() {
private static void AutoRegisterPropertySerializers() {
var assembly = Assembly.GetExecutingAssembly();
foreach (var type in assembly.GetTypes()) {
if (typeof(IBsonPropertySerializer).IsAssignableFrom(type) && type != typeof(IBsonPropertySerializer)) {
@ -309,10 +314,27 @@ namespace MongoDB.BsonLibrary.Serialization {
#region public methods
public void AutoMap() {
var discriminatorAttribute = (BsonDiscriminatorAttribute) classType.GetCustomAttributes(typeof(BsonDiscriminatorAttribute), false).FirstOrDefault();
if (discriminatorAttribute != null) {
discriminator = discriminatorAttribute.Discriminator;
discriminatorIsRequired = discriminatorAttribute.Required;
}
var useCompactRepresentationAttribute = (BsonUseCompactRepresentationAttribute) classType.GetCustomAttributes(typeof(BsonUseCompactRepresentationAttribute), false).FirstOrDefault();
if (useCompactRepresentationAttribute != null) {
useCompactRepresentation = useCompactRepresentationAttribute.UseCompactRepresentation;
}
// only auto map properties declared in this class (and not in base classes)
var hasOrderedElements = false;
var bindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly;
foreach (var propertyInfo in classType.GetProperties(bindingFlags)) {
if (propertyInfo.CanRead && (propertyInfo.CanWrite || isAnonymous)) {
var ignoreAttribute = (BsonIgnoreAttribute) propertyInfo.GetCustomAttributes(typeof(BsonIgnoreAttribute), false).FirstOrDefault();
if (ignoreAttribute != null) {
continue; // ignore this property
}
var genericMapPropertyInfo = this.GetType().GetMethod(
"MapProperty", // name
BindingFlags.NonPublic | BindingFlags.Instance,
@ -323,17 +345,72 @@ namespace MongoDB.BsonLibrary.Serialization {
var mapPropertyInfo = genericMapPropertyInfo.MakeGenericMethod(propertyInfo.PropertyType);
var elementName = propertyInfo.Name;
var order = int.MaxValue;
var idAttribute = (BsonIdAttribute) propertyInfo.GetCustomAttributes(typeof(BsonIdAttribute), false).FirstOrDefault();
if (idAttribute != null) {
elementName = "_id"; // if BsonIdAttribute is present ignore BsonElementAttribute
} else {
var elementAttribute = (BsonElementAttribute) propertyInfo.GetCustomAttributes(typeof(BsonElementAttribute), false).FirstOrDefault();
if (elementAttribute != null) {
elementName = elementAttribute.ElementName;
order = elementAttribute.Order;
}
}
var propertyMap = (BsonPropertyMap) mapPropertyInfo.Invoke(this, new object[] { propertyInfo, elementName });
if (order != int.MaxValue) {
propertyMap.SetOrder(order);
hasOrderedElements = true;
}
if (idAttribute != null) {
idPropertyMap = propertyMap;
}
var defaultValueAttribute = (BsonDefaultValueAttribute) propertyInfo.GetCustomAttributes(typeof(BsonDefaultValueAttribute), false).FirstOrDefault();
if (defaultValueAttribute != null) {
propertyMap.SetDefaultValue(defaultValueAttribute.DefaultValue);
propertyMap.SetSerializeDefaultValue(defaultValueAttribute.SerializeDefaultValue);
}
if (propertyInfo.GetCustomAttributes(typeof(BsonUseCompactRepresentationAttribute), false).Length != 0) {
propertyMap.SetUseCompactRepresentation(true);
var ignoreIfNullAttribute = (BsonIgnoreIfNullAttribute) propertyInfo.GetCustomAttributes(typeof(BsonIgnoreIfNullAttribute), false).FirstOrDefault();
if (ignoreIfNullAttribute != null) {
propertyMap.SetIgnoreIfNull(true);
}
var requiredAttribute = (BsonRequiredAttribute) propertyInfo.GetCustomAttributes(typeof(BsonRequiredAttribute), false).FirstOrDefault();
if (requiredAttribute != null) {
propertyMap.SetIsRequired(true);
}
propertyMap.SetUseCompactRepresentation(useCompactRepresentation);
useCompactRepresentationAttribute = (BsonUseCompactRepresentationAttribute) propertyInfo.GetCustomAttributes(typeof(BsonUseCompactRepresentationAttribute), false).FirstOrDefault();
if (useCompactRepresentationAttribute != null) {
propertyMap.SetUseCompactRepresentation(useCompactRepresentationAttribute.UseCompactRepresentation);
}
}
}
if (hasOrderedElements) {
// split out the items with a value for Order and sort them separately (because Sort is unstable, see online help)
// and then concatenate any items with no value for Order at the end (in their original order)
var ordered = new List<BsonPropertyMap>(propertyMaps.Where(pm => pm.Order != int.MaxValue));
ordered.Sort((x, y) => x.Order.CompareTo(y.Order));
propertyMaps = new List<BsonPropertyMap>(ordered.Concat(propertyMaps.Where(pm => pm.Order == int.MaxValue)));
}
if (idPropertyMap == null) {
idPropertyMap = propertyMaps.Where(pm => pm.PropertyName.EndsWith("Id")).FirstOrDefault();
}
RegisterDiscriminator(classType, discriminator);
}
public BsonPropertyMap GetPropertyMap(
string propertyName
) {
return PropertyMaps.FirstOrDefault(pm => pm.PropertyName == propertyName);
}
public BsonPropertyMap GetPropertyMapForElement(
string elementName
) {
@ -346,6 +423,20 @@ namespace MongoDB.BsonLibrary.Serialization {
this.discriminator = discriminator;
return this;
}
public BsonClassMap SetRequireDiscriminator(
bool requireDiscriminator
) {
this.discriminatorIsRequired = requireDiscriminator;
return this;
}
public BsonClassMap SetUseCompactRepresentation(
bool useCompactRepresentation
) {
this.useCompactRepresentation = useCompactRepresentation;
return this;
}
#endregion
#region private methods

44
BsonLibrary/Serialization/BsonPropertyMap.cs

@ -26,6 +26,7 @@ namespace MongoDB.BsonLibrary.Serialization {
#region protected fields
protected string propertyName;
protected string elementName;
protected int order = int.MaxValue;
protected PropertyInfo propertyInfo;
protected Func<object, object> getter;
protected Action<object, object> setter;
@ -34,6 +35,8 @@ namespace MongoDB.BsonLibrary.Serialization {
protected bool isPolymorphicProperty;
protected bool isRequired;
protected bool hasDefaultValue;
protected bool serializeDefaultValue = true;
protected bool ignoreIfNull;
protected object defaultValue;
#endregion
@ -58,6 +61,10 @@ namespace MongoDB.BsonLibrary.Serialization {
get { return elementName; }
}
public int Order {
get { return order; }
}
public PropertyInfo PropertyInfo {
get { return propertyInfo; }
}
@ -95,6 +102,14 @@ namespace MongoDB.BsonLibrary.Serialization {
get { return hasDefaultValue; }
}
public bool SerializeDefaultValue {
get { return serializeDefaultValue; }
}
public bool IgnoreIfNull {
get { return ignoreIfNull; }
}
public object DefaultValue {
get { return defaultValue; }
}
@ -124,12 +139,27 @@ namespace MongoDB.BsonLibrary.Serialization {
public BsonPropertyMap SetDefaultValue(
object defaultValue
) {
return SetDefaultValue(defaultValue, true); // serializeDefaultValue
}
public BsonPropertyMap SetDefaultValue(
object defaultValue,
bool serializeDefaultValue
) {
this.hasDefaultValue = true;
this.serializeDefaultValue = serializeDefaultValue;
this.defaultValue = defaultValue;
return this;
}
public BsonPropertyMap SetIgnoreIfNull(
bool ignoreIfNull
) {
this.ignoreIfNull = ignoreIfNull;
return this;
}
public BsonPropertyMap SetIsRequired(
bool isRequired
) {
@ -137,6 +167,13 @@ namespace MongoDB.BsonLibrary.Serialization {
return this;
}
public BsonPropertyMap SetOrder(
int order
) {
this.order = order;
return this;
}
public BsonPropertyMap SetPropertySerializer(
IBsonPropertySerializer propertySerializer
) {
@ -144,6 +181,13 @@ namespace MongoDB.BsonLibrary.Serialization {
return this;
}
public BsonPropertyMap SetSerializeDefaultValue(
bool serializeDefaultValue
) {
this.serializeDefaultValue = serializeDefaultValue;
return this;
}
public BsonPropertyMap SetUseCompactRepresentation(
bool useCompactRepresentation
) {

174
BsonLibraryUnitTests/Attributes/BsonAttributeTests.cs

@ -0,0 +1,174 @@
/* Copyright 2010 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.BsonLibrary;
using MongoDB.BsonLibrary.Serialization;
namespace MongoDB.BsonLibrary.UnitTests.ObjectModel {
[TestFixture]
public class BsonAttributeTests {
[BsonDiscriminator("discriminator", Required=true)]
[BsonUseCompactRepresentation]
public class Test {
[BsonDefaultValue("default1")]
public string SerializedDefaultValue1 { get; set; }
[BsonDefaultValue("default2", SerializeDefaultValue=true)]
public string SerializedDefaultValue2 { get; set; }
[BsonDefaultValue("default3", SerializeDefaultValue=false)]
public string NotSerializedDefaultValue { get; set; }
public string NoDefaultValue { get; set; }
[BsonUseCompactRepresentation(false)]
public string NotCompact { get; set; }
public string Compact { get; set; }
[BsonId]
public ObjectId IsId { get; set; }
public ObjectId IsNotId { get; set; }
[BsonIgnore]
public string Ignored { get; set; }
public string NotIgnored { get; set; }
[BsonIgnoreIfNull]
public string IgnoredIfNull { get; set; }
public string NotIgnoredIfNull { get; set; }
[BsonRequired]
public string Required { get; set; }
public string NotRequired { get; set; }
[BsonElement("notordered")]
public string NotOrdered { get; set; }
[BsonElement("ordered", Order=1)]
public string Ordered { get; set; }
public string NoElement { get; set; }
}
[Test]
public void TestDiscriminator() {
var classMap = BsonClassMap.LookupClassMap(typeof(Test));
Assert.AreEqual("discriminator", classMap.Discriminator);
Assert.AreEqual(true, classMap.DiscriminatorIsRequired);
}
[Test]
public void TestDefaultValue() {
var classMap = BsonClassMap.LookupClassMap(typeof(Test));
var serializedDefaultValue1 = classMap.GetPropertyMap("SerializedDefaultValue1");
Assert.AreEqual(true, serializedDefaultValue1.HasDefaultValue);
Assert.AreEqual(true, serializedDefaultValue1.SerializeDefaultValue);
Assert.AreEqual("default1", serializedDefaultValue1.DefaultValue);
var serializedDefaultValue2 = classMap.GetPropertyMap("SerializedDefaultValue2");
Assert.AreEqual(true, serializedDefaultValue2.HasDefaultValue);
Assert.AreEqual(true, serializedDefaultValue2.SerializeDefaultValue);
Assert.AreEqual("default2", serializedDefaultValue2.DefaultValue);
var notSerializedDefaultValue = classMap.GetPropertyMap("NotSerializedDefaultValue");
Assert.AreEqual(true, notSerializedDefaultValue.HasDefaultValue);
Assert.AreEqual(false, notSerializedDefaultValue.SerializeDefaultValue);
Assert.AreEqual("default3", notSerializedDefaultValue.DefaultValue);
var noDefaultValue = classMap.GetPropertyMap("NoDefaultValue");
Assert.AreEqual(false, noDefaultValue.HasDefaultValue);
Assert.AreEqual(true, noDefaultValue.SerializeDefaultValue);
Assert.IsNull(noDefaultValue.DefaultValue);
}
[Test]
public void TestUseCompactRepresentation() {
var classMap = BsonClassMap.LookupClassMap(typeof(Test));
Assert.AreEqual(true, classMap.UseCompactRepresentation);
var notCompact = classMap.GetPropertyMap("NotCompact");
Assert.AreEqual(false, notCompact.UseCompactRepresentation);
var compact = classMap.GetPropertyMap("Compact");
Assert.AreEqual(true, compact.UseCompactRepresentation);
}
[Test]
public void TestId() {
var classMap = BsonClassMap.LookupClassMap(typeof(Test));
var isId = classMap.GetPropertyMap("IsId");
Assert.AreEqual("_id", isId.ElementName);
Assert.AreSame(classMap.IdPropertyMap, isId);
var isNotId = classMap.GetPropertyMap("IsNotId");
Assert.AreEqual("IsNotId", isNotId.ElementName);
Assert.AreNotSame(classMap.IdPropertyMap, isNotId);
}
[Test]
public void TestIgnored() {
var classMap = BsonClassMap.LookupClassMap(typeof(Test));
var ignored = classMap.GetPropertyMap("Ignored");
Assert.IsNull(ignored);
var notIgnored = classMap.GetPropertyMap("NotIgnored");
Assert.IsNotNull(notIgnored);
}
[Test]
public void TestIgnoredIfNull() {
var classMap = BsonClassMap.LookupClassMap(typeof(Test));
var ignoredIfNull = classMap.GetPropertyMap("IgnoredIfNull");
Assert.AreEqual(true, ignoredIfNull.IgnoreIfNull);
var notIgnoredIfNull = classMap.GetPropertyMap("NotIgnoredIfNull");
Assert.AreEqual(false, notIgnoredIfNull.IgnoreIfNull);
}
[Test]
public void TestRequired() {
var classMap = BsonClassMap.LookupClassMap(typeof(Test));
var required = classMap.GetPropertyMap("Required");
Assert.AreEqual(true, required.IsRequired);
var notRequired = classMap.GetPropertyMap("NotRequired");
Assert.AreEqual(false, notRequired.IsRequired);
}
[Test]
public void TestElement() {
var classMap = BsonClassMap.LookupClassMap(typeof(Test));
var notOrdered = classMap.GetPropertyMap("NotOrdered");
Assert.AreEqual("notordered", notOrdered.ElementName);
Assert.AreEqual(int.MaxValue, notOrdered.Order);
var ordered = classMap.GetPropertyMap("Ordered");
Assert.AreEqual("ordered", ordered.ElementName);
Assert.AreEqual(1, ordered.Order);
Assert.AreSame(classMap.PropertyMaps.First(), ordered);
var noElement = classMap.GetPropertyMap("NoElement");
Assert.AreEqual("NoElement", noElement.ElementName);
Assert.AreEqual(int.MaxValue, noElement.Order);
}
}
}

1
BsonLibraryUnitTests/BsonLibraryUnitTests.csproj

@ -46,6 +46,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Attributes\BsonAttributeTests.cs" />
<Compile Include="BsonDocumentTests.cs" />
<Compile Include="BsonElementTests.cs" />
<Compile Include="BsonEqualsTests.cs" />

Loading…
Cancel
Save