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.");
		}
Esempio n. 2
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);
        }
Esempio n. 3
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)));
 }
Esempio n. 4
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());
 }
Esempio n. 5
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();
        }
Esempio n. 6
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());
 }
        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);
        }
Esempio n. 8
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 TargetAtEndOfInterceptorList()
        {
            GoodCommand target = new GoodCommand();
            NopInterceptor advice = new NopInterceptor();

            IObjectFactory mock = (IObjectFactory) mocks.CreateMock(typeof(IObjectFactory));
            Expect.Call(mock.GetObject("advice")).Return(advice);
            Expect.Call(mock.GetObject("singleton")).Return(target);
            Expect.Call(mock.GetType("singleton")).Return(typeof(GoodCommand));
            mocks.ReplayAll();

            ProxyFactoryObject fac = new ProxyFactoryObject();
            fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName };
            fac.IsSingleton = true; // default, just being explicit...
            fac.InterceptorNames = new string[] { "advice", "singleton" };
            fac.ObjectFactory = mock;

            ICommand one = (ICommand)fac.GetObject();
            ICommand two = (ICommand)fac.GetObject();
            Assert.IsTrue(ReferenceEquals(one, two));
            one.Execute();
            Assert.AreEqual(1, advice.Count);
            two.Execute();
            Assert.AreEqual(2, advice.Count);

            mocks.VerifyAll();
        }
        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 SingletonInstancesAreEqual()
 {
     ITestObject test1 = (ITestObject)factory.GetObject("test1");
     ITestObject test1_1 = (ITestObject)factory.GetObject("test1");
     Assert.AreEqual(test1, test1_1, "Singleton instances ==");
     test1.Age = 25;
     Assert.AreEqual(test1.Age, test1_1.Age);
     test1.Age = 250;
     Assert.AreEqual(test1.Age, test1_1.Age);
     IAdvised pc1 = (IAdvised)test1;
     IAdvised pc2 = (IAdvised)test1_1;
     Assert.AreEqual(pc1.Advisors, pc2.Advisors);
     int oldLength = pc1.Advisors.Length;
     NopInterceptor di = new NopInterceptor();
     pc1.AddAdvice(1, di);
     Assert.AreEqual(pc1.Advisors, pc2.Advisors);
     Assert.AreEqual(oldLength + 1, pc2.Advisors.Length, "Now have one more advisor");
     Assert.AreEqual(di.Count, 0);
     test1.Age = (5);
     Assert.AreEqual(test1_1.Age, test1.Age);
     Assert.AreEqual(3, di.Count);
 }
        public void MakeSurePrototypeTargetIsNotNeedlesslyCreatedDuringInitialization_Unit()
        {
            GoodCommand target = new GoodCommand();
            NopInterceptor advice = new NopInterceptor();

            MockRepository mocks = new MockRepository();
            IObjectFactory factory = (IObjectFactory) mocks.CreateMock(typeof(IObjectFactory));

            ProxyFactoryObject fac = new ProxyFactoryObject();
            fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName };
            fac.IsSingleton = false;
            fac.InterceptorNames = new string[] { "advice", "prototype" };
            fac.ObjectFactory = factory;

            //            using (mocks.Record())
            {
                using (mocks.Unordered())
                {
                    Expect.Call(factory.IsSingleton("advice")).Return(true);
                    Expect.Call(factory.GetObject("advice")).Return(advice);
                    Expect.Call(factory.GetType("prototype")).Return(target.GetType());
                    Expect.Call(factory.GetObject("prototype")).Return(target);
                }
            }
            mocks.ReplayAll();

            //            using(mocks.Playback())
            {
                fac.GetObject();
            }
            mocks.VerifyAll();
        }
 public void CanAddAndRemoveAspectInterfacesOnSingletonByCasting()
 {
     ITestObject it = (ITestObject)factory.GetObject("test1");
     IAdvised pc = (IAdvised)it;
     object name = it.Age;
     NopInterceptor di = new NopInterceptor();
     pc.AddAdvice(0, di);
     Assert.AreEqual(0, di.Count);
     it.Age = 25;
     Assert.AreEqual(25, it.Age);
     Assert.AreEqual(2, di.Count);
 }
Esempio n. 14
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);
        }
        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);
        }
Esempio n. 16
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);
        }
        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);
        }
Esempio n. 18
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));
 }
		public void SelectiveApplication()
		{
			SerializablePerson target = new SerializablePerson();
			target.SetAge(27);
			NopInterceptor nop = new NopInterceptor();
			ControlFlowPointcut cflow = new ControlFlowPointcut(typeof (One));
			IPointcut settersUnderOne = Pointcuts.Intersection(SetterPointcut.Instance, cflow);
			ProxyFactory pf = new ProxyFactory(target);
			IPerson proxied = (IPerson) pf.GetProxy();
			pf.AddAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop));

			// Not advised, not under One
			target.SetAge(16);
			Assert.AreEqual(0, nop.Count);

			// Not advised; under One but not a setter
			Assert.AreEqual(16, new One().GetAge(proxied));
			Assert.AreEqual(0, nop.Count);

			// Won't be advised
			new One().Set(proxied);
			Assert.AreEqual(1, nop.Count);

			// We saved most evaluations
			Assert.AreEqual(1, cflow.EvaluationCount);
		}