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 AddRepeatedInterface()
        {
            ITimeStamped tst = new AnonymousClassTimeStamped(this);
            ProxyFactory pf  = new ProxyFactory(tst);

            // We've already implicitly added this interface.
            // This call should be ignored without error
            pf.AddInterface(typeof(ITimeStamped));
            // All cool
            ITimeStamped ts = (ITimeStamped)pf.GetProxy();
        }
        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;
        }
        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);
        }
Exemple #5
0
        public void TestIntroductionInterceptorWithDelegation()
        {
            TestObject raw = new TestObject();

            Assert.IsTrue(!(raw is ITimeStamped));
            ProxyFactory factory = new ProxyFactory(raw);

            ITimeStampedIntroduction ts = A.Fake <ITimeStampedIntroduction>();

            A.CallTo(() => ts.TimeStamp).Returns(EXPECTED_TIMESTAMP);

            DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(ts);

            factory.AddIntroduction(advisor);

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

            Assert.IsTrue(tsp.TimeStamp == EXPECTED_TIMESTAMP);
        }
        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);
        }
Exemple #7
0
        public void TestIntroductionInterceptorWithSuperInterface()
        {
            TestObject raw = new TestObject();

            Assert.IsTrue(!(raw is ITimeStamped));
            ProxyFactory factory = new ProxyFactory(raw);

            ISubTimeStampedIntroduction ts = A.Fake <ISubTimeStampedIntroduction>();

            A.CallTo(() => ts.TimeStamp).Returns(EXPECTED_TIMESTAMP);

            factory.AddIntroduction(0, new DefaultIntroductionAdvisor(
                                        ts,
                                        typeof(ITimeStamped))
                                    );

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

            Assert.IsTrue(!(tsp is ISubTimeStamped));
            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();
        }
Exemple #9
0
        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);
        }