public void apply_policy_selectively_with_a_func()
        {
            var activator1 = new ActivatorInterceptor <ITarget>(x => x.Activate());
            var policy     = new InterceptorPolicy <ITarget>(activator1, i => i.Name.StartsWith("A"));

            var container = new Container(x =>
            {
                x.Policies.Interceptors(policy);
                x.For <ITarget>().AddInstances(targets =>
                {
                    targets.Type <ATarget>().Named("A");
                    targets.Type <ATarget>().Named("A1");
                    targets.Type <ATarget>().Named("A2");
                    targets.Type <ATarget>().Named("B");
                    targets.Type <ATarget>().Named("C");
                    targets.Type <ATarget>().Named("D");
                });
            });

            container.GetInstance <ITarget>("A").ShouldBeOfType <ATarget>().WasActivated.ShouldBeTrue();
            container.GetInstance <ITarget>("A1").ShouldBeOfType <ATarget>().WasActivated.ShouldBeTrue();
            container.GetInstance <ITarget>("A2").ShouldBeOfType <ATarget>().WasActivated.ShouldBeTrue();
            container.GetInstance <ITarget>("B").ShouldBeOfType <ATarget>().WasActivated.ShouldBeFalse();
            container.GetInstance <ITarget>("C").ShouldBeOfType <ATarget>().WasActivated.ShouldBeFalse();
            container.GetInstance <ITarget>("D").ShouldBeOfType <ATarget>().WasActivated.ShouldBeFalse();
        }
Exemple #2
0
        public void select_interceptors()
        {
            var activator1 = new ActivatorInterceptor <ITarget>(x => x.Activate());
            var activator2 = new ActivatorInterceptor <Target>(x => x.UseSession(null));
            var activator3 = new ActivatorInterceptor <Target>(x => x.ThrowUp());
            var activator4 = new ActivatorInterceptor <ITarget>(x => x.Debug());
            var activator5 = new ActivatorInterceptor <IGateway>(x => x.DoSomething());

            var policies = Policies.Default();

            policies.Add(activator1.ToPolicy());
            policies.Add(activator2.ToPolicy());
            policies.Add(activator3.ToPolicy());
            policies.Add(activator4.ToPolicy());
            policies.Add(activator5.ToPolicy());

            var instance1 = new SmartInstance <Target>();

            policies.Apply(typeof(ITarget), instance1);
            instance1.Interceptors
            .ShouldHaveTheSameElementsAs(activator1, activator2, activator3, activator4);

            var instance2 = new SmartInstance <ATarget>();

            policies.Apply(typeof(ITarget), instance2);
            instance2.Interceptors
            .ShouldHaveTheSameElementsAs(activator1, activator4);

            var instance3 = new SmartInstance <StubbedGateway>();

            policies.Apply(typeof(ITarget), instance3);
            instance3.Interceptors
            .ShouldHaveTheSameElementsAs(activator5);
        }
        public void apply_policy_selectively_with_a_func()
        {
            var activator1 = new ActivatorInterceptor<ITarget>(x => x.Activate());
            var policy = new InterceptorPolicy<ITarget>(activator1, i => i.Name.StartsWith("A"));

            var container = new Container(x =>
            {
                x.Policies.Interceptors(policy);
                x.For<ITarget>().AddInstances(targets =>
                {
                    targets.Type<ATarget>().Named("A");
                    targets.Type<ATarget>().Named("A1");
                    targets.Type<ATarget>().Named("A2");
                    targets.Type<ATarget>().Named("B");
                    targets.Type<ATarget>().Named("C");
                    targets.Type<ATarget>().Named("D");
                });
            });

            container.GetInstance<ITarget>("A").ShouldBeOfType<ATarget>().WasActivated.ShouldBeTrue();
            container.GetInstance<ITarget>("A1").ShouldBeOfType<ATarget>().WasActivated.ShouldBeTrue();
            container.GetInstance<ITarget>("A2").ShouldBeOfType<ATarget>().WasActivated.ShouldBeTrue();
            container.GetInstance<ITarget>("B").ShouldBeOfType<ATarget>().WasActivated.ShouldBeFalse();
            container.GetInstance<ITarget>("C").ShouldBeOfType<ATarget>().WasActivated.ShouldBeFalse();
            container.GetInstance<ITarget>("D").ShouldBeOfType<ATarget>().WasActivated.ShouldBeFalse();
        }
        public void select_interceptors()
        {
            var activator1 = new ActivatorInterceptor<ITarget>(x => x.Activate());
            var activator2 = new ActivatorInterceptor<Target>(x => x.UseSession(null));
            var activator3 = new ActivatorInterceptor<Target>(x => x.ThrowUp());
            var activator4 = new ActivatorInterceptor<ITarget>(x => x.Debug());
            var activator5 = new ActivatorInterceptor<IGateway>(x => x.DoSomething());


            var policies = new InterceptorPolicies();
            policies.Add(activator1.ToPolicy());
            policies.Add(activator2.ToPolicy());
            policies.Add(activator3.ToPolicy());
            policies.Add(activator4.ToPolicy());
            policies.Add(activator5.ToPolicy());



            policies.SelectInterceptors(typeof(ITarget), new SmartInstance<Target>())
                .ShouldHaveTheSameElementsAs(activator1, activator2, activator3, activator4);

            policies.SelectInterceptors(typeof(ITarget), new SmartInstance<ATarget>())
                .ShouldHaveTheSameElementsAs(activator1, activator4);

            policies.SelectInterceptors(typeof(ITarget), new SmartInstance<StubbedGateway>())
                .ShouldHaveTheSameElementsAs(activator5);
        }
        /// <summary>
        /// Construct a new instance of the <see cref="EventAggregationRegistry"/>
        /// </summary>
        public EventAggregationRegistry()
        {
            ForSingletonOf <IEventAggregator>().Use <EventAggregator>();

            var interceptor = new ActivatorInterceptor <object>((c, s) => RegisterWithEventAggregator(c, s));

            Policies.Interceptors(interceptor.ToPolicy());
        }
        public void add_interceptor()
        {
            var interceptor =
                new ActivatorInterceptor <SimpleWidget>(w => w.Intercept());
            var instance = new SmartInstance <SimpleWidget>();

            instance.AddInterceptor(interceptor);

            new Container().GetInstance <SimpleWidget>(instance)
            .WasIntercepted.ShouldBeTrue();
        }
        public void add_interceptor()
        {
            var interceptor =
                new ActivatorInterceptor<SimpleWidget>(w => w.Intercept());
            var instance = new SmartInstance<SimpleWidget>();

            instance.AddInterceptor(interceptor);

            new Container().GetInstance<SimpleWidget>(instance)
                .WasIntercepted.ShouldBeTrue();
        }
Exemple #8
0
        public void add_interceptor_that_cannot_accept_the_returned_type()
        {
            var instance = new InstanceUnderTest
            {
                Type = typeof(ColorRule)
            };

            var interceptor = new ActivatorInterceptor <IGateway>(g => g.DoSomething());

            instance.ReturnedType.CanBeCastTo(interceptor.Accepts)
            .ShouldBeFalse();

            Exception <ArgumentOutOfRangeException> .ShouldBeThrownBy(() => { instance.AddInterceptor(interceptor); });
        }
        public void add_interceptor_that_cannot_accept_the_returned_type()
        {
            var instance = new InstanceUnderTest
            {
                Type = typeof(ColorRule)
            };

            var interceptor = new ActivatorInterceptor<IGateway>(g => g.DoSomething());

            instance.ReturnedType.CanBeCastTo(interceptor.Accepts)
                .ShouldBeFalse();

            Exception<ArgumentOutOfRangeException>.ShouldBeThrownBy(() => { instance.AddInterceptor(interceptor); });
        }
        public void accept_visitor_for_activator()
        {
            var target = new Target();
            var inner = Constant.For(target);

            var interceptor = new ActivatorInterceptor<Target>(x => x.ThrowUp());
            var plan = new InterceptionPlan(typeof(ITarget), inner, Policies.Default(),
                new IInterceptor[]
                {
                    interceptor
                });

            plan.AcceptVisitor(theVisitor);

            theVisitor.AssertWasCalled(x => x.Activator(interceptor));
        }
        public void do_not_duplicate_interceptor_policies()
        {
            var theActivator = new ActivatorInterceptor<ITarget>(x => x.Activate());
            var policy1 = new InterceptorPolicy<ITarget>(theActivator);
            var policy2 = new InterceptorPolicy<ITarget>(theActivator);

            policy1.ShouldEqual(policy2);

            var policies = new InterceptorPolicies();
            policies.Add(policy1);
            policies.Add(policy2);
            policies.Add(policy1);
            policies.Add(policy2);

            policies.Policies.Single().ShouldBeTheSameAs(policy1);
        }
        public void accept_visitor_for_activator()
        {
            var target = new Target();
            var inner  = Constant.For(target);

            var interceptor = new ActivatorInterceptor <Target>(x => x.ThrowUp());
            var plan        = new InterceptionPlan(typeof(ITarget), inner, new Policies(),
                                                   new IInterceptor[]
            {
                interceptor
            });

            plan.AcceptVisitor(theVisitor);

            theVisitor.AssertWasCalled(x => x.Activator(interceptor));
        }
Exemple #13
0
        public void add_interceptor_when_the_accept_type_is_possible_on_the_return_type()
        {
            var instance = new InstanceUnderTest
            {
                Type = typeof (StubbedGateway)
            };

            var interceptor = new ActivatorInterceptor<IGateway>(g => g.DoSomething());

            instance.ReturnedType.CanBeCastTo(interceptor.Accepts)
                .ShouldBeTrue();

            instance.AddInterceptor(interceptor);

            instance.Interceptors.Single()
                .ShouldBeTheSameAs(interceptor);
        }
        public void do_not_duplicate_interceptor_policies()
        {
            var theActivator = new ActivatorInterceptor <ITarget>(x => x.Activate());
            var policy1      = new InterceptorPolicy <ITarget>(theActivator);
            var policy2      = new InterceptorPolicy <ITarget>(theActivator);

            policy1.ShouldBe(policy2);

            var policies = Policies.Default();

            policies.Add(policy1);
            policies.Add(policy2);
            policies.Add(policy1);
            policies.Add(policy2);

            policies.Interception().Single().ShouldBeTheSameAs(policy1);
        }
Exemple #15
0
        public void add_interceptor_when_the_accept_type_is_possible_on_the_return_type()
        {
            var instance = new InstanceUnderTest
            {
                Type = typeof(StubbedGateway)
            };

            var interceptor = new ActivatorInterceptor <IGateway>(g => g.DoSomething());

            instance.ReturnedType.CanBeCastTo(interceptor.Accepts)
            .ShouldBeTrue();

            instance.AddInterceptor(interceptor);

            instance.Interceptors.Single()
            .ShouldBeTheSameAs(interceptor);
        }
        public void compile_and_use_by_itself_with_session()
        {
            var activator = new ActivatorInterceptor<Target>((s, t) => t.UseSession(s));
            var variable = Expression.Variable(typeof(Target), "target");

            var expression = activator.ToExpression(Policies.Default(), Parameters.Context, variable);

            var lambdaType = typeof(Action<IContext, Target>);
            var lambda = Expression.Lambda(lambdaType, expression, Parameters.Context, variable);

            var action = lambda.Compile().As<Action<IContext, Target>>();

            var target = new Target();
            var session = new FakeBuildSession();
            action(session, target);

            target.Session.ShouldBeTheSameAs(session);
        }
        public void compile_and_use_by_itself_with_session()
        {
            var activator = new ActivatorInterceptor <Target>((s, t) => t.UseSession(s));
            var variable  = Expression.Variable(typeof(Target), "target");

            var expression = activator.ToExpression(Policies.Default(), Parameters.Context, variable);

            var lambdaType = typeof(Action <IContext, Target>);
            var lambda     = Expression.Lambda(lambdaType, expression, Parameters.Context, variable);

            var action = lambda.Compile().As <Action <IContext, Target> >();

            var target  = new Target();
            var session = new FakeBuildSession();

            action(session, target);

            target.Session.ShouldBeTheSameAs(session);
        }
        public void activator_that_fails_gets_wrapped_in_descriptive_text()
        {
            var target = new Target();
            var inner  = Constant.For(target);

            var interceptor = new ActivatorInterceptor <Target>(x => x.ThrowUp());
            var plan        = new InterceptionPlan(typeof(ITarget), inner, new Policies(),
                                                   new IInterceptor[]
            {
                interceptor
            });

            var ex =
                Exception <StructureMapInterceptorException> .ShouldBeThrownBy(
                    () => {
                var session = new StubBuildSession();
                plan.ToBuilder <ITarget>()(session, session);
            });

            ex.Title.ShouldEqual("Activator interceptor failed during object creation.  See the inner exception for details.");
            ex.Message.ShouldContain(interceptor.Description);
        }
        public void activator_that_fails_gets_wrapped_in_descriptive_text()
        {
            var target = new Target();
            var inner = Constant.For(target);

            var interceptor = new ActivatorInterceptor<Target>(x => x.ThrowUp());
            var plan = new InterceptionPlan(typeof(ITarget), inner, Policies.Default(),
                new IInterceptor[]
                {
                    interceptor
                });

            var ex =
                Exception<StructureMapInterceptorException>.ShouldBeThrownBy(
                    () =>
                    {
                        var session = new StubBuildSession();
                        plan.ToBuilder<ITarget>()(session, session);
                    });

            ex.Title.Trim().ShouldBe(
                "Activator interceptor failed during object creation.  See the inner exception for details.");
            ex.Message.ShouldContain(interceptor.Description);
        }
        public void the_description_using_session_and_explicit_description()
        {
            var activator = new ActivatorInterceptor<Target>((s, t) => t.UseSession(s), "use the Force Luke!");

            activator.Description.ShouldContain("use the Force Luke!");
        }
 public InterceptorPolicyTester()
 {
     theActivator = new ActivatorInterceptor<ITarget>(x => x.Activate());
     thePolicy = new InterceptorPolicy<ITarget>(theActivator);
 }
 public void SetUp()
 {
     theActivator = new ActivatorInterceptor<ITarget>(x => x.Activate());
 }
 public void SetUp()
 {
     theActivator = new ActivatorInterceptor <ITarget>(x => x.Activate());
 }
        public void the_description_using_session()
        {
            var activator = new ActivatorInterceptor<Target>((s, t) => t.UseSession(s));

            activator.Description.ShouldContain("Target.UseSession(IContext)");
        }
Exemple #25
0
 public InterceptorPolicyTester()
 {
     theActivator = new ActivatorInterceptor <ITarget>(x => x.Activate());
     thePolicy    = new InterceptorPolicy <ITarget>(theActivator);
 }
 public ActivatorInterceptorTester()
 {
     theActivator = new ActivatorInterceptor<ITarget>(x => x.Activate());
 }
        public void description_is_set_explicitly()
        {
            theActivator = new ActivatorInterceptor <ITarget>(x => x.Activate(), "gonna start it up");

            theActivator.Description.ShouldContain("gonna start it up");
        }
        public void description_is_set_explicitly()
        {
            theActivator = new ActivatorInterceptor<ITarget>(x => x.Activate(), "gonna start it up");

            theActivator.Description.ShouldContain("gonna start it up");
        }
 public void SetUp()
 {
     theActivator = new ActivatorInterceptor <ITarget>(x => x.Activate());
     thePolicy    = new InterceptorPolicy <ITarget>(theActivator);
 }
        public void the_description_using_session()
        {
            var activator = new ActivatorInterceptor <Target>((s, t) => t.UseSession(s));

            activator.Description.ShouldContain("Target.UseSession(IContext)");
        }
 public void SetUp()
 {
     theActivator = new ActivatorInterceptor<ITarget>(x => x.Activate());
     thePolicy = new InterceptorPolicy<ITarget>(theActivator);
 }
 public ActivatorInterceptorTester()
 {
     theActivator = new ActivatorInterceptor <ITarget>(x => x.Activate());
 }
        public void the_description_using_session_and_explicit_description()
        {
            var activator = new ActivatorInterceptor <Target>((s, t) => t.UseSession(s), "use the Force Luke!");

            activator.Description.ShouldContain("use the Force Luke!");
        }