From: <jer...@us...> - 2008-06-16 02:12:57
|
Revision: 123 http://structuremap.svn.sourceforge.net/structuremap/?rev=123&view=rev Author: jeremydmiller Date: 2008-06-15 19:12:53 -0700 (Sun, 15 Jun 2008) Log Message: ----------- adding the ability to define IDictionary<T, U>, NameValueCollection, and primitive array arguments in the xml configuration Modified Paths: -------------- trunk/Source/StructureMap/Configuration/XmlExtensions.cs trunk/Source/StructureMap/Diagnostics/Error.cs trunk/Source/StructureMap/Graph/TypeRules.cs trunk/Source/StructureMap/InstanceMemento.cs trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs trunk/Source/StructureMap/Source/XmlAttributeInstanceMemento.cs trunk/Source/StructureMap/Source/XmlNodeInstanceMemento.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj trunk/Source/StructureMap.Testing/TestData/DataMother.cs Added Paths: ----------- trunk/Source/StructureMap/Configuration/DictionaryReader.cs trunk/Source/StructureMap/Configuration/ITypeReader.cs trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs trunk/Source/StructureMap/Configuration/TypeReaderFactory.cs trunk/Source/StructureMap.Testing/Configuration/DictionaryAndArrayArgumentTester.cs trunk/Source/StructureMap.Testing/Configuration/DictionaryReaderTester.cs trunk/Source/StructureMap.Testing/Configuration/PrimitiveArrayReaderTester.cs Added: trunk/Source/StructureMap/Configuration/DictionaryReader.cs =================================================================== --- trunk/Source/StructureMap/Configuration/DictionaryReader.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/DictionaryReader.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -0,0 +1,91 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Xml; +using StructureMap.Pipeline; + +namespace StructureMap.Configuration +{ + public class DictionaryReader : ITypeReader + { + public bool CanProcess(Type pluginType) + { + if (pluginType.Equals(typeof(NameValueCollection))) return true; + if (!pluginType.IsGenericType) return false; + + var definition = pluginType.GetGenericTypeDefinition(); + if (definition == null) return false; + + return definition.Equals(typeof (IDictionary<,>)) || definition.Equals(typeof (Dictionary<,>)); + } + + private static IBuilder findBuilder(Type pluginType) + { + if (pluginType.Equals(typeof(NameValueCollection))) return new NameValueCollectionBuilder(); + + var definition = pluginType.GetGenericTypeDefinition(); + if (definition.Equals(typeof(IDictionary<,>)) || definition.Equals(typeof(Dictionary<,>))) + { + var arguments = pluginType.GetGenericArguments(); + var builderType = typeof (DictionaryBuilder<,>).MakeGenericType(arguments); + return (IBuilder) Activator.CreateInstance(builderType); + } + + return null; + } + + public Instance Read(XmlNode node, Type pluginType) + { + var builder = findBuilder(pluginType); + node.ForEachChild("Pair").Do(element => builder.Read(element.GetAttribute("Key"), element.GetAttribute("Value"))); + + return new SerializedInstance(builder.Object); + } + + + + + + + + + internal interface IBuilder + { + void Read(string name, string value); + object Object { get; } + } + + internal class NameValueCollectionBuilder : IBuilder + { + private readonly NameValueCollection _collection = new NameValueCollection(); + + public void Read(string name, string value) + { + _collection.Add(name, value); + } + + public object Object + { + get { return _collection; } + } + } + + internal class DictionaryBuilder<KEY, VALUE> : IBuilder + { + private Dictionary<KEY, VALUE> _dictionary = new Dictionary<KEY, VALUE>(); + + public void Read(string name, string value) + { + KEY key = (KEY) Convert.ChangeType(name, typeof (KEY)); + VALUE theValue = (VALUE) Convert.ChangeType(value, typeof (VALUE)); + + _dictionary.Add(key, theValue); + } + + public object Object + { + get { return _dictionary; } + } + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Configuration/ITypeReader.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ITypeReader.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/ITypeReader.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -0,0 +1,14 @@ +using System; +using System.Linq; +using System.Text; +using System.Xml; +using StructureMap.Pipeline; + +namespace StructureMap.Configuration +{ + public interface ITypeReader + { + bool CanProcess(Type pluginType); + Instance Read(XmlNode node, Type pluginType); + } +} Added: trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs =================================================================== --- trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/PrimitiveArrayReader.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -0,0 +1,38 @@ +using System; +using System.Xml; +using StructureMap.Graph; +using StructureMap.Pipeline; + +namespace StructureMap.Configuration +{ + public class PrimitiveArrayReader : TypeRules, ITypeReader + { + #region ITypeReader Members + + public bool CanProcess(Type pluginType) + { + return IsPrimitiveArray(pluginType); + } + + public Instance Read(XmlNode node, Type pluginType) + { + Type elementType = pluginType.GetElementType(); + char concatenator = node.GetAttribute("Concatenator", ",").ToCharArray()[0]; + + var valueString = node.GetAttribute("Values", string.Empty); + string[] rawValues = valueString.Split(new[]{concatenator}, StringSplitOptions.RemoveEmptyEntries); + + + var array = Array.CreateInstance(elementType, rawValues.Length); + for (int i = 0; i < rawValues.Length; i++) + { + object convertedType = Convert.ChangeType(rawValues[i].Trim(), elementType); + array.SetValue(convertedType, i); + } + + return new SerializedInstance(array); + } + + #endregion + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Configuration/TypeReaderFactory.cs =================================================================== --- trunk/Source/StructureMap/Configuration/TypeReaderFactory.cs (rev 0) +++ trunk/Source/StructureMap/Configuration/TypeReaderFactory.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; + +namespace StructureMap.Configuration +{ + public static class TypeReaderFactory + { + private static List<ITypeReader> _readers = new List<ITypeReader>(); + + static TypeReaderFactory() + { + _readers.Add(new DictionaryReader()); + _readers.Add(new PrimitiveArrayReader()); + } + + public static ITypeReader GetReader(Type pluginType) + { + foreach (var reader in _readers) + { + if (reader.CanProcess(pluginType)) + { + return reader; + } + } + + return null; + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/XmlExtensions.cs =================================================================== --- trunk/Source/StructureMap/Configuration/XmlExtensions.cs 2008-06-15 14:28:14 UTC (rev 122) +++ trunk/Source/StructureMap/Configuration/XmlExtensions.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -5,6 +5,12 @@ { public static class XmlExtensions { + public static string GetAttribute(this XmlNode node, string attributeName, string defaultValue) + { + var attribute = node.Attributes.GetNamedItem(attributeName); + return attribute == null ? defaultValue : attribute.InnerText; + } + public static XmlTextExpression ForTextInChild(this XmlNode node, string xpath) { return new XmlTextExpression(node, xpath); Modified: trunk/Source/StructureMap/Diagnostics/Error.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/Error.cs 2008-06-15 14:28:14 UTC (rev 122) +++ trunk/Source/StructureMap/Diagnostics/Error.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -8,7 +8,7 @@ { private readonly int _code; private readonly string _message; - private readonly string _stackTrace = string.Empty; + private string _stackTrace = string.Empty; public InstanceToken Instance; public string Source; @@ -27,8 +27,9 @@ public Error(int errorCode, Exception ex, params object[] args) : this(errorCode, args) { - _message += "\n\n" + ex.ToString(); - _stackTrace = ex.StackTrace; + _message += "\n\n" + ex.Message; + + writeStackTrace(ex); } @@ -36,10 +37,23 @@ { _code = exception.ErrorCode; _message = exception.Message; - _stackTrace = exception.StackTrace; + + writeStackTrace(exception); } + private void writeStackTrace(Exception exception) + { + _stackTrace = string.Empty; + Exception ex = exception; + while (ex != null) + { + _stackTrace += exception.ToString(); + _stackTrace += "\n\n"; + ex = ex.InnerException; + } + } + public int Code { get { return _code; } Modified: trunk/Source/StructureMap/Graph/TypeRules.cs =================================================================== --- trunk/Source/StructureMap/Graph/TypeRules.cs 2008-06-15 14:28:14 UTC (rev 122) +++ trunk/Source/StructureMap/Graph/TypeRules.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -77,7 +77,7 @@ protected bool IsChild(Type type) { - return !type.IsArray && !IsSimple(type); + return IsPrimitiveArray(type) || (!type.IsArray && !IsSimple(type)); } protected bool IsChildArray(Type type) @@ -85,6 +85,11 @@ return type.IsArray && !IsSimple(type.GetElementType()); } + protected bool IsPrimitiveArray(Type type) + { + return type.IsArray && IsSimple(type.GetElementType()); + } + protected bool IsConcrete(Type type) { return !type.IsInterface && !type.IsAbstract; Modified: trunk/Source/StructureMap/InstanceMemento.cs =================================================================== --- trunk/Source/StructureMap/InstanceMemento.cs 2008-06-15 14:28:14 UTC (rev 122) +++ trunk/Source/StructureMap/InstanceMemento.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -147,6 +147,8 @@ /// <returns></returns> protected abstract string getPropertyValue(string Key); + + /// <summary> /// Returns the named child InstanceMemento /// </summary> @@ -160,6 +162,12 @@ return returnValue; } + public virtual Instance ReadChildInstance(string name, PluginGraph graph, Type childType) + { + InstanceMemento child = GetChildMemento(name); + return child == null ? null : child.ReadInstance(graph, childType); + } + /// <summary> /// Template method for implementation specific retrieval of the named property /// </summary> @@ -207,7 +215,7 @@ } catch (Exception e) { - throw new StructureMapException(260, InstanceKey, pluginType.FullName); + throw new StructureMapException(260, e, InstanceKey, pluginType.FullName); } } @@ -225,5 +233,7 @@ return new ConfiguredInstance(this, pluginGraph, pluginType); } + + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs 2008-06-15 14:28:14 UTC (rev 122) +++ trunk/Source/StructureMap/Pipeline/InstanceMementoPropertyReader.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -81,9 +81,11 @@ private void copyChild(string name, Type childType) { - InstanceMemento child = _memento.GetChildMemento(name); + //InstanceMemento child = _memento.GetChildMemento(name); + //Instance childInstance = child == null ? new DefaultInstance() : child.ReadInstance(_pluginGraph, childType); - Instance childInstance = child == null ? new DefaultInstance() : child.ReadInstance(_pluginGraph, childType); + Instance childInstance = _memento.ReadChildInstance(name, _pluginGraph, childType) ?? new DefaultInstance(); + _instance.Child(name).Is(childInstance); } Modified: trunk/Source/StructureMap/Source/XmlAttributeInstanceMemento.cs =================================================================== --- trunk/Source/StructureMap/Source/XmlAttributeInstanceMemento.cs 2008-06-15 14:28:14 UTC (rev 122) +++ trunk/Source/StructureMap/Source/XmlAttributeInstanceMemento.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -1,6 +1,9 @@ +using System; using System.Collections; using System.Xml; using StructureMap.Configuration; +using StructureMap.Graph; +using StructureMap.Pipeline; namespace StructureMap.Source { @@ -110,5 +113,17 @@ { return _element.OuterXml; } + + public override Instance ReadChildInstance(string name, PluginGraph graph, Type childType) + { + var reader = TypeReaderFactory.GetReader(childType); + if (reader == null) + { + return base.ReadChildInstance(name, graph, childType); + } + + XmlElement element = _element[name]; + return reader.Read(element, childType); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Source/XmlNodeInstanceMemento.cs =================================================================== --- trunk/Source/StructureMap/Source/XmlNodeInstanceMemento.cs 2008-06-15 14:28:14 UTC (rev 122) +++ trunk/Source/StructureMap/Source/XmlNodeInstanceMemento.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Xml; using StructureMap.Configuration; +using StructureMap.Graph; +using StructureMap.Pipeline; namespace StructureMap.Source { @@ -41,6 +43,11 @@ { get { + if (!string.IsNullOrEmpty(getPluggedType())) + { + return false; + } + bool returnValue = false; string typeName = getAttribute("Type"); @@ -66,10 +73,10 @@ return getAttribute(XmlConstants.PLUGGED_TYPE); } - private XmlNode getChildNode(string Key) + private XmlElement getChildNode(string Key) { string xpath = string.Format("Property[@Name='{0}']", Key); - XmlNode nodeProperty = _innerNode.SelectSingleNode(xpath); + XmlElement nodeProperty = (XmlElement) _innerNode.SelectSingleNode(xpath); return nodeProperty; } @@ -94,6 +101,18 @@ } + public override Instance ReadChildInstance(string name, PluginGraph graph, Type childType) + { + var reader = TypeReaderFactory.GetReader(childType); + if (reader == null) + { + return base.ReadChildInstance(name, graph, childType); + } + + XmlElement element = getChildNode(name); + return reader.Read(element, childType); + } + protected override InstanceMemento getChild(string Key) { InstanceMemento returnValue = null; Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2008-06-15 14:28:14 UTC (rev 122) +++ trunk/Source/StructureMap/StructureMap.csproj 2008-06-16 02:12:53 UTC (rev 123) @@ -392,6 +392,10 @@ <None Include="..\structuremap.snk"> <Link>Properties\structuremap.snk</Link> </None> + <Compile Include="Configuration\DictionaryReader.cs" /> + <Compile Include="Configuration\ITypeReader.cs" /> + <Compile Include="Configuration\PrimitiveArrayReader.cs" /> + <Compile Include="Configuration\TypeReaderFactory.cs" /> <Compile Include="Configuration\XmlExtensions.cs" /> <Compile Include="Diagnostics\Doctor.cs" /> <Compile Include="Diagnostics\DoctorReport.cs" /> Added: trunk/Source/StructureMap.Testing/Configuration/DictionaryAndArrayArgumentTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DictionaryAndArrayArgumentTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Configuration/DictionaryAndArrayArgumentTester.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -0,0 +1,138 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using StructureMap.Graph; +using StructureMap.Pipeline; +using StructureMap.Source; +using StructureMap.Testing.Pipeline; +using StructureMap.Testing.TestData; + +namespace StructureMap.Testing.Configuration +{ + [TestFixture] + public class DictionaryAndArrayArgumentTester + { + [Test] + public void Read_in_a_dictionary_type_from_an_attribute_normalized_memento() + { + string xml = @" +<root> + <dictionary> + <Pair Key='color' Value='red'/> + <Pair Key='state' Value='texas'/> + <Pair Key='direction' Value='north'/> + </dictionary> +</root> +"; + + var element = DataMother.BuildDocument(xml).DocumentElement; + element.SetAttribute("PluggedType", TypePath.GetAssemblyQualifiedName(typeof (ClassWithDictionary))); + + XmlAttributeInstanceMemento memento = new XmlAttributeInstanceMemento(element); + + Instance instance = memento.ReadInstance(new PluginGraph(), typeof (ClassWithDictionary)); + + + ClassWithDictionary theObject = + (ClassWithDictionary) instance.Build(typeof(ClassWithDictionary), new BuildSession(new PluginGraph())); + + + theObject.Dictionary["color"].ShouldEqual("red"); + theObject.Dictionary["state"].ShouldEqual("texas"); + theObject.Dictionary["direction"].ShouldEqual("north"); + } + + [Test] + public void Read_in_a_dictionary_type_from_a_node_normalized_memento() + { + string xml = @" +<root> + <Property Name='dictionary'> + <Pair Key='color' Value='red'/> + <Pair Key='state' Value='texas'/> + <Pair Key='direction' Value='north'/> + </Property> +</root> +"; + + var element = DataMother.BuildDocument(xml).DocumentElement; + element.SetAttribute("PluggedType", TypePath.GetAssemblyQualifiedName(typeof(ClassWithDictionary))); + + XmlNodeInstanceMemento memento = new XmlNodeInstanceMemento(element, "Type", "Key"); + + Instance instance = memento.ReadInstance(new PluginGraph(), typeof(ClassWithDictionary)); + + + ClassWithDictionary theObject = + (ClassWithDictionary)instance.Build(typeof(ClassWithDictionary), new BuildSession(new PluginGraph())); + + + theObject.Dictionary["color"].ShouldEqual("red"); + theObject.Dictionary["state"].ShouldEqual("texas"); + theObject.Dictionary["direction"].ShouldEqual("north"); + } + + [Test] + public void Read_in_a_class_with_primitive_arrays() + { + string xml = @" +<root> + <numbers Values='1,2,3'/> + <strings Values='1,2,3'/> +</root> +"; + + var element = DataMother.BuildDocument(xml).DocumentElement; + element.SetAttribute("PluggedType", TypePath.GetAssemblyQualifiedName(typeof(ClassWithStringAndIntArray))); + + XmlAttributeInstanceMemento memento = new XmlAttributeInstanceMemento(element); + PluginGraph graph = new PluginGraph(); + Instance instance = memento.ReadInstance(graph, typeof(ClassWithStringAndIntArray)); + + ClassWithStringAndIntArray theObject = (ClassWithStringAndIntArray) instance.Build(typeof (ClassWithStringAndIntArray), + new BuildSession(graph)); + + theObject.Numbers.ShouldEqual(new int[] {1, 2, 3}); + theObject.Strings.ShouldEqual(new string[] {"1", "2", "3"}); + } + } + + public class ClassWithStringAndIntArray + { + private int[] _numbers; + private string[] _strings; + + public ClassWithStringAndIntArray(int[] numbers, string[] strings) + { + _numbers = numbers; + _strings = strings; + } + + public int[] Numbers + { + get { return _numbers; } + } + + public string[] Strings + { + get { return _strings; } + } + } + + public class ClassWithDictionary + { + private readonly IDictionary<string, string> _dictionary; + + public ClassWithDictionary(IDictionary<string, string> dictionary) + { + _dictionary = dictionary; + } + + public IDictionary<string, string> Dictionary + { + get { return _dictionary; } + } + } +} Added: trunk/Source/StructureMap.Testing/Configuration/DictionaryReaderTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DictionaryReaderTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Configuration/DictionaryReaderTester.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -0,0 +1,154 @@ +using System.Collections.Generic; +using System.Collections.Specialized; +using NUnit.Framework; +using StructureMap.Configuration; +using StructureMap.Pipeline; +using StructureMap.Testing.Pipeline; +using StructureMap.Testing.TestData; + +namespace StructureMap.Testing.Configuration +{ + [TestFixture] + public class DictionaryReaderTester + { + [Test] + public void Can_process_a_dictionary() + { + var reader = new DictionaryReader(); + reader.CanProcess(typeof (IDictionary<string, string>)).ShouldBeTrue(); + reader.CanProcess(typeof (IDictionary<int, string>)).ShouldBeTrue(); + reader.CanProcess(typeof (IDictionary<string, int>)).ShouldBeTrue(); + reader.CanProcess(typeof (Dictionary<string, string>)).ShouldBeTrue(); + reader.CanProcess(typeof (Dictionary<int, string>)).ShouldBeTrue(); + reader.CanProcess(typeof (Dictionary<string, int>)).ShouldBeTrue(); + } + + [Test] + public void Can_process_NameValueCollection() + { + new DictionaryReader().CanProcess(typeof (NameValueCollection)).ShouldBeTrue(); + } + + [Test] + public void Read_a_string_int_dictionary2() + { + string xml = + @" +<node> + <Pair Key='color' Value='1'/> + <Pair Key='state' Value='2'/> + <Pair Key='direction' Value='3'/> +</node> +"; + + Instance instance = new DictionaryReader().Read(DataMother.BuildDocument(xml).DocumentElement, + typeof (Dictionary<string, int>)); + instance.ShouldBeOfType(typeof (SerializedInstance)); + + var collection = + (Dictionary<string, int>) instance.Build(typeof (Dictionary<string, int>), new StubBuildSession()); + + collection["color"].ShouldEqual(1); + collection["state"].ShouldEqual(2); + collection["direction"].ShouldEqual(3); + } + + [Test] + public void Read_a_string_string_dictionary() + { + string xml = + @" +<node> + <Pair Key='color' Value='red'/> + <Pair Key='state' Value='texas'/> + <Pair Key='direction' Value='north'/> +</node> +"; + + Instance instance = new DictionaryReader().Read(DataMother.BuildDocument(xml).DocumentElement, + typeof (IDictionary<string, string>)); + instance.ShouldBeOfType(typeof (SerializedInstance)); + + var collection = + (IDictionary<string, string>) + instance.Build(typeof (IDictionary<string, string>), new StubBuildSession()); + + collection["color"].ShouldEqual("red"); + collection["state"].ShouldEqual("texas"); + collection["direction"].ShouldEqual("north"); + } + + + [Test] + public void Read_a_string_string_dictionary2() + { + string xml = + @" +<node> + <Pair Key='color' Value='red'/> + <Pair Key='state' Value='texas'/> + <Pair Key='direction' Value='north'/> +</node> +"; + + Instance instance = new DictionaryReader().Read(DataMother.BuildDocument(xml).DocumentElement, + typeof (Dictionary<string, string>)); + instance.ShouldBeOfType(typeof (SerializedInstance)); + + var collection = + (Dictionary<string, string>) instance.Build(typeof (Dictionary<string, string>), new StubBuildSession()); + + collection["color"].ShouldEqual("red"); + collection["state"].ShouldEqual("texas"); + collection["direction"].ShouldEqual("north"); + } + + [Test] + public void Read_an_instance_for_NameValueCollection() + { + string xml = + @" +<node> + <Pair Key='color' Value='red'/> + <Pair Key='state' Value='texas'/> + <Pair Key='direction' Value='north'/> +</node> +"; + + Instance instance = new DictionaryReader().Read(DataMother.BuildDocument(xml).DocumentElement, + typeof (NameValueCollection)); + instance.ShouldBeOfType(typeof (SerializedInstance)); + + var collection = (NameValueCollection) instance.Build(typeof (NameValueCollection), new StubBuildSession()); + + collection["color"].ShouldEqual("red"); + collection["state"].ShouldEqual("texas"); + collection["direction"].ShouldEqual("north"); + } + + + [Test] + public void Read_an_int_string_dictionary() + { + string xml = + @" +<node> + <Pair Key='1' Value='red'/> + <Pair Key='2' Value='texas'/> + <Pair Key='3' Value='north'/> +</node> +"; + + Instance instance = new DictionaryReader().Read(DataMother.BuildDocument(xml).DocumentElement, + typeof (IDictionary<int, string>)); + instance.ShouldBeOfType(typeof (SerializedInstance)); + + var collection = + (IDictionary<int, string>) instance.Build(typeof (IDictionary<int, string>), new StubBuildSession()); + + collection[1].ShouldEqual("red"); + collection[2].ShouldEqual("texas"); + collection[3].ShouldEqual("north"); + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap.Testing/Configuration/PrimitiveArrayReaderTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/PrimitiveArrayReaderTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Configuration/PrimitiveArrayReaderTester.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml; +using NUnit.Framework; +using StructureMap.Configuration; +using StructureMap.Pipeline; +using StructureMap.Testing.Pipeline; +using StructureMap.Testing.TestData; +using StructureMap.Testing.Widget; + +namespace StructureMap.Testing.Configuration +{ + [TestFixture] + public class PrimitiveeArrayReaderTester + { + [Test] + public void CanProcess_an_array_of_primitive_types() + { + PrimitiveArrayReader reader = new PrimitiveArrayReader(); + + reader.CanProcess(typeof(string)).ShouldBeFalse(); + reader.CanProcess(typeof(IWidget[])).ShouldBeFalse(); + + reader.CanProcess(typeof(string[])).ShouldBeTrue(); + reader.CanProcess(typeof(int[])).ShouldBeTrue(); + reader.CanProcess(typeof(double[])).ShouldBeTrue(); + } + + private object parseNode(string xml, Type pluginType) + { + XmlElement element = DataMother.BuildDocument(xml).DocumentElement; + PrimitiveArrayReader reader = new PrimitiveArrayReader(); + Instance instance = reader.Read(element, pluginType); + + return instance.Build(pluginType, new StubBuildSession()); + } + + [Test] + public void Parse_a_string_array_with_the_default_concatenator() + { + parseNode("<node Values='a,b,c,d'></node>", typeof (string[])).ShouldEqual(new string[] {"a", "b", "c", "d"}); + } + + + [Test] + public void Parse_a_string_array_with_overridden_concatenator() + { + parseNode("<node Values='a,b,c,d' Concatenator=';'></node>", typeof(string[])).ShouldEqual(new string[] { "a,b,c,d" }); + parseNode("<node Values='a;b;c;d' Concatenator=';'></node>", typeof(string[])).ShouldEqual(new string[] { "a", "b", "c", "d" }); + } + + + [Test] + public void Parse_a_string_array_with_the_default_concatenator_and_deal_with_leading_or_trailing_spaces() + { + parseNode("<node Values='a , b,c,d'></node>", typeof(string[])).ShouldEqual(new string[] { "a", "b", "c", "d" }); + } + + + [Test] + public void Parse_an_int_array_with_the_default_concatenator() + { + parseNode("<node Values='1,2,3,4'></node>", typeof(int[])).ShouldEqual(new int[] { 1, 2, 3, 4 }); + } + } +} Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-06-15 14:28:14 UTC (rev 122) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-06-16 02:12:53 UTC (rev 123) @@ -180,6 +180,8 @@ <Compile Include="Configuration\ConfigurationParserBuilderTester.cs" /> <Compile Include="Configuration\ConfigurationParserTester.cs" /> <Compile Include="Configuration\DefaultInstanceNodeTester.cs" /> + <Compile Include="Configuration\DictionaryAndArrayArgumentTester.cs" /> + <Compile Include="Configuration\DictionaryReaderTester.cs" /> <Compile Include="Configuration\DSL\AddInstanceTester.cs" /> <Compile Include="Configuration\DSL\AddTypesTester.cs" /> <Compile Include="Configuration\DSL\ConstructorExpressionTester.cs" /> @@ -203,6 +205,7 @@ </Compile> <Compile Include="Configuration\InlineInstanceDefinitionInProfileAndMachineNodesTester.cs" /> <Compile Include="Configuration\NormalGraphBuilderTester.cs" /> + <Compile Include="Configuration\PrimitiveArrayReaderTester.cs" /> <Compile Include="Configuration\ProfileBuilderTester.cs" /> <Compile Include="Configuration\ShortcuttedInstanceNodeTester.cs" /> <Compile Include="Diagnostics\DoctorTester.cs" /> Modified: trunk/Source/StructureMap.Testing/TestData/DataMother.cs =================================================================== --- trunk/Source/StructureMap.Testing/TestData/DataMother.cs 2008-06-15 14:28:14 UTC (rev 122) +++ trunk/Source/StructureMap.Testing/TestData/DataMother.cs 2008-06-16 02:12:53 UTC (rev 123) @@ -19,16 +19,22 @@ public static PluginGraph BuildPluginGraphFromXml(string xml) { - xml = xml.Replace("'", "\""); - XmlDocument document = new XmlDocument(); - document.LoadXml(xml); + XmlDocument document = BuildDocument(xml); ConfigurationParser parser = new ConfigurationParser(document.DocumentElement); PluginGraphBuilder builder = new PluginGraphBuilder(parser); return builder.Build(); } + public static XmlDocument BuildDocument(string xml) + { + xml = xml.Replace("'", "\""); + XmlDocument document = new XmlDocument(); + document.LoadXml(xml); + return document; + } + public static void BackupStructureMapConfig() { if (File.Exists("StructureMap.config.bak")) File.Delete("StructureMap.config.bak"); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |