Exemple #1
0
        public void EventsAreIntercepted()
        {
            CallCountHandler fireHandler = new CallCountHandler();
            CallCountHandler addHandler  = new CallCountHandler();

            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();

            Assert.IsTrue(interceptor.CanIntercept(typeof(OverrideableProperies)));

            Type            proxyType = interceptor.CreateProxyType(typeof(ClassWithEvent));
            ClassWithEvent  instance  = (ClassWithEvent)Activator.CreateInstance(proxyType);
            PipelineManager manager   = new PipelineManager();

            ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
            SetPipeline(manager, instance, "add_MyEvent", addHandler);
            SetPipeline(manager, instance, "FireMyEvent", fireHandler);

            bool raised = false;

            instance.MyEvent += delegate { raised = true; };

            instance.FireMyEvent();
            instance.FireMyEvent();

            Assert.IsTrue(raised);

            Assert.AreEqual(2, fireHandler.CallCount);
            Assert.AreEqual(1, addHandler.CallCount);
        }
Exemple #2
0
        public void AttemptingToInterceptInvalidClassThrows()
        {
            PostCallCountHandler     handler     = new PostCallCountHandler();
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();

            interceptor.CreateProxyType(typeof(CantTouchThis));
        }
Exemple #3
0
        public void CanGenerateDerivedTypeForAbstractType()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(AbstractClassWithPublicConstructor));

            Assert.AreSame(typeof(AbstractClassWithPublicConstructor), proxyType.BaseType);
        }
Exemple #4
0
        public void CanCreateInstanceForGeneratedTypeForAbstractTypeWithProtectedConstructor()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(AbstractClassWithProtectedConstructor));

            Activator.CreateInstance(proxyType, 100);
        }
        public void CanInterceptProperties()
        {
            CallCountHandler getHandler = new CallCountHandler();
            CallCountHandler setHandler = new CallCountHandler();

            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Assert.IsTrue(interceptor.CanIntercept(typeof(OverrideableProperies)));

            Type proxyType = interceptor.CreateProxyType(typeof(OverrideableProperies));
            OverrideableProperies instance = (OverrideableProperies)Activator.CreateInstance(proxyType);

            PipelineManager manager = new PipelineManager();
            ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
            SetPipeline(manager, instance, "get_IntProperty", getHandler);
            SetPipeline(manager, instance, "set_IntProperty", setHandler);

            instance.IntProperty = 12;
            instance.IntProperty = 15;

            int total = 0;
            for (int i = 0; i < 5; ++i)
            {
                total += instance.IntProperty;
            }

            Assert.AreEqual(5 * 15, total);

            Assert.AreEqual(5, getHandler.CallCount);
            Assert.AreEqual(2, setHandler.CallCount);
        }
Exemple #6
0
        public void CanInterceptProperties()
        {
            CallCountHandler getHandler = new CallCountHandler();
            CallCountHandler setHandler = new CallCountHandler();

            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();

            Assert.IsTrue(interceptor.CanIntercept(typeof(OverrideableProperies)));

            Type proxyType = interceptor.CreateProxyType(typeof(OverrideableProperies));
            OverrideableProperies instance = (OverrideableProperies)Activator.CreateInstance(proxyType);

            PipelineManager manager = new PipelineManager();

            ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
            SetPipeline(manager, instance, "get_IntProperty", getHandler);
            SetPipeline(manager, instance, "set_IntProperty", setHandler);

            instance.IntProperty = 12;
            instance.IntProperty = 15;

            int total = 0;

            for (int i = 0; i < 5; ++i)
            {
                total += instance.IntProperty;
            }

            Assert.AreEqual(5 * 15, total);

            Assert.AreEqual(5, getHandler.CallCount);
            Assert.AreEqual(2, setHandler.CallCount);
        }
Exemple #7
0
        public void AssortedParameterKindsAreProperlyHandled()
        {
            var interceptor = new VirtualMethodInterceptor();

            var type = interceptor.CreateProxyType(typeof(AssortedParameterKindsAreProperlyHandledHelper.TypeWithAssertedParameterKinds));

            IInterceptingProxy proxy = Activator.CreateInstance(type) as IInterceptingProxy;

            AssortedParameterKindsAreProperlyHandledHelper.PerformTest(proxy);
        }
Exemple #8
0
        public void ReflectingOverProxyTypesGivesOnlyBaseProperties()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();

            Assert.IsTrue(interceptor.CanIntercept(typeof(OverrideableProperies)));

            Type proxyType = interceptor.CreateProxyType(typeof(OverrideableProperies));

            PropertyInfo[] properties = proxyType.GetProperties();

            Assert.AreEqual(2, properties.Length);
            Assert.IsTrue(Sequence.ForAll(properties, delegate(PropertyInfo pi) { return(pi.DeclaringType == typeof(OverrideableProperies)); }));
        }
        public void AttemptingToInterceptInvalidClassThrows()
        {
            PostCallCountHandler     handler     = new PostCallCountHandler();
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();

            try
            {
                interceptor.CreateProxyType(typeof(CantTouchThis));
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
            }
        }
Exemple #10
0
        public void CanInterceptMethods()
        {
            CallCountHandler         h1          = new CallCountHandler();
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(TwoOverrideableMethods));

            TwoOverrideableMethods instance =
                (TwoOverrideableMethods)Activator.CreateInstance(proxyType);

            SetPipeline(instance, "DoSomething", h1);

            instance.DoSomething();

            Assert.IsTrue(instance.DidSomething);
            Assert.AreEqual(1, h1.CallCount);
        }
Exemple #11
0
        public void CanInvokeOverridenAbstractMethodMethodOnInterceptedAbstractDerivedFromAbstractTypeInstance()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(AbstractDerivedFromAbstractClassWithPublicConstructor));

            AbstractDerivedFromAbstractClassWithPublicConstructor instance =
                (AbstractDerivedFromAbstractClassWithPublicConstructor)Activator.CreateInstance(proxyType);
            bool invoked = false;

            ((IInterceptingProxy)instance).AddInterceptionBehavior(
                new DelegateInterceptionBehavior((mi, gn) => { invoked = true; return(gn()(mi, gn)); }));

            int value = instance.AbstractMethod();

            Assert.AreEqual(200, value);
            Assert.IsTrue(invoked);
        }
Exemple #12
0
        public void InvokingAbstractMethodFromInterceptedAbstracTypeInstanceThrows()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(AbstractClassWithPublicConstructor));

            AbstractClassWithPublicConstructor instance =
                (AbstractClassWithPublicConstructor)Activator.CreateInstance(proxyType);

            try
            {
                instance.AbstractMethod();
                Assert.Fail("should have thrown");
            }
            catch (NotImplementedException)
            {
            }
        }
        public void CanInterceptMethods()
        {
            CallCountHandler h1 = new CallCountHandler();
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(TwoOverrideableMethods));

            TwoOverrideableMethods instance =
                (TwoOverrideableMethods)Activator.CreateInstance(proxyType);
            PipelineManager manager = new PipelineManager();
            ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
            SetPipeline(manager, instance, "DoSomething", h1);

            instance.DoSomething();

            Assert.IsTrue(instance.DidSomething);
            Assert.AreEqual(1, h1.CallCount);
        }
Exemple #14
0
        public void CanInterceptMethods()
        {
            CallCountHandler         h1          = new CallCountHandler();
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(TwoOverrideableMethods));

            TwoOverrideableMethods instance =
                (TwoOverrideableMethods)Activator.CreateInstance(proxyType);
            PipelineManager manager = new PipelineManager();

            ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
            SetPipeline(manager, instance, "DoSomething", h1);

            instance.DoSomething();

            Assert.IsTrue(instance.DidSomething);
            Assert.AreEqual(1, h1.CallCount);
        }
Exemple #15
0
        public void CanInterceptTypeWithNonDefaultCtor()
        {
            CallCountHandler         h1          = new CallCountHandler();
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(ClassWithNonDefaultCtor));

            ClassWithNonDefaultCtor instance =
                (ClassWithNonDefaultCtor)Activator.CreateInstance(proxyType, "arg-value");

            PipelineManager manager = new PipelineManager();

            ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
            SetPipeline(manager, instance, "GetArg", h1);

            Assert.AreEqual("arg-value", instance.GetArg());

            Assert.AreEqual(1, h1.CallCount);
        }
Exemple #16
0
        public void CanInterceptNonPublicPropertiesUsingVirtualInterceptor()
        {
            VirtualMethodInterceptor interceptor =
                new VirtualMethodInterceptor();

            Type generatedType = interceptor.CreateProxyType(typeof(ClassWithNonPublicVirtualProperties));

            ClassWithNonPublicVirtualProperties instance = (ClassWithNonPublicVirtualProperties)Activator.CreateInstance(generatedType);

            bool intercepted = false;

            ((IInterceptingProxy)instance)
            .AddInterceptionBehavior(
                new ActionInterceptionBehavior(() => intercepted = true));

            var result = instance.GetSomePropertyValue();

            Assert.IsTrue(intercepted);
        }
        public void ReflectingOverProxyTypesGivesOnlyBaseProperties()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Assert.IsTrue(interceptor.CanIntercept(typeof(OverrideableProperies)));

            Type proxyType = interceptor.CreateProxyType(typeof(OverrideableProperies));
            PropertyInfo[] properties = proxyType.GetProperties();

            Assert.AreEqual(2, properties.Length);

            Assert.IsTrue(properties.All(pi => pi.DeclaringType == typeof(OverrideableProperies)));
        }
        public void EventsAreIntercepted()
        {
            CallCountHandler fireHandler = new CallCountHandler();
            CallCountHandler addHandler = new CallCountHandler();

            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Assert.IsTrue(interceptor.CanIntercept(typeof(OverrideableProperies)));

            Type proxyType = interceptor.CreateProxyType(typeof(ClassWithEvent));
            ClassWithEvent instance = (ClassWithEvent)Activator.CreateInstance(proxyType);
            PipelineManager manager = new PipelineManager();
            ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
            SetPipeline(manager, instance, "add_MyEvent", addHandler);
            SetPipeline(manager, instance, "FireMyEvent", fireHandler);


            bool raised = false;
            instance.MyEvent += delegate { raised = true; };

            instance.FireMyEvent();
            instance.FireMyEvent();

            Assert.IsTrue(raised);

            Assert.AreEqual(2, fireHandler.CallCount);
            Assert.AreEqual(1, addHandler.CallCount);

        }
        public void CanInterceptTypeWithNonDefaultCtor()
        {
            CallCountHandler h1 = new CallCountHandler();
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(ClassWithNonDefaultCtor));

            ClassWithNonDefaultCtor instance =
                (ClassWithNonDefaultCtor)Activator.CreateInstance(proxyType, "arg-value");

            PipelineManager manager = new PipelineManager();
            ((IInterceptingProxy)instance).AddInterceptionBehavior(new PolicyInjectionBehavior(manager));
            SetPipeline(manager, instance, "GetArg", h1);

            Assert.AreEqual("arg-value", instance.GetArg());

            Assert.AreEqual(1, h1.CallCount);
        }
        public void CanGenerateDerivedTypeForAbstractType()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(AbstractClassWithPublicConstructor));

            Assert.AreSame(typeof(AbstractClassWithPublicConstructor), proxyType.BaseType);
        }
        public void InvokingAbstractMethodFromInterceptedAbstracTypeInstanceThrows()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(AbstractClassWithPublicConstructor));

            AbstractClassWithPublicConstructor instance =
                (AbstractClassWithPublicConstructor)Activator.CreateInstance(proxyType);

            try
            {
                instance.AbstractMethod();
                Assert.Fail("should have thrown");
            }
            catch (NotImplementedException)
            {
            }
        }
        public void CanCreateInstanceForGeneratedTypeForAbstractTypeWithProtectedConstructor()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(AbstractClassWithProtectedConstructor));

            Activator.CreateInstance(proxyType, 100);
        }
        public void AssortedParameterKindsAreProperlyHandled()
        {
            var interceptor = new VirtualMethodInterceptor();

            var type = interceptor.CreateProxyType(typeof(AssortedParameterKindsAreProperlyHandledHelper.TypeWithAssertedParameterKinds));

            IInterceptingProxy proxy = Activator.CreateInstance(type) as IInterceptingProxy;

            AssortedParameterKindsAreProperlyHandledHelper.PerformTest(proxy);
        }
        public void CanInvokeOverridenAbstractMethodMethodOnInterceptedAbstractDerivedFromAbstractTypeInstance()
        {
            VirtualMethodInterceptor interceptor = new VirtualMethodInterceptor();
            Type proxyType = interceptor.CreateProxyType(typeof(AbstractDerivedFromAbstractClassWithPublicConstructor));

            AbstractDerivedFromAbstractClassWithPublicConstructor instance =
                (AbstractDerivedFromAbstractClassWithPublicConstructor)Activator.CreateInstance(proxyType);
            bool invoked = false;
            ((IInterceptingProxy)instance).AddInterceptionBehavior(
                new DelegateInterceptionBehavior((mi, gn) => { invoked = true; return gn()(mi, gn); }));

            int value = instance.AbstractMethod();

            Assert.AreEqual(200, value);
            Assert.IsTrue(invoked);
        }