public void ResolvingAParameterSetsTheCurrentOperation() { var resolverPolicy = new CurrentOperationSensingResolverPolicy <object>(); MockBuilderContext context = GetContext(); var key = new NamedTypeBuildKey <ObjectWithSingleInjectionMethod>(); context.BuildKey = key; context.Existing = new ObjectWithSingleInjectionMethod(); context.Policies.Set <IMethodSelectorPolicy>( new TestSingleArgumentMethodSelectorPolicy <ObjectWithSingleInjectionMethod>(resolverPolicy), key); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, key); plan.BuildUp(context); Assert.IsNotNull(resolverPolicy.CurrentOperation); }
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 ResolvingAPropertyValueSetsTheCurrentOperation() { var resolverPolicy = new CurrentOperationSensingResolverPolicy <object>(); MockBuilderContext context = GetContext(); var key = new NamedTypeBuildKey <OnePropertyClass>(); context.BuildKey = key; context.Existing = new OnePropertyClass(); context.Policies.Set <IPropertySelectorPolicy>( new TestSinglePropertySelectorPolicy <OnePropertyClass>(resolverPolicy), key); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, key); plan.BuildUp(context); Assert.NotNull(resolverPolicy.CurrentOperation); }
public void CanResolveSimpleParameterTypes() { TestingBuilderContext context = GetContext(); SingletonLifetimePolicy lifetimePolicy = new SingletonLifetimePolicy(); lifetimePolicy.SetValue("C:\\Log.txt"); context.Policies.Set <ILifetimePolicy>(lifetimePolicy, 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 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); }
public void ExceptionThrownWhileResolvingAParameterIsBubbledUpAndTheCurrentOperationIsNotCleared() { var resolverPolicy = new ExceptionThrowingTestResolverPolicy(new ArgumentException()); MockBuilderContext context = GetContext(); context.BuildKey = new NamedTypeBuildKey(typeof(ConstructorInjectionTestClass)); context.Policies.Set <IConstructorSelectorPolicy>( new TestSingleArgumentConstructorSelectorPolicy <ConstructorInjectionTestClass>(resolverPolicy), context.BuildKey); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, context.BuildKey); var exception = Assert.Throws <ArgumentException>(() => plan.BuildUp(context)); var operation = context.CurrentOperation as ConstructorArgumentResolveOperation; Assert.NotNull(operation); Assert.IsType <ConstructorInjectionTestClass>(operation.TypeBeingConstructed); Assert.Equal("parameter", operation.ParameterName); }
public void CanResolveSimpleParameterTypes() { MockBuilderContext context = GetContext(); var key = new NamedTypeBuildKey <FileLogger>(); var lifetimePolicy = new ContainerControlledLifetimeManager(); lifetimePolicy.SetValue("C:\\Log.txt"); context.Policies.Set <ILifetimePolicy>(lifetimePolicy, new NamedTypeBuildKey <string>()); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, key); context.BuildKey = key; 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 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 CanInjectProperties() { MockBuilderContext context = GetContext(); object existingObject = new object(); var lifetimePolicy = new ContainerControlledLifetimeManager(); lifetimePolicy.SetValue(existingObject); context.Policies.Set <ILifetimePolicy>(lifetimePolicy, new NamedTypeBuildKey <object>()); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, new NamedTypeBuildKey(typeof(OnePropertyClass))); OnePropertyClass existing = new OnePropertyClass(); context.Existing = existing; context.BuildKey = new NamedTypeBuildKey(typeof(OnePropertyClass)); plan.BuildUp(context); Assert.NotNull(existing.Key); Assert.Same(existingObject, existing.Key); }
public void ExceptionThrownWhileInvokingTheConstructorIsBubbledUpAndTheCurrentOperationIsNotCleared() { MockBuilderContext context = GetContext(); context.BuildKey = new NamedTypeBuildKey(typeof(ThrowingConstructorInjectionTestClass)); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, context.BuildKey); try { plan.BuildUp(context); Assert.Fail("failure expected"); } catch (Exception e) { Assert.AreSame(ThrowingConstructorInjectionTestClass.ConstructorException, e); var operation = (InvokingConstructorOperation)context.CurrentOperation; Assert.IsNotNull(operation); Assert.AreSame(typeof(ThrowingConstructorInjectionTestClass), operation.TypeBeingConstructed); } }
public void ExceptionThrownWhileResolvingAPropertyValueIsBubbledUpAndTheCurrentOperationIsNotCleared() { var resolverPolicy = new ExceptionThrowingTestResolverPolicy(new ArgumentException()); MockBuilderContext context = GetContext(); var key = new NamedTypeBuildKey <OnePropertyClass>(); context.BuildKey = key; context.Existing = new OnePropertyClass(); context.Policies.Set <IPropertySelectorPolicy>( new TestSinglePropertySelectorPolicy <OnePropertyClass>(resolverPolicy), key); IBuildPlanPolicy plan = GetPlanCreator(context).CreatePlan(context, key); var exception = Assert.Throws <ArgumentException>(() => plan.BuildUp(context)); var operation = context.CurrentOperation as ResolvingPropertyValueOperation; Assert.NotNull(operation); Assert.IsType <OnePropertyClass>(operation.TypeBeingConstructed); Assert.Equal("Key", operation.PropertyName); }
/// <summary> /// Execute this strategy chain against the given context, /// calling the Buildup methods on the strategies. /// </summary> /// <param name="policy"></param> /// <param name="context">Context for the build process.</param> /// <returns>The build up object</returns> public static object ExecuteBuildUp(this IBuildPlanPolicy policy, IBuilderContext context) { policy.BuildUp(context ?? throw new ArgumentNullException(nameof(context))); return(context.Existing); }