From: <jer...@us...> - 2007-03-29 18:31:03
|
Revision: 33 http://structuremap.svn.sourceforge.net/structuremap/?rev=33&view=rev Author: jeremydmiller Date: 2007-03-29 11:31:00 -0700 (Thu, 29 Mar 2007) Log Message: ----------- adding diagnostics to StructureMapConfiguration Modified Paths: -------------- trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap/StructureMapConfiguration.cs trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs Added Paths: ----------- trunk/Source/StructureMap/Verification/IStartUp.cs trunk/Source/StructureMap/Verification/StartUp.cs Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2007-03-29 15:58:10 UTC (rev 32) +++ trunk/Source/StructureMap/StructureMap.csproj 2007-03-29 18:31:00 UTC (rev 33) @@ -556,9 +556,11 @@ <Compile Include="StubbedInstanceFactory.cs"> <SubType>Code</SubType> </Compile> + <Compile Include="Verification\IStartUp.cs" /> <Compile Include="Verification\PluginGraphConsoleWriter.cs"> <SubType>Code</SubType> </Compile> + <Compile Include="Verification\StartUp.cs" /> <Compile Include="XmlMapping\ConfigEditor.cs"> <SubType>Code</SubType> </Compile> Modified: trunk/Source/StructureMap/StructureMapConfiguration.cs =================================================================== --- trunk/Source/StructureMap/StructureMapConfiguration.cs 2007-03-29 15:58:10 UTC (rev 32) +++ trunk/Source/StructureMap/StructureMapConfiguration.cs 2007-03-29 18:31:00 UTC (rev 33) @@ -5,6 +5,7 @@ using StructureMap.Configuration; using StructureMap.Configuration.DSL; using StructureMap.Graph; +using StructureMap.Verification; namespace StructureMap { @@ -14,6 +15,7 @@ private static ConfigurationParserCollection _collection = new ConfigurationParserCollection(); private static Registry _registry = new Registry(); private static List<Registry> _registries = new List<Registry>(); + private static StartUp _startUp; static StructureMapConfiguration() { @@ -51,10 +53,16 @@ _registry = new Registry(); _registries = new List<Registry>(); _registries.Add(_registry); + _startUp = null; } public static PluginGraph GetPluginGraph() { + if (_startUp != null) + { + _startUp.RunDiagnostics(); + } + PluginGraphBuilder builder = createBuilder(); return builder.Build(); } @@ -65,10 +73,10 @@ return new PluginGraphBuilder(parsers, _registries.ToArray()); } - public static PluginGraph GetDiagnosticPluginGraph() + public static PluginGraphReport GetDiagnosticReport() { PluginGraphBuilder builder = createBuilder(); - return builder.BuildDiagnosticPluginGraph(); + return builder.Report; } public static void IncludeConfigurationFromFile(string filename) @@ -146,5 +154,15 @@ { _registries.Add(registry); } + + public static IStartUp OnStartUp() + { + if (_startUp == null) + { + _startUp = new StartUp(); + } + + return _startUp; + } } } \ No newline at end of file Added: trunk/Source/StructureMap/Verification/IStartUp.cs =================================================================== --- trunk/Source/StructureMap/Verification/IStartUp.cs (rev 0) +++ trunk/Source/StructureMap/Verification/IStartUp.cs 2007-03-29 18:31:00 UTC (rev 33) @@ -0,0 +1,9 @@ +namespace StructureMap.Verification +{ + public interface IStartUp + { + IStartUp WriteProblemsTo(string fileName); + IStartUp FailOnException(); + IStartUp WriteAllTo(string fileName); + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Verification/StartUp.cs =================================================================== --- trunk/Source/StructureMap/Verification/StartUp.cs (rev 0) +++ trunk/Source/StructureMap/Verification/StartUp.cs 2007-03-29 18:31:00 UTC (rev 33) @@ -0,0 +1,85 @@ +using System; +using System.IO; +using StructureMap.Configuration; + +namespace StructureMap.Verification +{ + public class StartUp : IStartUp + { + private string _problemFile = "StructureMap.error"; + private bool _fail = false; + private string _allFile; + + public IStartUp WriteProblemsTo(string fileName) + { + _problemFile = fileName; + return this; + } + + public IStartUp FailOnException() + { + _fail = true; + return this; + } + + public IStartUp WriteAllTo(string fileName) + { + _allFile = fileName; + return this; + } + + public void RunDiagnostics() + { + PluginGraphReport report = StructureMapConfiguration.GetDiagnosticReport(); + + writeProblems(report); + + writeAll(report); + + fail(report); + } + + private void fail(PluginGraphReport report) + { + if (_fail) + { + ProblemFinder finder = new ProblemFinder(report); + Problem[] problems = finder.GetProblems(); + + if (problems.Length > 0) + { + throw new ApplicationException( + "StructureMap detected configuration or environmental problems. Check the StructureMap error file"); + } + } + } + + private void writeAll(PluginGraphReport report) + { + if (!string.IsNullOrEmpty(_allFile)) + { + PluginGraphConsoleWriter consoleWriter = new PluginGraphConsoleWriter(report); + consoleWriter.WriteAll = true; + consoleWriter.WriteProblems = true; + + using (TextWriter writer = new StreamWriter(_allFile)) + { + consoleWriter.Write(writer); + } + } + } + + private void writeProblems(PluginGraphReport report) + { + using (TextWriter writer = new StreamWriter(_problemFile)) + { + PluginGraphConsoleWriter consoleWriter = new PluginGraphConsoleWriter(report); + consoleWriter.IncludeAllInstances = false; + consoleWriter.IncludePlugins = false; + consoleWriter.IncludeSource = false; + consoleWriter.WriteProblems = true; + consoleWriter.Write(writer); + } + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs 2007-03-29 15:58:10 UTC (rev 32) +++ trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs 2007-03-29 18:31:00 UTC (rev 33) @@ -1,4 +1,7 @@ +using System; +using System.IO; using NUnit.Framework; +using StructureMap.Configuration; using StructureMap.Graph; namespace StructureMap.Testing @@ -20,10 +23,67 @@ } [Test] - public void BuildDiagnosticPluginGraph() + public void BuildReport() { - PluginGraph graph = StructureMapConfiguration.GetDiagnosticPluginGraph(); - Assert.IsNotNull(graph); + PluginGraphReport report = StructureMapConfiguration.GetDiagnosticReport(); + Assert.IsNotNull(report); } + + [Test, ExpectedException(typeof(ApplicationException), "StructureMap detected configuration or environmental problems. Check the StructureMap error file")] + public void OnStartUpFail() + { + StructureMapConfiguration.OnStartUp().FailOnException(); + StructureMapConfiguration.AddInstanceOf<ISomething>().UsingConcreteType<Something>(); + + StructureMapConfiguration.GetPluginGraph(); + } + + [Test] + public void WriteAllFile() + { + string filePath = "all.txt"; + if (File.Exists(filePath)) + { + File.Delete(filePath); + } + + StructureMapConfiguration.OnStartUp().WriteAllTo(filePath); + StructureMapConfiguration.AddInstanceOf<ISomething>().UsingConcreteType<Something>(); + + StructureMapConfiguration.GetPluginGraph(); + + Assert.IsTrue(File.Exists(filePath)); + } + + + [Test] + public void WriteProblems() + { + string filePath = "problems.txt"; + if (File.Exists(filePath)) + { + File.Delete(filePath); + } + + StructureMapConfiguration.OnStartUp().WriteProblemsTo(filePath); + StructureMapConfiguration.AddInstanceOf<ISomething>().UsingConcreteType<Something>(); + + StructureMapConfiguration.GetPluginGraph(); + + Assert.IsTrue(File.Exists(filePath)); + } } + + public interface ISomething + { + + } + + public class Something : ISomething + { + public Something() + { + throw new ApplicationException("You can't make me!"); + } + } } \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |