Exemplo n.º 1
0
        public void ReplaceAdvisor()
        {
            TestObject           target   = new TestObject();
            ProxyFactory         pf       = new ProxyFactory(target);
            NopInterceptor       nop      = new NopInterceptor();
            CountingBeforeAdvice cba1     = new CountingBeforeAdvice();
            CountingBeforeAdvice cba2     = new CountingBeforeAdvice();
            IAdvisor             advisor1 = new DefaultPointcutAdvisor(cba1);
            IAdvisor             advisor2 = new DefaultPointcutAdvisor(cba2);

            pf.AddAdvisor(advisor1);
            pf.AddAdvice(nop);
            ITestObject proxied = (ITestObject)pf.GetProxy();
            // Use the type cast feature
            // Replace etc methods on advised should be same as on ProxyFactory
            IAdvised advised = (IAdvised)proxied;

            proxied.Age = 5;
            Assert.AreEqual(1, cba1.GetCalls());
            Assert.AreEqual(0, cba2.GetCalls());
            Assert.AreEqual(1, nop.Count);
            Assert.IsFalse(advised.ReplaceAdvisor(null, null));
            Assert.IsFalse(advised.ReplaceAdvisor(null, advisor2));
            Assert.IsFalse(advised.ReplaceAdvisor(advisor1, null));
            Assert.IsTrue(advised.ReplaceAdvisor(advisor1, advisor2));
            Assert.AreEqual(advisor2, pf.Advisors[0]);
            Assert.AreEqual(5, proxied.Age);
            Assert.AreEqual(1, cba1.GetCalls());
            Assert.AreEqual(2, nop.Count);
            Assert.AreEqual(1, cba2.GetCalls());
            Assert.IsFalse(pf.ReplaceAdvisor(new DefaultPointcutAdvisor(null), advisor1));
        }
Exemplo n.º 2
0
        public void RemoveAdvisorByIndex()
        {
            TestObject           target  = new TestObject();
            ProxyFactory         pf      = new ProxyFactory(target);
            NopInterceptor       nop     = new NopInterceptor();
            CountingBeforeAdvice cba     = new CountingBeforeAdvice();
            IAdvisor             advisor = new DefaultPointcutAdvisor(cba);

            pf.AddAdvice(nop);
            pf.AddAdvisor(advisor);
            NopInterceptor nop2 = new NopInterceptor(2); // make instance unique (see SPRNET-847)

            pf.AddAdvice(nop2);
            ITestObject proxied = (ITestObject)pf.GetProxy();

            proxied.Age = 5;
            Assert.AreEqual(1, cba.GetCalls());
            Assert.AreEqual(1, nop.Count);
            Assert.AreEqual(1, nop2.Count);
            // Removes counting before advisor
            pf.RemoveAdvisor(1);
            Assert.AreEqual(5, proxied.Age);
            Assert.AreEqual(1, cba.GetCalls());
            Assert.AreEqual(2, nop.Count);
            Assert.AreEqual(2, nop2.Count);
            // Removes Nop1
            pf.RemoveAdvisor(0);
            Assert.AreEqual(5, proxied.Age);
            Assert.AreEqual(1, cba.GetCalls());
            Assert.AreEqual(2, nop.Count);
            Assert.AreEqual(3, nop2.Count);

            // Check out of bounds
            try
            {
                pf.RemoveAdvisor(-1);
                Assert.Fail("Supposed to throw exception");
            }
            catch (AopConfigException)
            {
                // Ok
            }

            try
            {
                pf.RemoveAdvisor(2);
                Assert.Fail("Supposed to throw exception");
            }
            catch (AopConfigException)
            {
                // Ok
            }

            Assert.AreEqual(5, proxied.Age);
            Assert.AreEqual(4, nop2.Count);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public static T CreateAopProxy <T>(this T obj) where T : NotifiableObject
        {
            var factory = new ProxyFactory(obj)
            {
                ProxyTargetType = true
            };
            DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(new PropertyMethodMatchPointcut(), new PropertyInterceptor(obj));

            factory.AddAdvisor(advisor);
            obj.AopWapper = factory.GetProxy();
            return((T)obj.AopWapper);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Method run after all the properties have been set for this object.
        /// Responsible for actual proxy creation.
        /// </summary>
        public void AfterPropertiesSet()
        {
            _transactionInterceptor.AfterPropertiesSet();

            if (_target == null)
            {
                throw new ArgumentException("'target' is required.");
            }
            ProxyFactory proxyFactory = new ProxyFactory();

            if (_preInterceptors != null)
            {
                for (int i = 0; i < _preInterceptors.Length; i++)
                {
                    proxyFactory.AddAdvisor(_advisorAdapterRegistry.Wrap(_preInterceptors[i]));
                }
            }
            if (_pointcut != null)
            {
                IAdvisor advice = new DefaultPointcutAdvisor(_pointcut, _transactionInterceptor);
                proxyFactory.AddAdvisor(advice);
            }
            else
            {
                proxyFactory.AddAdvisor(new TransactionAttributeSourceAdvisor(_transactionInterceptor));
            }
            if (_postInterceptors != null)
            {
                for (int i = 0; i < _postInterceptors.Length; i++)
                {
                    proxyFactory.AddAdvisor(_advisorAdapterRegistry.Wrap(_postInterceptors[i]));
                }
            }
            proxyFactory.CopyFrom(this);
            proxyFactory.TargetSource = createTargetSource(_target);
            if (_proxyInterfaces != null)
            {
                proxyFactory.Interfaces = _proxyInterfaces;
            }
            else if (!ProxyTargetType)
            {
                if (_target is ITargetSource)
                {
                    throw new AopConfigException("Either 'ProxyInterfaces' or 'ProxyTargetType' is required " +
                                                 "when using an ITargetSource as 'target'");
                }
                proxyFactory.Interfaces = AopUtils.GetAllInterfaces(_target);
            }
            _proxy = proxyFactory.GetProxy();
        }
Exemplo n.º 6
0
 /// <summary>
 /// Gets the proxy by applying throw advices.
 ///
 /// </summary>
 /// <param name="target"></param>
 /// <returns></returns>
 public static object GetProxy(object target)
 {
     try
     {
         ProxyFactory           proxyFactory             = new ProxyFactory(target);
         DefaultPointcutAdvisor exceptionHandlingAdvisor =
             new DefaultPointcutAdvisor(aASAExceptionAdvice);
         proxyFactory.AddAdvisor(exceptionHandlingAdvisor);
         return(proxyFactory.GetProxy());
     }
     catch (Exception ex)
     {
         return(new ASAException("ASAExceptionAdvice failed  " + ex.Message));
     }
 }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the proxy by applying throw advices. The methods identified by the
        /// <code>methodRE</code> regular expression will be intercepted by the throw advice.
        ///
        /// </summary>
        /// <param name="target"></param>
        /// <param name="methodRE"></param>
        /// <returns></returns>
        public static object GetProxy(object target, string methodRE)
        {
            try
            {
                SdkRegularExpressionMethodPointcut reMethodPointcut = new SdkRegularExpressionMethodPointcut(methodRE);

                ProxyFactory           proxyFactory             = new ProxyFactory(target);
                DefaultPointcutAdvisor exceptionHandlingAdvisor =
                    new DefaultPointcutAdvisor(reMethodPointcut, aASAExceptionAdvice);
                proxyFactory.AddAdvisor(exceptionHandlingAdvisor);
                return(proxyFactory.GetProxy());
            }
            catch (Exception ex)
            {
                return(new ASAException("ASAExceptionAdvice failed  " + ex.Message));
            }
        }
Exemplo n.º 8
0
        public void IndexOfMethods()
        {
            TestObject     target  = new TestObject();
            ProxyFactory   pf      = new ProxyFactory(target);
            NopInterceptor nop     = new NopInterceptor();
            IAdvisor       advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
            IAdvised       advised = (IAdvised)pf.GetProxy();

            // Can use advised and ProxyFactory interchangeably
            advised.AddAdvice(nop);
            pf.AddAdvisor(advisor);
            Assert.AreEqual(-1, pf.IndexOf((IInterceptor)null));
            Assert.AreEqual(-1, pf.IndexOf(new NopInterceptor()));
            Assert.AreEqual(0, pf.IndexOf(nop));
            Assert.AreEqual(-1, advised.IndexOf((IAdvisor)null));
            Assert.AreEqual(1, pf.IndexOf(advisor));
            Assert.AreEqual(-1, advised.IndexOf(new DefaultPointcutAdvisor(null)));
        }
Exemplo n.º 9
0
        public void RemoveAdvisorByReference()
        {
            TestObject           target  = new TestObject();
            ProxyFactory         pf      = new ProxyFactory(target);
            NopInterceptor       nop     = new NopInterceptor();
            CountingBeforeAdvice cba     = new CountingBeforeAdvice();
            IAdvisor             advisor = new DefaultPointcutAdvisor(cba);

            pf.AddAdvice(nop);
            pf.AddAdvisor(advisor);
            ITestObject proxied = (ITestObject)pf.GetProxy();

            proxied.Age = 5;
            Assert.AreEqual(1, cba.GetCalls());
            Assert.AreEqual(1, nop.Count);
            Assert.IsFalse(pf.RemoveAdvisor(null));
            Assert.IsTrue(pf.RemoveAdvisor(advisor));
            Assert.AreEqual(5, proxied.Age);
            Assert.AreEqual(1, cba.GetCalls());
            Assert.AreEqual(2, nop.Count);
            Assert.IsFalse(pf.RemoveAdvisor(new DefaultPointcutAdvisor(null)));
        }