Refactoring Tests
This commit is contained in:
Родитель
15314f5488
Коммит
c4436c7d4a
|
@ -20,6 +20,15 @@ namespace Compiled.Constructor
|
|||
return new UnityContainer(UnityContainer.BuildStrategy.Compiled);
|
||||
}
|
||||
}
|
||||
|
||||
[TestClass]
|
||||
public class Parameters : Unity.Specification.Constructor.Parameters.SpecificationTests
|
||||
{
|
||||
public override IUnityContainer GetContainer()
|
||||
{
|
||||
return new UnityContainer(UnityContainer.BuildStrategy.Compiled);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Resolved.Constructor
|
||||
|
@ -42,4 +51,13 @@ namespace Resolved.Constructor
|
|||
}
|
||||
}
|
||||
|
||||
[TestClass]
|
||||
public class Parameters : Unity.Specification.Constructor.Parameters.SpecificationTests
|
||||
{
|
||||
public override IUnityContainer GetContainer()
|
||||
{
|
||||
return new UnityContainer(UnityContainer.BuildStrategy.Resolved);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,6 +29,15 @@ namespace Compiled.Method
|
|||
return new UnityContainer(UnityContainer.BuildStrategy.Compiled);
|
||||
}
|
||||
}
|
||||
|
||||
[TestClass]
|
||||
public class Parameters : Unity.Specification.Method.Parameters.SpecificationTests
|
||||
{
|
||||
public override IUnityContainer GetContainer()
|
||||
{
|
||||
return new UnityContainer(UnityContainer.BuildStrategy.Compiled);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace Resolved.Method
|
||||
|
@ -59,4 +68,13 @@ namespace Resolved.Method
|
|||
return new UnityContainer(UnityContainer.BuildStrategy.Resolved);
|
||||
}
|
||||
}
|
||||
|
||||
[TestClass]
|
||||
public class Parameters : Unity.Specification.Method.Parameters.SpecificationTests
|
||||
{
|
||||
public override IUnityContainer GetContainer()
|
||||
{
|
||||
return new UnityContainer(UnityContainer.BuildStrategy.Resolved);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Unity;
|
||||
using Unity.Specification.Resolution.Basics;
|
||||
|
||||
namespace Resolution
|
||||
{
|
||||
[TestClass]
|
||||
public class Basics : SpecificationTests
|
||||
{
|
||||
public override IUnityContainer GetContainer()
|
||||
{
|
||||
return new UnityContainer();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Unity;
|
||||
using Unity.Specification.Resolution.Lazy;
|
||||
|
||||
namespace Resolution
|
||||
{
|
||||
[TestClass]
|
||||
public class Lazy : SpecificationTests
|
||||
{
|
||||
public override IUnityContainer GetContainer()
|
||||
{
|
||||
return new UnityContainer();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,60 +0,0 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace Unity.Tests.v5
|
||||
{
|
||||
[TestClass]
|
||||
public class ConstructorWithOutAndRefParametersFixture
|
||||
{
|
||||
|
||||
[TestMethod]
|
||||
public void ResolvingANewInstanceOfTypeWithCtorWithRefParameterThrows()
|
||||
{
|
||||
IUnityContainer container = new UnityContainer();
|
||||
|
||||
try
|
||||
{
|
||||
TypeWithConstructorWithRefParameter instance = container.Resolve<TypeWithConstructorWithRefParameter>();
|
||||
Assert.Fail("should have thrown");
|
||||
}
|
||||
catch (ResolutionFailedException)
|
||||
{
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolvingANewInstanceOfTypeWithCtorWithOutParameterThrows()
|
||||
{
|
||||
IUnityContainer container = new UnityContainer();
|
||||
|
||||
try
|
||||
{
|
||||
TypeWithConstructorWithOutParameter instance = container.Resolve<TypeWithConstructorWithOutParameter>();
|
||||
Assert.Fail("should have thrown");
|
||||
}
|
||||
catch (ResolutionFailedException)
|
||||
{
|
||||
// expected
|
||||
}
|
||||
}
|
||||
|
||||
public class TypeWithConstructorWithRefParameter
|
||||
{
|
||||
public TypeWithConstructorWithRefParameter(ref string ignored)
|
||||
{
|
||||
}
|
||||
|
||||
public int Property { get; set; }
|
||||
}
|
||||
|
||||
public class TypeWithConstructorWithOutParameter
|
||||
{
|
||||
public TypeWithConstructorWithOutParameter(out string ignored)
|
||||
{
|
||||
ignored = null;
|
||||
}
|
||||
|
||||
public int Property { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,260 +0,0 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Tests.v5.TestSupport;
|
||||
|
||||
namespace Unity.Tests.v5.Container
|
||||
{
|
||||
[TestClass]
|
||||
public class ContainerBasicFixture
|
||||
{
|
||||
[TestMethod]
|
||||
public void ResovleIUnityContainer()
|
||||
{
|
||||
UnityContainer uc1 = new UnityContainer();
|
||||
|
||||
Assert.IsNotNull(uc1.Resolve<IUnityContainer>());
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void ResolveRegistered()
|
||||
{
|
||||
UnityContainer uc1 = new UnityContainer();
|
||||
|
||||
uc1.RegisterType<ITest, ATest>();
|
||||
|
||||
var res = uc1.Resolve<ITest>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolveAllOnlyReturnsInterfaceRegistrations()
|
||||
{
|
||||
ITest iTest;
|
||||
|
||||
ATest objA = new ATest();
|
||||
BTest objB = new BTest();
|
||||
CTest objC = new CTest();
|
||||
|
||||
objA.Strtest = "Hi";
|
||||
|
||||
UnityContainer uc1 = new UnityContainer();
|
||||
|
||||
uc1.RegisterType<ITest, ATest>();
|
||||
|
||||
iTest = objA;
|
||||
uc1.RegisterInstance("ATest", iTest);
|
||||
iTest = objB;
|
||||
uc1.RegisterInstance("BTest", iTest);
|
||||
iTest = objC;
|
||||
uc1.RegisterInstance("CTest", iTest);
|
||||
|
||||
List<ATest> list = new List<ATest>(uc1.ResolveAll<ATest>());
|
||||
List<ITest> list3 = new List<ITest>(uc1.ResolveAll<ITest>());
|
||||
|
||||
Assert.AreEqual(0, list.Count);
|
||||
Assert.AreEqual(3, list3.Count);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void NamedMappedInterfaceInstanceRegistrationCanBeResolved()
|
||||
{
|
||||
ITest iTest;
|
||||
ATest objA = null;
|
||||
|
||||
IUnityContainer uc1 = new UnityContainer();
|
||||
|
||||
iTest = objA;
|
||||
uc1.RegisterInstance<ITest>("ATest", new ATest());
|
||||
iTest = (ITest)uc1.Resolve(typeof(ITest), "ATest");
|
||||
|
||||
Assert.IsNotNull(iTest);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void UnnamedMappingThrowsResolutionFailedException()
|
||||
{
|
||||
//The inner exception says {"The type ITest does not have an accessible constructor."} where as it should
|
||||
//specify that the named registration for ITest does not exist
|
||||
|
||||
//There is an unnamed mapping
|
||||
|
||||
ATest objA = null;
|
||||
|
||||
UnityContainer uc1 = new UnityContainer();
|
||||
|
||||
uc1.RegisterType<ITest, ATest>();
|
||||
AssertHelper.ThrowsException<ResolutionFailedException>(() => objA = (ATest)uc1.Resolve<ITest>("ATest"));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WhenInstanceIsRegisteredAsSingletonEnsureItIsNotGarbageCollected()
|
||||
{
|
||||
ITest iTest;
|
||||
BTest objB = new BTest();
|
||||
|
||||
IUnityContainer uc1 = new UnityContainer();
|
||||
|
||||
uc1.RegisterType<ITest, ATest>("ATest");
|
||||
iTest = objB;
|
||||
|
||||
uc1.RegisterInstance<ITest>("BTest", iTest);
|
||||
|
||||
iTest = (ITest)uc1.Resolve(typeof(ITest), "BTest");
|
||||
Assert.IsNotNull(iTest);
|
||||
|
||||
iTest = null;
|
||||
|
||||
GC.Collect();
|
||||
|
||||
iTest = (ITest)uc1.Resolve(typeof(ITest), "BTest");
|
||||
|
||||
Assert.IsNotNull(iTest);
|
||||
|
||||
iTest = (ITest)uc1.Resolve(typeof(ITest), "ATest");
|
||||
|
||||
Assert.IsNotNull(iTest);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResovleCollection()
|
||||
{
|
||||
UnityContainer uc1 = new UnityContainer();
|
||||
|
||||
uc1.RegisterType<ITestColl, BTestColl>("BTest");
|
||||
BTestColl b = (BTestColl)uc1.Resolve<BTestColl>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CollectionWhenConstructorParameterNotRegisteredThrowsResolutionFailedException()
|
||||
{
|
||||
UnityContainer uc1 = new UnityContainer();
|
||||
|
||||
uc1.RegisterType<ITestColl, CTestColl>("CTest");
|
||||
AssertHelper.ThrowsException<ResolutionFailedException>(() => uc1.Resolve<CTestColl>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void WhenResolvePrimitiveThrowsResolutionFailedException()
|
||||
{
|
||||
// Primitive type not supported
|
||||
UnityContainer uc1 = new UnityContainer();
|
||||
|
||||
uc1.RegisterType<int>("i");
|
||||
AssertHelper.ThrowsException<ResolutionFailedException>(() => uc1.Resolve<int>("i"));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolveListAtest()
|
||||
{
|
||||
////List of class type not supported
|
||||
UnityContainer uc1 = new UnityContainer();
|
||||
|
||||
uc1.RegisterType<List<ATest>>("List");
|
||||
AssertHelper.ThrowsException<ResolutionFailedException>(() => uc1.Resolve<IList<ATest>>("List"));
|
||||
}
|
||||
|
||||
#region Basic Parameterized Constructor
|
||||
|
||||
[TestMethod]
|
||||
public void ResolveListAtestAsParameterToConstructor()
|
||||
{
|
||||
////Constructor of List of class as parameter not supported
|
||||
UnityContainer uc1 = new UnityContainer();
|
||||
|
||||
uc1.RegisterType<ListOfClassParameter>("List");
|
||||
AssertHelper.ThrowsException<ResolutionFailedException>(() => uc1.Resolve<ListOfClassParameter>("List"));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolveArrayOfAtestAsParameterToConstructor()
|
||||
{
|
||||
UnityContainer uc1 = new UnityContainer();
|
||||
|
||||
uc1.RegisterType<ArrParameter>("Array");
|
||||
ArrParameter arr = (ArrParameter)uc1.Resolve<ArrParameter>("Array");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolveDotNetClassAsParameterToConstructor()
|
||||
{
|
||||
//Constructor Int32 as parameter not supported
|
||||
UnityContainer uc1 = new UnityContainer();
|
||||
Int32 i32 = new Int32();
|
||||
uc1.RegisterInstance<Int32>(i32);
|
||||
|
||||
uc1.RegisterType<IntParameter>("Int32");
|
||||
IntParameter int32 = uc1.Resolve<IntParameter>("Int32");
|
||||
}
|
||||
|
||||
#endregion Basic Parameterized Constructor
|
||||
}
|
||||
|
||||
public interface ITest
|
||||
{ }
|
||||
|
||||
public class ATest : ITest
|
||||
{
|
||||
public string Strtest = "Hello";
|
||||
}
|
||||
|
||||
public class BTest : ATest
|
||||
{ }
|
||||
|
||||
public class CTest : BTest
|
||||
{ }
|
||||
|
||||
public interface ITestColl
|
||||
{ }
|
||||
|
||||
public class ATestColl : ITestColl
|
||||
{
|
||||
public string Strtest = "Hello";
|
||||
|
||||
public ATestColl()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class PremitiveParameter : ITestColl
|
||||
{
|
||||
public PremitiveParameter(int i)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class ListOfClassParameter : ITestColl
|
||||
{
|
||||
public ListOfClassParameter(IList<ATest> lst)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class IntParameter : ITestColl
|
||||
{
|
||||
public IntParameter(Int32 i32)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class ArrParameter : ITestColl
|
||||
{
|
||||
public ArrParameter(ATest[] i)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class BTestColl : ATestColl
|
||||
{
|
||||
public BTestColl(ATestColl[] acoll)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class CTestColl : ITestColl
|
||||
{
|
||||
public CTestColl(char acoll)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,122 +0,0 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Unity.Exceptions;
|
||||
using Unity.Tests.v5.TestSupport;
|
||||
|
||||
namespace Unity.Tests.v5
|
||||
{
|
||||
[TestClass]
|
||||
public class DependencyArrayAttributeFixture
|
||||
{
|
||||
[TestMethod]
|
||||
public void CanResolveArrayForConstructorParameter()
|
||||
{
|
||||
ILogger o1 = new MockLogger();
|
||||
ILogger o2 = new SpecialLogger();
|
||||
|
||||
IUnityContainer container
|
||||
= new UnityContainer()
|
||||
.RegisterInstance<ILogger>("o1", o1)
|
||||
.RegisterInstance<ILogger>("o2", o2);
|
||||
|
||||
TypeWithArrayConstructorParameter resolved = container.Resolve<TypeWithArrayConstructorParameter>();
|
||||
|
||||
Assert.IsNotNull(resolved.Loggers);
|
||||
Assert.AreEqual(2, resolved.Loggers.Length);
|
||||
Assert.AreSame(o1, resolved.Loggers[0]);
|
||||
Assert.AreSame(o2, resolved.Loggers[1]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolveArrayForProperty()
|
||||
{
|
||||
ILogger o1 = new MockLogger();
|
||||
ILogger o2 = new SpecialLogger();
|
||||
|
||||
IUnityContainer container
|
||||
= new UnityContainer()
|
||||
.RegisterInstance<ILogger>("o1", o1)
|
||||
.RegisterInstance<ILogger>("o2", o2);
|
||||
|
||||
TypeWithArrayProperty resolved = container.Resolve<TypeWithArrayProperty>();
|
||||
|
||||
Assert.IsNotNull(resolved.Loggers);
|
||||
Assert.AreEqual(2, resolved.Loggers.Length);
|
||||
Assert.AreSame(o1, resolved.Loggers[0]);
|
||||
Assert.AreSame(o2, resolved.Loggers[1]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolveArrayForConstructorParameterOnClosedGenericType()
|
||||
{
|
||||
ILogger o1 = new MockLogger();
|
||||
ILogger o2 = new SpecialLogger();
|
||||
|
||||
IUnityContainer container
|
||||
= new UnityContainer()
|
||||
.RegisterInstance<ILogger>("o1", o1)
|
||||
.RegisterInstance<ILogger>("o2", o2);
|
||||
|
||||
GenericTypeWithArrayConstructorParameter<ILogger> resolved
|
||||
= container.Resolve<GenericTypeWithArrayConstructorParameter<ILogger>>();
|
||||
|
||||
Assert.IsNotNull(resolved.Values);
|
||||
Assert.AreEqual(2, resolved.Values.Length);
|
||||
Assert.AreSame(o1, resolved.Values[0]);
|
||||
Assert.AreSame(o2, resolved.Values[1]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void BindingDependencyArrayToArrayParameterWithRankOverOneThrows()
|
||||
{
|
||||
IUnityContainer container = new UnityContainer();
|
||||
|
||||
try
|
||||
{
|
||||
container.Resolve<TypeWithArrayConstructorParameterOfRankTwo>();
|
||||
Assert.Fail("Call to Resolve<>() should have failed");
|
||||
}
|
||||
catch (ResolutionFailedException)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public class TypeWithArrayConstructorParameter
|
||||
{
|
||||
public readonly ILogger[] Loggers;
|
||||
|
||||
public TypeWithArrayConstructorParameter(ILogger[] loggers)
|
||||
{
|
||||
this.Loggers = loggers;
|
||||
}
|
||||
}
|
||||
|
||||
public class GenericTypeWithArrayConstructorParameter<T>
|
||||
{
|
||||
public readonly T[] Values;
|
||||
|
||||
public GenericTypeWithArrayConstructorParameter(T[] values)
|
||||
{
|
||||
this.Values = values;
|
||||
}
|
||||
}
|
||||
|
||||
public class TypeWithArrayProperty
|
||||
{
|
||||
private ILogger[] loggers;
|
||||
|
||||
[Dependency]
|
||||
public ILogger[] Loggers
|
||||
{
|
||||
get { return loggers; }
|
||||
set { this.loggers = value; }
|
||||
}
|
||||
}
|
||||
|
||||
public class TypeWithArrayConstructorParameterOfRankTwo
|
||||
{
|
||||
public TypeWithArrayConstructorParameterOfRankTwo(ILogger[,] loggers)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,96 +0,0 @@
|
|||
using System;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Unity.Tests.TestObjects;
|
||||
|
||||
namespace Unity.Tests.v5.Lazy
|
||||
{
|
||||
[TestClass]
|
||||
public class LazyFixture
|
||||
{
|
||||
[TestMethod]
|
||||
public void ResolveLazyHavingDependencies()
|
||||
{
|
||||
IUnityContainer container = new UnityContainer();
|
||||
|
||||
container.RegisterType<IService, EmailService>();
|
||||
container.RegisterType<IBase, Base>();
|
||||
var lazy = container.Resolve<Lazy<IBase>>();
|
||||
|
||||
Assert.IsFalse(lazy.IsValueCreated);
|
||||
|
||||
var value = lazy.Value;
|
||||
|
||||
Assert.IsNotNull(value.Service);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolveLazyHavingLazyDependencies()
|
||||
{
|
||||
IUnityContainer container = new UnityContainer();
|
||||
|
||||
container.RegisterType<Lazy<EmailService>>();
|
||||
//container.RegisterType<IService, EmailService>();
|
||||
container.RegisterType<ILazyDependency, LazyDependency>();
|
||||
var lazy = container.Resolve<Lazy<ILazyDependency>>();
|
||||
|
||||
Assert.IsFalse(lazy.IsValueCreated);
|
||||
|
||||
var ld = (LazyDependency)lazy.Value;
|
||||
|
||||
Assert.IsFalse(ld.Service.IsValueCreated);
|
||||
Assert.IsNotNull(ld.Service.Value);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void RegisterLazyInstanceAndResolve()
|
||||
{
|
||||
IUnityContainer container = new UnityContainer();
|
||||
|
||||
var lazy = new Lazy<EmailService>();
|
||||
container.RegisterInstance(lazy);
|
||||
//container.RegisterType<IService, EmailService>();
|
||||
var lazy1 = container.Resolve<Lazy<EmailService>>();
|
||||
var lazy3 = container.Resolve<Lazy<EmailService>>();
|
||||
|
||||
Assert.IsTrue(lazy == lazy1);
|
||||
Assert.IsTrue(lazy == lazy3);
|
||||
Assert.IsFalse(lazy.IsValueCreated);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void BuildupLazyInstance()
|
||||
{
|
||||
IUnityContainer container = new UnityContainer();
|
||||
|
||||
container.RegisterType<Lazy<EmailService>>();
|
||||
var lazyDependency = new Lazy<LazyDependency>();
|
||||
var lazy = container.BuildUp(lazyDependency);
|
||||
var lazyreturned = container.Resolve<Lazy<LazyDependency>>();
|
||||
|
||||
Assert.AreEqual(lazy.GetType(), lazyDependency.GetType());
|
||||
Assert.IsFalse(lazyreturned.IsValueCreated);
|
||||
|
||||
var ld = (LazyDependency)lazyreturned.Value;
|
||||
|
||||
Assert.IsFalse(ld.Service.IsValueCreated);
|
||||
Assert.IsNotNull(ld.Service.Value);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void InjectToNonDefaultConstructorWithLazy()
|
||||
{
|
||||
IUnityContainer container = new UnityContainer();
|
||||
|
||||
container.RegisterType<Lazy<EmailService>>();
|
||||
container.RegisterType<Lazy<LazyDependency>>();
|
||||
var resolved = container.Resolve<Lazy<LazyDependency>>();
|
||||
|
||||
Assert.IsFalse(resolved.IsValueCreated);
|
||||
|
||||
var lazy = resolved.Value;
|
||||
|
||||
Assert.IsFalse(resolved.Value.Service.IsValueCreated);
|
||||
Assert.IsNotNull(resolved.Value.Service.Value);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,161 +0,0 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Tests.v5.TestSupport;
|
||||
|
||||
namespace Unity.Tests.v5.Lazy
|
||||
{
|
||||
/// <summary>
|
||||
/// Summary description for DeferredResolveFixture
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class LazyResolveFixture
|
||||
{
|
||||
[TestMethod]
|
||||
public void CanResolveALazy()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>();
|
||||
|
||||
var lazy = container.Resolve<Lazy<ILogger>>();
|
||||
Assert.IsNotNull(lazy);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolvedLazyHasNoValue()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>();
|
||||
|
||||
var lazy = container.Resolve<Lazy<ILogger>>();
|
||||
Assert.IsFalse(lazy.IsValueCreated);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolvedLazyResolvesThroughContainer()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>();
|
||||
|
||||
var lazy = container.Resolve<Lazy<ILogger>>();
|
||||
var logger = lazy.Value;
|
||||
|
||||
AssertExtensions.IsInstanceOfType(logger, typeof(MockLogger));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolvedLazyGetsInjectedAsADependency()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>();
|
||||
|
||||
var result = container.Resolve<ObjectThatGetsALazy>();
|
||||
|
||||
Assert.IsNotNull(result.LoggerLazy);
|
||||
AssertExtensions.IsInstanceOfType(result.LoggerLazy.Value, typeof(MockLogger));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolveLazyWithName()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>()
|
||||
.RegisterType<ILogger, SpecialLogger>("special");
|
||||
|
||||
var lazy = container.Resolve<Lazy<ILogger>>("special");
|
||||
|
||||
Assert.IsNotNull(lazy);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolvedLazyWithNameResolvedThroughContainerWithName()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
var lazy = container.Resolve<Lazy<ILogger>>("special");
|
||||
|
||||
container
|
||||
.RegisterType<ILogger, MockLogger>()
|
||||
.RegisterType<ILogger, SpecialLogger>("special");
|
||||
|
||||
var result = lazy.Value;
|
||||
|
||||
Assert.IsNotNull(result);
|
||||
AssertExtensions.IsInstanceOfType(result, typeof(SpecialLogger));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DifferentResolveCallsReturnDifferentLazyInstances()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>();
|
||||
|
||||
var lazy1 = container.Resolve<Lazy<ILogger>>();
|
||||
var lazy2 = container.Resolve<Lazy<ILogger>>();
|
||||
|
||||
Assert.AreNotSame(lazy1.Value, lazy2.Value);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DifferentLazyGenericsGetTheirOwnBuildPlan()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>()
|
||||
.RegisterInstance<string>("the instance");
|
||||
|
||||
var lazy1 = container.Resolve<Lazy<ILogger>>();
|
||||
var lazy2 = container.Resolve<Lazy<string>>();
|
||||
|
||||
AssertExtensions.IsInstanceOfType(lazy1.Value, typeof(ILogger));
|
||||
Assert.AreEqual("the instance", lazy2.Value);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ObservesPerResolveSingleton()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>()
|
||||
.RegisterType(typeof(Lazy<>), new PerResolveLifetimeManager());
|
||||
|
||||
var result = container.Resolve<ObjectThatGetsMultipleLazy>();
|
||||
|
||||
Assert.IsNotNull(result.LoggerLazy1);
|
||||
Assert.IsNotNull(result.LoggerLazy2);
|
||||
Assert.AreSame(result.LoggerLazy1, result.LoggerLazy2);
|
||||
AssertExtensions.IsInstanceOfType(result.LoggerLazy1.Value, typeof(MockLogger));
|
||||
AssertExtensions.IsInstanceOfType(result.LoggerLazy2.Value, typeof(MockLogger));
|
||||
Assert.AreSame(result.LoggerLazy1.Value, result.LoggerLazy2.Value);
|
||||
|
||||
Assert.AreNotSame(result.LoggerLazy1.Value, container.Resolve<Lazy<ILogger>>().Value);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolvingLazyOfIEnumerableCallsResolveAll()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterInstance("one", "first")
|
||||
.RegisterInstance("two", "second")
|
||||
.RegisterInstance("three", "third");
|
||||
|
||||
var lazy = container.Resolve<Lazy<IEnumerable<string>>>();
|
||||
var result = lazy.Value;
|
||||
|
||||
result.AssertContainsInAnyOrder("first", "second", "third");
|
||||
}
|
||||
|
||||
public class ObjectThatGetsALazy
|
||||
{
|
||||
[Dependency]
|
||||
public Lazy<ILogger> LoggerLazy { get; set; }
|
||||
}
|
||||
|
||||
public class ObjectThatGetsMultipleLazy
|
||||
{
|
||||
[Dependency]
|
||||
public Lazy<ILogger> LoggerLazy1 { get; set; }
|
||||
|
||||
[Dependency]
|
||||
public Lazy<ILogger> LoggerLazy2 { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
|
@ -441,6 +441,11 @@ namespace Unity.Tests.v5.Lifetime
|
|||
Assert.IsNotNull(obj);
|
||||
}
|
||||
|
||||
private class ATest
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void TestEmpty()
|
||||
{
|
||||
|
|
|
@ -1,116 +0,0 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using Unity.Builder;
|
||||
using Unity.Extension;
|
||||
using Unity.Injection;
|
||||
using Unity.Policy;
|
||||
using Unity.Storage;
|
||||
using Unity.Strategies;
|
||||
using Unity.Tests.v5.TestSupport;
|
||||
|
||||
namespace Unity.Tests.v5
|
||||
{
|
||||
[TestClass]
|
||||
public class ResolvingArraysFixture
|
||||
{
|
||||
[TestMethod]
|
||||
public void ResolveAllReturnsRegisteredObjects()
|
||||
{
|
||||
IUnityContainer container = new UnityContainer();
|
||||
object o1 = new object();
|
||||
object o2 = new object();
|
||||
|
||||
container
|
||||
.RegisterInstance<object>("o1", o1)
|
||||
.RegisterInstance<object>("o2", o2);
|
||||
|
||||
List<object> results = new List<object>(container.ResolveAll<object>());
|
||||
|
||||
CollectionAssertExtensions.AreEqual(new object[] { o1, o2 }, results);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolveAllReturnsRegisteredObjectsForBaseClass()
|
||||
{
|
||||
IUnityContainer container = new UnityContainer();
|
||||
ILogger o1 = new MockLogger();
|
||||
ILogger o2 = new SpecialLogger();
|
||||
|
||||
container
|
||||
.RegisterInstance<ILogger>("o1", o1)
|
||||
.RegisterInstance<ILogger>("o2", o2);
|
||||
|
||||
List<ILogger> results = new List<ILogger>(container.ResolveAll<ILogger>());
|
||||
CollectionAssertExtensions.AreEqual(new ILogger[] { o1, o2 }, results);
|
||||
}
|
||||
|
||||
private class InjectedObjectConfigurationExtension : UnityContainerExtension
|
||||
{
|
||||
private readonly IResolve _resolvePolicy;
|
||||
|
||||
public InjectedObjectConfigurationExtension(IResolve resolvePolicy)
|
||||
{
|
||||
this._resolvePolicy = resolvePolicy;
|
||||
}
|
||||
|
||||
protected override void Initialize()
|
||||
{
|
||||
Context.Policies.Set(typeof(InjectedObject), null,
|
||||
typeof(ISelect<ConstructorInfo>),
|
||||
new InjectedObjectSelectorPolicy(this._resolvePolicy));
|
||||
}
|
||||
}
|
||||
|
||||
private class InjectedObjectSelectorPolicy : ISelect<ConstructorInfo>
|
||||
{
|
||||
private readonly IResolve _resolvePolicy;
|
||||
|
||||
public InjectedObjectSelectorPolicy(IResolve resolvePolicy)
|
||||
{
|
||||
this._resolvePolicy = resolvePolicy;
|
||||
}
|
||||
|
||||
public IEnumerable<object> Select(Type type, IPolicySet registration)
|
||||
{
|
||||
var ctr = typeof(InjectedObject).GetMatchingConstructor(new[] { typeof(object) });
|
||||
|
||||
return new []{ new InjectionConstructor(ctr, _resolvePolicy) } ;
|
||||
}
|
||||
}
|
||||
|
||||
public class InjectedObject
|
||||
{
|
||||
public readonly object InjectedValue;
|
||||
|
||||
public InjectedObject(object injectedValue)
|
||||
{
|
||||
this.InjectedValue = injectedValue;
|
||||
}
|
||||
}
|
||||
|
||||
public class SimpleClass
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
internal class ReturnContainerStrategy : BuilderStrategy
|
||||
{
|
||||
private IUnityContainer container;
|
||||
|
||||
public ReturnContainerStrategy(IUnityContainer container)
|
||||
{
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
public override void PreBuildUp(ref BuilderContext context)
|
||||
{
|
||||
if (typeof(IUnityContainer) == context.Type)
|
||||
{
|
||||
context.Existing = container;
|
||||
context.BuildComplete = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,690 +0,0 @@
|
|||
using Microsoft.Practices.Unity.Tests.TestObjects;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Unity.Builder;
|
||||
using Unity.Extension;
|
||||
using Unity.Injection;
|
||||
using Unity.Strategies;
|
||||
using Unity.Tests.v5.TestObjects;
|
||||
using Unity.Tests.v5.TestSupport;
|
||||
|
||||
namespace Unity.Tests.v5
|
||||
{
|
||||
[TestClass]
|
||||
public class UnityContainerFixture
|
||||
{
|
||||
[TestMethod]
|
||||
public void CanCreateObjectFromUnconfiguredContainer()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
object o = container.Resolve<object>();
|
||||
|
||||
Assert.IsNotNull(o);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanCreateOptimizingResolver()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
object o = container.Resolve<object>();
|
||||
Thread.Sleep(100);
|
||||
Assert.IsNotNull(container.Resolve<object>());
|
||||
Thread.Sleep(100);
|
||||
Assert.IsNotNull(container.Resolve<object>());
|
||||
Thread.Sleep(100);
|
||||
Assert.IsNotNull(container.Resolve<object>());
|
||||
Thread.Sleep(100);
|
||||
Assert.IsNotNull(container.Resolve<object>());
|
||||
Thread.Sleep(100);
|
||||
Assert.IsNotNull(container.Resolve<object>());
|
||||
Thread.Sleep(100);
|
||||
Assert.IsNotNull(container.Resolve<object>());
|
||||
Thread.Sleep(100);
|
||||
Assert.IsNotNull(container.Resolve<object>());
|
||||
Thread.Sleep(100);
|
||||
Assert.IsNotNull(container.Resolve<object>());
|
||||
Thread.Sleep(100);
|
||||
Assert.IsNotNull(container.Resolve<object>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ContainerResolvesRecursiveConstructorDependencies()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
var dep = container.Resolve<ObjectWithOneDependency>();
|
||||
|
||||
Assert.IsNotNull(dep);
|
||||
Assert.IsNotNull(dep.InnerObject);
|
||||
Assert.AreNotSame(dep, dep.InnerObject);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ContainerResolvesMultipleRecursiveConstructorDependencies()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
var dep = container.Resolve<ObjectWithTwoConstructorDependencies>();
|
||||
|
||||
dep.Validate();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanResolveTypeMapping()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>();
|
||||
|
||||
var logger = container.Resolve<ILogger>();
|
||||
|
||||
Assert.IsNotNull(logger);
|
||||
Assert.IsInstanceOfType(logger, typeof(MockLogger));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanRegisterTypeMappingsWithNames()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>()
|
||||
.RegisterType<ILogger, SpecialLogger>("special");
|
||||
|
||||
var defaultLogger = container.Resolve<ILogger>();
|
||||
var specialLogger = container.Resolve<ILogger>("special");
|
||||
|
||||
Assert.IsNotNull(defaultLogger);
|
||||
Assert.IsNotNull(specialLogger);
|
||||
|
||||
Assert.IsInstanceOfType(defaultLogger, typeof(MockLogger));
|
||||
Assert.IsInstanceOfType(specialLogger, typeof(SpecialLogger));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldDoPropertyInjection()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
var obj = container.Resolve<ObjectWithTwoProperties>();
|
||||
|
||||
obj.Validate();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ShouldDoAllInjections()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>();
|
||||
|
||||
var obj = container.Resolve<ObjectWithLotsOfDependencies>();
|
||||
|
||||
Assert.IsNotNull(obj);
|
||||
obj.Validate();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanGetObjectsUsingNongenericMethod()
|
||||
{
|
||||
IUnityContainer container = new UnityContainer()
|
||||
.RegisterType(typeof(ILogger), typeof(MockLogger));
|
||||
|
||||
var logger = container.Resolve(typeof(ILogger));
|
||||
|
||||
Assert.IsNotNull(logger);
|
||||
Assert.IsInstanceOfType(logger, typeof(MockLogger));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanGetNamedObjectsUsingNongenericMethod()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType(typeof(ILogger), typeof(MockLogger))
|
||||
.RegisterType(typeof(ILogger), typeof(SpecialLogger), "special");
|
||||
|
||||
var defaultLogger = container.Resolve(typeof(ILogger)) as ILogger;
|
||||
var specialLogger = container.Resolve(typeof(ILogger), "special") as ILogger;
|
||||
|
||||
Assert.IsNotNull(defaultLogger);
|
||||
Assert.IsNotNull(specialLogger);
|
||||
|
||||
Assert.IsInstanceOfType(defaultLogger, typeof(MockLogger));
|
||||
Assert.IsInstanceOfType(specialLogger, typeof(SpecialLogger));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void AllInjectionsWorkFromNongenericMethods()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType(typeof(ILogger), typeof(MockLogger));
|
||||
|
||||
var obj = (ObjectWithLotsOfDependencies)container.Resolve(typeof(ObjectWithLotsOfDependencies));
|
||||
obj.Validate();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ContainerSupportsSingletons()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>(new ContainerControlledLifetimeManager());
|
||||
|
||||
var logger1 = container.Resolve<ILogger>();
|
||||
var logger2 = container.Resolve<ILogger>();
|
||||
|
||||
Assert.IsInstanceOfType(logger1, typeof(MockLogger));
|
||||
Assert.AreSame(logger1, logger2);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanCreatedNamedSingletons()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>()
|
||||
.RegisterType<ILogger, SpecialLogger>("special", new ContainerControlledLifetimeManager());
|
||||
|
||||
var logger1 = container.Resolve<ILogger>();
|
||||
var logger2 = container.Resolve<ILogger>();
|
||||
var logger3 = container.Resolve<ILogger>("special");
|
||||
var logger4 = container.Resolve<ILogger>("special");
|
||||
|
||||
Assert.AreNotSame(logger1, logger2);
|
||||
Assert.AreSame(logger3, logger4);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanRegisterSingletonsWithNongenericMethods()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>(new ContainerControlledLifetimeManager())
|
||||
.RegisterType<ILogger, SpecialLogger>("special", new ContainerControlledLifetimeManager());
|
||||
|
||||
var logger1 = container.Resolve<ILogger>();
|
||||
var logger2 = container.Resolve<ILogger>();
|
||||
var logger3 = container.Resolve<ILogger>("special");
|
||||
var logger4 = container.Resolve<ILogger>("special");
|
||||
|
||||
Assert.AreSame(logger1, logger2);
|
||||
Assert.AreSame(logger3, logger4);
|
||||
Assert.AreNotSame(logger1, logger3);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisposingContainerDisposesSingletons()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<DisposableObject>(new ContainerControlledLifetimeManager());
|
||||
|
||||
var dobj = container.Resolve<DisposableObject>();
|
||||
|
||||
Assert.IsFalse(dobj.WasDisposed);
|
||||
container.Dispose();
|
||||
|
||||
Assert.IsTrue(dobj.WasDisposed);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void SingletonsRegisteredAsDefaultGetInjected()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ObjectWithOneDependency>(new ContainerControlledLifetimeManager());
|
||||
|
||||
var dep = container.Resolve<ObjectWithOneDependency>();
|
||||
var dep2 = container.Resolve<ObjectWithTwoConstructorDependencies>();
|
||||
|
||||
Assert.AreSame(dep, dep2.OneDep);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanDoInjectionOnExistingObjects()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
var o = new ObjectWithTwoProperties();
|
||||
|
||||
Assert.IsNull(o.Obj1);
|
||||
Assert.IsNull(o.Obj2);
|
||||
|
||||
container.BuildUp(o);
|
||||
|
||||
o.Validate();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanBuildUpExistingObjectWithNonGenericObject()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>();
|
||||
|
||||
var o = new ObjectUsingLogger();
|
||||
var result = container.BuildUp(o);
|
||||
|
||||
Assert.IsInstanceOfType(result, typeof(ObjectUsingLogger));
|
||||
Assert.AreSame(o, result);
|
||||
Assert.IsNotNull(o.Logger);
|
||||
Assert.IsInstanceOfType(o.Logger, typeof(MockLogger));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanUseInstanceAsSingleton()
|
||||
{
|
||||
var logger = new MockLogger();
|
||||
|
||||
IUnityContainer container = new UnityContainer();
|
||||
container.RegisterInstance(typeof(ILogger), "logger", logger, new ContainerControlledLifetimeManager());
|
||||
|
||||
var o = container.Resolve<ILogger>("logger");
|
||||
Assert.AreSame(logger, o);
|
||||
}
|
||||
|
||||
|
||||
[TestMethod]
|
||||
public void CanUseInstanceAsSingletonViaGenericMethod()
|
||||
{
|
||||
var logger = new MockLogger();
|
||||
|
||||
var container = new UnityContainer()
|
||||
.RegisterInstance<ILogger>("logger", logger);
|
||||
|
||||
var o = container.Resolve<ILogger>("logger");
|
||||
Assert.AreSame(logger, o);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisposingContainerDisposesOwnedInstances()
|
||||
{
|
||||
var o = new DisposableObject();
|
||||
var container = new UnityContainer()
|
||||
.RegisterInstance(typeof(object), o);
|
||||
|
||||
container.Dispose();
|
||||
Assert.IsTrue(o.WasDisposed);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void DisposingContainerDoesNotDisposeUnownedInstances()
|
||||
{
|
||||
var o = new DisposableObject();
|
||||
var container = new UnityContainer()
|
||||
.RegisterInstance(typeof(object), o, new ExternallyControlledLifetimeManager());
|
||||
|
||||
container.Dispose();
|
||||
Assert.IsFalse(o.WasDisposed);
|
||||
GC.KeepAlive(o);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ContainerDefaultsToInstanceOwnership()
|
||||
{
|
||||
var o = new DisposableObject();
|
||||
var container = new UnityContainer()
|
||||
.RegisterInstance(typeof(object), o);
|
||||
container.Dispose();
|
||||
Assert.IsTrue(o.WasDisposed);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ContainerDefaultsToInstanceOwnershipViaGenericMethod()
|
||||
{
|
||||
var o = new DisposableObject();
|
||||
var container = new UnityContainer()
|
||||
.RegisterInstance(typeof(DisposableObject), o);
|
||||
container.Dispose();
|
||||
Assert.IsTrue(o.WasDisposed);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void InstanceRegistrationWithoutNameRegistersDefault()
|
||||
{
|
||||
var l = new MockLogger();
|
||||
var container = new UnityContainer()
|
||||
.RegisterInstance(typeof(ILogger), l);
|
||||
|
||||
var o = container.Resolve<ILogger>();
|
||||
Assert.AreSame(l, o);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void InstanceRegistrationWithoutNameRegistersDefaultViaGenericMethod()
|
||||
{
|
||||
var l = new MockLogger();
|
||||
var container = new UnityContainer()
|
||||
.RegisterInstance<ILogger>(l);
|
||||
|
||||
var o = container.Resolve<ILogger>();
|
||||
Assert.AreSame(l, o);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanRegisterDefaultInstanceWithoutLifetime()
|
||||
{
|
||||
var o = new DisposableObject();
|
||||
|
||||
var container = new UnityContainer()
|
||||
.RegisterInstance(typeof(object), o, new ExternallyControlledLifetimeManager());
|
||||
|
||||
var result = container.Resolve<object>();
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreSame(o, result);
|
||||
|
||||
container.Dispose();
|
||||
Assert.IsFalse(o.WasDisposed);
|
||||
GC.KeepAlive(o);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanRegisterDefaultInstanceWithoutLifetimeViaGenericMethod()
|
||||
{
|
||||
var o = new DisposableObject();
|
||||
|
||||
var container = new UnityContainer()
|
||||
.RegisterInstance<object>(o, new ExternallyControlledLifetimeManager());
|
||||
|
||||
object result = container.Resolve<object>();
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreSame(o, result);
|
||||
|
||||
container.Dispose();
|
||||
Assert.IsFalse(o.WasDisposed);
|
||||
GC.KeepAlive(o);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanSpecifyInjectionConstructorWithDefaultDependencies()
|
||||
{
|
||||
string sampleString = "Hi there";
|
||||
var container = new UnityContainer()
|
||||
.RegisterInstance(sampleString);
|
||||
|
||||
var o = container.Resolve<ObjectWithInjectionConstructor>();
|
||||
|
||||
Assert.IsNotNull(o.ConstructorDependency);
|
||||
Assert.AreSame(sampleString, o.ConstructorDependency);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanGetInstancesOfAllRegisteredTypes()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>("mock")
|
||||
.RegisterType<ILogger, SpecialLogger>("special")
|
||||
.RegisterType<ILogger, MockLogger>("another");
|
||||
|
||||
List<ILogger> loggers = new List<ILogger>(
|
||||
container.ResolveAll<ILogger>());
|
||||
|
||||
Assert.AreEqual(3, loggers.Count);
|
||||
Assert.IsInstanceOfType(loggers[0], typeof(MockLogger));
|
||||
Assert.IsInstanceOfType(loggers[1], typeof(SpecialLogger));
|
||||
Assert.IsInstanceOfType(loggers[2], typeof(MockLogger));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GetAllDoesNotReturnTheDefault()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, SpecialLogger>("special")
|
||||
.RegisterType<ILogger, MockLogger>();
|
||||
|
||||
var loggers = container.ResolveAll<ILogger>().ToList();
|
||||
|
||||
Assert.AreEqual(1, loggers.Count);
|
||||
Assert.IsInstanceOfType(loggers[0], typeof(SpecialLogger));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanGetAllWithNonGenericMethod()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>("mock")
|
||||
.RegisterType<ILogger, SpecialLogger>("special")
|
||||
.RegisterType<ILogger, MockLogger>("another");
|
||||
|
||||
var loggers = container.ResolveAll(typeof(ILogger)).ToList();
|
||||
|
||||
Assert.AreEqual(3, loggers.Count);
|
||||
Assert.IsInstanceOfType(loggers[0], typeof(MockLogger));
|
||||
Assert.IsInstanceOfType(loggers[1], typeof(SpecialLogger));
|
||||
Assert.IsInstanceOfType(loggers[2], typeof(MockLogger));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GetAllReturnsRegisteredInstances()
|
||||
{
|
||||
var l = new MockLogger();
|
||||
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>("normal")
|
||||
.RegisterType<ILogger, SpecialLogger>("special")
|
||||
.RegisterInstance<ILogger>("instance", l);
|
||||
|
||||
var loggers = container.ResolveAll<ILogger>().ToList();
|
||||
|
||||
Assert.AreEqual(3, loggers.Count);
|
||||
Assert.IsInstanceOfType(loggers[0], typeof(MockLogger));
|
||||
Assert.IsInstanceOfType(loggers[1], typeof(SpecialLogger));
|
||||
Assert.AreSame(l, loggers[2]);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanRegisterLifetimeAsSingleton()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>()
|
||||
.RegisterType<ILogger, SpecialLogger>("special", new ContainerControlledLifetimeManager());
|
||||
|
||||
var logger1 = container.Resolve<ILogger>();
|
||||
var logger2 = container.Resolve<ILogger>();
|
||||
var logger3 = container.Resolve<ILogger>("special");
|
||||
var logger4 = container.Resolve<ILogger>("special");
|
||||
|
||||
Assert.AreNotSame(logger1, logger2);
|
||||
Assert.AreSame(logger3, logger4);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ResolutionFailedException))]
|
||||
public void ShouldThrowIfAttemptsToResolveUnregisteredInterface()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
container.Resolve<ILogger>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanBuildSameTypeTwice()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
container.Resolve<ObjectWithTwoConstructorDependencies>();
|
||||
container.Resolve<ObjectWithTwoConstructorDependencies>();
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanRegisterMultipleStringInstances()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
string first = "first";
|
||||
string second = "second";
|
||||
|
||||
container
|
||||
.RegisterInstance<string>(first)
|
||||
.RegisterInstance<string>(second);
|
||||
|
||||
var result = container.Resolve<string>();
|
||||
|
||||
Assert.AreEqual(second, result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void GetReasonableExceptionWhenRegisteringNullInstance()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
container.RegisterInstance<SomeType>(null);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(InvalidOperationException))]
|
||||
public void RegisteringTheSameLifetimeManagerTwiceThrows()
|
||||
{
|
||||
var singleton = new ContainerControlledLifetimeManager();
|
||||
|
||||
new UnityContainer()
|
||||
.RegisterType<ILogger, MockLogger>(singleton)
|
||||
.RegisterType<ILogger, SpecialLogger>("special", singleton);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanRegisterGenericTypesAndResolveThem()
|
||||
{
|
||||
var myDict = new Dictionary<string, string>();
|
||||
myDict.Add("One", "two");
|
||||
myDict.Add("Two", "three");
|
||||
|
||||
var container = new UnityContainer()
|
||||
.RegisterInstance(myDict)
|
||||
.RegisterType(typeof(IDictionary<,>), typeof(Dictionary<,>));
|
||||
|
||||
var result = container.Resolve<IDictionary<string, string>>();
|
||||
Assert.AreSame(myDict, result);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void CanSpecializeGenericsViaTypeMappings()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType(typeof(IRepository<>), typeof(MockRespository<>))
|
||||
.RegisterType<IRepository<SomeType>, SomeTypRepository>();
|
||||
|
||||
var generalResult = container.Resolve<IRepository<string>>();
|
||||
var specializedResult = container.Resolve<IRepository<SomeType>>();
|
||||
|
||||
Assert.IsInstanceOfType(generalResult, typeof(MockRespository<string>));
|
||||
Assert.IsInstanceOfType(specializedResult, typeof(SomeTypRepository));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ContainerResolvesItself()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
|
||||
Assert.AreSame(container, container.Resolve<IUnityContainer>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ContainerResolvesItselfEvenAfterGarbageCollect()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
container.AddNewExtension<GarbageCollectingExtension>();
|
||||
|
||||
Assert.IsNotNull(container.Resolve<IUnityContainer>());
|
||||
}
|
||||
|
||||
public class GarbageCollectingExtension : UnityContainerExtension
|
||||
{
|
||||
protected override void Initialize()
|
||||
{
|
||||
this.Context.Strategies.Add(new GarbageCollectingStrategy(), UnityBuildStage.Setup);
|
||||
}
|
||||
|
||||
public class GarbageCollectingStrategy : BuilderStrategy
|
||||
{
|
||||
public override void PreBuildUp(ref BuilderContext context)
|
||||
{
|
||||
GC.Collect();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ChildContainerResolvesChildNotParent()
|
||||
{
|
||||
IUnityContainer parent = new UnityContainer();
|
||||
var child = parent.CreateChildContainer();
|
||||
|
||||
Assert.AreSame(child, child.Resolve<IUnityContainer>());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ParentContainerResolvesParentNotChild()
|
||||
{
|
||||
IUnityContainer parent = new UnityContainer();
|
||||
var child = parent.CreateChildContainer();
|
||||
|
||||
Assert.AreSame(parent, parent.Resolve<IUnityContainer>());
|
||||
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ResolutionFailedException))]
|
||||
public void ResolvingOpenGenericGivesInnerInvalidOperationException()
|
||||
{
|
||||
var container = new UnityContainer()
|
||||
.RegisterType(typeof(List<>), new InjectionConstructor(10));
|
||||
|
||||
container.Resolve(typeof(List<>));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void ResolvingUnconfiguredPrimitiveDependencyGivesReasonableException()
|
||||
{
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<string>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<bool>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<char>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<float>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<double>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<byte>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<short>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<int>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<long>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<IntPtr>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<UIntPtr>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<ushort>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<uint>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<ulong>();
|
||||
ResolvingUnconfiguredPrimitiveGivesResonableException<sbyte>();
|
||||
}
|
||||
|
||||
private void ResolvingUnconfiguredPrimitiveGivesResonableException<T>()
|
||||
{
|
||||
var container = new UnityContainer();
|
||||
try
|
||||
{
|
||||
container.Resolve<TypeWithPrimitiveDependency<T>>();
|
||||
Assert.Fail("Expected exception did not occur");
|
||||
}
|
||||
catch (ResolutionFailedException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
internal class SomeType
|
||||
{
|
||||
}
|
||||
|
||||
public interface IRepository<TEntity>
|
||||
{
|
||||
}
|
||||
|
||||
public class MockRespository<TEntity> : IRepository<TEntity>
|
||||
{
|
||||
}
|
||||
|
||||
public class SomeTypRepository : IRepository<SomeType>
|
||||
{
|
||||
}
|
||||
|
||||
public class ObjectWithPrivateSetter
|
||||
{
|
||||
[Dependency]
|
||||
public object Obj1 { get; private set; }
|
||||
}
|
||||
|
||||
public class TypeWithPrimitiveDependency<T>
|
||||
{
|
||||
public TypeWithPrimitiveDependency(T dependency)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче