Ejemplo n.º 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());
        }
Ejemplo n.º 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();
        }
Ejemplo n.º 3
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();
        }
		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 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();
		}
Ejemplo n.º 6
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 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);
		}
		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 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;
		}
Ejemplo n.º 10
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);
        }
Ejemplo n.º 11
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();
        }
Ejemplo n.º 12
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());
 }
        private IRequestHandler GetRequestHandler()
        {
            if (this.requestHandler == null)
            {
                ProxyFactory proxyFactory = new ProxyFactory(this);
                proxyFactory.AddInterface(typeof(IRequestHandler));
                proxyFactory.AddIntroduction(new DefaultIntroductionAdvisor(new HttpRequestHandler()));
                proxyFactory.ProxyTargetType = true;
                this.requestHandler = proxyFactory.GetProxy() as IRequestHandler;
            }

            return this.requestHandler;
        }
		public void testIntroductionInterceptorWithInterfaceHierarchy() 
		{
			TestObject raw = new TestObject();
			Assert.IsTrue(! (raw is ISubTimeStamped));
			ProxyFactory factory = new ProxyFactory(raw);

			IDynamicMock tsControl = new DynamicMock(typeof(ISubTimeStampedIntroduction));
			ISubTimeStampedIntroduction ts = (ISubTimeStampedIntroduction) tsControl.Object;
			tsControl.ExpectAndReturn("TimeStamp", 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);

			tsControl.Verify();
		}
        /// <summary>
        /// Create an AOP proxy for the given object.
        /// </summary>
        /// <param name="objectType">Type of the object.</param>
        /// <param name="objectName">The name of the object.</param>
        /// <returns>The AOP Proxy for the object.</returns>
        protected virtual ProxyFactory CreateProxyFactory(Type objectType, string objectName)
        {
            ProxyFactory proxyFactory = new ProxyFactory();
            proxyFactory.ProxyTargetAttributes = this.ProxyTargetAttributes;
            proxyFactory.TargetSource = new InheritanceBasedAopTargetSource(objectType);
            if (!ProxyInterfaces)
            {
                proxyFactory.Interfaces = Type.EmptyTypes;
            }

            IAdvisor[] advisors = ResolveInterceptorNames();
            foreach (IAdvisor advisor in advisors)
            {
                if (advisor is IIntroductionAdvisor)
                {
                    proxyFactory.AddIntroduction((IIntroductionAdvisor)advisor);
                }
                else
                {
                    proxyFactory.AddAdvisor(advisor);
                }
            }

            return proxyFactory;
        }