public void CanCreateObjectWithoutExplicitConstructorDefined() { TestingBuilderContext context = GetContext(); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, typeof(InternalObjectWithoutExplicitConstructor)); context.BuildKey = typeof(InternalObjectWithoutExplicitConstructor); plan.BuildUp(context); InternalObjectWithoutExplicitConstructor result = (InternalObjectWithoutExplicitConstructor)context.Existing; Assert.IsNotNull(result); }
public void CanCreatePlanForPrivateClass() { TestingBuilderContext context = GetContext(); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, typeof(PrivateClassWithoutExplicitConstructor)); context.BuildKey = typeof(PrivateClassWithoutExplicitConstructor); plan.BuildUp(context); object result = context.Existing; Assert.IsNotNull(result); Assert.IsInstanceOfType(result, typeof(PrivateClassWithoutExplicitConstructor)); }
public void StrategyGetsBuildPlanFromPolicySet() { TestingBuilderContext context = new TestingBuilderContext(); context.Strategies.Add(new BuildPlanStrategy()); object instance = new object(); ReturnInstanceBuildPlan plan = new ReturnInstanceBuildPlan(instance); context.Policies.Set <IBuildPlanPolicy>(plan, typeof(object)); object result = context.ExecuteBuildUp(typeof(object), null); Assert.IsTrue(plan.BuildUpCalled); Assert.AreSame(instance, result); }
public void GetProperExceptionMessageWhenBuildingInterface() { TestingBuilderContext context = TestingBuilderContext.GetFullContext(); try { context.ExecuteBuildUp(typeof(ISomethingOrOther), null); Assert.Fail(); } catch (BuildFailedException ex) { Assert.IsTrue(ex.Message.Contains("is an interface")); return; } }
public void GetParameterNameInExceptionMessageOnFailedResolution() { TestingBuilderContext context = TestingBuilderContext.GetFullContext(); try { context.ExecuteBuildUp(typeof(FileLogger), null); Assert.Fail(); } catch (BuildFailedException ex) { Assert.IsTrue(ex.Message.Contains("logFile")); return; } }
public void GetExpectedExceptionMessageWhenPropertiesAreNotResolved() { TestingBuilderContext context = TestingBuilderContext.GetFullContext(); ClassThatTakesInterface existing = new ClassThatTakesInterface(); try { context.ExecuteBuildUp(typeof(ClassThatTakesInterface), existing); Assert.Fail(); } catch (BuildFailedException ex) { StringAssert.Contains(ex.Message, "property \"Foo\""); return; } }
public void ExistingObjectIsUntouchedByConstructionPlan() { TestingBuilderContext context = GetContext(); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, typeof(OptionalLogger)); OptionalLogger existing = new OptionalLogger("C:\\foo.bar"); context.BuildKey = typeof(OptionalLogger); context.Existing = existing; plan.BuildUp(context); object result = context.Existing; Assert.AreSame(existing, result); Assert.AreEqual("C:\\foo.bar", existing.LogFile); }
public void InjectionConstructorInsertsChooserForDefaultConstructor() { InjectionConstructor ctor = new InjectionConstructor(); TestingBuilderContext context = new TestingBuilderContext(); IPolicyList policies = context.PersistentPolicies; ctor.AddPolicies(typeof(GuineaPig), policies); IConstructorSelectorPolicy selector = policies.Get <IConstructorSelectorPolicy>( new NamedTypeBuildKey(typeof(GuineaPig))); SelectedConstructor selected = selector.SelectConstructor(context); Assert.AreEqual(typeof(GuineaPig).GetConstructor(new Type[0]), selected.Constructor); Assert.AreEqual(0, selected.GetParameterKeys().Length); }
public void StrategyCreatesBuildPlanWhenItDoesntExist() { TestingBuilderContext context = new TestingBuilderContext(); context.Strategies.Add(new BuildPlanStrategy()); MockBuildPlanCreatorPolicy policy = new MockBuildPlanCreatorPolicy(); context.Policies.SetDefault <IBuildPlanCreatorPolicy>(policy); object result = context.ExecuteBuildUp(typeof(object), null); Assert.IsNotNull(result); Assert.IsTrue(policy.PolicyWasCreated); IBuildPlanPolicy plan = context.Policies.Get <IBuildPlanPolicy>(typeof(object)); Assert.IsNotNull(plan); }
public void CanResolveSimpleParameterTypes() { TestingBuilderContext context = GetContext(); context.Locator.Add(typeof(string), "C:\\Log.txt"); context.Policies.Set <ISingletonPolicy>(new SingletonPolicy(true), typeof(string)); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, typeof(FileLogger)); context.BuildKey = typeof(FileLogger); plan.BuildUp(context); object result = context.Existing; FileLogger logger = result as FileLogger; Assert.IsNotNull(result); Assert.IsNotNull(logger); Assert.AreEqual("C:\\Log.txt", logger.LogFile); }
public void ThrowsWhenBuildingPlanWithMethodWithRefParam() { bool handled = false; try { TestingBuilderContext context = GetContext(); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, typeof(ObjectWithRefParamMethod)); } catch (BuildFailedException ex) { Assert.IsInstanceOfType(ex.InnerException, typeof(IllegalInjectionMethodException)); handled = true; } if (!handled) { Assert.Fail("Should have gotten an exception here"); } }
public void CanInjectProperties() { TestingBuilderContext context = GetContext(); object existingObject = new object(); context.Locator.Add(typeof(object), existingObject); context.Policies.Set <ISingletonPolicy>(new SingletonPolicy(true), typeof(object)); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, typeof(OnePropertyClass)); OnePropertyClass existing = new OnePropertyClass(); context.Existing = existing; context.BuildKey = typeof(OnePropertyClass); plan.BuildUp(context); Assert.IsNotNull(existing.Key); Assert.AreSame(existingObject, existing.Key); }
private TestingBuilderContext GetContext() { StagedStrategyChain <BuilderStage> chain = new StagedStrategyChain <BuilderStage>(); chain.AddNew <DynamicMethodConstructorStrategy>(BuilderStage.Creation); DynamicMethodBuildPlanCreatorPolicy policy = new DynamicMethodBuildPlanCreatorPolicy(chain); TestingBuilderContext context = new TestingBuilderContext(); context.Strategies.Add(new SingletonStrategy()); context.PersistentPolicies.SetDefault <IConstructorSelectorPolicy>( new ConstructorSelectorPolicy <InjectionConstructorAttribute>()); context.PersistentPolicies.SetDefault <IBuildPlanCreatorPolicy>(policy); return(context); }
public void CanInjectProperties() { TestingBuilderContext context = GetContext(); object existingObject = new object(); SingletonLifetimePolicy lifetimePolicy = new SingletonLifetimePolicy(); lifetimePolicy.SetValue(existingObject); context.Policies.Set <ILifetimePolicy>(lifetimePolicy, typeof(object)); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, typeof(OnePropertyClass)); OnePropertyClass existing = new OnePropertyClass(); context.Existing = existing; context.BuildKey = typeof(OnePropertyClass); plan.BuildUp(context); Assert.IsNotNull(existing.Key); Assert.AreSame(existingObject, existing.Key); }
public void GetProperExceptionMessageWhenFailingToResolveMethodParameter() { TestingBuilderContext context = TestingBuilderContext.GetFullContext(); ILifetimePolicy intLifetime = new SingletonLifetimePolicy(); intLifetime.SetValue(42); context.PersistentPolicies.Set(intLifetime, typeof(int)); ObjectWithInjectionMethod existing = new ObjectWithInjectionMethod(); try { context.ExecuteBuildUp(typeof(ObjectWithInjectionMethod), existing); Assert.Fail(); } catch (BuildFailedException ex) { StringAssert.Contains(ex.Message, "DoSomething"); StringAssert.Contains(ex.Message, "stringParam"); } }
private TestingBuilderContext GetContext() { StagedStrategyChain <BuilderStage> chain = new StagedStrategyChain <BuilderStage>(); chain.AddNew <DynamicMethodPropertySetterStrategy>(BuilderStage.Initialization); DynamicMethodBuildPlanCreatorPolicy policy = new DynamicMethodBuildPlanCreatorPolicy(chain); TestingBuilderContext context = new TestingBuilderContext(); context.Strategies.Add(new LifetimeStrategy()); context.PersistentPolicies.SetDefault <IDynamicBuilderMethodCreatorPolicy>( new DefaultDynamicBuilderMethodCreatorPolicy()); context.Policies.SetDefault <IConstructorSelectorPolicy>( new ConstructorSelectorPolicy <InjectionConstructorAttribute>()); context.Policies.SetDefault <IPropertySelectorPolicy>( new PropertySelectorPolicy <DependencyAttribute>()); context.Policies.SetDefault <IBuildPlanCreatorPolicy>(policy); return(context); }
public void InjectionConstructorSetsResolverForInterfaceToLookupInContainer() { InjectionConstructor ctor = new InjectionConstructor("Logger", typeof(ILogger)); TestingBuilderContext context = new TestingBuilderContext(); IPolicyList policies = context.PersistentPolicies; ctor.AddPolicies(typeof(GuineaPig), policies); IConstructorSelectorPolicy selector = policies.Get <IConstructorSelectorPolicy>( new NamedTypeBuildKey(typeof(GuineaPig))); SelectedConstructor selected = selector.SelectConstructor(context); string[] keys = selected.GetParameterKeys(); Assert.AreEqual(typeof(GuineaPig).GetConstructor(Sequence.Collect(typeof(string), typeof(ILogger))), selected.Constructor); Assert.AreEqual(2, keys.Length); IDependencyResolverPolicy policy = context.Policies.Get <IDependencyResolverPolicy>(keys[1]); Assert.IsTrue(policy is NamedTypeDependencyResolverPolicy); }
public void InjectionConstructorInsertsChooserForConstructorWithParameters() { string expectedString = "Hello"; int expectedInt = 12; InjectionConstructor ctor = new InjectionConstructor(expectedString, expectedInt); TestingBuilderContext context = new TestingBuilderContext(); IPolicyList policies = context.PersistentPolicies; ctor.AddPolicies(typeof(GuineaPig), policies); IConstructorSelectorPolicy selector = policies.Get <IConstructorSelectorPolicy>( new NamedTypeBuildKey(typeof(GuineaPig))); SelectedConstructor selected = selector.SelectConstructor(context); string[] keys = selected.GetParameterKeys(); Assert.AreEqual(typeof(GuineaPig).GetConstructor(Sequence.Collect(typeof(string), typeof(int))), selected.Constructor); Assert.AreEqual(2, keys.Length); Assert.AreEqual(expectedString, (string)ResolveValue(policies, keys[0])); Assert.AreEqual(expectedInt, (int)ResolveValue(policies, keys[1])); }