From: <jer...@us...> - 2008-12-20 18:50:31
|
Revision: 206 http://structuremap.svn.sourceforge.net/structuremap/?rev=206&view=rev Author: jeremydmiller Date: 2008-12-20 18:50:25 +0000 (Sat, 20 Dec 2008) Log Message: ----------- Fixed the problem with missing dictionary data in Xml configuration for non-mandatory setters Modified Paths: -------------- 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/Extensions.cs trunk/Source/StructureMap.Testing/Bugs/IDictionaryAndXmlBugTester.cs Added: trunk/Source/StructureMap/Extensions.cs =================================================================== --- trunk/Source/StructureMap/Extensions.cs (rev 0) +++ trunk/Source/StructureMap/Extensions.cs 2008-12-20 18:50:25 UTC (rev 206) @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace StructureMap +{ + public static class StringExtensions + { + public static string ToFormat(this string template, params object[] parameters) + { + return string.Format(template, parameters); + } + + public static string[] ToDelimitedArray(this string content) + { + string[] array = content.Split(','); + for (int i = 0; i < array.Length; i++) + { + array[i] = array[i].Trim(); + } + + return array; + } + } +} Modified: trunk/Source/StructureMap/Source/XmlAttributeInstanceMemento.cs =================================================================== --- trunk/Source/StructureMap/Source/XmlAttributeInstanceMemento.cs 2008-12-20 18:16:59 UTC (rev 205) +++ trunk/Source/StructureMap/Source/XmlAttributeInstanceMemento.cs 2008-12-20 18:50:25 UTC (rev 206) @@ -123,7 +123,7 @@ } XmlElement element = _element[name]; - return reader.Read(element, childType); + return element == null ? null : reader.Read(element, childType); } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Source/XmlNodeInstanceMemento.cs =================================================================== --- trunk/Source/StructureMap/Source/XmlNodeInstanceMemento.cs 2008-12-20 18:16:59 UTC (rev 205) +++ trunk/Source/StructureMap/Source/XmlNodeInstanceMemento.cs 2008-12-20 18:50:25 UTC (rev 206) @@ -110,7 +110,7 @@ } XmlElement element = getChildNode(name); - return reader.Read(element, childType); + return element == null ? null : reader.Read(element, childType); } protected override InstanceMemento getChild(string Key) Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2008-12-20 18:16:59 UTC (rev 205) +++ trunk/Source/StructureMap/StructureMap.csproj 2008-12-20 18:50:25 UTC (rev 206) @@ -399,6 +399,7 @@ <Compile Include="Diagnostics\ValidationError.cs" /> <Compile Include="Emitting\Parameters\Methods.cs" /> <Compile Include="ErrorMessages.cs" /> + <Compile Include="Extensions.cs" /> <Compile Include="Graph\FamilyAttributeScanner.cs" /> <Compile Include="Graph\FindAllTypesFilter.cs" /> <Compile Include="Graph\FindRegistriesScanner.cs" /> Added: trunk/Source/StructureMap.Testing/Bugs/IDictionaryAndXmlBugTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Bugs/IDictionaryAndXmlBugTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Bugs/IDictionaryAndXmlBugTester.cs 2008-12-20 18:50:25 UTC (rev 206) @@ -0,0 +1,132 @@ +using System.Collections.Generic; +using NUnit.Framework; +using StructureMap.Attributes; +using StructureMap.Testing.TestData; + +namespace StructureMap.Testing.Bugs +{ + [TestFixture] + public class IDictionaryAndXmlBugTester + { + [Test] + public void read_a_dictionary_from_configuration() + { + string className = typeof (ClassWithDictionaryInCtor).AssemblyQualifiedName; + string xml = + @" +<StructureMap> + <DefaultInstance PluginType='{0}' PluggedType='{0}'> + <Property Name='values'> + <Pair Key='color' Value='red'/> + <Pair Key='state' Value='texas'/> + <Pair Key='direction' Value='north'/> + </Property> + </DefaultInstance> +</StructureMap> +" + .ToFormat(className); + + Container container = DataMother.BuildContainerForXml(xml); + + var dict = container.GetInstance<ClassWithDictionaryInCtor>(); + dict.Values["color"].ShouldEqual("red"); + } + + + [Test] + public void + read_a_dictionary_from_configuration_in_attribute_style_that_is_missing_a_dictionary_and_blow_up_with_good_message + () + { + string className = typeof (ClassWithDictionaryInCtor).AssemblyQualifiedName; + string xml = + @" +<StructureMap MementoStyle='Attribute'> + <DefaultInstance PluginType='{0}' PluggedType='{0}'></DefaultInstance> +</StructureMap> +" + .ToFormat(className); + + + try + { + Container container = DataMother.BuildContainerForXml(xml); + container.GetInstance<ClassWithDictionaryInCtor>(); + } + catch (StructureMapException e) + { + e.ErrorCode.ShouldEqual(202); + } + } + + + [Test] + public void + read_a_dictionary_from_configuration_in_attribute_style_that_is_missing_a_dictionary_for_an_optional_setter() + { + string className = typeof (ClassWithDictionaryProperty).AssemblyQualifiedName; + string xml = + @" +<StructureMap MementoStyle='Attribute'> + <DefaultInstance PluginType='{0}' PluggedType='{0}'></DefaultInstance> +</StructureMap> +" + .ToFormat(className); + + + Container container = DataMother.BuildContainerForXml(xml); + container.GetInstance<ClassWithDictionaryProperty>().ShouldNotBeNull(); + } + + [Test] + public void read_a_dictionary_from_configuration_when_the_property_is_missing_on_mandatory_setter() + { + string className = typeof (ClassWithDictionaryProperty2).AssemblyQualifiedName; + string xml = + @" +<StructureMap> + <DefaultInstance PluginType='{0}' PluggedType='{0}'></DefaultInstance> +</StructureMap> +" + .ToFormat(className); + + + try + { + Container container = DataMother.BuildContainerForXml(xml); + container.GetInstance<ClassWithDictionaryProperty2>(); + } + catch (StructureMapException e) + { + e.ErrorCode.ShouldEqual(202); + } + } + + } + + public class ClassWithDictionaryInCtor + { + private readonly IDictionary<string, string> _values; + + public ClassWithDictionaryInCtor(IDictionary<string, string> values) + { + _values = values; + } + + public IDictionary<string, string> Values + { + get { return _values; } + } + } + + public class ClassWithDictionaryProperty + { + public IDictionary<string, string> Values { get; set; } + } + + public class ClassWithDictionaryProperty2 + { + [SetterProperty] + public IDictionary<string, string> Values { get; set; } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-12-20 18:16:59 UTC (rev 205) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-12-20 18:50:25 UTC (rev 206) @@ -178,6 +178,7 @@ <Compile Include="AutoMocking\MoqFactoryTester.cs" /> <Compile Include="AutoMocking\RhinoAutoMockerTester.cs" /> <Compile Include="AutoMocking\RhinoMockRepositoryProxyTester.cs" /> + <Compile Include="Bugs\IDictionaryAndXmlBugTester.cs" /> <Compile Include="Bugs\ScanIndexerBugTester.cs" /> <Compile Include="BuildSessionTester.cs" /> <Compile Include="Configuration\ConfigurationParserBuilderTester.cs" /> Modified: trunk/Source/StructureMap.Testing/TestData/DataMother.cs =================================================================== --- trunk/Source/StructureMap.Testing/TestData/DataMother.cs 2008-12-20 18:16:59 UTC (rev 205) +++ trunk/Source/StructureMap.Testing/TestData/DataMother.cs 2008-12-20 18:50:25 UTC (rev 206) @@ -17,6 +17,12 @@ { } + public static Container BuildContainerForXml(string xml) + { + var graph = BuildPluginGraphFromXml(xml); + return new Container(graph); + } + public static PluginGraph BuildPluginGraphFromXml(string xml) { XmlDocument document = BuildDocument(xml); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |