Exemplo n.º 1
0
        public void InterceptorCreatesProxyInstance()
        {
            IInstanceInterceptor   interceptor = new InterfaceInterceptor();
            ImplementsInterfaceOne target      = new ImplementsInterfaceOne();

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

            IInterfaceOne intercepted = (IInterfaceOne)proxy;

            intercepted.TargetMethod();

            Assert.IsTrue(target.TargetMethodCalled);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public void GeneratedProxyCallsHandlers()
        {
            IInstanceInterceptor   interceptor = new InterfaceInterceptor();
            ImplementsInterfaceOne target      = new ImplementsInterfaceOne();

            IInterceptingProxy proxy   = interceptor.CreateProxy(typeof(IInterfaceOne), target);
            CallCountHandler   handler = new CallCountHandler();

            proxy.SetPipeline(typeof(IInterfaceOne).GetMethod("TargetMethod"),
                              new HandlerPipeline(new ICallHandler[] { handler }));

            IInterfaceOne intercepted = (IInterfaceOne)proxy;

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

            Assert.AreEqual(3, handler.CallCount);
        }
Exemplo n.º 4
0
        public void InterfaceInterceptorSetsTargetToTargetObject()
        {
            object suppliedTarget = null;

            var behavior = new DelegateInterceptionBehavior((inputs, getNext) =>
            {
                suppliedTarget = inputs.Target;
                return(getNext()(inputs, getNext));
            });

            var actualTarget = new ImplementsInterfaceOne();

            var proxy = Intercept.ThroughProxy <IInterfaceOne>(
                actualTarget, new InterfaceInterceptor(),
                new[] { behavior });

            proxy.TargetMethod();

            Assert.IsTrue(actualTarget.TargetMethodCalled);
            Assert.AreSame(actualTarget, suppliedTarget);
        }