|
From: <jer...@us...> - 2008-06-05 13:32:35
|
Revision: 118
http://structuremap.svn.sourceforge.net/structuremap/?rev=118&view=rev
Author: jeremydmiller
Date: 2008-06-05 06:32:32 -0700 (Thu, 05 Jun 2008)
Log Message:
-----------
missed checkin stuff
Modified Paths:
--------------
trunk/Source/StructureMap/ObjectFactory.cs
Added Paths:
-----------
trunk/Source/StructureMap.Testing/Graph/ExceptionHandling/
trunk/Source/StructureMap.Testing/Graph/ExceptionHandling/StructureMapExceptionTester.cs
trunk/Source/StructureMap.Testing/Graph/Interceptors/
trunk/Source/StructureMap.Testing/Graph/Interceptors/CompoundInterceptorTester.cs
trunk/Source/StructureMap.Testing/Graph/Interceptors/InterceptorLibraryTester.cs
trunk/Source/StructureMap.Testing/Graph/Interceptors/MockTypeInterceptor.cs
trunk/Source/StructureMap.Testing/Graph/Interceptors/TypeInterceptionTester.cs
Modified: trunk/Source/StructureMap/ObjectFactory.cs
===================================================================
--- trunk/Source/StructureMap/ObjectFactory.cs 2008-06-02 16:28:50 UTC (rev 117)
+++ trunk/Source/StructureMap/ObjectFactory.cs 2008-06-05 13:32:32 UTC (rev 118)
@@ -33,6 +33,19 @@
}
/// <summary>
+ /// Restarts ObjectFactory and blows away all Singleton's and cached instances. Use with caution.
+ /// </summary>
+ public static void Reset()
+ {
+ _manager = buildManager();
+
+ if (_notify != null)
+ {
+ _notify();
+ }
+ }
+
+ /// <summary>
/// Attempts to create a new instance of the requested type. Automatically inserts the default
/// configured instance for each dependency in the StructureMap constructor function.
/// </summary>
@@ -70,7 +83,12 @@
manager.InjectStub(typeof (PLUGINTYPE), stub);
}
+ public static void InjectStub<PLUGINTYPE>(string name, PLUGINTYPE stub)
+ {
+ manager.InjectStub<PLUGINTYPE>(name, stub);
+ }
+
public static string WhatDoIHave()
{
return manager.WhatDoIHave();
@@ -124,18 +142,7 @@
get { return _profile; }
}
- /// <summary>
- /// Restarts ObjectFactory. Use with caution.
- /// </summary>
- public static void Reset()
- {
- _manager = buildManager();
- if (_notify != null)
- {
- _notify();
- }
- }
/// <summary>
/// Strictly used for testing scenarios
@@ -178,7 +185,8 @@
/// <summary>
- /// Restores all default instance settings according to the StructureMap.config files
+ /// Restores all default instance settings and removes any Profile settings applied
+ /// at runtime
/// </summary>
public static void ResetDefaults()
{
Added: trunk/Source/StructureMap.Testing/Graph/ExceptionHandling/StructureMapExceptionTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Graph/ExceptionHandling/StructureMapExceptionTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Graph/ExceptionHandling/StructureMapExceptionTester.cs 2008-06-05 13:32:32 UTC (rev 118)
@@ -0,0 +1,237 @@
+using System;
+using System.IO;
+using System.Runtime.Serialization.Formatters.Binary;
+using System.Xml;
+using NUnit.Framework;
+using StructureMap.Configuration;
+using StructureMap.Graph;
+using StructureMap.Testing.TestData;
+using StructureMap.Testing.Widget;
+
+namespace StructureMap.Testing.Graph.ExceptionHandling
+{
+ [TestFixture]
+ public class StructureMapExceptionTester
+ {
+ private void assertErrorIsLogged(int errorCode, string xml)
+ {
+ PluginGraph graph = DataMother.BuildPluginGraphFromXml(xml);
+ graph.Log.AssertHasError(errorCode);
+ }
+
+
+ private void assertErrorIsThrown(int errorCode, string xml, Action<Container> action)
+ {
+ XmlDocument document = new XmlDocument();
+ document.LoadXml(xml.Replace("\"", "'"));
+
+ ConfigurationParser parser = new ConfigurationParser(document.DocumentElement);
+ PluginGraphBuilder builder = new PluginGraphBuilder(parser);
+ Container manager = new Container(builder.Build());
+
+ try
+ {
+ action(manager);
+ Assert.Fail("Should have thrown exception");
+ }
+ catch (StructureMapException ex)
+ {
+ Assert.AreEqual(errorCode, ex.ErrorCode, "Expected error code");
+ }
+ }
+
+ [Test]
+ public void CanSerialize()
+ {
+ ApplicationException ex = new ApplicationException("Oh no!");
+ StructureMapException smapEx = new StructureMapException(200, ex, "a", "b");
+
+ BinaryFormatter formatter = new BinaryFormatter();
+ MemoryStream stream = new MemoryStream();
+ formatter.Serialize(stream, smapEx);
+
+ stream.Position = 0;
+
+ StructureMapException smapEx2 = (StructureMapException) formatter.Deserialize(stream);
+ Assert.AreNotSame(smapEx, smapEx2);
+
+ Assert.AreEqual(smapEx.Message, smapEx2.Message);
+ }
+
+ [Test]
+ public void CouldNotFindConcreteKey()
+ {
+ assertErrorIsLogged(201,
+ @"
+ <StructureMap>
+ <Assembly Name='StructureMap.Testing.Widget'/>
+
+ <PluginFamily Type='StructureMap.Testing.Widget.IWidget' Assembly='StructureMap.Testing.Widget' DefaultKey=''>
+ <Instance Key='BadConcreteKey' Type='NotARealConcreteKey'></Instance>
+ </PluginFamily>
+ </StructureMap>
+ ");
+ }
+
+ [Test]
+ public void CouldNotFindInstanceKey()
+ {
+ assertErrorIsThrown(200,
+ @"
+ <StructureMap>
+ <Assembly Name='StructureMap.Testing.Widget'/>
+
+ <PluginFamily Type='StructureMap.Testing.Widget.IWidget' Assembly='StructureMap.Testing.Widget' DefaultKey=''> </PluginFamily>
+ </StructureMap>
+ ",
+ delegate(Container manager) { manager.GetInstance<IWidget>("NotAnActualInstanceName"); }
+ );
+ }
+
+ [Test]
+ public void CouldNotUpcastDesignatedPluggedTypeIntoPluginType()
+ {
+ assertErrorIsLogged(104,
+ @"
+ <StructureMap>
+ <Assembly Name='StructureMap.Testing.Widget'/>
+
+ <PluginFamily Type='StructureMap.Testing.Widget.IWidget' Assembly='StructureMap.Testing.Widget' DefaultKey=''>
+ <Plugin Assembly='StructureMap.Testing.Widget' Type='StructureMap.Testing.Widget.ComplexRule' ConcreteKey='Rule'/>
+ </PluginFamily>
+ </StructureMap>
+ ");
+ }
+
+ [Test]
+ public void ExceptionMessage()
+ {
+ StructureMapException exception = new StructureMapException(100, "StructureMap.config");
+ string expected =
+ "StructureMap Exception Code: 100\nExpected file \"StructureMap.config\" cannot be opened at StructureMap.config";
+
+ string actual = exception.Message;
+
+ Assert.AreEqual(expected, actual);
+ }
+
+ [Test]
+ public void Log_101_if_CannotLoadAssemblyInAssemblyNode()
+ {
+ assertErrorIsLogged(101,
+ @"
+ <StructureMap>
+ <Assembly Name='StructureMap.NonExistent'/>
+ </StructureMap>
+ ");
+ }
+
+
+ [Test]
+ public void Log_103_CannotLoadTypeFromPluginFamilyNode()
+ {
+ assertErrorIsLogged(103,
+ @"
+ <StructureMap>
+ <PluginFamily Assembly='StructureMap.Testing.Widget' Type='StructureMap.Testing.Widget.NotARealType'/>
+ </StructureMap>
+ ");
+ }
+
+ [Test]
+ public void Log_112_if_MissingConcreteKeyOnPluginNode()
+ {
+ assertErrorIsLogged(112,
+ @"
+ <StructureMap>
+ <Assembly Name='StructureMap.Testing.Widget'/>
+
+ <PluginFamily Type='StructureMap.Testing.Widget.IWidget' Assembly='StructureMap.Testing.Widget' DefaultKey=''>
+ <Plugin Assembly='StructureMap.Testing.Widget' Type='StructureMap.Testing.Widget.NotPluggableWidget' ConcreteKey=''/>
+ </PluginFamily>
+ </StructureMap>
+");
+ }
+
+ [Test]
+ public void Log_113_if_a_duplicate_Plugin_ConcreteKey_is_detected()
+ {
+ assertErrorIsLogged(113,
+ @"
+
+ <StructureMap>
+ <Assembly Name='StructureMap.Testing.Widget'/>
+
+ <PluginFamily Type='StructureMap.Testing.Widget.IWidget' Assembly='StructureMap.Testing.Widget' DefaultKey=''>
+ <Plugin Assembly='StructureMap.Testing.Widget' Type='StructureMap.Testing.Widget.NotPluggableWidget' ConcreteKey='Dup'/>
+ <Plugin Assembly='StructureMap.Testing.Widget' Type='StructureMap.Testing.Widget.NotPluggableWidget' ConcreteKey='Dup'/>
+ </PluginFamily>
+ </StructureMap>
+
+");
+ }
+
+ [Test]
+ public void Log_130_if_an_error_occurs_when_trying_to_create_an_interceptor_configured_in_xml()
+ {
+ assertErrorIsLogged(130,
+ @"
+ <StructureMap>
+ <Assembly Name='StructureMap.Testing.Widget'/>
+
+ <PluginFamily Type='StructureMap.Testing.Widget.IWidget' Assembly='StructureMap.Testing.Widget' DefaultKey=''>
+ <Interceptors>
+ <Interceptor Type='NotARealType'></Interceptor>
+ </Interceptors>
+ </PluginFamily>
+ </StructureMap>
+");
+ }
+
+ [Test]
+ public void Log_130_if_there_is_an_error_while_creating_TheDesignatedMementoSourceForAPluginFamily()
+ {
+ assertErrorIsLogged(130,
+ @"
+ <StructureMap>
+ <Assembly Name='StructureMap.Testing.Widget'/>
+
+ <PluginFamily Type='StructureMap.Testing.Widget.IWidget' Assembly='StructureMap.Testing.Widget' DefaultKey=''>
+ <Plugin Assembly='StructureMap.Testing.Widget' Type='StructureMap.Testing.Widget.NotPluggableWidget' ConcreteKey='Dup'/>
+ <Source Type='Nonexistent'/>
+ </PluginFamily>
+ </StructureMap>
+");
+ }
+
+ [Test]
+ public void Log_131_if_Plugin_type_could_not_be_loaded_from_Xml_configuration()
+ {
+ assertErrorIsLogged(131,
+ @"
+ <StructureMap>
+ <Assembly Name='StructureMap.Testing.Widget'/>
+
+ <PluginFamily Type='StructureMap.Testing.Widget.IWidget' Assembly='StructureMap.Testing.Widget' DefaultKey=''>
+ <Plugin Assembly='StructureMap.Testing' Type='StructureMap.Testing.Widget.NotARealClass' ConcreteKey='NotReal'/>
+ </PluginFamily>
+ </StructureMap>
+ ");
+ }
+
+ [Test]
+ public void Throw_202_when_DefaultKeyDoesNotExist()
+ {
+ assertErrorIsThrown(202,
+ @"
+ <StructureMap>
+ <Assembly Name='StructureMap.Testing.Widget'/>
+
+ <PluginFamily Type='StructureMap.Testing.Widget.IWidget' Assembly='StructureMap.Testing.Widget' DefaultKey=''></PluginFamily>
+ </StructureMap>
+ ",
+ delegate(Container manager) { manager.GetInstance<IWidget>(); }
+ );
+ }
+ }
+}
\ No newline at end of file
Added: trunk/Source/StructureMap.Testing/Graph/Interceptors/CompoundInterceptorTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Graph/Interceptors/CompoundInterceptorTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Graph/Interceptors/CompoundInterceptorTester.cs 2008-06-05 13:32:32 UTC (rev 118)
@@ -0,0 +1,46 @@
+using NUnit.Framework;
+using Rhino.Mocks;
+using StructureMap.Interceptors;
+
+namespace StructureMap.Testing.Graph.Interceptors
+{
+ [TestFixture]
+ public class CompoundInterceptorTester
+ {
+ #region Setup/Teardown
+
+ [SetUp]
+ public void SetUp()
+ {
+ }
+
+ #endregion
+
+ [Test]
+ public void CallAllTheInterceptors()
+ {
+ MockRepository mocks = new MockRepository();
+ InstanceInterceptor interceptor1 = mocks.CreateMock<InstanceInterceptor>();
+ InstanceInterceptor interceptor2 = mocks.CreateMock<InstanceInterceptor>();
+ InstanceInterceptor interceptor3 = mocks.CreateMock<InstanceInterceptor>();
+ InstanceInterceptor interceptor4 = mocks.CreateMock<InstanceInterceptor>();
+
+ Expect.Call(interceptor1.Process("0")).Return("1");
+ Expect.Call(interceptor2.Process("1")).Return("2");
+ Expect.Call(interceptor3.Process("2")).Return("3");
+ Expect.Call(interceptor4.Process("3")).Return("4");
+
+ mocks.ReplayAll();
+ CompoundInterceptor compoundInterceptor = new CompoundInterceptor(new InstanceInterceptor[]
+ {
+ interceptor1,
+ interceptor2,
+ interceptor3,
+ interceptor4
+ });
+
+ Assert.AreEqual("4", compoundInterceptor.Process("0"));
+ mocks.VerifyAll();
+ }
+ }
+}
\ No newline at end of file
Added: trunk/Source/StructureMap.Testing/Graph/Interceptors/InterceptorLibraryTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Graph/Interceptors/InterceptorLibraryTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Graph/Interceptors/InterceptorLibraryTester.cs 2008-06-05 13:32:32 UTC (rev 118)
@@ -0,0 +1,156 @@
+using System;
+using NUnit.Framework;
+using Rhino.Mocks;
+using StructureMap.Interceptors;
+
+namespace StructureMap.Testing.Graph.Interceptors
+{
+ [TestFixture]
+ public class InterceptorLibraryTester
+ {
+ #region Setup/Teardown
+
+ [SetUp]
+ public void SetUp()
+ {
+ _interceptor1 = new MockTypeInterceptor(typeof (string));
+ _interceptor2 = new MockTypeInterceptor(typeof (int), typeof (double));
+ _interceptor3 = new MockTypeInterceptor(typeof (string), typeof (bool));
+ _interceptor4 = new MockTypeInterceptor(typeof (string), typeof (double));
+
+ _library = new InterceptorLibrary();
+ _library.AddInterceptor(_interceptor1);
+ _library.AddInterceptor(_interceptor2);
+ _library.AddInterceptor(_interceptor3);
+ _library.AddInterceptor(_interceptor4);
+ }
+
+ #endregion
+
+ private MockTypeInterceptor _interceptor1;
+ private MockTypeInterceptor _interceptor2;
+ private MockTypeInterceptor _interceptor3;
+ private MockTypeInterceptor _interceptor4;
+ private InterceptorLibrary _library;
+
+ [Test]
+ public void Find_All_Of_The_Interceptors_For_A_Type_On_Multiple_Passes()
+ {
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor1, _interceptor3, _interceptor4},
+ _library.FindInterceptors(typeof (string)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor2, _interceptor4},
+ _library.FindInterceptors(typeof (double)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor2}, _library.FindInterceptors(typeof (int)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor3}, _library.FindInterceptors(typeof (bool)));
+
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor1, _interceptor3, _interceptor4},
+ _library.FindInterceptors(typeof (string)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor2, _interceptor4},
+ _library.FindInterceptors(typeof (double)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor2}, _library.FindInterceptors(typeof (int)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor3}, _library.FindInterceptors(typeof (bool)));
+
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor1, _interceptor3, _interceptor4},
+ _library.FindInterceptors(typeof (string)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor2, _interceptor4},
+ _library.FindInterceptors(typeof (double)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor2}, _library.FindInterceptors(typeof (int)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor3}, _library.FindInterceptors(typeof (bool)));
+
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor1, _interceptor3, _interceptor4},
+ _library.FindInterceptors(typeof (string)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor2, _interceptor4},
+ _library.FindInterceptors(typeof (double)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor2}, _library.FindInterceptors(typeof (int)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor3}, _library.FindInterceptors(typeof (bool)));
+ }
+
+ [Test]
+ public void Find_All_Of_The_Interceptors_For_A_Type_On_The_First_Pass()
+ {
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor1, _interceptor3, _interceptor4},
+ _library.FindInterceptors(typeof (string)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor2, _interceptor4},
+ _library.FindInterceptors(typeof (double)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor2}, _library.FindInterceptors(typeof (int)));
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor3}, _library.FindInterceptors(typeof (bool)));
+ }
+
+ [Test]
+ public void Find_CompoundInterceptor_For_A_Type_On_The_First_Pass()
+ {
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor1, _interceptor3, _interceptor4},
+ _library.FindInterceptor(typeof (string)).Interceptors);
+ Assert.AreEqual(new TypeInterceptor[] {_interceptor2, _interceptor4},
+ _library.FindInterceptor(typeof (double)).Interceptors);
+ }
+
+ [Test]
+ public void Import_from_gets_all_interceptors_and_resets_the_type_filter()
+ {
+ InterceptorLibrary sourceLibrary = new InterceptorLibrary();
+ sourceLibrary.AddInterceptor(new MockTypeInterceptor(typeof (string)));
+ sourceLibrary.FindInterceptor(typeof (string));
+
+ InterceptorLibrary destinationLibrary = new InterceptorLibrary();
+ destinationLibrary.AddInterceptor(new MockTypeInterceptor(typeof (string)));
+
+ destinationLibrary.ImportFrom(sourceLibrary);
+
+ InstanceInterceptor[] interceptors = destinationLibrary.FindInterceptors(typeof (string));
+ Assert.AreEqual(2, interceptors.Length);
+ }
+
+
+ [Test]
+ public void
+ When_Interceptors_Are_Requested_For_A_Type_For_The_First_Time_The_Library_Will_Scan_All_The_TypeInterceptors
+ ()
+ {
+ MockRepository mocks = new MockRepository();
+ TypeInterceptor interceptor1 = mocks.CreateMock<TypeInterceptor>();
+ TypeInterceptor interceptor2 = mocks.CreateMock<TypeInterceptor>();
+ TypeInterceptor interceptor3 = mocks.CreateMock<TypeInterceptor>();
+
+ _library.AddInterceptor(interceptor1);
+ _library.AddInterceptor(interceptor2);
+ _library.AddInterceptor(interceptor3);
+
+ Type type = typeof (string);
+ Expect.Call(interceptor1.MatchesType(type)).Return(true);
+ Expect.Call(interceptor2.MatchesType(type)).Return(true);
+ Expect.Call(interceptor3.MatchesType(type)).Return(true);
+
+ mocks.ReplayAll();
+ _library.FindInterceptors(type);
+ mocks.VerifyAll();
+ }
+
+ [Test]
+ public void When_Interceptors_Are_Requested_For_The_Second_Time_The_Library_Will_NOT_Scan_The_Interceptors_Again
+ ()
+ {
+ MockRepository mocks = new MockRepository();
+ TypeInterceptor interceptor1 = mocks.CreateMock<TypeInterceptor>();
+ TypeInterceptor interceptor2 = mocks.CreateMock<TypeInterceptor>();
+ TypeInterceptor interceptor3 = mocks.CreateMock<TypeInterceptor>();
+
+ _library.AddInterceptor(interceptor1);
+ _library.AddInterceptor(interceptor2);
+ _library.AddInterceptor(interceptor3);
+
+ Type type = typeof (string);
+ Expect.Call(interceptor1.MatchesType(type)).Return(true);
+ Expect.Call(interceptor2.MatchesType(type)).Return(true);
+ Expect.Call(interceptor3.MatchesType(type)).Return(true);
+
+ mocks.ReplayAll();
+ _library.FindInterceptors(type);
+ _library.FindInterceptors(type);
+ _library.FindInterceptors(type);
+ _library.FindInterceptors(type);
+ _library.FindInterceptors(type);
+ mocks.VerifyAll();
+ }
+ }
+}
\ No newline at end of file
Added: trunk/Source/StructureMap.Testing/Graph/Interceptors/MockTypeInterceptor.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Graph/Interceptors/MockTypeInterceptor.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Graph/Interceptors/MockTypeInterceptor.cs 2008-06-05 13:32:32 UTC (rev 118)
@@ -0,0 +1,73 @@
+using System;
+using System.Collections.Generic;
+using StructureMap.Interceptors;
+
+namespace StructureMap.Testing.Graph.Interceptors
+{
+ public class MockTypeInterceptor : TypeInterceptor
+ {
+ #region Delegates
+
+ public delegate object InterceptionDelegate<T>(T target);
+
+ #endregion
+
+ private readonly Dictionary<Type, InstanceInterceptor> _innerInterceptors
+ = new Dictionary<Type, InstanceInterceptor>();
+
+ private readonly List<Type> _types = new List<Type>();
+
+ public MockTypeInterceptor(params Type[] types)
+ {
+ _types.AddRange(types);
+ }
+
+ #region TypeInterceptor Members
+
+ public bool MatchesType(Type type)
+ {
+ return _types.Contains(type);
+ }
+
+ public object Process(object target)
+ {
+ return _innerInterceptors[target.GetType()].Process(target);
+ }
+
+ #endregion
+
+ public void SetToMatch<T>()
+ {
+ _types.Add(typeof (T));
+ }
+
+ public void AddHandler<T>(InterceptionDelegate<T> handler)
+ {
+ _types.Add(typeof (T));
+ _innerInterceptors.Add(typeof (T), new CommonInterceptor<T>(handler));
+ }
+
+ #region Nested type: CommonInterceptor
+
+ public class CommonInterceptor<T> : InstanceInterceptor
+ {
+ private readonly InterceptionDelegate<T> _handler;
+
+ public CommonInterceptor(InterceptionDelegate<T> handler)
+ {
+ _handler = handler;
+ }
+
+ #region InstanceInterceptor Members
+
+ public object Process(object target)
+ {
+ return _handler((T) target);
+ }
+
+ #endregion
+ }
+
+ #endregion
+ }
+}
\ No newline at end of file
Added: trunk/Source/StructureMap.Testing/Graph/Interceptors/TypeInterceptionTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Graph/Interceptors/TypeInterceptionTester.cs (rev 0)
+++ trunk/Source/StructureMap.Testing/Graph/Interceptors/TypeInterceptionTester.cs 2008-06-05 13:32:32 UTC (rev 118)
@@ -0,0 +1,119 @@
+using System;
+using NUnit.Framework;
+using StructureMap.Configuration.DSL;
+
+namespace StructureMap.Testing.Graph.Interceptors
+{
+ [TestFixture]
+ public class TypeInterceptionTester : Registry
+ {
+ #region Setup/Teardown
+
+ [SetUp]
+ public void SetUp()
+ {
+ manager = null;
+
+ registry = new Registry();
+ registry.ForRequestedType<IAnInterfaceOfSomeSort>()
+ .AddInstance(Instance<RedSomething>().WithName("Red"))
+ .AddInstance(Instance<GreenSomething>().WithName("Green"))
+ .AddInstance(Instance<BlueSomething>().WithName("Blue"));
+ }
+
+ #endregion
+
+ private Registry registry;
+ private IContainer manager;
+
+ private void assertThisIsType<T>(string name)
+ {
+ if (manager == null)
+ {
+ manager = new Container(registry);
+ }
+
+ Assert.IsInstanceOfType(typeof (T), manager.GetInstance<IAnInterfaceOfSomeSort>(name));
+ }
+
+ private void assertThatThisIsWrappedSomething<OUTERTYPE, INNERTYPE>(string name)
+ where OUTERTYPE : WrappedSomething
+ {
+ if (manager == null)
+ {
+ manager = new Container(registry);
+ }
+
+ OUTERTYPE something = (OUTERTYPE) manager.GetInstance<IAnInterfaceOfSomeSort>(name);
+ Assert.IsInstanceOfType(typeof (INNERTYPE), something.Inner);
+ }
+
+ public interface IAnInterfaceOfSomeSort
+ {
+ }
+
+ public class RedSomething : IAnInterfaceOfSomeSort
+ {
+ }
+
+ public class GreenSomething : IAnInterfaceOfSomeSort
+ {
+ }
+
+ public class BlueSomething : IAnInterfaceOfSomeSort
+ {
+ }
+
+ public class WrappedSomething : IAnInterfaceOfSomeSort
+ {
+ private readonly IAnInterfaceOfSomeSort _inner;
+
+ public WrappedSomething(IAnInterfaceOfSomeSort inner)
+ {
+ _inner = inner;
+ }
+
+
+ public IAnInterfaceOfSomeSort Inner
+ {
+ get { return _inner; }
+ }
+ }
+
+ public class WrappedSomething2 : WrappedSomething
+ {
+ public WrappedSomething2(IAnInterfaceOfSomeSort inner) : base(inner)
+ {
+ }
+ }
+
+ [Test]
+ public void If_An_Interceptor_Is_Registered_At_The_PluginGraph_It_Will_Be_Used_To_Construct_An_Instance()
+ {
+ MockTypeInterceptor interceptor = new MockTypeInterceptor();
+ interceptor.AddHandler<RedSomething>(
+ delegate(RedSomething something) { return new WrappedSomething(something); });
+
+ interceptor.AddHandler<GreenSomething>(
+ delegate(GreenSomething something) { return new WrappedSomething2(something); });
+
+ registry.RegisterInterceptor(interceptor);
+
+ assertThisIsType<BlueSomething>("Blue");
+ assertThatThisIsWrappedSomething<WrappedSomething, RedSomething>("Red");
+ assertThatThisIsWrappedSomething<WrappedSomething2, GreenSomething>("Green");
+ }
+
+ [Test]
+ public void Register_A_Type_Interceptor_By_The_Fluent_Interface()
+ {
+ registry.IfTypeMatches(delegate(Type type) { return type.Equals(typeof (BlueSomething)); })
+ .InterceptWith(
+ delegate(object rawInstance) { return new WrappedSomething((IAnInterfaceOfSomeSort) rawInstance); });
+
+ assertThisIsType<RedSomething>("Red");
+ assertThisIsType<GreenSomething>("Green");
+ assertThatThisIsWrappedSomething<WrappedSomething, BlueSomething>("Blue");
+ }
+ }
+}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|