public void CanAddInterceptionBehaviorsToPipeline()
        {
            ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>();
            IInterceptingProxy   pm       = (IInterceptingProxy)instance;

            CallCountInterceptionBehavior interceptor = new CallCountInterceptionBehavior();

            pm.AddInterceptionBehavior(interceptor);
        }
        public void CanInterceptEventsMethods()
        {
            CallCountInterceptionBehavior interceptor = new CallCountInterceptionBehavior();
            ClassWithAVirtualEvent        instance    = WireupHelper.GetInterceptingInstance <ClassWithAVirtualEvent>();

            ((IInterceptingProxy)instance).AddInterceptionBehavior(interceptor);

            instance.SomeEvent += (sender, args) => { };

            Assert.AreEqual(1, interceptor.CallCount);
        }
        public void FiringAnEventIsNotIntercepted()
        {
            CallCountInterceptionBehavior interceptor = new CallCountInterceptionBehavior();
            ClassWithAVirtualEvent        instance    = WireupHelper.GetInterceptingInstance <ClassWithAVirtualEvent>();

            ((IInterceptingProxy)instance).AddInterceptionBehavior(interceptor);

            instance.SomeEvent += (sender, args) => { };
            instance.TriggerIt();

            Assert.AreEqual(1, interceptor.CallCount);
        }
        public void Then_CanConfigureBehaviorWithNameOnly()
        {
            var callCount = new CallCountInterceptionBehavior();

            IUnityContainer container = this.ConfiguredContainer("canConfigureBehaviorWithNameOnly")
                                        .RegisterInstance <IInterceptionBehavior>("call count", callCount);

            var instance = container.Resolve <Interceptable>();

            instance.DoSomething();

            Assert.AreEqual(1, callCount.CallCount);
        }
示例#5
0
        public void ProxyInterceptsEvents()
        {
            IInstanceInterceptor          interceptor          = new InterfaceInterceptor();
            ClassWithEvents               target               = new ClassWithEvents();
            IInterceptingProxy            proxy                = interceptor.CreateProxy(typeof(IDoEvents), target);
            CallCountInterceptionBehavior interceptionBehavior = new CallCountInterceptionBehavior();

            proxy.AddInterceptionBehavior(interceptionBehavior);

            ((IDoEvents)proxy).SomeEvent += (s, a) => { };

            Assert.AreEqual(1, interceptionBehavior.CallCount);
        }
        public void Then_CanConfigureInterceptorThroughConfigurationFile()
        {
            var container = ConfiguredContainer("configuringInterceptorThroughConfigurationFile");

            var callCount = new CallCountInterceptionBehavior();

            container.RegisterType <Interceptable>(new InterceptionBehavior(callCount));

            var instance = container.Resolve <Interceptable>();

            instance.DoSomething();

            Assert.AreEqual(1, callCount.CallCount);
        }
        public void Then_CanConfigureAdditionalInterfaceThroughConfigurationFile()
        {
            IUnityContainer container = ConfiguredContainer("configuringAdditionalInterfaceThroughConfigurationFile");

            var callCount = new CallCountInterceptionBehavior();

            container.RegisterType <Interceptable>(
                new InterceptionBehavior(callCount),
                new Interceptor(new VirtualMethodInterceptor()));

            var instance = container.Resolve <Interceptable>();

            instance.DoSomething();

            Assert.AreEqual(1, callCount.CallCount);
            Assert.IsTrue(instance is IServiceProvider);
        }
示例#8
0
        public void CanInterceptGenericMethodOnInterface()
        {
            var interceptor = new CallCountInterceptionBehavior();

            var original    = new ObjectWithGenericMethod();
            var intercepted = new InterceptingRealProxy(original, typeof(IInterfaceWithGenericMethod))
                              .GetTransparentProxy() as IInterfaceWithGenericMethod;

            IInterceptingProxy proxy = (IInterceptingProxy)intercepted;

            proxy.AddInterceptionBehavior(interceptor);

            var result = intercepted.GetTypeName(6);

            Assert.AreEqual("Int32", result);
            Assert.AreEqual(1, interceptor.CallCount);
        }
示例#9
0
        public void CanInterceptMethodsThroughProxy()
        {
            CallCountInterceptionBehavior interceptor = new CallCountInterceptionBehavior();

            MBROWithOneMethod original    = new MBROWithOneMethod();
            MBROWithOneMethod intercepted = new InterceptingRealProxy(original, typeof(MBROWithOneMethod))
                                            .GetTransparentProxy() as MBROWithOneMethod;

            IInterceptingProxy proxy = (IInterceptingProxy)intercepted;

            proxy.AddInterceptionBehavior(interceptor);

            int result = intercepted.DoSomething(5);

            Assert.AreEqual(5 * 3, result);
            Assert.AreEqual(1, interceptor.CallCount);
        }
示例#10
0
        public void InterceptorCanInterceptProxyInstances()
        {
            CallCountInterceptionBehavior callCounter = new CallCountInterceptionBehavior();

            ProxiedInterfaceImpl impl     = new ProxiedInterfaceImpl();
            IProxiedInterface    instance = (IProxiedInterface) new MyProxy(typeof(IProxiedInterface), impl).GetTransparentProxy();

            IInstanceInterceptor interceptor = new InterfaceInterceptor();

            IInterceptingProxy proxy = (IInterceptingProxy)interceptor.CreateProxy(typeof(IProxiedInterface), (IProxiedInterface)instance);

            proxy.AddInterceptionBehavior(callCounter);

            IProxiedInterface inter = (IProxiedInterface)proxy;

            Assert.AreEqual("hello world", inter.DoSomething());
            Assert.AreEqual(1, callCounter.CallCount);
        }
示例#11
0
        public void GeneratedProxyCallsInterceptionBehaviors()
        {
            IInstanceInterceptor   interceptor = new InterfaceInterceptor();
            ImplementsInterfaceOne target      = new ImplementsInterfaceOne();

            IInterceptingProxy            proxy = interceptor.CreateProxy(typeof(IInterfaceOne), target);
            CallCountInterceptionBehavior interceptionBehavior = new CallCountInterceptionBehavior();

            proxy.AddInterceptionBehavior(interceptionBehavior);

            IInterfaceOne intercepted = (IInterfaceOne)proxy;

            intercepted.TargetMethod();
            intercepted.TargetMethod();
            intercepted.TargetMethod();

            Assert.AreEqual(3, interceptionBehavior.CallCount);
        }
示例#12
0
        public void ProxyInterceptsAddingAHandlerToAnEvent()
        {
            // arrange
            CallCountInterceptionBehavior interceptor
                = new CallCountInterceptionBehavior();

            MBROWithAnEvent original    = new MBROWithAnEvent();
            MBROWithAnEvent intercepted = new InterceptingRealProxy(original, typeof(MBROWithAnEvent))
                                          .GetTransparentProxy() as MBROWithAnEvent;

            ((IInterceptingProxy)intercepted).AddInterceptionBehavior(interceptor);

            // act
            intercepted.SomeEvent += (s, a) => { };

            // assert
            Assert.AreEqual(1, interceptor.CallCount);
        }
示例#13
0
        public void CanGenerateProxyForClosedGeneric()
        {
            IInstanceInterceptor interceptor           = new InterfaceInterceptor();
            GenericImplementationOne <DateTime> target = new GenericImplementationOne <DateTime>();

            IInterceptingProxy            proxy = interceptor.CreateProxy(typeof(IGenericOne <DateTime>), target);
            CallCountInterceptionBehavior interceptionBehavior = new CallCountInterceptionBehavior();

            proxy.AddInterceptionBehavior(interceptionBehavior);

            IGenericOne <DateTime> intercepted = (IGenericOne <DateTime>)proxy;
            DateTime now = DateTime.Now;

            DateTime result = intercepted.DoSomething(now);

            Assert.AreEqual(now, result);
            Assert.IsTrue(target.DidSomething);
            Assert.AreEqual(1, interceptionBehavior.CallCount);
        }
示例#14
0
        public void CanInterceptClassWithSingleNonDefaultConstructor()
        {
            CallCountInterceptionBehavior callCountBehavior = new CallCountInterceptionBehavior();

            IUnityContainer container =
                new UnityContainer()
                .AddNewExtension <Interception>()
                .RegisterType <ClassWithSingleNonDefaultConstructor>(
                    new InjectionConstructor("some value"),
                    new Interceptor <VirtualMethodInterceptor>(),
                    new InterceptionBehavior(callCountBehavior));

            ClassWithSingleNonDefaultConstructor instance = container.Resolve <ClassWithSingleNonDefaultConstructor>();

            string value = instance.GetValue();

            Assert.AreEqual("some value", value);
            Assert.AreEqual(1, callCountBehavior.CallCount);
        }
示例#15
0
        public void CanSetUpInterceptorThroughInjectionMemberForExistingInterceptor()
        {
            CallCountInterceptionBehavior interceptionBehavior = new CallCountInterceptionBehavior();

            IUnityContainer container = new UnityContainer();

            container.AddNewExtension <Interception>();

            container.RegisterType <IInterface, BaseClass>(
                "test",
                new Interceptor <InterfaceInterceptor>(),
                new InterceptionBehavior(interceptionBehavior));

            IInterface instance = container.Resolve <IInterface>("test");

            instance.DoSomething("1");

            Assert.AreEqual(1, interceptionBehavior.CallCount);
        }
示例#16
0
        public void ProxySendsOriginalWhenRaisingEvent()
        {
            // arrange
            IInstanceInterceptor          interceptor          = new InterfaceInterceptor();
            ClassWithEvents               target               = new ClassWithEvents();
            IInterceptingProxy            proxy                = interceptor.CreateProxy(typeof(IDoEvents), target);
            CallCountInterceptionBehavior interceptionBehavior = new CallCountInterceptionBehavior();

            proxy.AddInterceptionBehavior(interceptionBehavior);
            object sender = null;

            ((IDoEvents)proxy).SomeEvent += (s, a) => { sender = s; };

            // act
            ((IDoEvents)proxy).TriggerIt();

            // assert
            Assert.AreSame(target, sender);
            Assert.AreEqual(2, interceptionBehavior.CallCount);  // adding + calling TriggerIt
        }
示例#17
0
        public void ProxySendsOriginalWhenRaisingEvent()
        {
            // arrange
            CallCountInterceptionBehavior interceptor = new CallCountInterceptionBehavior();

            MBROWithAnEvent original    = new MBROWithAnEvent();
            MBROWithAnEvent intercepted = new InterceptingRealProxy(original, typeof(MBROWithAnEvent))
                                          .GetTransparentProxy() as MBROWithAnEvent;

            ((IInterceptingProxy)intercepted).AddInterceptionBehavior(interceptor);
            object sender = null;

            intercepted.SomeEvent += (s, a) => { sender = s; };

            // act
            intercepted.TriggerIt();

            // assert
            Assert.AreSame(original, sender);
            Assert.AreEqual(2, interceptor.CallCount);  // adding + calling TriggerIt
        }
示例#18
0
        public void RefsAndOutsAreProperlyHandled()
        {
            IInstanceInterceptor          interceptor = new InterfaceInterceptor();
            ImplementsHaveSomeRefsAndOuts target      = new ImplementsHaveSomeRefsAndOuts();

            IInterceptingProxy            proxy = interceptor.CreateProxy(typeof(IHaveSomeRefsAndOuts), target);
            CallCountInterceptionBehavior interceptionBehavior = new CallCountInterceptionBehavior();

            proxy.AddInterceptionBehavior(interceptionBehavior);

            IHaveSomeRefsAndOuts intercepted = (IHaveSomeRefsAndOuts)proxy;

            int    a;
            string s = "something";

            intercepted.DoSomething(out a, ref s);

            Assert.AreEqual(37, a);
            Assert.AreEqual("+++something***", s);
            Assert.AreEqual(1, interceptionBehavior.CallCount);
        }