Пример #1
0
        public void AddAndRemoveEventHandlerThroughIntroduction()
        {
            TestObject target = new TestObject();
            DoubleClickableIntroduction dci     = new DoubleClickableIntroduction();
            DefaultIntroductionAdvisor  advisor = new DefaultIntroductionAdvisor(dci);
            CountingBeforeAdvice        countingBeforeAdvice = new CountingBeforeAdvice();

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

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

            Assert.IsNotNull(to);
            Assert.AreEqual("SOME-NAME", to.Name);
            IDoubleClickable doubleClickable = proxy as IDoubleClickable;

            // add event handler through introduction
            doubleClickable.DoubleClick += new EventHandler(OnClick);
            OnClickWasCalled             = false;
            doubleClickable.FireDoubleClickEvent();
            Assert.IsTrue(OnClickWasCalled);
            Assert.AreEqual(3, countingBeforeAdvice.GetCalls());
            // remove event handler through introduction
            doubleClickable.DoubleClick -= new EventHandler(OnClick);
            OnClickWasCalled             = false;
            doubleClickable.FireDoubleClickEvent();
            Assert.IsFalse(OnClickWasCalled);
            Assert.AreEqual(5, countingBeforeAdvice.GetCalls());
        }
Пример #2
0
        public void GetsAllInterfaces()
        {
            // Extend to get new interface
            TestObjectSubclass raw     = new TestObjectSubclass();
            ProxyFactory       factory = new ProxyFactory(raw);

            Assert.AreEqual(8, factory.Interfaces.Count, "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.CollectionToDelimitedString(factory.Interfaces, "/"));

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

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

            ITimeStamped ts = (ITimeStamped)factory.GetProxy();

            Assert.IsTrue(ts.TimeStamp == t);
            // Shouldn't fail;
            ((IOther)ts).Absquatulate();
        }
Пример #3
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);
        }