public void MultiplePatterns()
        {
            IObjectFactory iof     = new XmlObjectFactory(new ReadOnlyXmlTestResource("RegularExpressionSetterTests.xml", GetType()));
            IPerson        advised = (IPerson)iof.GetObject("SettersAndReturnsThisAdvised");

            // Interceptor behind regexp advisor
            NopInterceptor nop = (NopInterceptor)iof.GetObject("NopInterceptor");

            Assert.AreEqual(0, nop.Count);

            int newAge = 12;

            // Not advised
            advised.Exceptional(null);
            Assert.AreEqual(0, nop.Count);

            // This is proxied
            advised.ReturnsThis();
            Assert.AreEqual(1, nop.Count);

            // Only setter is advised
            advised.SetAge(newAge);
            Assert.AreEqual(2, nop.Count);

            Assert.AreEqual(newAge, advised.GetAge());
            Assert.AreEqual(2, nop.Count);
        }
        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);
        }
        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();
        }
Exemplo n.º 4
0
        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.Count;
            NopInterceptor di        = new NopInterceptor();

            pc1.AddAdvice(1, di);
            Assert.AreEqual(pc1.Advisors, pc2.Advisors);
            Assert.AreEqual(oldLength + 1, pc2.Advisors.Count, "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);
        }
Exemplo n.º 5
0
        public void ReplaceAdvisor()
        {
            TestObject           target   = new TestObject();
            ProxyFactory         pf       = new ProxyFactory(target);
            NopInterceptor       nop      = new NopInterceptor();
            CountingBeforeAdvice cba1     = new CountingBeforeAdvice();
            CountingBeforeAdvice cba2     = new CountingBeforeAdvice();
            IAdvisor             advisor1 = new DefaultPointcutAdvisor(cba1);
            IAdvisor             advisor2 = new DefaultPointcutAdvisor(cba2);

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

            proxied.Age = 5;
            Assert.AreEqual(1, cba1.GetCalls());
            Assert.AreEqual(0, cba2.GetCalls());
            Assert.AreEqual(1, nop.Count);
            Assert.IsFalse(advised.ReplaceAdvisor(null, null));
            Assert.IsFalse(advised.ReplaceAdvisor(null, advisor2));
            Assert.IsFalse(advised.ReplaceAdvisor(advisor1, null));
            Assert.IsTrue(advised.ReplaceAdvisor(advisor1, advisor2));
            Assert.AreEqual(advisor2, pf.Advisors[0]);
            Assert.AreEqual(5, proxied.Age);
            Assert.AreEqual(1, cba1.GetCalls());
            Assert.AreEqual(2, nop.Count);
            Assert.AreEqual(1, cba2.GetCalls());
            Assert.IsFalse(pf.ReplaceAdvisor(new DefaultPointcutAdvisor(null), advisor1));
        }
Exemplo n.º 6
0
        public void TargetAtEndOfInterceptorList()
        {
            GoodCommand    target = new GoodCommand();
            NopInterceptor advice = new NopInterceptor();

            IObjectFactory mock = A.Fake <IObjectFactory>();

            A.CallTo(() => mock.GetObject("advice")).Returns(advice);
            A.CallTo(() => mock.GetObject("singleton")).Returns(target);
            A.CallTo(() => mock.GetType("singleton")).Returns(typeof(GoodCommand));

            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);
        }
Exemplo n.º 7
0
        public void InterceptNonVirtualMethodThatBelongsToAnInterface()
        {
            NopInterceptor ni = new NopInterceptor();

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

            advised.AddAdvice(ni);

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

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

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

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

            proxiedInterface.Increment();
            Assert.AreEqual(1, proxiedInterface.Value);
            Assert.AreEqual(2, ni.Count);
        }
Exemplo n.º 8
0
        public void DoesNotInterceptFinalMethod()
        {
            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();

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

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

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

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

            proxiedClass.Reset();
            Assert.AreEqual(2, ni.Count);
            Assert.AreEqual("InheritanceTestObject", proxiedClass.Name);
            Assert.AreEqual(3, ni.Count);
        }
Exemplo n.º 9
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);
        }
        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);
        }
        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.");
        }
        public void Serialization()
        {
            IObjectFactory iof = new XmlObjectFactory(new ReadOnlyXmlTestResource("RegularExpressionSetterTests.xml", GetType()));
            IPerson        p   = (IPerson)iof.GetObject("SerializableSettersAdvised");
            // Interceptor behind regexp advisor
            NopInterceptor nop = (NopInterceptor)iof.GetObject("NopInterceptor");

            Assert.AreEqual(0, nop.Count);

            int newAge = 12;

            // Not advised
            Assert.AreEqual(0, p.GetAge());
            Assert.AreEqual(0, nop.Count);

            // This is proxied
            p.SetAge(newAge);
            Assert.AreEqual(1, nop.Count);
            p.SetAge(newAge);
            Assert.AreEqual(newAge, p.GetAge());
            // Only setter fired
            Assert.AreEqual(2, nop.Count);

            // Serialize and continue...
            p = (IPerson)SerializationTestUtils.SerializeAndDeserialize(p);
            Assert.AreEqual(newAge, p.GetAge());
            // Remembers count, but we need to get a new reference to nop...
            nop = (SerializableNopInterceptor)((IAdvised)p).Advisors[0].Advice;
            Assert.AreEqual(2, nop.Count);
            Assert.AreEqual("SerializableSettersAdvised", p.GetName());
            p.SetAge(newAge + 1);
            Assert.AreEqual(3, nop.Count);
            Assert.AreEqual(newAge + 1, p.GetAge());
        }
Exemplo n.º 14
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);
        }
        private void ProxyAssertions(ITestObject testObject, int nopInterceptorCount)
        {
            NopInterceptor nop = (NopInterceptor)ctx.GetObject("nopInterceptor");

            Assert.AreEqual(0, nop.Count);
            Assert.IsTrue(AopUtils.IsCompositionAopProxy(testObject), testObject + " is not an AOP Proxy");
            int age = 5;

            testObject.Age = age;
            Assert.AreEqual(age, testObject.Age);
            Assert.AreEqual(2 * nopInterceptorCount, nop.Count);
        }
Exemplo n.º 16
0
        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);
        }
Exemplo n.º 17
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();
        }
Exemplo n.º 18
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);
        }
        private void DecoratorProxyAssertions(ITestObject testObject)
        {
            CountingBeforeAdvice cba = (CountingBeforeAdvice)ctx.GetObject("countingBeforeAdvice");
            NopInterceptor       nop = (NopInterceptor)ctx.GetObject("nopInterceptor");

            Assert.AreEqual(0, cba.GetCalls());
            Assert.AreEqual(0, nop.Count);
            Assert.IsTrue(AopUtils.IsDecoratorAopProxy(testObject), testObject + " is not an AOP Proxy");
            //extra advice calls are due to test IsDecoratorAopProxy and call to .GetType in impl
            Assert.AreEqual(1, nop.Count);
            Assert.AreEqual(1, cba.GetCalls());
            int age = 5;

            testObject.Age = age;
            Assert.AreEqual(age, testObject.Age);
            Assert.AreEqual(3, nop.Count);
            Assert.AreEqual(3, cba.GetCalls());
        }
Exemplo n.º 20
0
        public void InterceptProtectedMethod()
        {
            NopInterceptor ni = new NopInterceptor();

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

            advised.AddAdvice(ni);

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

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

            proxiedClass.Todo();
            Assert.AreEqual(1, ni.Count);
        }
Exemplo n.º 21
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());
        }
Exemplo n.º 22
0
        public void IndexOfMethods()
        {
            TestObject     target  = new TestObject();
            ProxyFactory   pf      = new ProxyFactory(target);
            NopInterceptor nop     = new NopInterceptor();
            IAdvisor       advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice());
            IAdvised       advised = (IAdvised)pf.GetProxy();

            // Can use advised and ProxyFactory interchangeably
            advised.AddAdvice(nop);
            pf.AddAdvisor(advisor);
            Assert.AreEqual(-1, pf.IndexOf((IInterceptor)null));
            Assert.AreEqual(-1, pf.IndexOf(new NopInterceptor()));
            Assert.AreEqual(0, pf.IndexOf(nop));
            Assert.AreEqual(-1, advised.IndexOf((IAdvisor)null));
            Assert.AreEqual(1, pf.IndexOf(advisor));
            Assert.AreEqual(-1, advised.IndexOf(new DefaultPointcutAdvisor(null)));
        }
Exemplo n.º 23
0
        //SPRNET-1168
        public void InterceptVirtualMethodAndAmbiguousMatches()
        {
            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();

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

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

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

            proxiedClass.DoIt(new TestObject());
            Assert.AreEqual(1, ni.Count);
        }
Exemplo n.º 24
0
        public void InterceptThisCalls()
        {
            NopInterceptor ni = new NopInterceptor();

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

            advised.AddAdvice(ni);

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

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

            proxiedClass.IncrementTwice();
            Assert.AreEqual(2, ni.Count);
            Assert.AreEqual(2, proxiedClass.Value);
        }
Exemplo n.º 25
0
        // SPRNET-1429
        public void InterceptVirtualGenericMethodWithGenericParameter()
        {
            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();

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

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

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

            Assert.AreEqual("Hello", proxiedClass.AnotherGenericMethod <string>("Hello"));
            Assert.AreEqual(1, ni.Count);
        }
Exemplo n.º 26
0
        public void InterceptVirtualGenericMethod()
        {
            NopInterceptor ni = new NopInterceptor();

            AdvisedSupport advised = new AdvisedSupport();

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

            object proxy = CreateProxy(advised);

            //DynamicProxyManager.SaveAssembly();

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

            Assert.AreEqual(typeof(int), proxiedClass.GenericMethod <int>());
            Assert.AreEqual(1, ni.Count);
        }
        public void Introduction()
        {
            object obj = ctx.GetObject("introductionUsingDecorator");

            Assert.IsNotNull(obj as IIsModified);
            ITestObject    testObject = (ITestObject)obj;
            NopInterceptor nop        = (NopInterceptor)ctx.GetObject("introductionNopInterceptor");

            Assert.AreEqual(0, nop.Count);
            Assert.IsTrue(AopUtils.IsCompositionAopProxy(testObject), testObject + " is not an Composition AOP Proxy");
            int age = 5;

            testObject.Age = age;
            Assert.AreEqual(age, testObject.Age);
            Assert.IsNotNull(testObject as IIsModified);
            Assert.IsTrue(((IIsModified)testObject).IsModified);
            Assert.AreEqual(3, nop.Count);
            Assert.AreEqual("introductionUsingDecorator", testObject.Name);
        }
Exemplo n.º 28
0
        public void MakeSurePrototypeTargetIsNotNeedlesslyCreatedDuringInitialization_Unit()
        {
            GoodCommand    target = new GoodCommand();
            NopInterceptor advice = new NopInterceptor();

            IObjectFactory factory = A.Fake <IObjectFactory>();

            ProxyFactoryObject fac = new ProxyFactoryObject();

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

            A.CallTo(() => factory.IsSingleton("advice")).Returns(true);
            A.CallTo(() => factory.GetObject("advice")).Returns(advice);
            A.CallTo(() => factory.GetType("prototype")).Returns(target.GetType());
            A.CallTo(() => factory.GetObject("prototype")).Returns(target);

            fac.GetObject();
        }
Exemplo n.º 29
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 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);
        }