diff --git a/Foundation.ObjectHydrator.Tests/Foundation.ObjectHydrator.Tests.csproj b/Foundation.ObjectHydrator.Tests/Foundation.ObjectHydrator.Tests.csproj index 53e19e7..d050530 100644 --- a/Foundation.ObjectHydrator.Tests/Foundation.ObjectHydrator.Tests.csproj +++ b/Foundation.ObjectHydrator.Tests/Foundation.ObjectHydrator.Tests.csproj @@ -61,6 +61,7 @@ AllRules.ruleset + ..\packages\NUnitTestAdapter.1.2\lib\nunit.core.dll False @@ -101,6 +102,8 @@ + + diff --git a/Foundation.ObjectHydrator.Tests/HydratorTests/GenericHydrator_SimpleCustomer_Tests.cs b/Foundation.ObjectHydrator.Tests/HydratorTests/GenericHydrator_SimpleCustomer_Tests.cs new file mode 100644 index 0000000..635b676 --- /dev/null +++ b/Foundation.ObjectHydrator.Tests/HydratorTests/GenericHydrator_SimpleCustomer_Tests.cs @@ -0,0 +1,56 @@ +using Foundation.ObjectHydrator.Tests.POCOs; +using NUnit.Framework; +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Foundation.ObjectHydrator.Tests.HydratorTests { + [TestFixture] + public class GenericHydrator_SimpleCustomer_Tests { + + Hydrator hydrator; + + [TestFixtureSetUp] + public void Initialize() { + hydrator = new Hydrator(typeof(SimpleCustomer)); + } + + [Test] + public void Generic_CanGetSingleSimpleCustomer() { + var customer = hydrator.GetSingle() as SimpleCustomer; + Assert.IsTrue(!String.IsNullOrEmpty(customer.Description), "Customer Description should exist."); + TestUtilities.DumpSimpleCustomer(customer); + } + + [Test] + public void Generic_CanGetSingleRestrictedDescriptionCustomer() { + var customer = hydrator.GetSingle() as SimpleCustomer; + + Assert.IsTrue(!String.IsNullOrEmpty(customer.Description), "Customer Description should exist."); + Assert.IsTrue(customer.Description.Length <= 5, "Length not restricted"); + TestUtilities.DumpSimpleCustomer(customer); + } + + [Test] + public void Generic_CanGetList() { + var listCount = 50; + var customers = hydrator.GetList(listCount) as IList; + + Assert.IsTrue(customers.Count == listCount, "Customer count is wrong."); + + //DumpCustomers(customers); + } + + [Test] + public void Generic_CanGetDescription() { + var customer = hydrator.GetSingle() as SimpleCustomer; + + Assert.IsTrue(!String.IsNullOrEmpty(customer.Description), "Customer Description should exist."); + + TestUtilities.DumpSimpleCustomer(customer); + } + + + + } +} diff --git a/Foundation.ObjectHydrator.Tests/HydratorTests/Hydrator_SimpleCustomer_Tests.cs b/Foundation.ObjectHydrator.Tests/HydratorTests/Hydrator_SimpleCustomer_Tests.cs index 72ddec1..7b4f9d5 100644 --- a/Foundation.ObjectHydrator.Tests/HydratorTests/Hydrator_SimpleCustomer_Tests.cs +++ b/Foundation.ObjectHydrator.Tests/HydratorTests/Hydrator_SimpleCustomer_Tests.cs @@ -20,7 +20,7 @@ public void CanGetSingleSimpleCustomer() Assert.IsTrue(!String.IsNullOrEmpty(customer.Description), "Customer Description should exist."); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -31,7 +31,7 @@ public void CanGetSingleRestrictedDescriptionCustomer() Assert.IsTrue(!String.IsNullOrEmpty(customer.Description), "Customer Description should exist."); Assert.IsTrue(customer.Description.Length <= 5,"Length not restricted"); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } @@ -55,7 +55,7 @@ public void CanGetDescription() Assert.IsTrue(!String.IsNullOrEmpty(customer.Description), "Customer Description should exist."); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -69,7 +69,7 @@ public void CanConstrainIntegers() Assert.IsTrue(customer.Locations >= 5 && customer.Locations <= 10, String.Format("Customer Locations [{0}] is outside expected range.", customer.Locations)); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } @@ -85,7 +85,7 @@ public void CanDefaultString() Assert.IsTrue(defaultValue == customer.Description, String.Format("Default value is not as expected[{0}]", defaultValue)); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -108,7 +108,7 @@ public void CanGetInteger() Assert.IsTrue(customer.Locations >= 0, String.Format("Customer Locations is expected.")); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -120,7 +120,7 @@ public void CanGetDouble() Assert.IsTrue(customer.Revenue >= 0, String.Format("Customer Revenue is expected.")); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -135,7 +135,7 @@ public void CanConstrainDoubleDecimalPlaces() var decimalPart = customer.Revenue - (int) customer.Revenue; Assert.IsTrue(decimalPart >= 0, String.Format("Customer Revenue decimal part is expected.")); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -149,7 +149,7 @@ public void CanConstrainDoubleRange() var customer = hydrator.GetSingle(); Assert.That(customer.Revenue, Is.InRange(minimum, maximum)); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -168,7 +168,7 @@ public void CanConstrainDoubleRangeAndDecimals() Assert.That(customer.Revenue, Is.InRange(minimum, maximum)); Assert.IsTrue(decimalPart >= 0, String.Format("Customer Revenue decimal part is expected.")); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -183,7 +183,7 @@ public void CanDefaultInteger() Assert.IsTrue(defaultValue == customer.Locations, String.Format("Default value is not as expected[{0}]", defaultValue)); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -199,7 +199,7 @@ public void CanDefaultDateTime() Assert.IsTrue(defaultValue == customer.IncorporatedOn, String.Format("Default value is not as expected[{0}]", defaultValue)); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -220,7 +220,7 @@ public void CanChainWithDefaultDescription() Assert.That(customer.Locations, Is.InRange(minimumValue, maximumValue), String.Format("Customer Locations [{0}] is outside expected range [{1},{2}].", customer.Locations, minimumValue, maximumValue)); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -238,7 +238,7 @@ public void CanConstrainWithDates() Assert.That(customer.IncorporatedOn, Is.InRange(minimumValue, maximumValue), String.Format("Customer IncorporatedOn [{0}] is outside expected range [{1}, {2}].", customer.IncorporatedOn, minimumValue, maximumValue)); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -256,7 +256,7 @@ public void CanConstrainDates() Assert.That(customer.IncorporatedOn, Is.InRange(minimumValue, maximumValue), String.Format("Customer IncorporatedOn [{0}] is outside expected range [{1}, {2}].", customer.IncorporatedOn, minimumValue, maximumValue)); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -270,7 +270,7 @@ public void CanGetByteArray() Assert.IsTrue(customer.Version.Length == length, String.Format("Customer Version Length is expected to be {0}.", length)); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -288,7 +288,7 @@ public void CanGetCreditCardNumber() String.Format("Credit Card Number [{0}] should be {1} long.", customer.CreditCardNumber, length)); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } private bool IsWebsiteAddressValid(string webaddy) @@ -394,7 +394,7 @@ public void BooleanGenerator() var customer = hydrator.GetSingle(); Assert.IsNotNull(customer.IsActive); Assert.IsInstanceOfType(typeof (bool), customer.IsActive); - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } [Test] @@ -622,31 +622,10 @@ private void DumpCustomers(IList customers) { foreach (SimpleCustomer customer in customers) { - DumpSimpleCustomer(customer); + TestUtilities.DumpSimpleCustomer(customer); } } - private void DumpSimpleCustomer(Object theObject) - { - Trace.WriteLine(""); - foreach (PropertyInfo propertyInfo in theObject.GetType().GetProperties()) - { - Trace.WriteLine(String.Format("{0} [{1}]", propertyInfo.Name, propertyInfo.GetValue(theObject, null))); - - if (propertyInfo.PropertyType == typeof (byte[])) - { - var theArray = propertyInfo.GetValue(theObject, null) as byte[]; - if (theArray != null) - { - Trace.Write(" byte[] "); - for (var i = 0; i < theArray.Length; i++) - { - Trace.Write(String.Format("[{0}]", theArray[i])); - } - Trace.WriteLine(String.Empty); - } - } - } - } + } } diff --git a/Foundation.ObjectHydrator.Tests/HydratorTests/TestUtilities.cs b/Foundation.ObjectHydrator.Tests/HydratorTests/TestUtilities.cs new file mode 100644 index 0000000..2321524 --- /dev/null +++ b/Foundation.ObjectHydrator.Tests/HydratorTests/TestUtilities.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Reflection; +using System.Text; + +namespace Foundation.ObjectHydrator.Tests.HydratorTests { + public static class TestUtilities { + public static void DumpSimpleCustomer(Object theObject) { + Trace.WriteLine(""); + foreach (PropertyInfo propertyInfo in theObject.GetType().GetProperties()) { + Trace.WriteLine(String.Format("{0} [{1}]", propertyInfo.Name, propertyInfo.GetValue(theObject, null))); + + if (propertyInfo.PropertyType == typeof(byte[])) { + var theArray = propertyInfo.GetValue(theObject, null) as byte[]; + if (theArray != null) { + Trace.Write(" byte[] "); + for (var i = 0; i < theArray.Length; i++) { + Trace.Write(String.Format("[{0}]", theArray[i])); + } + Trace.WriteLine(String.Empty); + } + } + } + } + + } +} diff --git a/Foundation.ObjectHydrator/Foundation.ObjectHydrator.csproj b/Foundation.ObjectHydrator/Foundation.ObjectHydrator.csproj index e1646ea..59146bc 100644 --- a/Foundation.ObjectHydrator/Foundation.ObjectHydrator.csproj +++ b/Foundation.ObjectHydrator/Foundation.ObjectHydrator.csproj @@ -116,6 +116,7 @@ + diff --git a/Foundation.ObjectHydrator/GenericHydrator.cs b/Foundation.ObjectHydrator/GenericHydrator.cs new file mode 100644 index 0000000..3980d9c --- /dev/null +++ b/Foundation.ObjectHydrator/GenericHydrator.cs @@ -0,0 +1,111 @@ +using Foundation.ObjectHydrator.Generators; +using Foundation.ObjectHydrator.Interfaces; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Reflection; +using System.Text; + +namespace Foundation.ObjectHydrator { + public class Hydrator : IGenerator { + + protected readonly Type typeOfT = null; + protected readonly IDictionary propertyMap; + protected readonly IList typeMap; + protected IList defaultTypeMap; + + public Hydrator(Type t) : this(t, new DefaultTypeMap()) { + } + + public Hydrator(Type t, IList defaultMap) { + typeOfT = t; + propertyMap = new Dictionary(); + typeMap = new List(); + defaultTypeMap = defaultMap; + } + + public Random Random { + get { + return RandomSingleton.Instance.Random; + } + } + + public object Generate() { + var instance = Activator.CreateInstance(typeOfT); + Populate(instance); + return instance; + } + + public object GetSingle() { + return Generate(); + } + + public IList GetList() { + int length; + length = Random.Next(1, 10); + return GetList(length); + } + + public IList GetList(int size) { + if (size < 1) { + throw new ArgumentOutOfRangeException("size", "size must be provided"); + } + IList toReturn = new ArrayList(); + for (int i = 0; i < size; i++) { + var instance = Generate(); + Populate(instance); + toReturn.Add(instance); + } + return toReturn; + } + + protected void Populate(object instance) { + AddTypeMapToPropertyMap(); + foreach (IMapping mapping in propertyMap.Values) { + PropertyInfo propertyInfo = instance.GetType().GetProperty(mapping.PropertyName, BindingFlags.Public | BindingFlags.Instance); + + + if (propertyInfo != null) { + propertyInfo.SetValue(instance, mapping.Generate(), null); + } + } + } + + protected void AddTypeMapToPropertyMap() { + AddDefaultTypeMapToTypeMap(); + + foreach (PropertyInfo propertyInfo in typeOfT.GetProperties()) { + if (propertyInfo.CanWrite && !propertyMap.ContainsKey(propertyInfo.Name)) { + PropertyInfo info = propertyInfo; + var map = typeMap.FirstOrDefault(infer => infer.Type == info.PropertyType && infer.Match(info)); + + if (map != null) { + propertyMap[propertyInfo.Name] = map.Mapping(propertyInfo); + } else if (!propertyInfo.PropertyType.IsInterface) { + propertyMap[propertyInfo.Name] = new Mapping(propertyInfo, new Generator(propertyInfo)); + } + } + } + } + + protected void AddDefaultTypeMapToTypeMap() { + foreach (var map in defaultTypeMap) { + typeMap.Add(map); + } + } + + protected void SetPropertyMap(string name, IGenerator generator) { + PropertyInfo pi = typeOfT.GetProperty(name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); + if (pi == null) { + throw new ArgumentException("The property cannot be found.", name); + } + if (!pi.CanWrite) { + throw new ArgumentException("The property cannot be written.", name); + } + propertyMap[pi.Name] = new Mapping(pi, generator); + } + + } +} diff --git a/Foundation.ObjectHydrator/Hydrator.cs b/Foundation.ObjectHydrator/Hydrator.cs index 5d6b437..aca1a2a 100644 --- a/Foundation.ObjectHydrator/Hydrator.cs +++ b/Foundation.ObjectHydrator/Hydrator.cs @@ -10,14 +10,9 @@ namespace Foundation.ObjectHydrator { - public class Hydrator:IGenerator - { - readonly Type typeOfT = null; - readonly IDictionary propertyMap; - private readonly IList typeMap; - private IList defaultTypeMap; - + public class Hydrator:Hydrator,IGenerator + { #region Ctors public Hydrator() @@ -25,12 +20,8 @@ public Hydrator() { } - public Hydrator(IList defaultMap) + public Hydrator(IList defaultMap) : base(typeof(T), defaultMap) { - typeOfT = typeof(T); - propertyMap = new Dictionary(); - typeMap = new List(); - defaultTypeMap = defaultMap; } #endregion @@ -47,7 +38,7 @@ public Random Random { get { - return RandomSingleton.Instance.Random; + return base.Random; } } @@ -64,9 +55,7 @@ public T GetSingle() public T Generate() { - var instance = (T)Activator.CreateInstance(typeOfT); - Populate(instance); - return instance; + return (T)base.Generate(); } #endregion @@ -108,24 +97,10 @@ public IList GetList(int size) return toReturn; } - private void SetPropertyMap(Expression> expression, IGenerator generator) - { - var propertyName = ((MemberExpression)expression.Body).Member.Name; - PropertyInfo propertyInfo = typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); - - // Check to see if we have this property... - if (propertyInfo == null) - { - throw new ArgumentException("The Property can not be found.", propertyName); - } - - if (!propertyInfo.CanWrite) - { - throw new ArgumentException("The Property can not be written.", propertyName); - } - - propertyMap[propertyInfo.Name] = new Mapping(propertyInfo, generator); - } + private void SetPropertyMap(Expression> expression, IGenerator generator) { + var propertyName = ((MemberExpression)expression.Body).Member.Name; + base.SetPropertyMap(propertyName, generator); + } #region WithTypes /// @@ -518,53 +493,9 @@ public Hydrator For(Map map) { typeMap.Add(map); return this; - } + } - private void Populate(object instance) - { - AddTypeMapToPropertyMap(); - foreach (IMapping mapping in propertyMap.Values) - { - PropertyInfo propertyInfo = instance.GetType().GetProperty(mapping.PropertyName, BindingFlags.Public | BindingFlags.Instance); - - - if (propertyInfo != null) - { - propertyInfo.SetValue(instance, mapping.Generate(), null); - } - } - } - - private void AddTypeMapToPropertyMap() - { - AddDefaultTypeMapToTypeMap(); - - foreach (PropertyInfo propertyInfo in typeOfT.GetProperties()) - { - if (propertyInfo.CanWrite && !propertyMap.ContainsKey(propertyInfo.Name)) - { - PropertyInfo info = propertyInfo; - var map = typeMap.FirstOrDefault(infer => infer.Type == info.PropertyType && infer.Match(info)); - - if (map != null) - { - propertyMap[propertyInfo.Name] = map.Mapping(propertyInfo); - } - else if (!propertyInfo.PropertyType.IsInterface) - { - propertyMap[propertyInfo.Name] = new Mapping(propertyInfo, new Generator(propertyInfo)); - } - } - } - } - - private void AddDefaultTypeMapToTypeMap() - { - foreach (var map in defaultTypeMap) - { - typeMap.Add(map); - } - } + }