Example #1
0
        public void ChangeValueInAfterCall()
        {
            var impl = new SomeInterfaceImplementation();
            var i    = new RecordingInterceptor();
            var h    = new InterceptingAdapterEmittedTypeHanlder(impl, i);
            var o    = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h);

            i.AfterCallAction = delegate(CallInfo ci)
            {
                ci.ReturnValue = "AnotherValue";
            };

            var result = o.SomeMethod("SomeString", 17);

            // Adapted method should have been called

            Assert.IsNotNull(impl.SomeMethodS);
            Assert.AreEqual("SomeString", impl.SomeMethodS);
            Assert.AreEqual(17, impl.SomeMethodI);

            // Both BeforeCall and AfterCall should have been called

            Assert.IsNotNull(i.BeforeCallInfo);
            Assert.IsNotNull(i.AfterCallInfo);

            // Result should be the value set by interceptor

            Assert.AreEqual("SomeValue", i.AfterCallInfo.ReturnValue);
            Assert.AreEqual("AnotherValue", result);
        }
        public void OnComponentCreated(ContractIdentity identity, IComponentFactory componentFactory, Type componentTargetType,
                                       ref object componentInstance, object originalInstance)
        {
            var match = false;

            if (IncludedContracts != null)
            {
                match |= IncludedContracts.Match(identity.Type);
            }

            if (!match)
            {
                return;
            }

            if (!identity.Type.IsInterface)
            {
                throw new ArgumentException(
                          "Can not create wrapper for non-interface type contracts. " +
                          "The transaction composition listener should be configured such that " +
                          "the non-interface type contracts do not match the wrapping criteria.");
            }


            var interceptor = new TracingInterceptor(identity.Type.Name);
            var callHanlder = new InterceptingAdapterEmittedTypeHanlder(componentInstance, interceptor);

            componentInstance = ClassEmitter.EmitInterfaceInstance(callHanlder, identity.Type);
        }
Example #3
0
        public void AdaptedAndInterceptor()
        {
            var impl = new SomeInterfaceImplementation();
            var i    = new RecordingInterceptor();
            var h    = new InterceptingAdapterEmittedTypeHanlder(impl, i);

            var o      = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h);
            var result = o.SomeMethod("StringParam", 17);

            Assert.IsNotNull(i.BeforeCallInfo);
            Assert.IsNotNull(i.AfterCallInfo);

            Assert.AreEqual("StringParam", impl.SomeMethodS);
            Assert.AreEqual(17, impl.SomeMethodI);

            Assert.AreEqual("SomeValue", result);

            //
            // BeforeCall

            Assert.AreEqual(2, i.BeforeCallInfo.Arguments.Length);
            Assert.AreEqual("StringParam", i.BeforeCallInfo.Arguments[0]);
            Assert.AreEqual(17, i.BeforeCallInfo.Arguments[1]);

            Assert.AreEqual(2, i.BeforeCallInfo.ArgumentTypes.Length);
            Assert.AreEqual(typeof(string), i.BeforeCallInfo.ArgumentTypes[0]);
            Assert.AreEqual(typeof(int), i.BeforeCallInfo.ArgumentTypes[1]);

            Assert.AreEqual(typeof(ISomeInterface), i.BeforeCallInfo.MethodOwner);
            Assert.AreEqual("SomeMethod", i.BeforeCallInfo.MethodName);
            Assert.AreEqual(typeof(string), i.BeforeCallInfo.ResultType);

            Assert.IsNull(i.BeforeCallInfo.ReturnValue);
            Assert.IsNull(i.BeforeCallInfo.ThrownException);
            Assert.IsFalse(i.BeforeCallInfo.Completed);

            //
            // AfterCall

            Assert.AreEqual(2, i.AfterCallInfo.Arguments.Length);
            Assert.AreEqual("StringParam", i.AfterCallInfo.Arguments[0]);
            Assert.AreEqual(17, i.AfterCallInfo.Arguments[1]);

            Assert.AreEqual(2, i.AfterCallInfo.ArgumentTypes.Length);
            Assert.AreEqual(typeof(string), i.AfterCallInfo.ArgumentTypes[0]);
            Assert.AreEqual(typeof(int), i.AfterCallInfo.ArgumentTypes[1]);

            Assert.AreEqual(typeof(ISomeInterface), i.AfterCallInfo.MethodOwner);
            Assert.AreEqual("SomeMethod", i.AfterCallInfo.MethodName);
            Assert.AreEqual(typeof(string), i.AfterCallInfo.ResultType);

            Assert.AreEqual("SomeValue", i.AfterCallInfo.ReturnValue);
            Assert.IsNull(i.AfterCallInfo.ThrownException);
            Assert.IsTrue(i.AfterCallInfo.Completed);
        }
        public void OnComponentCreated(ContractIdentity identity, IComponentFactory componentFactory, Type componentTargetType, ref object componentInstance, object originalInstance)
        {
            Console.WriteLine("LISTENER     - BigNumberFilterListener.OnComponentCreated({0})", identity == null ? "<null>" : identity.Type.Name);
            Console.WriteLine("             - Wrapping component for filtering big numbers.");

            if ((identity != null) && (identity.Type.IsInterface))
            {
                var handler          = new InterceptingAdapterEmittedTypeHanlder(componentInstance, new BigNumberFilterInterceptor());
                var wrappedComponent = ClassEmitter.EmitInterfaceInstance(handler, identity.Type);

                componentInstance = wrappedComponent;
            }
        }
        public void OnComponentCreated(ContractIdentity identity, IComponentFactory componentFactory, Type componentTargetType, ref object componentInstance, object originalInstance)
        {
            Console.WriteLine("LISTENER     - OnComponentCreated: {0}", identity.Type.Name);
            Console.WriteLine("             - Wrapping component for logging.");

            if (identity.Type.IsInterface)
            {
                var handler          = new InterceptingAdapterEmittedTypeHanlder(componentInstance, new LogToConsoleInterceptor());
                var wrappedComponent = ClassEmitter.EmitInterfaceInstance(handler, identity.Type);

                componentInstance = wrappedComponent;
            }
        }
Example #6
0
        public void AdaptedOnly()
        {
            var impl = new SomeInterfaceImplementation();
            var h    = new InterceptingAdapterEmittedTypeHanlder(impl);

            var o      = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h);
            var result = o.SomeMethod("StringParam", 17);

            Assert.AreEqual("StringParam", impl.SomeMethodS);
            Assert.AreEqual(17, impl.SomeMethodI);

            Assert.AreEqual("SomeValue", result);
        }
        public void SetThrownExceptionInAfterCall()
        {
            var impl = new SomeInterfaceImplementation();
            var i    = new RecordingInterceptor();
            var h    = new InterceptingAdapterEmittedTypeHanlder(impl, i);
            var o    = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h);

            i.AfterCallAction = delegate(CallInfo ci) { ci.ThrownException = new NullReferenceException(); };

            bool exceptionBubbledUp = false;

            try
            {
                o.SomeMethod("SomeString", 17);
            }
            catch (Exception e)
            {
                exceptionBubbledUp = true;
                Assert.IsInstanceOfType(e, typeof(AdaptedException));
                Assert.IsNotNull(e.InnerException);

                Assert.IsNotNull(((AdaptedException)e).Arguments);
                Assert.AreEqual("SomeMethod", ((AdaptedException)e).MethodName);

                Assert.AreEqual(2, ((AdaptedException)e).Arguments.Length);
                Assert.AreEqual("SomeString", ((AdaptedException)e).Arguments[0]);
                Assert.AreEqual(17, ((AdaptedException)e).Arguments[1]);

                Assert.IsFalse(((AdaptedException)e).BeforeCall);
                Assert.IsFalse(((AdaptedException)e).DuringCall);
                Assert.IsTrue(((AdaptedException)e).AfterCall);

                Assert.IsInstanceOfType(e.InnerException, typeof(NullReferenceException));
                Assert.IsNull(e.InnerException.InnerException);
            }

            Assert.IsTrue(exceptionBubbledUp);

            Assert.IsNotNull(impl.SomeMethodS);
            Assert.AreEqual("SomeString", impl.SomeMethodS);
            Assert.AreEqual(17, impl.SomeMethodI);

            Assert.IsNotNull(i.BeforeCallInfo);
            Assert.IsNotNull(i.AfterCallInfo);

            Assert.IsNull(i.BeforeCallInfo.ThrownException);
            Assert.IsNull(i.AfterCallInfo.ThrownException);
        }
        public void ExceptionInAdaptedCall()
        {
            var impl = new SomeInterfaceImplementation();
            var i    = new RecordingInterceptor();
            var h    = new InterceptingAdapterEmittedTypeHanlder(impl, i);
            var o    = _classEmitter.EmitInterfaceInstance <ISomeInterface>(h);

            bool exceptionBubbledUp = false;

            try
            {
                o.ThrowException();
            }
            catch (Exception e)
            {
                exceptionBubbledUp = true;
                Assert.IsInstanceOfType(e, typeof(AdaptedException));
                Assert.IsNotNull(e.InnerException);

                Assert.IsNotNull(((AdaptedException)e).Arguments);
                Assert.AreEqual("ThrowException", ((AdaptedException)e).MethodName);
                Assert.AreEqual(0, ((AdaptedException)e).Arguments.Length);

                Assert.IsFalse(((AdaptedException)e).BeforeCall);
                Assert.IsTrue(((AdaptedException)e).DuringCall);
                Assert.IsFalse(((AdaptedException)e).AfterCall);

                Assert.IsInstanceOfType(e.InnerException, typeof(NullReferenceException));
                Assert.IsNull(e.InnerException.InnerException);
            }

            Assert.IsTrue(exceptionBubbledUp);

            Assert.IsNotNull(i.BeforeCallInfo);
            Assert.IsNotNull(i.AfterCallInfo);

            Assert.IsNull(i.BeforeCallInfo.ThrownException);
            Assert.IsNotNull(i.AfterCallInfo.ThrownException);

            Assert.IsInstanceOfType(i.AfterCallInfo.ThrownException, typeof(NullReferenceException));
        }