示例#1
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();
        }
        public void CanAddAndRemoveAspectInterfacesOnPrototype()
        {
            try
            {
                ITimeStamped ts = (ITimeStamped)factory.GetObject("test2");
                Assert.Fail("Shouldn't implement ITimeStamped before manipulation");
            }
            catch (InvalidCastException)
            {
            }

            IAdvised config = (IAdvised)factory.GetObject("&test2");
            long     time   = 666L;
            TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor();

            ti.TimeStamp = new DateTime(time);
            IIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ti, typeof(ITimeStamped));

            // add to front of introduction chain
            int oldCount = config.Introductions.Count;

            config.AddIntroduction(0, advisor);
            Assert.IsTrue(config.Introductions.Count == oldCount + 1);

            ITimeStamped ts2 = (ITimeStamped)factory.GetObject("test2");

            Assert.IsTrue(ts2.TimeStamp == new DateTime(time));

            // Can remove
            config.RemoveIntroduction(advisor);
            Assert.IsTrue(config.Introductions.Count == oldCount);

            // Existing reference will still work
            object o = ts2.TimeStamp;

            // But new proxies should not implement ITimeStamped
            try
            {
                ts2 = (ITimeStamped)factory.GetObject("test2");
                Assert.Fail("Should no longer implement ITimeStamped");
            }
            catch (InvalidCastException)
            {
            }

            // Now check non-effect of removing interceptor that isn't there
            ITestObject it = (ITestObject)factory.GetObject("test2");

            config = (IAdvised)it;

            oldCount = config.Advisors.Count;
            config.RemoveAdvice(new DebugAdvice());
            Assert.IsTrue(config.Advisors.Count == oldCount);

            DebugAdvice debugInterceptor = new DebugAdvice();

            config.AddAdvice(0, debugInterceptor);
            object foo = it.Spouse;

            Assert.AreEqual(1, debugInterceptor.Count);
            config.RemoveAdvice(debugInterceptor);
            foo = it.Spouse;
            // not invoked again
            Assert.IsTrue(debugInterceptor.Count == 1);
        }