protected override object CreateProxy(AdvisedSupport advisedSupport)
 {
     Assert.IsFalse(advisedSupport.ProxyTargetType, "Not forcible decorator-based proxy");
     object proxy = CreateAopProxy(advisedSupport).GetProxy();
     Assert.IsTrue(AopUtils.IsCompositionAopProxy(proxy), "Should be a composition-based proxy: " + proxy.GetType());
     return proxy;
 }
        private object NewPrototypeInstance()
        {
            // in the case of a prototype, we need to give the proxy
            // an independent instance of the configuration...

            #region Instrumentation

            if (logger.IsDebugEnabled)
            {
                logger.Debug("Creating copy of prototype ProxyFactoryObject config: " + this);
            }

            #endregion

            // The copy needs a fresh advisor chain, and a fresh TargetSource.
            ITargetSource  targetSource      = FreshTargetSource();
            IList          advisorChain      = FreshAdvisorChain();
            IList          introductionChain = FreshIntroductionChain();
            AdvisedSupport copy = new AdvisedSupport();
            copy.CopyConfigurationFrom(this, targetSource, advisorChain, introductionChain);

            #region Instrumentation
            if (logger.IsDebugEnabled)
            {
                logger.Debug("Using ProxyConfig: " + copy);
            }
            #endregion

            object generatedProxy = copy.CreateAopProxy().GetProxy();
            base.IsFrozen = this.freezeProxy; // freeze after creating proxy to allow for interface autodetection
            return(generatedProxy);
        }
Example #3
0
        public void DoesNotInterceptFinalMethod()
        {
            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();

            advised.Target = new InheritanceTestObject();
            advised.AddAdvice(ni);

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

            Assert.IsTrue(proxy is InheritanceTestObject);
            InheritanceTestObject proxiedClass = proxy as InheritanceTestObject;

            proxiedClass.Name = "DoesNotInterceptFinalMethod";
            Assert.AreEqual("DoesNotInterceptFinalMethod", proxiedClass.Name);
            Assert.AreEqual(2, ni.Count);

            proxiedClass.Reset();
            Assert.AreEqual(2, ni.Count);
            Assert.AreEqual("InheritanceTestObject", proxiedClass.Name);
            Assert.AreEqual(3, ni.Count);
        }
Example #4
0
        public void InterceptNonVirtualMethodThatBelongsToAnInterface()
        {
            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport(new InheritanceTestObject());

            advised.AddAdvice(ni);

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

            Assert.IsTrue(proxy is InheritanceTestObject);
            InheritanceTestObject proxiedClass = proxy as InheritanceTestObject;

            Assert.AreEqual(0, proxiedClass.Value);
            Assert.AreEqual(0, ni.Count);

            Assert.IsTrue(proxy is IIncrementable);
            IIncrementable proxiedInterface = proxy as IIncrementable;

            proxiedInterface.Increment();
            Assert.AreEqual(1, proxiedInterface.Value);
            Assert.AreEqual(2, ni.Count);
        }
Example #5
0
        /// <summary>
        /// If possible, checks for advisor duplicates on the supplied <paramref name="advisedSupport"/> and
        /// eliminates them.
        /// </summary>
        protected virtual void EliminateDuplicateAdvisors(AdvisedSupport advisedSupport)
        {
            if (advisedSupport.TargetSource is SingletonTargetSource &&
                IsAopProxyType(advisedSupport.TargetSource))
            {
                IAdvised innerProxy = (IAdvised)advisedSupport.TargetSource.GetTarget();
                // eliminate duplicate advisors
                List <IAdvisor> thisAdvisors = new List <IAdvisor>(advisedSupport.Advisors);
                foreach (IAdvisor innerAdvisor in innerProxy.Advisors)
                {
                    foreach (IAdvisor thisAdvisor in thisAdvisors)
                    {
                        if (ReferenceEquals(thisAdvisor, innerAdvisor) ||
                            (thisAdvisor.GetType() == typeof(DefaultPointcutAdvisor) &&
                             thisAdvisor.Equals(innerAdvisor)
                            )
                            )
                        {
                            advisedSupport.RemoveAdvisor(thisAdvisor);
                        }
                    }
                }

                // elimination of duplicate introductions is not necessary
                // since they do not propagate to nested proxy anyway
            }
        }
        public void InterceptInheritedVirtualMethods()
        {
            DoesNotImplementInterfaceTestObject target = new DerivedDoesNotImplementInterfaceTestObject();

            target.Name = "Bruno";
            mockTargetSource.SetTarget(target);

            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();

            advised.TargetSource = mockTargetSource;
            advised.AddAdvice(ni);

            DoesNotImplementInterfaceTestObject proxy = CreateProxy(advised) as DoesNotImplementInterfaceTestObject;

            Assert.IsNotNull(proxy);

            // GetName() calls underlying protected "GetNameInternal()" which calls get_Name
            Assert.AreEqual(target.Name, proxy.GetName(), "Incorrect name");
            proxy.Name = "Bruno Baia";
            Assert.AreEqual("Bruno Baia", proxy.Name, "Incorrect name");

            Assert.AreEqual(3, ni.Count);
        }
        public void CannotInterceptFinalMethodThatDoesNotBelongToAnInterface()
        {
            DoesNotImplementInterfaceTestObject target = new DoesNotImplementInterfaceTestObject();

            target.Location = "Paris";
            mockTargetSource.SetTarget(target);

            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();

            advised.TargetSource = mockTargetSource;
            advised.AddAdvice(ni);

            DoesNotImplementInterfaceTestObject proxy = CreateProxy(advised) as DoesNotImplementInterfaceTestObject;

            Assert.IsNotNull(proxy);

            // Location is final and doesn't belong to an interface so can't proxy.
            // method call goes directly to the proxy
            // and will not have access to the valid _location field
            Assert.IsNull(proxy.Location);

            Assert.AreEqual(0, ni.Count);
        }
 protected override object CreateProxy(AdvisedSupport advisedSupport)
 {
     advisedSupport.ProxyTargetType = true;
     object proxy = CreateAopProxy(advisedSupport).GetProxy();
     Assert.IsTrue(AopUtils.IsDecoratorAopProxy(proxy));
     return proxy;
 }
        /// <summary>
        /// If possible, checks for advisor duplicates on the supplied <paramref name="advisedSupport"/> and 
        /// eliminates them.
        /// </summary>
        protected virtual void EliminateDuplicateAdvisors(AdvisedSupport advisedSupport)
        {
            if (advisedSupport.TargetSource is SingletonTargetSource
                && IsAopProxyType(advisedSupport.TargetSource))
            {
                IAdvised innerProxy = (IAdvised)advisedSupport.TargetSource.GetTarget();
                // eliminate duplicate advisors
                List<IAdvisor> thisAdvisors = new List<IAdvisor>(advisedSupport.Advisors);
                foreach (IAdvisor innerAdvisor in innerProxy.Advisors)
                {
                    foreach (IAdvisor thisAdvisor in thisAdvisors)
                    {
                        if (ReferenceEquals(thisAdvisor, innerAdvisor)
                            || (thisAdvisor.GetType() == typeof(DefaultPointcutAdvisor)
                                && thisAdvisor.Equals(innerAdvisor)
                               )
                            )
                        {
                            advisedSupport.RemoveAdvisor(thisAdvisor);
                        }
                    }
                }

                // elimination of duplicate introductions is not necessary 
                // since they do not propagate to nested proxy anyway
            }
        }
Example #10
0
        protected override object CreateProxy(AdvisedSupport advisedSupport)
        {
            Assert.IsFalse(advisedSupport.ProxyTargetType, "Not forcible decorator-based proxy");
            object proxy = CreateAopProxy(advisedSupport).GetProxy();

            Assert.IsTrue(AopUtils.IsCompositionAopProxy(proxy), "Should be a composition-based proxy: " + proxy.GetType());
            return(proxy);
        }
Example #11
0
        protected object CreateProxy(AdvisedSupport advisedSupport)
        {
            Type            proxyType     = new InheritanceAopProxyTypeBuilder(advisedSupport).BuildProxyType();
            ConstructorInfo proxyCtorInfo = proxyType.GetConstructor(new Type[] { typeof(IAdvised) });

            ExpressionEvaluator.GetValue(advisedSupport, "Activate()");
            return(((IAopProxy)proxyCtorInfo.Invoke(new object[] { advisedSupport })).GetProxy());
        }
        protected override object CreateProxy(AdvisedSupport advisedSupport)
        {
            advisedSupport.ProxyTargetType = true;
            object proxy = CreateAopProxy(advisedSupport).GetProxy();

            Assert.IsTrue(AopUtils.IsDecoratorAopProxy(proxy));
            return(proxy);
        }
        public void CannotProxyNonPublicClass()
        {
            NonPublicTestObject target = new NonPublicTestObject();
            mockTargetSource.SetTarget(target);
            AdvisedSupport advised = new AdvisedSupport(new Type[] { });
            advised.TargetSource = mockTargetSource;

            Assert.Throws<AopConfigException>(() => CreateAopProxy(advised));
        }
        public void CannotProxyNonPublicClass()
        {
            NonPublicTestObject target = new NonPublicTestObject();
            mockTargetSource.SetTarget(target);
            AdvisedSupport advised = new AdvisedSupport(new Type[] { });
            advised.TargetSource = mockTargetSource;

            IAopProxy aop = CreateAopProxy(advised);
        }
        public void CannotProxyNonPublicClass()
        {
            NonPublicTestObject target = new NonPublicTestObject();

            mockTargetSource.SetTarget(target);
            AdvisedSupport advised = new AdvisedSupport(new Type[] { });

            advised.TargetSource = mockTargetSource;

            IAopProxy aop = CreateAopProxy(advised);
        }
        public void CannotProxyNonPublicClass()
        {
            NonPublicTestObject target = new NonPublicTestObject();

            mockTargetSource.SetTarget(target);
            AdvisedSupport advised = new AdvisedSupport(new Type[] { });

            advised.TargetSource = mockTargetSource;

            Assert.Throws <AopConfigException>(() => CreateAopProxy(advised));
        }
Example #17
0
        public void IgnoresAdvisorDuplicates()
        {
            CountingBeforeAdvice cba1     = new CountingBeforeAdvice();
            IAdvisor             advisor1 = new DefaultPointcutAdvisor(cba1);

            AdvisedSupport advSup = new AdvisedSupport();

            advSup.AddAdvisor(advisor1);
            advSup.AddAdvisor(advisor1);

            Assert.AreEqual(1, advSup.Advisors.Count);
        }
        public void ProxyIsJustInterface()
        {
            TestObject target = new TestObject();
            target.Age = 32;

            AdvisedSupport advised = new AdvisedSupport();
            advised.Target = target;
            advised.Interfaces = new Type[] { typeof(ITestObject) };

            object proxy = CreateProxy(advised);

            Assert.IsTrue(proxy is ITestObject);
            Assert.IsFalse(proxy is TestObject);
        }
Example #19
0
        /// <summary>
        /// Creates an <see cref="Oragon.Spring.Aop.Framework.IAopProxy"/> for the
        /// supplied <paramref name="advisedSupport"/> configuration.
        /// </summary>
        /// <param name="advisedSupport">The AOP configuration.</param>
        /// <returns>An <see cref="Oragon.Spring.Aop.Framework.IAopProxy"/>.</returns>
        /// <exception cref="AopConfigException">
        /// If the supplied <paramref name="advisedSupport"/> configuration is
        /// invalid.
        /// </exception>
        /// <seealso cref="Oragon.Spring.Aop.Framework.IAopProxyFactory.CreateAopProxy"/>
        public IAopProxy CreateAopProxy(AdvisedSupport advisedSupport)
        {
            if (advisedSupport == null)
            {
                throw new AopConfigException("Cannot create IAopProxy with null ProxyConfig");
            }
            if (advisedSupport.Advisors.Count == 0 && advisedSupport.TargetSource == EmptyTargetSource.Empty)
            {
                throw new AopConfigException("Cannot create IAopProxy with no advisors and no target source");
            }

            EliminateDuplicateAdvisors(advisedSupport);

            return(DoCreateAopProxyInstance(advisedSupport));
        }
        public void ReturnsThisWhenProxyIsIncompatible() 
        {
		    FooBar obj = new FooBar();

		    AdvisedSupport advised = new AdvisedSupport();
            advised.Target = obj;
            advised.Interfaces = new Type[] { typeof(IFoo) };

            IFoo proxy = (IFoo)CreateProxy(advised);

            Assert.AreSame(obj, proxy.GetBarThis(),
                "Target should be returned when return types are incompatible");
		    Assert.AreSame(proxy, proxy.GetFooThis(),
                "Proxy should be returned when return types are compatible");
	    }
        /// <summary>
        /// Creates an <see cref="Spring.Aop.Framework.IAopProxy"/> for the
        /// supplied <paramref name="advisedSupport"/> configuration.
        /// </summary>
        /// <param name="advisedSupport">The AOP configuration.</param>
        /// <returns>An <see cref="Spring.Aop.Framework.IAopProxy"/>.</returns>
        /// <exception cref="AopConfigException">
        /// If the supplied <paramref name="advisedSupport"/> configuration is
        /// invalid.
        /// </exception>
        /// <seealso cref="Spring.Aop.Framework.IAopProxyFactory.CreateAopProxy"/>
        public IAopProxy CreateAopProxy(AdvisedSupport advisedSupport)
        {
            if (advisedSupport == null)
            {
                throw new AopConfigException("Cannot create IAopProxy with null ProxyConfig");
            }
            if (advisedSupport.Advisors.Count == 0 && advisedSupport.TargetSource == EmptyTargetSource.Empty)
            {
                throw new AopConfigException("Cannot create IAopProxy with no advisors and no target source");
            }

            EliminateDuplicateAdvisors(advisedSupport);

            return DoCreateAopProxyInstance(advisedSupport);
        }
Example #22
0
        public void ProxyTargetTypeOnly()
        {
            AdvisedSupport advised = new AdvisedSupport();

            advised.Target = new InheritanceTestObject("ProxyTargetTypeOnly");

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

            Assert.IsTrue(proxy is InheritanceTestObject);
            InheritanceTestObject proxiedClass = proxy as InheritanceTestObject;

            Assert.AreNotEqual("ProxyTargetTypeOnly", proxiedClass.Name);
        }
Example #23
0
        public void CallBaseConstructor()
        {
            AdvisedSupport advised = new AdvisedSupport();

            advised.Target = new InheritanceTestObject();

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

            Assert.IsTrue(proxy is InheritanceTestObject);
            InheritanceTestObject proxiedClass = proxy as InheritanceTestObject;

            Assert.AreEqual("InheritanceTestObject", proxiedClass.Name);
        }
Example #24
0
        public void ProxyIsJustInterface()
        {
            TestObject target = new TestObject();

            target.Age = 32;

            AdvisedSupport advised = new AdvisedSupport();

            advised.Target     = target;
            advised.Interfaces = new Type[] { typeof(ITestObject) };

            object proxy = CreateProxy(advised);

            Assert.IsTrue(proxy is ITestObject);
            Assert.IsFalse(proxy is TestObject);
        }
Example #25
0
        public void ReturnsThisWhenProxyIsIncompatible()
        {
            FooBar obj = new FooBar();

            AdvisedSupport advised = new AdvisedSupport();

            advised.Target     = obj;
            advised.Interfaces = new Type[] { typeof(IFoo) };

            IFoo proxy = (IFoo)CreateProxy(advised);

            Assert.AreSame(obj, proxy.GetBarThis(),
                           "Target should be returned when return types are incompatible");
            Assert.AreSame(proxy, proxy.GetFooThis(),
                           "Proxy should be returned when return types are compatible");
        }
Example #26
0
        /// <summary>
        /// Invoked when advice is changed after a proxy is created.
        /// </summary>
        /// <param name="source">
        /// The relevant <see cref="Oragon.Spring.Aop.Framework.AdvisedSupport"/> source.
        /// </param>
        public void AdviceChanged(AdvisedSupport source)
        {
#if !NET_4_0
            cacheLock.EnterWriteLock();
            try
            {
#endif
            methodCache.Clear();
#if !NET_4_0
        }

        finally
        {
            cacheLock.ExitWriteLock();
        }
#endif
        }
Example #27
0
        public void InterceptProtectedMethod()
        {
            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport(new InheritanceTestObject());

            advised.AddAdvice(ni);

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

            Assert.IsTrue(proxy is InheritanceTestObject);
            InheritanceTestObject proxiedClass = proxy as InheritanceTestObject;

            proxiedClass.Todo();
            Assert.AreEqual(1, ni.Count);
        }
 /// <summary>
 /// Creates an actual proxy instance based on the supplied <paramref name="advisedSupport"/>
 /// </summary>
 /// <param name="advisedSupport"></param>
 /// <returns></returns>
 protected override IAopProxy DoCreateAopProxyInstance(AdvisedSupport advisedSupport)
 {
     if (advisedSupport.ProxyType == null)
     {
         IProxyTypeBuilder typeBuilder;
         if ((advisedSupport.ProxyTargetType) ||
             (advisedSupport.Interfaces.Count == 0))
         {
             typeBuilder = new DecoratorAopProxyTypeBuilder(advisedSupport);
         }
         else
         {
             typeBuilder = new CompositionAopProxyTypeBuilder(advisedSupport);
         }
         advisedSupport.ProxyType        = BuildProxyType(typeBuilder);
         advisedSupport.ProxyConstructor = advisedSupport.ProxyType.GetConstructor(new Type[] { typeof(IAdvised) });
     }
     return((IAopProxy)advisedSupport.ProxyConstructor.Invoke(new object[] { advisedSupport }));
 }
Example #29
0
        public void InterceptThisCalls()
        {
            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport(new InheritanceTestObject());

            advised.AddAdvice(ni);

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

            Assert.IsTrue(proxy is InheritanceTestObject);
            InheritanceTestObject proxiedClass = proxy as InheritanceTestObject;

            proxiedClass.IncrementTwice();
            Assert.AreEqual(2, ni.Count);
            Assert.AreEqual(2, proxiedClass.Value);
        }
Example #30
0
        // SPRNET-1429
        public void InterceptVirtualGenericMethodWithGenericParameter()
        {
            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();

            advised.Target = new AnotherTestObject();
            advised.AddAdvice(ni);

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

            Assert.IsTrue(proxy is AnotherTestObject);
            AnotherTestObject proxiedClass = proxy as AnotherTestObject;

            Assert.AreEqual("Hello", proxiedClass.AnotherGenericMethod <string>("Hello"));
            Assert.AreEqual(1, ni.Count);
        }
Example #31
0
        public void InterceptVirtualGenericMethod()
        {
            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();

            advised.Target = new AnotherTestObject();
            advised.AddAdvice(ni);

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

            Assert.IsTrue(proxy is AnotherTestObject);
            AnotherTestObject proxiedClass = proxy as AnotherTestObject;

            Assert.AreEqual(typeof(int), proxiedClass.GenericMethod <int>());
            Assert.AreEqual(1, ni.Count);
        }
 /// <summary>
 /// Creates an actual proxy instance based on the supplied <paramref name="advisedSupport"/>
 /// </summary>
 /// <param name="advisedSupport"></param>
 /// <returns></returns>
 protected override IAopProxy DoCreateAopProxyInstance(AdvisedSupport advisedSupport)
 {
     if (advisedSupport.ProxyType == null)
     {
         IProxyTypeBuilder typeBuilder;
         if ((advisedSupport.ProxyTargetType) ||
             (advisedSupport.Interfaces.Length == 0))
         {
             typeBuilder = new DecoratorAopProxyTypeBuilder(advisedSupport);
         }
         else
         {
             typeBuilder = new CompositionAopProxyTypeBuilder(advisedSupport);
         }
         advisedSupport.ProxyType = BuildProxyType(typeBuilder);
         advisedSupport.ProxyConstructor = advisedSupport.ProxyType.GetConstructor(new Type[] { typeof(IAdvised) });
     }
     return (IAopProxy)advisedSupport.ProxyConstructor.Invoke(new object[] { advisedSupport });
 }
Example #33
0
        //SPRNET-1168
        public void InterceptVirtualMethodAndAmbiguousMatches()
        {
            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();

            advised.Target = new AmbiguousMatchesTestObject();
            advised.AddAdvice(ni);

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

            Assert.IsTrue(proxy is AmbiguousMatchesTestObject);
            AmbiguousMatchesTestObject proxiedClass = proxy as AmbiguousMatchesTestObject;

            proxiedClass.DoIt(new TestObject());
            Assert.AreEqual(1, ni.Count);
        }
        public void DoesNotProxyTargetVirtualMethodReturnValueAttributesWithProxyTargetAttributesEqualsFalse()
        {
            MarkerClass target = new MarkerClass();

            AdvisedSupport advised = new AdvisedSupport(new Type[] { typeof(IMarkerInterface) });

            advised.Target = target;
            advised.ProxyTargetAttributes = false;
            IAopProxy aopProxy = CreateAopProxy(advised);

            object proxy = aopProxy.GetProxy();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to GetProxy() was null.");

            MethodInfo method = proxy.GetType().GetMethod("MarkerVirtualMethod");

            Assert.IsNotNull(method);

            object[] attrs = method.ReturnTypeCustomAttributes.GetCustomAttributes(false);
            Assert.IsNotNull(attrs);
            Assert.AreEqual(0, attrs.Length, "Should not have attribute applied to the method's return value.");
        }
Example #35
0
        public void DirectCall()
        {
            AdvisedSupport advised = new AdvisedSupport();

            advised.Target = new InheritanceTestObject();

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

            Assert.IsTrue(proxy is InheritanceTestObject);
            InheritanceTestObject proxiedClass = proxy as InheritanceTestObject;

            proxiedClass.Name = "DirectCall";
            Assert.AreEqual("DirectCall", proxiedClass.Name);

            Assert.IsTrue(proxy is IIncrementable);
            IIncrementable proxiedIntf = proxy as IIncrementable;

            proxiedIntf.Increment();
            Assert.AreEqual(1, proxiedIntf.Value);
        }
        public void ProxyTargetVirtualMethodParameterAttributes()
        {
            MarkerClass target = new MarkerClass();

            AdvisedSupport advised = new AdvisedSupport(new Type[] { typeof(IMarkerInterface) });

            advised.Target = target;
            IAopProxy aopProxy = CreateAopProxy(advised);

            object proxy = aopProxy.GetProxy();

            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to GetProxy() was null.");

            MethodInfo method = proxy.GetType().GetMethod("MarkerVirtualMethod");

            Assert.IsNotNull(method);

            object[] attrs = method.GetParameters()[1].GetCustomAttributes(false);
            Assert.IsNotNull(attrs, "Should have had 1 attribute applied to the method's parameter.");
            Assert.AreEqual(1, attrs.Length, "Should have had 1 attribute applied to the method's parameter.");
            Assert.AreEqual(typeof(MarkerAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the method's parameter.");
        }
        public void ProxyCanBeClassAndInterface()
        {
            TestObject target = new TestObject();

            target.Age = 32;
            mockTargetSource.SetTarget(target);
            AdvisedSupport advised = new AdvisedSupport();

            advised.TargetSource = mockTargetSource;
            IAopProxy aop = CreateAopProxy(advised);

            object proxy = aop.GetProxy();

            Assert.IsTrue(AopUtils.IsDecoratorAopProxy(proxy), "Should be a decorator-based proxy");
            Assert.IsTrue(proxy is ITestObject);
            Assert.IsTrue(proxy is TestObject);

            ITestObject itb = (ITestObject)proxy;

            Assert.AreEqual(32, itb.Age, "Incorrect age");
            TestObject tb = (TestObject)proxy;

            Assert.AreEqual(32, tb.Age, "Incorrect age");
        }
        public void InterceptFinalMethodThatBelongsToAnInterface()
        {
            TestObject target = new TestObject();

            target.Name = "Bruno";
            mockTargetSource.SetTarget(target);

            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport(mockTargetSource);

            advised.AddAdvice(ni);

            // Cast to the interface that method belongs to
            ITestObject proxy = CreateProxy(advised) as ITestObject;

            Assert.IsNotNull(proxy);

            Assert.AreEqual(target.Name, proxy.Name, "Incorrect name");
            proxy.Name = "Bruno Baia";
            Assert.AreEqual("Bruno Baia", proxy.Name, "Incorrect name");

            Assert.AreEqual(3, ni.Count);
        }
        public void InterceptVirtualMethod()
        {
            DoesNotImplementInterfaceTestObject target = new DoesNotImplementInterfaceTestObject();

            target.Name = "Bruno";
            mockTargetSource.SetTarget(target);

            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();

            advised.TargetSource = mockTargetSource;
            advised.AddAdvice(ni);

            DoesNotImplementInterfaceTestObject proxy = CreateProxy(advised) as DoesNotImplementInterfaceTestObject;

            Assert.IsNotNull(proxy);

            Assert.AreEqual(target.Name, proxy.Name, "Incorrect name");
            proxy.Name = "Bruno Baia";
            Assert.AreEqual("Bruno Baia", proxy.Name, "Incorrect name");

            Assert.AreEqual(3, ni.Count);
        }
        public void InterceptInheritedVirtualMethods()
        {
            DoesNotImplementInterfaceTestObject target = new DerivedDoesNotImplementInterfaceTestObject();
            target.Name = "Bruno";
            mockTargetSource.SetTarget(target);

            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();
            advised.TargetSource = mockTargetSource;
            advised.AddAdvice(ni);

            DoesNotImplementInterfaceTestObject proxy = CreateProxy(advised) as DoesNotImplementInterfaceTestObject;
            Assert.IsNotNull(proxy);

            // GetName() calls underlying protected "GetNameInternal()" which calls get_Name
            Assert.AreEqual(target.Name, proxy.GetName(), "Incorrect name");
            proxy.Name = "Bruno Baia";
            Assert.AreEqual("Bruno Baia", proxy.Name, "Incorrect name");

            Assert.AreEqual(3, ni.Count);
        }
 /// <summary>
 /// Actually creates the proxy instance based on the supplied <see cref="AdvisedSupport"/>.
 /// </summary>
 /// <returns>the proxy instance described by <paramref name="advisedSupport"/>. Must not be <c>null</c></returns>
 protected abstract IAopProxy DoCreateAopProxyInstance(AdvisedSupport advisedSupport);
        public void InterceptFinalMethodThatBelongsToAnInterface()
        {
            TestObject target = new TestObject();
            target.Name = "Bruno";
            mockTargetSource.SetTarget(target);

            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport(mockTargetSource);
            advised.AddAdvice(ni);

            // Cast to the interface that method belongs to
            ITestObject proxy = CreateProxy(advised) as ITestObject;
            Assert.IsNotNull(proxy);

            Assert.AreEqual(target.Name, proxy.Name, "Incorrect name");
            proxy.Name = "Bruno Baia";
            Assert.AreEqual("Bruno Baia", proxy.Name, "Incorrect name");

            Assert.AreEqual(3, ni.Count);
        }
        public void ProxyTargetVirtualMethodParameterAttributes()
        {
            MarkerClass target = new MarkerClass();

            AdvisedSupport advised = new AdvisedSupport(new Type[] { typeof(IMarkerInterface) });
            advised.Target = target;
            IAopProxy aopProxy = CreateAopProxy(advised);

            object proxy = aopProxy.GetProxy();
            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to GetProxy() was null.");

            MethodInfo method = proxy.GetType().GetMethod("MarkerVirtualMethod");
            Assert.IsNotNull(method);

            object[] attrs = method.GetParameters()[1].GetCustomAttributes(false);
            Assert.IsNotNull(attrs, "Should have had 1 attribute applied to the method's parameter.");
            Assert.AreEqual(1, attrs.Length, "Should have had 1 attribute applied to the method's parameter.");
            Assert.AreEqual(typeof(MarkerAttribute), attrs[0].GetType(), "Wrong System.Type of Attribute applied to the method's parameter.");
        }
 protected override Type CreateAopProxyType(AdvisedSupport advisedSupport)
 {
     return new CompositionAopProxyTypeBuilder(advisedSupport).BuildProxyType();
 }
        public void DoesNotProxyTargetVirtualMethodReturnValueAttributesWithProxyTargetAttributesEqualsFalse()
        {
            MarkerClass target = new MarkerClass();

            AdvisedSupport advised = new AdvisedSupport(new Type[] { typeof(IMarkerInterface) });
            advised.Target = target;
            advised.ProxyTargetAttributes = false;
            IAopProxy aopProxy = CreateAopProxy(advised);

            object proxy = aopProxy.GetProxy();
            Assert.IsNotNull(proxy, "The proxy generated by a (valid) call to GetProxy() was null.");

            MethodInfo method = proxy.GetType().GetMethod("MarkerVirtualMethod");
            Assert.IsNotNull(method);

            object[] attrs = method.ReturnTypeCustomAttributes.GetCustomAttributes(false);
            Assert.IsNotNull(attrs);
            Assert.AreEqual(0, attrs.Length, "Should not have attribute applied to the method's return value.");
        }
        public void InterceptVirtualMethod()
        {
            DoesNotImplementInterfaceTestObject target = new DoesNotImplementInterfaceTestObject();
            target.Name = "Bruno";
            mockTargetSource.SetTarget(target);

            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();
            advised.TargetSource = mockTargetSource;
            advised.AddAdvice(ni);

            DoesNotImplementInterfaceTestObject proxy = CreateProxy(advised) as DoesNotImplementInterfaceTestObject;
            Assert.IsNotNull(proxy);

            Assert.AreEqual(target.Name, proxy.Name, "Incorrect name");
            proxy.Name = "Bruno Baia";
            Assert.AreEqual("Bruno Baia", proxy.Name, "Incorrect name");

            Assert.AreEqual(3, ni.Count);
        }
 protected override Type CreateAopProxyType(AdvisedSupport advisedSupport)
 {
     return new DecoratorAopProxyTypeBuilder(advisedSupport).BuildProxyType(); 
 }
        public void ProxyCanBeClassAndInterface()
        {
            TestObject target = new TestObject();
            target.Age = 32;
            mockTargetSource.SetTarget(target);
            AdvisedSupport advised = new AdvisedSupport();
            advised.TargetSource = mockTargetSource;
            IAopProxy aop = CreateAopProxy(advised);

            object proxy = aop.GetProxy();
            Assert.IsTrue(AopUtils.IsDecoratorAopProxy(proxy), "Should be a decorator-based proxy");
            Assert.IsTrue(proxy is ITestObject);
            Assert.IsTrue(proxy is TestObject);

            ITestObject itb = (ITestObject)proxy;
            Assert.AreEqual(32, itb.Age, "Incorrect age");
            TestObject tb = (TestObject)proxy;
            Assert.AreEqual(32, tb.Age, "Incorrect age");
        }
        public void CannotInterceptFinalMethodThatDoesNotBelongToAnInterface()
        {
            DoesNotImplementInterfaceTestObject target = new DoesNotImplementInterfaceTestObject();
            target.Location = "Paris";
            mockTargetSource.SetTarget(target);

            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();
            advised.TargetSource = mockTargetSource;
            advised.AddAdvice(ni);

            DoesNotImplementInterfaceTestObject proxy = CreateProxy(advised) as DoesNotImplementInterfaceTestObject;
            Assert.IsNotNull(proxy);

            // Location is final and doesn't belong to an interface so can't proxy.
            // method call goes directly to the proxy 
            // and will not have access to the valid _location field
            Assert.IsNull(proxy.Location);

            Assert.AreEqual(0, ni.Count);
        }