From: <jer...@us...> - 2008-05-27 23:46:17
|
Revision: 104 http://structuremap.svn.sourceforge.net/structuremap/?rev=104&view=rev Author: jeremydmiller Date: 2008-05-27 16:46:15 -0700 (Tue, 27 May 2008) Log Message: ----------- Killing off the TODO's in the code, lots of defensive programming Modified Paths: -------------- trunk/Source/StructureMap/Configuration/FamilyParser.cs trunk/Source/StructureMap/Configuration/GraphBuilder.cs trunk/Source/StructureMap/Configuration/ProfileBuilder.cs trunk/Source/StructureMap/Diagnostics/Error.cs trunk/Source/StructureMap/Diagnostics/GraphLog.cs trunk/Source/StructureMap/Exceptions/StructureMapException.cs trunk/Source/StructureMap/Graph/Plugin.cs trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs trunk/Source/StructureMap/Pipeline/ProfileManager.cs trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/StructureMapException.resx trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj trunk/Source/StructureMap.Testing/TestData/AttributeNormalized.xml trunk/Source/StructureMap.Testing/TestData/PluggedTypeTest.xml Added Paths: ----------- trunk/Source/StructureMap/ErrorMessages.cs trunk/Source/StructureMap.Testing/Pipeline/ConstructorInstanceTester.cs Modified: trunk/Source/StructureMap/Configuration/FamilyParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/FamilyParser.cs 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap/Configuration/FamilyParser.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -75,7 +75,6 @@ return returnValue; } - // TODO: change to many private void attachMementoSource(PluginFamily family, XmlElement familyElement) { XmlNode sourceNode = familyElement[XmlConstants.MEMENTO_SOURCE_NODE]; Modified: trunk/Source/StructureMap/Configuration/GraphBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/GraphBuilder.cs 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap/Configuration/GraphBuilder.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -99,22 +99,9 @@ } } - public void WithType(TypePath path, string context, Action<Type> action) { - try - { - Type type = path.FindType(); - action(type); - } - catch (StructureMapException ex) - { - _pluginGraph.Log.RegisterError(ex); - } - catch (Exception ex) - { - _pluginGraph.Log.RegisterError(131, ex, path.AssemblyQualifiedName, context); - } + _pluginGraph.Log.WithType(path, context, action); } #endregion Modified: trunk/Source/StructureMap/Configuration/ProfileBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ProfileBuilder.cs 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap/Configuration/ProfileBuilder.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -47,12 +47,11 @@ public void OverrideProfile(TypePath typePath, string instanceKey) { - _pluginGraph.Log.Try(delegate() + _pluginGraph.Log.WithType(typePath, "while trying to add an override for a Profile", delegate(Type pluginType) { ReferencedInstance instance = new ReferencedInstance(instanceKey); - _pluginGraph.SetDefault(_lastProfile, typePath.FindType(), instance); - - }).AndReportErrorAs(107, typePath.AssemblyQualifiedName); + _pluginGraph.SetDefault(_lastProfile, pluginType, instance); + }); } public void AddMachine(string machineName, string profileName) @@ -72,9 +71,13 @@ return; } - // TODO: what if the Type cannot be found? - ReferencedInstance instance = new ReferencedInstance(instanceKey); - _profileManager.SetMachineDefault(typePath.FindType(), instance); + _pluginGraph.Log.WithType(typePath, + "trying to configure a Machine Override", + delegate(Type pluginType) + { + ReferencedInstance instance = new ReferencedInstance(instanceKey); + _profileManager.SetMachineDefault(pluginType, instance); + }); } public void SetDefaultProfileName(string profileName) Modified: trunk/Source/StructureMap/Diagnostics/Error.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/Error.cs 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap/Diagnostics/Error.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -22,10 +22,7 @@ public Error(int errorCode, params object[] args) { _code = errorCode; - string template = getMessage(errorCode); - if (template == null) template = string.Empty; - - _message = string.Format(template, args); + _message = ErrorMessages.GetMessage(errorCode, args); } public Error(int errorCode, Exception ex, params object[] args) : this(errorCode, args) @@ -62,11 +59,6 @@ #endregion - private static string getMessage(int errorCode) - { - ResourceManager resources = new ResourceManager(typeof (StructureMapException)); - return resources.GetString(errorCode.ToString()); - } public override bool Equals(object obj) { Modified: trunk/Source/StructureMap/Diagnostics/GraphLog.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/GraphLog.cs 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap/Diagnostics/GraphLog.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -127,6 +127,23 @@ } } + public void WithType(TypePath path, string context, Action<Type> action) + { + try + { + Type type = path.FindType(); + action(type); + } + catch (StructureMapException ex) + { + RegisterError(ex); + } + catch (Exception ex) + { + RegisterError(131, ex, path.AssemblyQualifiedName, context); + } + } + public TryAction Try(Action action) { return new TryAction(action, this); Added: trunk/Source/StructureMap/ErrorMessages.cs =================================================================== --- trunk/Source/StructureMap/ErrorMessages.cs (rev 0) +++ trunk/Source/StructureMap/ErrorMessages.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Resources; +using System.Text; + +namespace StructureMap +{ + internal static class ErrorMessages + { + public static string GetMessage(int errorCode, params object[] args) + { + string msg = "StructureMap Exception Code: " + errorCode + "\n"; + + for (int i = 0; i < args.Length; i++) + { + object arg = args[i]; + Type type = arg as Type; + if (type != null) + { + args[i] = type.AssemblyQualifiedName; + } + } + + string errorMsg = getMessage(errorCode); + if (errorMsg == null) + { + errorMsg = string.Empty; + } + + return string.Format(errorMsg, args); + } + + private static string getMessage(int errorCode) + { + ResourceManager resources = new ResourceManager(typeof(StructureMapException)); + return resources.GetString(errorCode.ToString()); + } + } +} Modified: trunk/Source/StructureMap/Exceptions/StructureMapException.cs =================================================================== --- trunk/Source/StructureMap/Exceptions/StructureMapException.cs 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap/Exceptions/StructureMapException.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -25,13 +25,17 @@ public StructureMapException(int ErrorCode, params object[] args) : base() { - initialize(ErrorCode, args); + _errorCode = ErrorCode; + _msg = string.Format("StructureMap Exception Code: {0}\n", _errorCode); + _msg += ErrorMessages.GetMessage(ErrorCode, args); } public StructureMapException(int ErrorCode, Exception InnerException, params object[] args) : base(string.Empty, InnerException) { - initialize(ErrorCode, args); + _errorCode = ErrorCode; + _msg = string.Format("StructureMap Exception Code: {0}\n", _errorCode); + _msg += ErrorMessages.GetMessage(ErrorCode, args); } public override string Message @@ -44,38 +48,8 @@ get { return _errorCode; } } - // TODO: Centralize this code somewhere so it isn't duplicated - private void initialize(int errorCode, params object[] args) - { - _errorCode = errorCode; - _msg = "StructureMap Exception Code: " + _errorCode + "\n"; - for (int i = 0; i < args.Length; i++) - { - object arg = args[i]; - Type type = arg as Type; - if (type != null) - { - args[i] = type.AssemblyQualifiedName; - } - } - string errorMsg = getMessage(ErrorCode); - if (errorMsg == null) - { - errorMsg = string.Empty; - } - - _msg += string.Format(errorMsg, args); - } - - private string getMessage(int errorCode) - { - ResourceManager resources = new ResourceManager(GetType()); - - return resources.GetString(errorCode.ToString()); - } - public override void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("errorCode", _errorCode, typeof (int)); Modified: trunk/Source/StructureMap/Graph/Plugin.cs =================================================================== --- trunk/Source/StructureMap/Graph/Plugin.cs 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap/Graph/Plugin.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -24,7 +24,6 @@ { if (concreteKey == string.Empty) { - // TODO: Move into PluginFamily and get the exception logged somewhere throw new StructureMapException(112, pluggedType.FullName); } Modified: trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap/Pipeline/ConstructorInstance.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -26,8 +26,14 @@ protected override object build(Type pluginType, IBuildSession session) { - // TODO: specific error message - return _builder(); + try + { + return _builder(); + } + catch (Exception ex) + { + throw new StructureMapException(207, ex, Name, pluginType); + } } protected override string getDescription() Modified: trunk/Source/StructureMap/Pipeline/ProfileManager.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ProfileManager.cs 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap/Pipeline/ProfileManager.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using StructureMap.Diagnostics; using StructureMap.Graph; namespace StructureMap.Pipeline @@ -36,21 +37,29 @@ get { return _currentProfile.Name; } set { - // TODO: Profile cannot be found - if (string.IsNullOrEmpty(value)) { _currentProfile = _default; } else { + validateHasProfile(value); + _currentProfile = getProfile(value); _default.FillAllTypesInto(_currentProfile); } } } + private void validateHasProfile(string profileName) + { + if (!_profiles.ContainsKey(profileName)) + { + throw new StructureMapException(280, profileName); + } + } + public Instance GetDefault(Type pluginType, string profileName) { Profile profile = getProfile(profileName); @@ -98,7 +107,7 @@ { findMasterInstances(graph); - setProfileDefaults(); + setProfileDefaults(graph.Log); processMachineDefaults(graph); @@ -117,14 +126,18 @@ } } - private void setProfileDefaults() + private void setProfileDefaults(GraphLog log) { if (string.IsNullOrEmpty(_defaultProfileName)) { return; } - // TODO: What if Profile doesn't exist? + if (!_profiles.ContainsKey(_defaultProfileName)) + { + log.RegisterError(280, _defaultProfileName); + } + Profile profile = getProfile(_defaultProfileName); profile.FillAllTypesInto(_default); } @@ -152,7 +165,10 @@ if (!string.IsNullOrEmpty(_defaultMachineProfileName)) { - // TODO: Machine profile name cannot be found + if (!_profiles.ContainsKey(_defaultMachineProfileName)) + { + graph.Log.RegisterError(280, _defaultMachineProfileName); + } Profile profile = getProfile(_defaultMachineProfileName); profile.FillAllTypesInto(_default); } Modified: trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap/Pipeline/PrototypeInstance.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -5,7 +5,7 @@ { public class PrototypeInstance : ExpressedInstance<PrototypeInstance> { - private ICloneable _prototype; + private readonly ICloneable _prototype; public PrototypeInstance(ICloneable prototype) @@ -21,7 +21,6 @@ protected override object build(Type pluginType, IBuildSession session) { - // TODO: VALIDATION IF IT CAN'T BE CAST return _prototype.Clone(); } Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap/StructureMap.csproj 2008-05-27 23:46:15 UTC (rev 104) @@ -373,6 +373,7 @@ <None Include="..\structuremap.snk"> <Link>Properties\structuremap.snk</Link> </None> + <Compile Include="ErrorMessages.cs" /> <Compile Include="Pipeline\ConfiguredInstance.Building.cs" /> <Compile Include="Pipeline\IStructuredInstance.cs" /> </ItemGroup> Modified: trunk/Source/StructureMap/StructureMapException.resx =================================================================== --- trunk/Source/StructureMap/StructureMapException.resx 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap/StructureMapException.resx 2008-05-27 23:46:15 UTC (rev 104) @@ -161,7 +161,7 @@ <value>Invalid property value(s), InstanceKey "{0}"</value> </data> <data name="207" xml:space="preserve"> - <value>Internal exception in the constructor function of the targeted concrete type. InstanceKey "{0}", PluginFamily {1}</value> + <value>Internal exception while creating Instance '{0}' of PluginType {1}. Check the inner exception for more details.</value> </data> <data name="104" xml:space="preserve"> <value>Type {0} cannot be plugged into type {1}</value> @@ -259,4 +259,7 @@ <data name="210" xml:space="preserve"> <value>The designated default instance '{0}' for PluginType {1} cannot be found</value> </data> + <data name="280" xml:space="preserve"> + <value>Requested Profile {0} cannot be found</value> + </data> </root> \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -63,8 +63,31 @@ Assert.AreEqual(instance, _graph.ProfileManager.GetMachineDefault(this.GetType())); } + [Test] + public void Log_131_if_trying_to_register_override_for_a_machine_when_the_PluginType_cannot_be_found() + { + _builder.AddMachine(THE_MACHINE_NAME, "TheProfile"); + + _graph.Log.AssertHasNoError(131); + + _builder.OverrideMachine(new TypePath("not a real type"), "Purple"); + _graph.Log.AssertHasError(131); + } + [Test] + public void Log_131_if_trying_to_register_override_for_a_profile_when_the_PluginType_cannot_be_found() + { + _builder.AddProfile("TheProfile"); + + _graph.Log.AssertHasNoError(131); + + _builder.OverrideProfile(new TypePath("not a real type"), "Purple"); + + _graph.Log.AssertHasError(131); + } + + [Test] public void Do_not_register_a_machine_override_if_it_is_NOT_the_matching_machine() { _builder.AddMachine("Some other machine", "TheProfile"); @@ -73,7 +96,24 @@ Assert.IsNull(_graph.ProfileManager.GetMachineDefault(this.GetType())); } + [Test] + public void Throw_280_if_requesting_an_invalid_profile() + { + try + { + ProfileManager manager = new ProfileManager(); + manager.CurrentProfile = "some profile that does not exist"; + + Assert.Fail("Should have thrown error"); + } + catch (StructureMapException ex) + { + Assert.AreEqual(280, ex.ErrorCode); + } + } + + [Test] public void SetDefaultProfileName() { string theProfileName = "some profile name"; @@ -82,17 +122,6 @@ Assert.AreEqual(theProfileName, _graph.ProfileManager.DefaultProfileName); } - [Test] - public void Override_profile_with_a_bad_TypePath_should_log_107() - { - PluginGraph graph = new PluginGraph(); - ProfileBuilder builder = new ProfileBuilder(graph); - - builder.AddProfile("something"); - builder.OverrideProfile(new TypePath("a type that does not exist"), "key"); - - graph.Log.AssertHasError(107); - } } } \ No newline at end of file Added: trunk/Source/StructureMap.Testing/Pipeline/ConstructorInstanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ConstructorInstanceTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Pipeline/ConstructorInstanceTester.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Text; +using NUnit.Framework; +using StructureMap.Pipeline; +using StructureMap.Testing.Widget; + +namespace StructureMap.Testing.Pipeline +{ + [TestFixture] + public class ConstructorInstanceTester + { + [Test] + public void Sad_path_inner_function_throws_exception_207_with_key_and_plugin_type() + { + ConstructorInstance instance = new ConstructorInstance(delegate() + { + throw new NotImplementedException(); + }); + + try + { + instance.Build(typeof (IWidget), null); + Assert.Fail("Should have thrown an exception"); + } + catch (StructureMapException ex) + { + Assert.AreEqual(207, ex.ErrorCode); + } + } + } +} Modified: trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs 2008-05-27 23:46:15 UTC (rev 104) @@ -243,5 +243,30 @@ Assert.AreSame(_manager.GetDefault(typeof(IBuildPolicy), "Profile"), _manager.GetDefault(typeof(ISomething), "Profile")); Assert.AreSame(_manager.GetDefault(typeof(IBuildPolicy), "Profile2"), _manager.GetDefault(typeof(ISomething), "Profile2")); } + + [Test] + public void Log_280_if_the_default_profile_does_not_exist_upon_call_to_Seal() + { + ProfileManager manager = new ProfileManager(); + manager.DefaultProfileName = "something that doesn't exist"; + + PluginGraph graph = new PluginGraph(); + manager.Seal(graph); + + graph.Log.AssertHasError(280); + + } + + [Test] + public void Log_280_if_the_machine_default_profile_cannot_be_found() + { + ProfileManager manager = new ProfileManager(); + manager.DefaultMachineProfileName = "something that doesn't exist"; + + PluginGraph graph = new PluginGraph(); + manager.Seal(graph); + + graph.Log.AssertHasError(280); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj =================================================================== --- trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj 2008-05-27 23:46:15 UTC (rev 104) @@ -326,6 +326,7 @@ <Compile Include="PipelineGraphTester.cs" /> <Compile Include="Pipeline\BuildStrategiesTester.cs" /> <Compile Include="Pipeline\ConfiguredInstanceTester.cs" /> + <Compile Include="Pipeline\ConstructorInstanceTester.cs" /> <Compile Include="Pipeline\DefaultInstanceTester.cs" /> <Compile Include="Pipeline\InstanceTester.cs" /> <Compile Include="Pipeline\LiteralInstanceTester.cs" /> Modified: trunk/Source/StructureMap.Testing/TestData/AttributeNormalized.xml =================================================================== --- trunk/Source/StructureMap.Testing/TestData/AttributeNormalized.xml 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap.Testing/TestData/AttributeNormalized.xml 2008-05-27 23:46:15 UTC (rev 104) @@ -1,5 +1,5 @@ <!--<?xml version="1.0" encoding="utf-8" ?>--> -<StructureMap DefaultProfile="TheDefaultProfile" MementoStyle="Attribute"> +<StructureMap MementoStyle="Attribute"> <Assembly Name="StructureMap.Testing.Widget"/> <Assembly Name="StructureMap.Testing.Widget2"/> <Assembly Name="StructureMap.Testing.Widget3"/> Modified: trunk/Source/StructureMap.Testing/TestData/PluggedTypeTest.xml =================================================================== --- trunk/Source/StructureMap.Testing/TestData/PluggedTypeTest.xml 2008-05-27 22:12:33 UTC (rev 103) +++ trunk/Source/StructureMap.Testing/TestData/PluggedTypeTest.xml 2008-05-27 23:46:15 UTC (rev 104) @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="utf-8" ?> -<StructureMap DefaultProfile="TheDefaultProfile" MementoStyle="Attribute"> +<StructureMap MementoStyle="Attribute"> <PluginFamily Type="StructureMap.Testing.Widget.IWidget" Assembly="StructureMap.Testing.Widget"> <Instance Key="Me" PluggedType="StructureMap.Testing.Widget.NotPluggableWidget,StructureMap.Testing.Widget" name="Jeremy"/> </PluginFamily> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |