Permalink
Browse files

Add CollectionMemberMapTests to check if they correctly handle get an…

…d set null value.
  • Loading branch information...
1 parent 9b7d930 commit 52bda60a6f420266f2f4357ddf50d869c6282ff6 @lanwin lanwin committed Sep 28, 2010
@@ -97,6 +97,7 @@
<Compile Include="UnitTests\Bson\BsonTestBase.cs" />
<Compile Include="UnitTests\Bson\TestBsonBinary.cs" />
<Compile Include="UnitTests\Bson\TestRoundTrips.cs" />
+ <Compile Include="UnitTests\Configuration\Mapping\Model\CollectionMemberMapTests.cs" />
<Compile Include="UnitTests\Configuration\MongoConfigurationBuilderTests.cs" />
<Compile Include="IntegrationTests\Connections\TestConnection.cs" />
<Compile Include="IntegrationTests\Connections\TestConnectionFactory.cs" />
@@ -0,0 +1,33 @@
+using System;
+using System.Collections.Generic;
+using MongoDB.Configuration.Mapping.Model;
+using NUnit.Framework;
+
+namespace MongoDB.UnitTests.Configuration.Mapping.Model
+{
+ public class CollectionMemberMapTests
+ {
+ [Test]
+ public void SetValue_Null_ReturnsNullAndSkipCollectionAdapterCall()
+ {
+ object setterValue = string.Empty;
+ Action<object, object> setter = (instance, value) => setterValue=value;
+ var map = new CollectionMemberMap("Test",typeof(IEnumerable<string>),o => null ,setter,"Test",false,null,typeof(string));
+
+ map.SetValue(null,null);
+
+ Assert.IsNull(setterValue);
+ }
+
+ [Test]
+ public void GetValue_Null_ReturnsNullAndSkipCollectionAdapterCall()
+ {
+ Func<object, object> getter = instance => null;
+ var map = new CollectionMemberMap("Test", typeof(IEnumerable<string>), getter, (i, v) => {}, "Test", false, null, typeof(string));
+
+ var value = map.GetValue(null);
+
+ Assert.IsNull(value);
+ }
+ }
+}
@@ -1,69 +1,84 @@
using System;
using System.Collections;
-
using MongoDB.Configuration.CollectionAdapters;
namespace MongoDB.Configuration.Mapping.Model
{
/// <summary>
- ///
/// </summary>
public class CollectionMemberMap : PersistentMemberMap
{
private readonly ICollectionAdapter _collectionAdapter;
/// <summary>
- /// Gets the type of the element.
+ /// Initializes a new instance of the <see cref = "CollectionMemberMap" /> class.
/// </summary>
- /// <value>The type of the element.</value>
- public Type ElementType { get; private set; }
-
- /// <summary>
- /// Initializes a new instance of the <see cref="CollectionMemberMap"/> class.
- /// </summary>
- /// <param name="memberName">Name of the member.</param>
- /// <param name="memberReturnType">Type of the member return.</param>
- /// <param name="getter">The getter.</param>
- /// <param name="setter">The setter.</param>
- /// <param name="alias">The alias.</param>
- /// <param name="persistDefaultValue">if set to <c>true</c> [persist default value].</param>
- /// <param name="collectionAdapter">Type of the collection.</param>
- /// <param name="elementType">Type of the element.</param>
- public CollectionMemberMap(string memberName, Type memberReturnType, Func<object, object> getter, Action<object, object> setter, string alias, bool persistDefaultValue, ICollectionAdapter collectionAdapter, Type elementType)
+ /// <param name = "memberName">Name of the member.</param>
+ /// <param name = "memberReturnType">Type of the member return.</param>
+ /// <param name = "getter">The getter.</param>
+ /// <param name = "setter">The setter.</param>
+ /// <param name = "alias">The alias.</param>
+ /// <param name = "persistDefaultValue">if set to <c>true</c> [persist default value].</param>
+ /// <param name = "collectionAdapter">Type of the collection.</param>
+ /// <param name = "elementType">Type of the element.</param>
+ public CollectionMemberMap(string memberName,
+ Type memberReturnType,
+ Func<object, object> getter,
+ Action<object, object> setter,
+ string alias,
+ bool persistDefaultValue,
+ ICollectionAdapter collectionAdapter,
+ Type elementType)
: base(memberName, memberReturnType, getter, setter, null, alias, persistDefaultValue)
{
_collectionAdapter = collectionAdapter;
ElementType = elementType;
}
/// <summary>
- /// Gets the value.
+ /// Gets the type of the element.
/// </summary>
- /// <param name="instance">The instance.</param>
+ /// <value>The type of the element.</value>
+ public Type ElementType { get; private set; }
+
+ /// <summary>
+ /// Gets the value.
+ /// </summary>
+ /// <param name = "instance">The instance.</param>
/// <returns></returns>
public override object GetValue(object instance)
{
- var elements = _collectionAdapter.GetElementsFromCollection(base.GetValue(instance));
+ var collection = base.GetValue(instance);
+
+ if(collection == null)
+ return null;
+
+ var elements = _collectionAdapter.GetElementsFromCollection(collection);
if(elements == null)
return null;
var list = new ArrayList();
-
- foreach (var element in elements)
+
+ foreach(var element in elements)
list.Add(element);
return list.ToArray();
}
/// <summary>
- /// Sets the value on the specified instance.
+ /// Sets the value on the specified instance.
/// </summary>
- /// <param name="instance">The instance.</param>
- /// <param name="value">The value.</param>
+ /// <param name = "instance">The instance.</param>
+ /// <param name = "value">The value.</param>
public override void SetValue(object instance, object value)
{
- base.SetValue(instance, _collectionAdapter.CreateCollection(ElementType, (object[])value));
+ object collection = null;
+
+ if(value != null)
+ collection = _collectionAdapter.CreateCollection(ElementType, (object[])value);
+
+ base.SetValue(instance, collection);
}
}
}

0 comments on commit 52bda60

Please sign in to comment.