public void CanInterceptVoidNoArgMethods()
        {
            ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>();

            instance.MethodOne();
            Assert.IsTrue(instance.OneWasCalled);
        }
        public void ThrowingFromInterceptedMethodStillRunsAllHandlers()
        {
            MethodInfo           thrower  = typeof(ClassWithDefaultCtor).GetMethod("NotImplemented");
            ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>();
            IInterceptingProxy   pm       = (IInterceptingProxy)instance;

            CallCountHandler     handler     = new CallCountHandler();
            PostCallCountHandler postHandler = new PostCallCountHandler();
            HandlerPipeline      pipeline    = new HandlerPipeline(new ICallHandler[] { postHandler, handler });
            PipelineManager      manager     = new PipelineManager();

            manager.SetPipeline(thrower, pipeline);
            pm.AddInterceptionBehavior(new PolicyInjectionBehavior(manager));

            try
            {
                instance.NotImplemented();
                Assert.Fail("Should have thrown before getting here");
            }
            catch (NotImplementedException)
            {
                // We're expecting this one
            }

            Assert.AreEqual(1, handler.CallCount);
            Assert.AreEqual(1, postHandler.CallsCompleted);
        }
        public void CanAddInterceptionBehaviorsToPipeline()
        {
            ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>();
            IInterceptingProxy   pm       = (IInterceptingProxy)instance;

            CallCountInterceptionBehavior interceptor = new CallCountInterceptionBehavior();

            pm.AddInterceptionBehavior(interceptor);
        }
        public void CanInterceptMethodWithGenericReturnTypeForValueTypeGenericParameter()
        {
            PostCallCountHandler handler = new PostCallCountHandler();
            ClassWithDefaultCtor instance
                = WireupHelper.GetInterceptedInstance <ClassWithDefaultCtor>("MethodWithGenericReturnType", handler);
            int value = instance.MethodWithGenericReturnType(5);

            Assert.AreEqual(5, value);
            Assert.AreEqual(1, handler.CallsCompleted);
        }
        public void CanInterceptNestedClass()
        {
            PostCallCountHandler handler  = new PostCallCountHandler();
            NestedClass          instance = WireupHelper.GetInterceptedInstance <NestedClass>("MakeAValue", handler);

            int result = instance.MakeAValue(12);

            Assert.AreEqual((12 * 37) + (12 / 2), result);
            Assert.AreEqual(1, handler.CallsCompleted);
        }
        public void CanInterceptMethodsWithParameters()
        {
            PostCallCountHandler handler  = new PostCallCountHandler();
            ClassWithDefaultCtor instance = WireupHelper.GetInterceptedInstance <ClassWithDefaultCtor>("AddUp", handler);

            string result = instance.AddUp(5, 12);

            Assert.AreEqual("17", result);
            Assert.AreEqual(1, handler.CallsCompleted);
        }
        public void CanInterceptMethodsThatReturnReferenceTypes()
        {
            PostCallCountHandler handler  = new PostCallCountHandler();
            ClassWithDefaultCtor instance = WireupHelper.GetInterceptedInstance <ClassWithDefaultCtor>("GimmeName", handler);

            string result = instance.GimmeName();

            Assert.AreEqual("name", result);
            Assert.AreEqual(1, handler.CallsCompleted);
        }
        public void CanInterceptMethodsThatHaveReturnValues()
        {
            PostCallCountHandler handler  = new PostCallCountHandler();
            ClassWithDefaultCtor instance = WireupHelper.GetInterceptedInstance <ClassWithDefaultCtor>("CalculateAnswer", handler);

            int result = instance.CalculateAnswer();

            Assert.AreEqual(42, result);
            Assert.AreEqual(1, handler.CallsCompleted);
        }
        public void ImplicitlyImplementedMethodsAreInterceptedIfVirtual()
        {
            CallCountHandler handler  = new CallCountHandler();
            Interesting      instance = WireupHelper.GetInterceptedInstance <Interesting>("DoSomethingInteresting", handler);

            instance.DoSomethingInteresting();

            Assert.IsTrue(instance.SomethingWasCalled);
            Assert.AreEqual(1, handler.CallCount);
        }
        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 CanInterceptGenericMethodOnClosedGenericType()
        {
            PostCallCountHandler handler = new PostCallCountHandler();
            InterceptingGenericClass <DateTime> instance =
                WireupHelper.GetInterceptedInstance <InterceptingGenericClass <DateTime> >("Reverse", handler);

            string result = instance.Reverse(137);

            Assert.AreEqual("731", result);
            Assert.AreEqual(1, handler.CallsCompleted);
        }
        public void CanInterceptMethodsWithRefParameters()
        {
            PostCallCountHandler handler  = new PostCallCountHandler();
            ClassWithDefaultCtor instance = WireupHelper.GetInterceptedInstance <ClassWithDefaultCtor>("MethodWithRefParameters", handler);

            string s      = "abc";
            int    result = instance.MethodWithRefParameters(5, ref s, 10);

            Assert.AreEqual(15, result);
            Assert.AreEqual("abc hooray!", s);
            Assert.AreEqual(1, handler.CallsCompleted);
        }
        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 CanInterceptClosedGenericType()
        {
            PostCallCountHandler handler = new PostCallCountHandler();
            InterceptingGenericClass <DateTime> instance =
                WireupHelper.GetInterceptedInstance <InterceptingGenericClass <DateTime> >("Decorate", handler);

            DateTime now = DateTime.Now;

            string result = instance.Decorate(now);

            Assert.AreEqual("**" + now + "**", result);
            Assert.AreEqual(1, handler.CallsCompleted);
        }
        public void CanInterceptMethodsWithOutParameters()
        {
            PostCallCountHandler handler  = new PostCallCountHandler();
            ClassWithDefaultCtor instance = WireupHelper.GetInterceptedInstance <ClassWithDefaultCtor>("OutParams", handler);

            int plusOne;
            int timesTwo;

            instance.OutParams(5, out plusOne, out timesTwo);

            Assert.AreEqual(5 + 1, plusOne);
            Assert.AreEqual(5 * 2, timesTwo);
            Assert.AreEqual(1, handler.CallsCompleted);
        }
        public void CanInterceptClassThatHasMultipleConstructors()
        {
            ClassWithMultipleCtors defaultInstance = WireupHelper.GetInterceptingInstance <ClassWithMultipleCtors>();

            Assert.IsTrue(defaultInstance.DefaultCalled);

            ClassWithMultipleCtors intInstance = WireupHelper.GetInterceptingInstance <ClassWithMultipleCtors>(42);

            Assert.AreEqual(42, intInstance.IntValue);
            Assert.IsFalse(intInstance.DefaultCalled);

            ClassWithMultipleCtors bothInstance = WireupHelper.GetInterceptingInstance <ClassWithMultipleCtors>(51, "Hello");

            Assert.AreEqual(51, bothInstance.IntValue);
            Assert.AreEqual("Hello", bothInstance.StringValue);
            Assert.IsFalse(bothInstance.DefaultCalled);
        }
        public void CallingMethodInvokesHandlers()
        {
            MethodInfo           methodOne = typeof(ClassWithDefaultCtor).GetMethod("MethodOne");
            ClassWithDefaultCtor instance  = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>();
            IInterceptingProxy   pm        = (IInterceptingProxy)instance;

            CallCountHandler     handler     = new CallCountHandler();
            PostCallCountHandler postHandler = new PostCallCountHandler();
            HandlerPipeline      pipeline    = new HandlerPipeline(new ICallHandler[] { postHandler, handler });
            PipelineManager      manager     = new PipelineManager();

            manager.SetPipeline(methodOne, pipeline);
            pm.AddInterceptionBehavior(new PolicyInjectionBehavior(manager));

            instance.MethodOne();

            Assert.AreEqual(1, handler.CallCount);
            Assert.AreEqual(1, postHandler.CallsCompleted);
        }
        public void CanCreateInterceptingClassOverClassWithDefaultConstructor()
        {
            ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>();

            Assert.AreNotSame(typeof(ClassWithDefaultCtor), instance.GetType());
        }
        public void InterceptingClassImplementsIInterceptingProxy()
        {
            ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>();

            Assert.IsTrue(instance is IInterceptingProxy);
        }
        public void CanCreateInterceptingClassOverClassWithoutDefaultConstructor()
        {
            ClassWithOneParamCtor instance = WireupHelper.GetInterceptingInstance <ClassWithOneParamCtor>(37);

            Assert.AreEqual(37, instance.CtorValue);
        }
        public void InterceptingClassCallsBaseClassConstructor()
        {
            ClassWithDefaultCtor instance = WireupHelper.GetInterceptingInstance <ClassWithDefaultCtor>();

            Assert.IsTrue(instance.CtorWasCalled);
        }