示例#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
文件: Program.cs 项目: laball/demo
 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");
 }
示例#3
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();
 }
        protected override IInventorRepository CreateInventorStore()
        {
            Region region = CreateRegion();
            cache = new GemFireCache(region);

            context.ObjectFactory.RegisterSingleton("inventors", cache);

            ProxyFactory pf = new ProxyFactory(new InventorRepository());
            pf.AddAdvisors(cacheAspect);

            Repository = (IInventorRepository)pf.GetProxy();
            return Repository;
        }
示例#7
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();
 }
示例#8
0
        private static Contact GetProxy()
        {
            Contact target = new Contact();
            target.FirstName = "Aleksandar";
            target.LastName = "Seovic";

            ProxyFactory pf = new ProxyFactory(target);
            pf.AddAdvisor(new ModificationAdvisor(target.GetType()));
            pf.AddIntroduction(new IsModifiedAdvisor());
            pf.ProxyTargetType = true;

            return (Contact)pf.GetProxy();
        }
示例#9
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();
        }
		public void TestIntroductionInterceptorWithDelegation()
		{
			TestObject raw = new TestObject();
			Assert.IsTrue(! (raw is ITimeStamped));
			ProxyFactory factory = new ProxyFactory(raw);
	
			ITimeStampedIntroduction ts = MockRepository.GenerateMock<ITimeStampedIntroduction>();
			ts.Stub(x => x.TimeStamp).Return(EXPECTED_TIMESTAMP);

			DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ts);
			factory.AddIntroduction(advisor);

			ITimeStamped tsp = (ITimeStamped) factory.GetProxy();
			Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP);
		}
        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;
        }
        public void RemoveAdvisedSupportListener()
        {
            IAdvisedSupportListener listener = MockRepository.GenerateMock <IAdvisedSupportListener>();

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

            factory.AddListener(listener);
            factory.RemoveListener(listener);

            factory.GetProxy();

            // check that no lifecycle callback methods were invoked on the listener...
            listener.AssertWasNotCalled(x => x.Activated(Arg <AdvisedSupport> .Is.Anything));
            listener.AssertWasNotCalled(x => x.AdviceChanged(Arg <AdvisedSupport> .Is.Anything));
            listener.AssertWasNotCalled(x => x.InterfacesChanged(Arg <AdvisedSupport> .Is.Anything));
        }
        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));
        }
示例#14
0
        public void AddAdvisedSupportListener()
        {
            //MLP SPRNET-1367
            //IDynamicMock mock = new DynamicMock(typeof(IAdvisedSupportListener));
            //IAdvisedSupportListener listener = (IAdvisedSupportListener)mock.Object;
            IAdvisedSupportListener listener = A.Fake <IAdvisedSupportListener>();
            //listener.Activated();
            //mock.Expect("Activated");

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

            factory.AddListener(listener);
            factory.GetProxy();

            A.CallTo(() => listener.Activated(A <AdvisedSupport> ._)).MustHaveHappened();
        }
示例#15
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();
        }
示例#16
0
        public void RemoveAdvisedSupportListener()
        {
            IAdvisedSupportListener listener = A.Fake <IAdvisedSupportListener>();

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

            factory.AddListener(listener);
            factory.RemoveListener(listener);

            factory.GetProxy();

            // check that no lifecycle callback methods were invoked on the listener...
            A.CallTo(() => listener.Activated(null)).WithAnyArguments().MustNotHaveHappened();
            A.CallTo(() => listener.AdviceChanged(null)).WithAnyArguments().MustNotHaveHappened();
            A.CallTo(() => listener.InterfacesChanged(null)).WithAnyArguments().MustNotHaveHappened();
        }
        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();
        }
示例#18
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();
        }
示例#19
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);
        }
		public void TestIntroductionInterceptorWithInterfaceHierarchy() 
		{
			TestObject raw = new TestObject();
			Assert.IsTrue(! (raw is ISubTimeStamped));
			ProxyFactory factory = new ProxyFactory(raw);

            ISubTimeStampedIntroduction ts = MockRepository.GenerateMock<ISubTimeStampedIntroduction>();
			ts.Stub(x => x.TimeStamp).Return(EXPECTED_TIMESTAMP);

			DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ts);
			// we must add introduction, not an advisor
			factory.AddIntroduction(advisor);

			object proxy = factory.GetProxy();
			ISubTimeStamped tsp = (ISubTimeStamped) proxy;
			Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP);
		}
		public void testIntroductionInterceptorWithDelegation()
		{
			TestObject raw = new TestObject();
			Assert.IsTrue(! (raw is ITimeStamped));
			ProxyFactory factory = new ProxyFactory(raw);
	
			IDynamicMock tsControl = new DynamicMock(typeof(ITimeStampedIntroduction));
			ITimeStampedIntroduction ts = (ITimeStampedIntroduction) tsControl.Object;
			tsControl.ExpectAndReturn("TimeStamp", EXPECTED_TIMESTAMP);

			DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ts);
			factory.AddIntroduction(advisor);

			ITimeStamped tsp = (ITimeStamped) factory.GetProxy();
			Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP);
	
			tsControl.Verify();
		}
示例#22
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());
        }
示例#23
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)));
        }
示例#24
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 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));
        }
示例#26
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();
        }
        public void AddAdvisedSupportListener()
        {
            //MLP SPRNET-1367
            //IDynamicMock mock = new DynamicMock(typeof(IAdvisedSupportListener));
            //IAdvisedSupportListener listener = (IAdvisedSupportListener)mock.Object;
            IAdvisedSupportListener listener =
                (IAdvisedSupportListener)mocks.CreateMock(typeof(IAdvisedSupportListener));

            listener.Activated(null);
            LastCall.On(listener).IgnoreArguments();
            //listener.Activated();
            //mock.Expect("Activated");

            mocks.ReplayAll();
            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddListener(listener);
            factory.GetProxy();
            mocks.VerifyAll();
        }
        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();
        }
        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();
        }
示例#30
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)));
        }
		public void Matches()
		{
			SerializablePerson target = new SerializablePerson();
			target.SetAge(27);
			ControlFlowPointcut cflow = new ControlFlowPointcut(typeof(One), "GetAge");
			ProxyFactory factory = new ProxyFactory(target);
			NopInterceptor nop = new NopInterceptor();
			IPerson proxied = (IPerson) factory.GetProxy();
			factory.AddAdvisor(new DefaultPointcutAdvisor(cflow, nop));

			// not advised, not under One...
			Assert.AreEqual(target.GetAge(), proxied.GetAge());
			Assert.AreEqual(0, nop.Count, "Whoops, appear to be advising when not under One's cflow.");

			// will be advised...
			One one = new One();
			Assert.AreEqual(27, one.GetAge(proxied));
			Assert.AreEqual(1, nop.Count, "Not advising when under One's cflow (must be).");

			// won't be advised...
			Assert.AreEqual(target.GetAge(), new One().NoMatch(proxied));
			Assert.AreEqual(1, nop.Count, "Whoops, appear to be advising when under One's cflow scope, BUT NOT under a target method's cflow scope.");
			Assert.AreEqual(3, cflow.EvaluationCount, "Pointcut not invoked the correct number of times.");
		}
示例#32
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);
        }
示例#33
0
 public void TryRemoveNonProxiedInterface()
 {
     ProxyFactory factory = new ProxyFactory(new TestObject());
     Assert.IsFalse(factory.RemoveInterface(typeof(IServiceProvider)));
 }
示例#34
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));
 }
示例#35
0
 public void RemoveProxiedInterface()
 {
     ProxyFactory factory = new ProxyFactory(new TestObject());
     Assert.IsTrue(factory.RemoveInterface(typeof(ITestObject)));
 }
示例#36
0
 public void RemoveNullAdvisedSupportListenerIsOk()
 {
     ProxyFactory factory = new ProxyFactory(new TestObject());
     factory.RemoveListener(null);
 }
示例#37
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)));
 }
示例#38
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);
        }
示例#39
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);
        }
示例#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
        public void RemoveProxiedInterface()
        {
            ProxyFactory factory = new ProxyFactory(new TestObject());

            Assert.IsTrue(factory.RemoveInterface(typeof(ITestObject)));
        }
        /// <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();
        }
		public void TestAutomaticInterfaceRecognitionInDelegate() 
		{
			IIntroductionAdvisor ia = new DefaultIntroductionAdvisor(new Test(EXPECTED_TIMESTAMP));
		
			TestObject target = new TestObject();
			ProxyFactory pf = new ProxyFactory(target);
			pf.AddIntroduction(0, ia);

			ITimeStamped ts = (ITimeStamped) pf.GetProxy();
		
			Assert.IsTrue(ts.TimeStamp == EXPECTED_TIMESTAMP);
			((ITest) ts).Foo();
		
			int age = ((ITestObject) ts).Age;
		}
示例#44
0
        public void RemoveAdvisedSupportListener()
        {
            IAdvisedSupportListener listener = MockRepository.GenerateMock<IAdvisedSupportListener>();

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

            factory.GetProxy();

            // check that no lifecycle callback methods were invoked on the listener...
            listener.AssertWasNotCalled(x => x.Activated(Arg<AdvisedSupport>.Is.Anything));
            listener.AssertWasNotCalled(x => x.AdviceChanged(Arg<AdvisedSupport>.Is.Anything));
            listener.AssertWasNotCalled(x => x.InterfacesChanged(Arg<AdvisedSupport>.Is.Anything));
        }
示例#45
0
        public void TryRemoveNonProxiedInterface()
        {
            ProxyFactory factory = new ProxyFactory(new TestObject());

            Assert.IsFalse(factory.RemoveInterface(typeof(IServiceProvider)));
        }
示例#46
0
        public void RemoveNullAdvisedSupportListenerIsOk()
        {
            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.RemoveListener(null);
        }
		public void TestIntroductionInterceptorWithSuperInterface()  
		{
			TestObject raw = new TestObject();
			Assert.IsTrue(! (raw is ITimeStamped));
			ProxyFactory factory = new ProxyFactory(raw);

            ISubTimeStamped ts = MockRepository.GenerateMock<ISubTimeStampedIntroduction>();
			ts.Stub(x => x.TimeStamp).Return(EXPECTED_TIMESTAMP);

			factory.AddIntroduction(0, new DefaultIntroductionAdvisor(
				(ISubTimeStampedIntroduction)ts,
				typeof(ITimeStamped))
				);

			ITimeStamped tsp = (ITimeStamped) factory.GetProxy();
			Assert.IsTrue(!(tsp is ISubTimeStamped));
			Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP);
		}
示例#48
0
        public void GetsAllInterfaces()
        {
            // Extend to get new interface
            TestObjectSubclass raw = new TestObjectSubclass();
            ProxyFactory factory = new ProxyFactory(raw);
            Assert.AreEqual(8, factory.Interfaces.Length, "Found correct number of interfaces");
            //System.out.println("Proxied interfaces are " + StringUtils.arrayToDelimitedString(factory.getProxiedInterfaces(), ","));
            ITestObject tb = (ITestObject)factory.GetProxy();
            Assert.IsTrue(tb is IOther, "Picked up secondary interface");

            raw.Age = 25;
            Assert.IsTrue(tb.Age == raw.Age);

            DateTime t = new DateTime(2004, 8, 1);
            TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t);

            Console.WriteLine(StringUtils.ArrayToDelimitedString(factory.Interfaces, "/"));

            //factory.addAdvisor(0, new DefaultIntroductionAdvisor(ti, typeof(ITimeStamped)));
            factory.AddIntroduction(
                new DefaultIntroductionAdvisor(ti, typeof(ITimeStamped))
                );

            Console.WriteLine(StringUtils.ArrayToDelimitedString(factory.Interfaces, "/"));

            ITimeStamped ts = (ITimeStamped)factory.GetProxy();
            Assert.IsTrue(ts.TimeStamp == t);
            // Shouldn't fail;
            ((IOther)ts).Absquatulate();
        }
示例#49
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();
            }
        }
示例#50
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)));
 }