Пример #1
0
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        public static void Main(string[] args)
        {
            try
            {
                // Create AOP proxy programmatically.
                ProxyFactory factory = new ProxyFactory(new ServiceCommand());
                factory.AddAdvice(new ConsoleLoggingBeforeAdvice());
                factory.AddAdvice(new ConsoleLoggingAfterAdvice());
                factory.AddAdvice(new ConsoleLoggingThrowsAdvice());
                ICommand command = (ICommand)factory.GetProxy();

                command.Execute();
                if (command.IsUndoCapable)
                {
                    command.UnExecute();
                }
            }
            catch (Exception ex)
            {
				Console.Out.WriteLine();
                Console.Out.WriteLine(ex);
            }
            finally
            {
				Console.Out.WriteLine();
                Console.Out.WriteLine("--- hit <return> to quit ---");
                Console.ReadLine();
            }
        }
Пример #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);
        }
Пример #3
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));
        }
Пример #4
0
 private static void Main(string[] args)
 {
     ProxyFactory factory = new ProxyFactory(new ServiceCommand());
     factory.AddAdvice(new ConsoleLoggingAroundAdvice());
     ICommand command = (ICommand)factory.GetProxy();
     command.Execute("This is the argument");
 }
Пример #5
0
        public void AddAndRemoveEventHandlerThroughInterceptor()
        {
            TestObject           target               = new TestObject();
            NopInterceptor       nopInterceptor       = new NopInterceptor();
            CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();

            target.Name = "SOME-NAME";
            ProxyFactory pf = new ProxyFactory(target);

            pf.AddAdvice(nopInterceptor);
            pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
            object      proxy = pf.GetProxy();
            ITestObject to    = proxy as ITestObject;

            // add event handler through proxy
            to.Click        += new EventHandler(OnClick);
            OnClickWasCalled = false;
            to.OnClick();
            Assert.IsTrue(OnClickWasCalled);
            Assert.AreEqual(2, countingBeforeAdvice.GetCalls());
            // remove event handler through proxy
            to.Click        -= new EventHandler(OnClick);
            OnClickWasCalled = false;
            to.OnClick();
            Assert.IsFalse(OnClickWasCalled);
            Assert.AreEqual(4, countingBeforeAdvice.GetCalls());
        }
Пример #6
0
        /// <summary>
        /// Creates a new proxy for the supplied <paramref name="proxyInterface"/>
        /// and <paramref name="interceptor"/>.
        /// </summary>
        /// <remarks>
        /// <p>
        /// This is a convenience method for creating a proxy for a single
        /// interceptor.
        /// </p>
        /// </remarks>
        /// <param name="proxyInterface">
        /// The interface that the proxy must implement.
        /// </param>
        /// <param name="interceptor">
        /// The interceptor that the proxy must invoke.
        /// </param>
        /// <returns>
        /// A new AOP proxy for the supplied <paramref name="proxyInterface"/>
        /// and <paramref name="interceptor"/>.
        /// </returns>
        public static object GetProxy(Type proxyInterface, IInterceptor interceptor)
        {
            ProxyFactory proxyFactory = new ProxyFactory();

            proxyFactory.AddInterface(proxyInterface);
            proxyFactory.AddAdvice(interceptor);
            return(proxyFactory.GetProxy());
        }
Пример #7
0
        public void InterceptorInclusionMethods()
        {
            NopInterceptor di       = new NopInterceptor();
            NopInterceptor diUnused = new NopInterceptor(1); // // make instance unique (see SPRNET-847)
            ProxyFactory   factory  = new ProxyFactory(new TestObject());

            factory.AddAdvice(0, di);
            ITestObject tb = (ITestObject)factory.GetProxy();

            Assert.IsTrue(factory.AdviceIncluded(di));
            Assert.IsTrue(!factory.AdviceIncluded(diUnused));
            Assert.IsTrue(factory.CountAdviceOfType(typeof(NopInterceptor)) == 1);

            factory.AddAdvice(0, diUnused);
            Assert.IsTrue(factory.AdviceIncluded(diUnused));
            Assert.IsTrue(factory.CountAdviceOfType(typeof(NopInterceptor)) == 2);
        }
Пример #8
0
        private ITestObject CreateProxy(object target, IAdvice interceptor, bool exposeProxy)
        {
            ProxyFactory pf = new ProxyFactory(target);

            pf.ExposeProxy = exposeProxy;
//            pf.Target = target;
            pf.AddAdvice(interceptor);

            return(pf.GetProxy() as ITestObject);
        }
Пример #9
0
        public void CanOnlyAddMethodInterceptors()
        {
            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddAdvice(0, new NopInterceptor());
            try
            {
                factory.AddAdvice(0, new AnonymousClassInterceptor());
                Assert.Fail("Should only be able to add MethodInterceptors");
            }
            catch (AopConfigException)
            {
            }

            // Check we can still use it
            IOther other = (IOther)factory.GetProxy();

            other.Absquatulate();
        }
Пример #10
0
 public static IXMLMailUtil GetEntity()
 {
     if (entity == null)
     {
         ProxyFactory factory = new ProxyFactory(new XMLMailUtil());
         factory.AddAdvice(new AroundAdvice());
         entity = (IXMLMailUtil)factory.GetProxy();
     }
     return entity;
 }
        protected override void AddPersistenceExceptionTranslation(ProxyFactory pf, IPersistenceExceptionTranslator pet)
        {
            if (AttributeUtils.FindAttribute(pf.TargetType, typeof(RepositoryAttribute)) != null)
            {
                DefaultListableObjectFactory of = new DefaultListableObjectFactory();
                of.RegisterObjectDefinition("peti", new RootObjectDefinition(typeof(PersistenceExceptionTranslationInterceptor)));
                of.RegisterSingleton("pet", pet);
                pf.AddAdvice((PersistenceExceptionTranslationInterceptor) of.GetObject("peti"));

            }
        }
 protected override object Advised(object target, IPlatformTransactionManager ptm,
                                   ITransactionAttributeSource tas)
 {
     TransactionInterceptor ti = new TransactionInterceptor();
     ti.TransactionManager = ptm;
     Assert.AreEqual(ptm, ti.TransactionManager);
     ti.TransactionAttributeSource = tas;
     Assert.AreEqual(tas, ti.TransactionAttributeSource);
     ProxyFactory pf = new ProxyFactory(target);
     pf.AddAdvice(0, ti);
     return pf.GetProxy();
 }
Пример #13
0
 static void Main(string[] args)
 {
     ProxyFactory factory = new ProxyFactory(new ServiceCommand());
     factory.AddAdvice(new ExceptionAdvice());
     ICommand cc = (ICommand)factory.GetProxy();
     cc.Execute();
     //cc.DoExecute();
     //var ctx = ContextRegistry.GetContext();
     //ICommand command = (ICommand)ctx.GetObject("myAfterAdvice");
     //command.Execute();
     //command.DoExecute();
     Console.ReadLine();
 }
Пример #14
0
        static void Main(string[] args)
        {
            //Person p=new Person();
              //  p.Pen=new Pen();
              //  p.Write();

            ProxyFactory proxy = new ProxyFactory(new Pen());
            //proxy.AddAdvice(new BeforeAdvice());
            proxy.AddAdvice(new AroundAdvice());
            IWrite p = proxy.GetProxy() as IWrite;
            p.Write();

            Console.ReadKey();
        }
Пример #15
0
        public void AdvisedSupportListenerMethodsAre_NOT_CalledIfProxyHasNotBeenCreated()
        {
            IDynamicMock            mock     = new DynamicMock(typeof(IAdvisedSupportListener));
            IAdvisedSupportListener listener = (IAdvisedSupportListener)mock.Object;

            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddListener(listener);

            // must not fire the AdviceChanged callback...
            factory.AddAdvice(new NopInterceptor());
            // must not fire the InterfacesChanged callback...
            factory.AddInterface(typeof(ISerializable));

            mock.Verify();
        }
        public void AdvisedSupportListenerMethodsAre_NOT_CalledIfProxyHasNotBeenCreated()
        {
            IAdvisedSupportListener listener = MockRepository.GenerateMock <IAdvisedSupportListener>();

            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddListener(listener);

            // must not fire the AdviceChanged callback...
            factory.AddAdvice(new NopInterceptor());
            // must not fire the InterfacesChanged callback...
            factory.AddInterface(typeof(ISerializable));

            listener.AssertWasNotCalled(x => x.AdviceChanged(Arg <AdvisedSupport> .Is.Anything));
            listener.AssertWasNotCalled(x => x.InterfacesChanged(Arg <AdvisedSupport> .Is.Anything));
        }
        public object GetServiceProxy(Assembly webServiceAssembly, string serviceName)
        {
            object proxy = webServiceAssembly.CreateInstance(serviceName);
            if (proxy == null)
                throw new Exception("Cannot create proxy instance");

            if (_modifier == null)
                return proxy;

            var factory = new ProxyFactory(proxy) { ProxyTargetType = true };
            factory.AddAdvice(new WebRequestModifyInterceptor(_modifier));
            var result = factory.GetProxy();

            SetUpMissingValues(proxy, result);
            return result;
        }
Пример #18
0
        public void AdvisedSupportListenerMethodsAre_NOT_CalledIfProxyHasNotBeenCreated()
        {
            IAdvisedSupportListener listener = A.Fake <IAdvisedSupportListener>();

            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddListener(listener);

            // must not fire the AdviceChanged callback...
            factory.AddAdvice(new NopInterceptor());
            // must not fire the InterfacesChanged callback...
            factory.AddInterface(typeof(ISerializable));

            A.CallTo(() => listener.AdviceChanged(A <AdvisedSupport> ._)).MustNotHaveHappened();
            A.CallTo(() => listener.InterfacesChanged(A <AdvisedSupport> ._)).MustNotHaveHappened();
        }
Пример #19
0
        public void CacheTest()
        {
            for (int i = 0; i < 2; i++)
            {
                TestObject           target               = new TestObject();
                NopInterceptor       nopInterceptor       = new NopInterceptor();
                CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
                ProxyFactory         pf = new ProxyFactory();
                pf.Target = target;
                pf.AddAdvice(nopInterceptor);
                pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
                object proxy = pf.GetProxy();
            }

            // fails when running in resharper/testdriven.net
            // DynamicProxyManager.SaveAssembly();
        }
Пример #20
0
        public void CreateProxyFactoryWithoutTargetThenSetTarget()
        {
            TestObject target = new TestObject();

            target.Name = "Adam";
            NopInterceptor       nopInterceptor       = new NopInterceptor();
            CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
            ProxyFactory         pf = new ProxyFactory();

            pf.Target = target;
            pf.AddAdvice(nopInterceptor);
            pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
            object      proxy = pf.GetProxy();
            ITestObject to    = (ITestObject)proxy;

            Assert.AreEqual("Adam", to.Name);
            Assert.AreEqual(1, countingBeforeAdvice.GetCalls());
        }
        public void AdvisedSupportListenerMethodsAreCalledAppropriately()
        {
            IAdvisedSupportListener listener = MockRepository.GenerateMock <IAdvisedSupportListener>();

            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddListener(listener);

            // must fire the Activated callback...
            factory.GetProxy();
            // must fire the AdviceChanged callback...
            factory.AddAdvice(new NopInterceptor());
            // must fire the InterfacesChanged callback...
            factory.AddInterface(typeof(ISerializable));

            listener.AssertWasCalled(x => x.Activated(Arg <AdvisedSupport> .Is.NotNull));
            listener.AssertWasCalled(x => x.AdviceChanged(Arg <AdvisedSupport> .Is.NotNull));
            listener.AssertWasCalled(x => x.InterfacesChanged(Arg <AdvisedSupport> .Is.NotNull));
        }
Пример #22
0
        public void AdvisedSupportListenerMethodsAreCalledAppropriately()
        {
            IAdvisedSupportListener listener = A.Fake <IAdvisedSupportListener>();

            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddListener(listener);

            // must fire the Activated callback...
            factory.GetProxy();
            // must fire the AdviceChanged callback...
            factory.AddAdvice(new NopInterceptor());
            // must fire the InterfacesChanged callback...
            factory.AddInterface(typeof(ISerializable));

            A.CallTo(() => listener.Activated(A <AdvisedSupport> .That.Not.IsNull())).MustHaveHappened();
            A.CallTo(() => listener.AdviceChanged(A <AdvisedSupport> .That.Not.IsNull())).MustHaveHappened();
            A.CallTo(() => listener.InterfacesChanged(A <AdvisedSupport> .That.Not.IsNull())).MustHaveHappened();
        }
        public void IntegrationTest()
        {
            ProxyFactory pf = new ProxyFactory(new TestTarget());

            ILog log = (ILog)mocks.CreateMock(typeof(ILog));
            SimpleLoggingAdvice loggingAdvice = new SimpleLoggingAdvice(log);
            pf.AddAdvice(loggingAdvice);

            Expect.Call(log.IsTraceEnabled).Return(true).Repeat.Any();
            log.Trace("Entering DoSomething");
            log.Trace("Exiting DoSomething");

            mocks.ReplayAll();

            object proxy = pf.GetProxy();
            ITestTarget ptt = (ITestTarget)proxy;
            ptt.DoSomething();

            mocks.VerifyAll();
        }
Пример #24
0
        public void AdvisedSupportListenerMethodsAreCalledAppropriately()
        {
            IDynamicMock            mock     = new DynamicMock(typeof(IAdvisedSupportListener));
            IAdvisedSupportListener listener = (IAdvisedSupportListener)mock.Object;

            mock.Expect("Activated");
            mock.Expect("AdviceChanged");
            mock.Expect("InterfacesChanged");

            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddListener(listener);

            // must fire the Activated callback...
            factory.GetProxy();
            // must fire the AdviceChanged callback...
            factory.AddAdvice(new NopInterceptor());
            // must fire the InterfacesChanged callback...
            factory.AddInterface(typeof(ISerializable));

            mock.Verify();
        }
Пример #25
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)));
        }
Пример #26
0
        public void NestedProxiesDontInvokeSameAdviceOrIntroductionTwice()
        {
            MultiProxyingTestClass testObj = new MultiProxyingTestClass();
            ProxyFactory           pf1     = new ProxyFactory();

            pf1.Target = testObj;

            NopInterceptor           di            = new NopInterceptor();
            NopInterceptor           diUnused      = new NopInterceptor(1); // // make instance unique (see SPRNET-847)
            TestCountingIntroduction countingMixin = new TestCountingIntroduction();

            pf1.AddAdvice(diUnused);
            pf1.AddAdvisor(new DefaultPointcutAdvisor(di));
            pf1.AddIntroduction(new DefaultIntroductionAdvisor(countingMixin));

            object       innerProxy = pf1.GetProxy();
            ProxyFactory pf2        = new ProxyFactory();

            pf2.Target = innerProxy;
            pf2.AddAdvice(diUnused);
            pf2.AddAdvisor(new DefaultPointcutAdvisor(di));
            pf2.AddIntroduction(new DefaultIntroductionAdvisor(countingMixin));

            object outerProxy = pf2.GetProxy();

            // any advice instance is invoked once only
            string result = ((IMultiProxyingTestInterface)outerProxy).TestMethod("arg");

            Assert.AreEqual(1, testObj.InvocationCounter);
            Assert.AreEqual("arg|arg", result);
            Assert.AreEqual(1, di.Count);

            // any introduction instance is invoked once only
            ((ICountingIntroduction)outerProxy).Inc();
            Assert.AreEqual(1, countingMixin.Counter);
        }
Пример #27
0
        public void CanOnlyAddMethodInterceptors()
        {
            ProxyFactory factory = new ProxyFactory(new TestObject());
            factory.AddAdvice(0, new NopInterceptor());
            try
            {
                factory.AddAdvice(0, new AnonymousClassInterceptor());
                Assert.Fail("Should only be able to add MethodInterceptors");
            }
            catch (AopConfigException)
            {
            }

            // Check we can still use it
            IOther other = (IOther)factory.GetProxy();
            other.Absquatulate();
        }
Пример #28
0
 public void CreateProxyFactoryWithoutTargetThenSetTarget()
 {
     TestObject target = new TestObject();
     target.Name = "Adam";
     NopInterceptor nopInterceptor = new NopInterceptor();
     CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
     ProxyFactory pf = new ProxyFactory();
     pf.Target = target;
     pf.AddAdvice(nopInterceptor);
     pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
     object proxy = pf.GetProxy();
     ITestObject to = (ITestObject)proxy;
     Assert.AreEqual("Adam", to.Name);
     Assert.AreEqual(1, countingBeforeAdvice.GetCalls());
 }
Пример #29
0
        public void AdvisedSupportListenerMethodsAre_NOT_CalledIfProxyHasNotBeenCreated()
        {
            IAdvisedSupportListener listener = MockRepository.GenerateMock<IAdvisedSupportListener>();

            ProxyFactory factory = new ProxyFactory(new TestObject());
            factory.AddListener(listener);

            // must not fire the AdviceChanged callback...
            factory.AddAdvice(new NopInterceptor());
            // must not fire the InterfacesChanged callback...
            factory.AddInterface(typeof(ISerializable));

            listener.AssertWasNotCalled(x => x.AdviceChanged(Arg<AdvisedSupport>.Is.Anything));
            listener.AssertWasNotCalled(x => x.InterfacesChanged(Arg<AdvisedSupport>.Is.Anything));
        }
Пример #30
0
        public void CacheTest()
        {
            for (int i = 0; i < 2; i++)
            {
                TestObject target = new TestObject();
                NopInterceptor nopInterceptor = new NopInterceptor();
                CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
                ProxyFactory pf = new ProxyFactory();
                pf.Target = target;
                pf.AddAdvice(nopInterceptor);
                pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
                object proxy = pf.GetProxy();
            }

            // fails when running in resharper/testdriven.net
            // DynamicProxyManager.SaveAssembly();
        }
Пример #31
0
        public void Main()
        {
            var host = new Host();

            var mp = new SimplePlugin();
            var pf = new ProxyFactory(mp);
            pf.AddAdvice(new DelegateToHostExceptionHandlingAdvice(host));

            var proxy = (IPlugin)pf.GetProxy();

            proxy.DoWork();
        }
Пример #32
0
        public void AdvisedSupportListenerMethodsAreCalledAppropriately()
        {
            IAdvisedSupportListener listener = MockRepository.GenerateMock<IAdvisedSupportListener>();

            ProxyFactory factory = new ProxyFactory(new TestObject());
            factory.AddListener(listener);

            // must fire the Activated callback...
            factory.GetProxy();
            // must fire the AdviceChanged callback...
            factory.AddAdvice(new NopInterceptor());
            // must fire the InterfacesChanged callback...
            factory.AddInterface(typeof(ISerializable));

            listener.AssertWasCalled(x => x.Activated(Arg<AdvisedSupport>.Is.NotNull));
            listener.AssertWasCalled(x => x.AdviceChanged(Arg<AdvisedSupport>.Is.NotNull));
            listener.AssertWasCalled(x => x.InterfacesChanged(Arg<AdvisedSupport>.Is.NotNull));
        }
        //public TypeConverter TypeConverter {
        //    set {
        //        AssertUtils.ArgumentNotNull(value, "typeConverter must not be null");
        //        _typeConverter = value;
        //    }
        //}
        //public void setBeanClassLoader(ClassLoader beanClassLoader) {
        //    this.beanClassLoader = beanClassLoader;
        //}
        protected override void OnInit()
        {
            lock(_initializationMonitor) {
                if(_initialized) {
                    return;
                }
                if(_serviceInterface == null) {
                    throw new ArgumentException("'serviceInterface' must not be null");
                }
                MethodInfo[] methods = _serviceInterface.GetMethods();
                foreach(MethodInfo method in methods) {
                    IMessagingGateway gateway = CreateGatewayForMethod(method);
                    _gatewayMap.Add(method, gateway);
                }

                ProxyFactory pf = new ProxyFactory(new[] {_serviceInterface});
                pf.AddAdvice(this);
                _serviceProxy = pf.GetProxy();
                Start();
                _initialized = true;
            }
        }
Пример #34
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);
        }
Пример #35
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));
 }
Пример #36
0
        public void AdvisedSupportListenerMethodsAre_NOT_CalledIfProxyHasNotBeenCreated()
        {
            IDynamicMock mock = new DynamicMock(typeof(IAdvisedSupportListener));
            IAdvisedSupportListener listener = (IAdvisedSupportListener)mock.Object;

            ProxyFactory factory = new ProxyFactory(new TestObject());
            factory.AddListener(listener);

            // must not fire the AdviceChanged callback...
            factory.AddAdvice(new NopInterceptor());
            // must not fire the InterfacesChanged callback...
            factory.AddInterface(typeof(ISerializable));

            mock.Verify();
        }
        private void EnhanceConfigurationClasses(IConfigurableListableObjectFactory objectFactory)
        {
            string[] objectNames = objectFactory.GetObjectDefinitionNames();

            foreach (string t in objectNames)
            {
                IObjectDefinition objDef = objectFactory.GetObjectDefinition(t);

                if (((AbstractObjectDefinition)objDef).HasObjectType)
                {
                    if (Attribute.GetCustomAttribute(objDef.ObjectType, typeof(ConfigurationAttribute)) != null)
                    {

                        ProxyFactory proxyFactory = new ProxyFactory();
                        proxyFactory.ProxyTargetAttributes = true;
                        proxyFactory.Interfaces = Type.EmptyTypes;
                        proxyFactory.TargetSource = new ObjectFactoryTargetSource(t, objectFactory);
                        SpringObjectMethodInterceptor methodInterceptor = new SpringObjectMethodInterceptor(objectFactory);
                        proxyFactory.AddAdvice(methodInterceptor);

                        //TODO check type of object isn't infrastructure type.

                        InheritanceAopProxyTypeBuilder iaptb = new InheritanceAopProxyTypeBuilder(proxyFactory);
                        //iaptb.ProxyDeclaredMembersOnly = true; // make configurable.
                        ((IConfigurableObjectDefinition)objDef).ObjectType = iaptb.BuildProxyType();

                        objDef.ConstructorArgumentValues.AddIndexedArgumentValue(objDef.ConstructorArgumentValues.ArgumentCount, proxyFactory);

                    }
                }
            }
        }
 private ITestObject CreateProxy()
 {
     exceptionHandlerAdvice.AfterPropertiesSet();
     ProxyFactory pf = new ProxyFactory(new TestObject());
     pf.AddAdvice(exceptionHandlerAdvice);
     return (ITestObject)pf.GetProxy();
 }
        public void LoggingTest()
        {
            LogExceptionHandler logHandler = new LogExceptionHandler();
            logHandler.LogName = "adviceHandler";
            string testText =
                @"'Hello World, exception message = ' + #e.Message + ', target method = ' + #method.Name";
            logHandler.SourceExceptionNames.Add("ArithmeticException");
            logHandler.ActionExpressionText = testText;

            exceptionHandlerAdvice.ExceptionHandlers.Add(logHandler);

            exceptionHandlerAdvice.AfterPropertiesSet();

            ProxyFactory pf = new ProxyFactory(new TestObject());
            pf.AddAdvice(exceptionHandlerAdvice);
            ITestObject to = (ITestObject)pf.GetProxy();

            try
            {
                to.Exceptional(new ArithmeticException());
                Assert.Fail("Should have thrown exception when only logging");
            }
            catch (ArithmeticException)
            {
                bool found = false;
                foreach (string message in loggerFactoryAdapter.LogMessages)
                {
                    if (message.IndexOf("Hello World") >= 0)
                    {
                        found = true;
                    }
                }
                Assert.IsTrue(found, "did not find logging output");
            }
        }
Пример #40
0
        public void InterceptorInclusionMethods()
        {
            NopInterceptor di = new NopInterceptor();
            NopInterceptor diUnused = new NopInterceptor(1); // // make instance unique (see SPRNET-847)
            ProxyFactory factory = new ProxyFactory(new TestObject());
            factory.AddAdvice(0, di);
            ITestObject tb = (ITestObject)factory.GetProxy();
            Assert.IsTrue(factory.AdviceIncluded(di));
            Assert.IsTrue(!factory.AdviceIncluded(diUnused));
            Assert.IsTrue(factory.CountAdviceOfType(typeof(NopInterceptor)) == 1);

            factory.AddAdvice(0, diUnused);
            Assert.IsTrue(factory.AdviceIncluded(diUnused));
            Assert.IsTrue(factory.CountAdviceOfType(typeof(NopInterceptor)) == 2);
        }
Пример #41
0
        private ITestObject CreateProxy(object target, IAdvice interceptor, bool exposeProxy)
        {
            ProxyFactory pf = new ProxyFactory(target);
            pf.ExposeProxy = exposeProxy;
//            pf.Target = target;
            pf.AddAdvice(interceptor);

            return pf.GetProxy() as ITestObject;
        }
Пример #42
0
        public void NestedProxiesDontInvokeSameAdviceOrIntroductionTwice()
        {
            MultiProxyingTestClass testObj = new MultiProxyingTestClass();
            ProxyFactory pf1 = new ProxyFactory();
            pf1.Target = testObj;

            NopInterceptor di = new NopInterceptor();
            NopInterceptor diUnused = new NopInterceptor(1); // // make instance unique (see SPRNET-847)
            TestCountingIntroduction countingMixin = new TestCountingIntroduction();

            pf1.AddAdvice(diUnused);
            pf1.AddAdvisor(new DefaultPointcutAdvisor(di));
            pf1.AddIntroduction(new DefaultIntroductionAdvisor(countingMixin));

            object innerProxy = pf1.GetProxy();
            ProxyFactory pf2 = new ProxyFactory();
            pf2.Target = innerProxy;
            pf2.AddAdvice(diUnused);
            pf2.AddAdvisor(new DefaultPointcutAdvisor(di));
            pf2.AddIntroduction(new DefaultIntroductionAdvisor(countingMixin));

            object outerProxy = pf2.GetProxy();

            // any advice instance is invoked once only
            string result = ((IMultiProxyingTestInterface)outerProxy).TestMethod("arg");
            Assert.AreEqual(1, testObj.InvocationCounter);
            Assert.AreEqual("arg|arg", result);
            Assert.AreEqual(1, di.Count);

            // any introduction instance is invoked once only
            ((ICountingIntroduction)outerProxy).Inc();
            Assert.AreEqual(1, countingMixin.Counter);
        }
Пример #43
0
 public void AddAndRemoveEventHandlerThroughInterceptor()
 {
     TestObject target = new TestObject();
     NopInterceptor nopInterceptor = new NopInterceptor();
     CountingBeforeAdvice countingBeforeAdvice = new CountingBeforeAdvice();
     target.Name = "SOME-NAME";
     ProxyFactory pf = new ProxyFactory(target);
     pf.AddAdvice(nopInterceptor);
     pf.AddAdvisor(new DefaultPointcutAdvisor(countingBeforeAdvice));
     object proxy = pf.GetProxy();
     ITestObject to = proxy as ITestObject;
     // add event handler through proxy
     to.Click += new EventHandler(OnClick);
     OnClickWasCalled = false;
     to.OnClick();
     Assert.IsTrue(OnClickWasCalled);
     Assert.AreEqual(2, countingBeforeAdvice.GetCalls());
     // remove event handler through proxy
     to.Click -= new EventHandler(OnClick);
     OnClickWasCalled = false;
     to.OnClick();
     Assert.IsFalse(OnClickWasCalled);
     Assert.AreEqual(4, countingBeforeAdvice.GetCalls());
 }
Пример #44
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)));
 }
        private Type GenerateProxyType(string objectName, IConfigurableListableObjectFactory objectFactory)
        {
            ProxyFactory proxyFactory = new ProxyFactory();
            proxyFactory.ProxyTargetAttributes = true;
            proxyFactory.Interfaces = Type.EmptyTypes;
            proxyFactory.TargetSource = new ObjectFactoryTargetSource(objectName, objectFactory);
            SpringObjectMethodInterceptor methodInterceptor = new SpringObjectMethodInterceptor(objectFactory);
            proxyFactory.AddAdvice(methodInterceptor);

            //TODO check type of object isn't infrastructure type.

            InheritanceAopProxyTypeBuilder iaptb = new InheritanceAopProxyTypeBuilder(proxyFactory);
            //iaptb.ProxyDeclaredMembersOnly = true; // make configurable.
            return iaptb.BuildProxyType();
        }
Пример #46
0
        /// <summary>
        /// Create a close-suppressing proxy for the given Hibernate Session.
        /// The proxy also prepares returned Query and Criteria objects.
        /// </summary>
        /// <param name="session">The session.</param>
        /// <returns>The session proxy.</returns>
        public virtual ISession CreateSessionProxy(ISession session)
        {
            //TODO can move to HibernateAccessor and make protected
            //     if issue reported with AOP+Multiple Threads resolve.
            //     have not been able to reproduce so added lock as a precaution.
            //     
            lock (syncRoot)
            {
                if (sessionProxyFactory == null)
                {
                    sessionProxyFactory = new ProxyFactory();
                    sessionProxyFactory.AddAdvice(new CloseSuppressingMethodInterceptor(this));
                }

                sessionProxyFactory.Target = session;

                return (ISession) sessionProxyFactory.GetProxy();
            }
        }
Пример #47
0
        public void AdvisedSupportListenerMethodsAreCalledAppropriately()
        {
            IDynamicMock mock = new DynamicMock(typeof(IAdvisedSupportListener));
            IAdvisedSupportListener listener = (IAdvisedSupportListener)mock.Object;

            mock.Expect("Activated");
            mock.Expect("AdviceChanged");
            mock.Expect("InterfacesChanged");

            ProxyFactory factory = new ProxyFactory(new TestObject());
            factory.AddListener(listener);

            // must fire the Activated callback...
            factory.GetProxy();
            // must fire the AdviceChanged callback...
            factory.AddAdvice(new NopInterceptor());
            // must fire the InterfacesChanged callback...
            factory.AddInterface(typeof(ISerializable));

            mock.Verify();
        }