public void AdvisedSupportListenerMethodsAre_NOT_CalledIfProxyHasNotBeenCreated()
        {
            IAdvisedSupportListener listener = MockRepository.GenerateMock <IAdvisedSupportListener>();

            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddListener(listener);

            // must not fire the AdviceChanged callback...
            factory.AddAdvice(new NopInterceptor());
            // must not fire the InterfacesChanged callback...
            factory.AddInterface(typeof(ISerializable));

            listener.AssertWasNotCalled(x => x.AdviceChanged(Arg <AdvisedSupport> .Is.Anything));
            listener.AssertWasNotCalled(x => x.InterfacesChanged(Arg <AdvisedSupport> .Is.Anything));
        }
        public void RemoveAdvisedSupportListener()
        {
            IAdvisedSupportListener listener = MockRepository.GenerateMock <IAdvisedSupportListener>();

            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddListener(listener);
            factory.RemoveListener(listener);

            factory.GetProxy();

            // check that no lifecycle callback methods were invoked on the listener...
            listener.AssertWasNotCalled(x => x.Activated(Arg <AdvisedSupport> .Is.Anything));
            listener.AssertWasNotCalled(x => x.AdviceChanged(Arg <AdvisedSupport> .Is.Anything));
            listener.AssertWasNotCalled(x => x.InterfacesChanged(Arg <AdvisedSupport> .Is.Anything));
        }
        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);
        }
        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();
        }
        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)));
        }
        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());
        }
        public void AdvisedSupportListenerMethodsAreCalledAppropriately()
        {
            IAdvisedSupportListener listener = MockRepository.GenerateMock <IAdvisedSupportListener>();

            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddListener(listener);

            // must fire the Activated callback...
            factory.GetProxy();
            // must fire the AdviceChanged callback...
            factory.AddAdvice(new NopInterceptor());
            // must fire the InterfacesChanged callback...
            factory.AddInterface(typeof(ISerializable));

            listener.AssertWasCalled(x => x.Activated(Arg <AdvisedSupport> .Is.NotNull));
            listener.AssertWasCalled(x => x.AdviceChanged(Arg <AdvisedSupport> .Is.NotNull));
            listener.AssertWasCalled(x => x.InterfacesChanged(Arg <AdvisedSupport> .Is.NotNull));
        }
        public void CanOnlyAddMethodInterceptors()
        {
            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddAdvice(0, new NopInterceptor());
            try
            {
                factory.AddAdvice(0, new AnonymousClassInterceptor());
                Assert.Fail("Should only be able to add MethodInterceptors");
            }
            catch (AopConfigException)
            {
            }

            // Check we can still use it
            IOther other = (IOther)factory.GetProxy();

            other.Absquatulate();
        }
        public void AddAdvisedSupportListener()
        {
            //MLP SPRNET-1367
            //IDynamicMock mock = new DynamicMock(typeof(IAdvisedSupportListener));
            //IAdvisedSupportListener listener = (IAdvisedSupportListener)mock.Object;
            IAdvisedSupportListener listener =
                (IAdvisedSupportListener)mocks.CreateMock(typeof(IAdvisedSupportListener));

            listener.Activated(null);
            LastCall.On(listener).IgnoreArguments();
            //listener.Activated();
            //mock.Expect("Activated");

            mocks.ReplayAll();
            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.AddListener(listener);
            factory.GetProxy();
            mocks.VerifyAll();
        }
        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 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 RemoveNullAdvisedSupportListenerIsOk()
        {
            ProxyFactory factory = new ProxyFactory(new TestObject());

            factory.RemoveListener(null);
        }
        public void RemoveProxiedInterface()
        {
            ProxyFactory factory = new ProxyFactory(new TestObject());

            Assert.IsTrue(factory.RemoveInterface(typeof(ITestObject)));
        }
        public void TryRemoveNonProxiedInterface()
        {
            ProxyFactory factory = new ProxyFactory(new TestObject());

            Assert.IsFalse(factory.RemoveInterface(typeof(IServiceProvider)));
        }