public void MakeSurePrototypeTargetIsNotNeedlesslyCreatedDuringInitialization_Unit()
        {
            GoodCommand    target = new GoodCommand();
            NopInterceptor advice = new NopInterceptor();

            MockRepository mocks   = new MockRepository();
            IObjectFactory factory = (IObjectFactory)mocks.CreateMock(typeof(IObjectFactory));

            ProxyFactoryObject fac = new ProxyFactoryObject();

            fac.ProxyInterfaces  = new string[] { typeof(ICommand).FullName };
            fac.IsSingleton      = false;
            fac.InterceptorNames = new string[] { "advice", "prototype" };
            fac.ObjectFactory    = factory;

//            using (mocks.Record())
            {
                using (mocks.Unordered())
                {
                    Expect.Call(factory.IsSingleton("advice")).Return(true);
                    Expect.Call(factory.GetObject("advice")).Return(advice);
                    Expect.Call(factory.GetType("prototype")).Return(target.GetType());
                    Expect.Call(factory.GetObject("prototype")).Return(target);
                }
            }
            mocks.ReplayAll();

//            using(mocks.Playback())
            {
                fac.GetObject();
            }
            mocks.VerifyAll();
        }
Exemplo n.º 2
0
        public void TargetAtEndOfInterceptorList()
        {
            GoodCommand    target = new GoodCommand();
            NopInterceptor advice = new NopInterceptor();

            IObjectFactory mock = A.Fake <IObjectFactory>();

            A.CallTo(() => mock.GetObject("advice")).Returns(advice);
            A.CallTo(() => mock.GetObject("singleton")).Returns(target);
            A.CallTo(() => mock.GetType("singleton")).Returns(typeof(GoodCommand));

            ProxyFactoryObject fac = new ProxyFactoryObject();

            fac.ProxyInterfaces  = new string[] { typeof(ICommand).FullName };
            fac.IsSingleton      = true; // default, just being explicit...
            fac.InterceptorNames = new string[] { "advice", "singleton" };
            fac.ObjectFactory    = mock;

            ICommand one = (ICommand)fac.GetObject();
            ICommand two = (ICommand)fac.GetObject();

            Assert.IsTrue(ReferenceEquals(one, two));
            one.Execute();
            Assert.AreEqual(1, advice.Count);
            two.Execute();
            Assert.AreEqual(2, advice.Count);
        }
Exemplo n.º 3
0
        public void ReplaceAdvisorWhenConfigIsFrozen()
        {
            ProxyFactoryObject fac = CreateFrozenProxyFactory();

            fac.IsFrozen = true;
            Assert.Throws <AopConfigException>(() => fac.ReplaceAdvisor(new PointcutForVoid(), new PointcutForVoid()));
        }
Exemplo n.º 4
0
        public void SingletonProxyWithPrototypeTargetCreatesTargetOnlyOnce()
        {
            try
            {
                RootObjectDefinition advice = new RootObjectDefinition(typeof(NopInterceptor));
                // prototype target...
                RootObjectDefinition target = new RootObjectDefinition(typeof(InstantiationCountingCommand), false);

                DefaultListableObjectFactory ctx = new DefaultListableObjectFactory();
                ctx.RegisterObjectDefinition("advice", advice);
                ctx.RegisterObjectDefinition("prototype", target);

                ProxyFactoryObject fac = new ProxyFactoryObject();
                fac.ProxyInterfaces  = new string[] { typeof(ICommand).FullName };
                fac.IsSingleton      = true;
                fac.InterceptorNames = new string[] { "advice", "prototype" };
                fac.ObjectFactory    = ctx;

                Assert.AreEqual(0, InstantiationCountingCommand.NumberOfInstantiations, "First Call");
                fac.GetObject();
                Assert.AreEqual(1, InstantiationCountingCommand.NumberOfInstantiations, "Second Call");
                fac.GetObject();
                Assert.AreEqual(1, InstantiationCountingCommand.NumberOfInstantiations, "Third Call");
            }

            finally
            {
                InstantiationCountingCommand.NumberOfInstantiations = 0;
            }
        }
Exemplo n.º 5
0
        public void MakeSurePrototypeTargetIsNotNeedlesslyCreatedDuringInitialization_Integration()
        {
            try
            {
                RootObjectDefinition advice = new RootObjectDefinition(typeof(NopInterceptor));
                // prototype target...
                RootObjectDefinition target = new RootObjectDefinition(typeof(InstantiationCountingCommand), false);

                DefaultListableObjectFactory ctx = new DefaultListableObjectFactory();
                ctx.RegisterObjectDefinition("advice", advice);
                ctx.RegisterObjectDefinition("prototype", target);

                ProxyFactoryObject fac = new ProxyFactoryObject();
                fac.ProxyInterfaces  = new string[] { typeof(ICommand).FullName };
                fac.IsSingleton      = false;
                fac.InterceptorNames = new string[] { "advice", "prototype" };
                fac.ObjectFactory    = ctx;

                Assert.AreEqual(0, InstantiationCountingCommand.NumberOfInstantiations,
                                "Prototype target instance is being (needlessly) created during PFO initialization.");
                fac.GetObject();
                Assert.AreEqual(1, InstantiationCountingCommand.NumberOfInstantiations, "Expected 1 inst");
                fac.GetObject();
                Assert.AreEqual(2, InstantiationCountingCommand.NumberOfInstantiations);
            }
            finally
            {
                InstantiationCountingCommand.NumberOfInstantiations = 0;
            }
        }
Exemplo n.º 6
0
        public void SupportsTransparentProxyAsTarget()
        {
            AppDomain domain = null;

            try
            {
                AppDomainSetup setup = new AppDomainSetup();
                setup.ApplicationBase = Environment.CurrentDirectory;
                domain = AppDomain.CreateDomain("Spring", new Evidence(AppDomain.CurrentDomain.Evidence), setup);
                object command = domain.CreateInstanceAndUnwrap(GetType().Assembly.FullName, typeof(RemotableCommand).FullName);

                ProxyFactoryObject fac = new ProxyFactoryObject();
                fac.AddInterface(typeof(ICommand));
                fac.AddAdvice(new NopInterceptor());
                fac.Target = command;

                IAdvised advised = fac.GetObject() as IAdvised;
                Assert.IsNotNull(advised);

                ICommand cmd = fac.GetObject() as ICommand;
                Assert.IsNotNull(cmd);

                cmd.Execute();
            }
            finally
            {
                AppDomain.Unload(domain);
            }
        }
        public void ReplaceAdvisorWhenConfigIsFrozen()
        {
            ProxyFactoryObject fac = CreateFrozenProxyFactory();

            fac.IsFrozen = true;
            fac.ReplaceAdvisor(new PointcutForVoid(), new PointcutForVoid());
        }
Exemplo n.º 8
0
        public void ProxyTypeDoesntChangeIfSameConfig()
        {
            ProxyFactoryObject factoryObject = (ProxyFactoryObject)this.factory.GetObject("&concurrentPrototype");
            Type testObjectType1             = factoryObject.GetObject().GetType();
            Type testObjectType2             = factoryObject.GetObject().GetType();

            Assert.AreSame(testObjectType1, testObjectType2);
        }
Exemplo n.º 9
0
        private ProxyFactoryObject CreateFrozenProxyFactory()
        {
            ProxyFactoryObject fac = new ProxyFactoryObject();

            fac.AddInterface(typeof(ITestObject));
            fac.IsFrozen = true;
            fac.AddAdvisor(new PointcutForVoid()); // this is ok, no proxy created yet
            fac.GetObject();
            return(fac);
        }
Exemplo n.º 10
0
        public void ProxyTypeChangesIfConfigChanges()
        {
            ProxyFactoryObject factoryObject = (ProxyFactoryObject)this.factory.GetObject("&concurrentPrototype");
            Type testObjectType1             = factoryObject.GetObject().GetType();

            factoryObject.Interfaces = new Type[] {};
            Type testObjectType2 = factoryObject.GetObject().GetType();

            Assert.AreNotSame(testObjectType1, testObjectType2);
        }
Exemplo n.º 11
0
        public void GetObjectTypeWithTargetViaTargetSource()
        {
            IObjectFactory bf = new XmlObjectFactory(
                new ReadOnlyXmlTestResource("proxyFactoryTargetSourceTests.xml",
                                            GetType()));
            ITestObject tb = (ITestObject)bf.GetObject("viaTargetSource");

            Assert.IsTrue(tb.Name.Equals("Adam"));
            ProxyFactoryObject pfb = (ProxyFactoryObject)bf.GetObject("&viaTargetSource");

            Assert.IsTrue(typeof(ITestObject).IsAssignableFrom(pfb.ObjectType), "Has correct object type");
        }
Exemplo n.º 12
0
        public void AddAdvisorWhenConfigIsFrozen()
        {
            ProxyFactoryObject fac = CreateFrozenProxyFactory();

            try
            {
                fac.AddAdvisor(new PointcutForVoid()); // not ok
                Assert.Fail("changing a frozen config must throw AopConfigException");
            }
            catch (AopConfigException)
            {}
        }
Exemplo n.º 13
0
        public void NullNameInInterceptorNamesArrayThrowAopConfigException()
        {
            IObjectFactory factory = A.Fake <IObjectFactory>();

            ProxyFactoryObject fac = new ProxyFactoryObject();

            fac.ProxyInterfaces  = new string[] { typeof(ICommand).FullName };
            fac.IsSingleton      = false;
            fac.InterceptorNames = new string[] { null, null };
            fac.ObjectFactory    = factory;
            Assert.Throws <AopConfigException>(() => fac.GetObject());
        }
Exemplo n.º 14
0
        public void FactoryWrapsObjectInSingletonTargetSource()
        {
            IObjectFactory bf = new XmlObjectFactory(
                new ReadOnlyXmlTestResource("proxyFactoryTargetSourceTests.xml",
                                            GetType()));
            ITestObject tb = (ITestObject)bf.GetObject("viaTargetSource");

            Assert.IsTrue(tb.Name.Equals("Adam"));
            ProxyFactoryObject pfb = (ProxyFactoryObject)bf.GetObject("&viaTargetSource");

            Assert.IsTrue(typeof(ITestObject).IsAssignableFrom(pfb.ObjectType), "Has correct object type");
            Assert.AreEqual(typeof(SingletonTargetSource), pfb.TargetSource.GetType(), "Incorrect target source, expected singleton");
        }
Exemplo n.º 15
0
        private void CallGetObject()
        {
            ProxyFactoryObject proxyFactory = (ProxyFactoryObject)this.factory.GetObject("&concurrentPrototype");

            ITestObject prevTestObject = null;

            for (int i = 0; i < 20; i++)
            {
                ITestObject o = proxyFactory.GetObject() as ITestObject;
                Assert.IsNotNull(o);
                Assert.AreNotSame(prevTestObject, o);
                prevTestObject = o;
                Thread.Sleep(0);
            }
        }
        public void NullNameInInterceptorNamesArrayThrowAopConfigException()
        {
            IObjectFactory factory = (IObjectFactory)mocks.CreateMock(typeof(IObjectFactory));

            ProxyFactoryObject fac = new ProxyFactoryObject();

            fac.ProxyInterfaces  = new string[] { typeof(ICommand).FullName };
            fac.IsSingleton      = false;
            fac.InterceptorNames = new string[] { null, null };
            fac.ObjectFactory    = factory;
            try
            {
                fac.GetObject();
                Assert.Fail();
            }
            catch (AopConfigException)
            {}
        }
Exemplo n.º 17
0
        public void GetObjectTypeWithDirectTarget()
        {
            IObjectFactory bf = new XmlObjectFactory(
                new ReadOnlyXmlTestResource("proxyFactoryTargetSourceTests.xml",
                                            GetType()));

            // We have a counting before advice here
            CountingBeforeAdvice cba = (CountingBeforeAdvice)bf.GetObject("countingBeforeAdvice");

            Assert.AreEqual(0, cba.GetCalls());

            ITestObject tb = (ITestObject)bf.GetObject("directTarget");

            Assert.IsTrue(tb.Name.Equals("Adam"));
            Assert.AreEqual(1, cba.GetCalls());

            ProxyFactoryObject pfb = (ProxyFactoryObject)bf.GetObject("&directTarget");

            Assert.IsTrue(typeof(ITestObject).IsAssignableFrom(pfb.ObjectType), "Has correct object type");
        }
Exemplo n.º 18
0
        public void IsSingletonTrueReturnsNew_ProxyInstance_NotNewProxyTargetSource()
        {
            GoodCommand    target = new GoodCommand();
            IObjectFactory mock   = A.Fake <IObjectFactory>();

            A.CallTo(() => mock.GetObject("singleton")).Returns(target);

            ProxyFactoryObject fac = new ProxyFactoryObject();

            fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName };
            fac.IsSingleton     = true; // default, just being explicit...
            fac.TargetName      = "singleton";
            fac.ObjectFactory   = mock;
            fac.AddAdvice(new NopInterceptor());

            ICommand one = (ICommand)fac.GetObject();
            ICommand two = (ICommand)fac.GetObject();

            Assert.IsTrue(ReferenceEquals(one, two));
        }
Exemplo n.º 19
0
        public void GlobalsCanAddAspectInterfaces()
        {
            IAddedGlobalInterface agi = (IAddedGlobalInterface)factory.GetObject("autoInvoker");

            Assert.IsTrue(agi.GlobalsAdded == -1);

            ProxyFactoryObject pfb = (ProxyFactoryObject)factory.GetObject("&validGlobals");

            pfb.GetObject(); // for creation
            Assert.AreEqual(2, pfb.Advisors.Count, "Proxy should have 1 global and 1 explicit advisor");
            Assert.AreEqual(1, pfb.Introductions.Count, "Proxy should have 1 global introduction");

            agi.GlobalsAdded = ((IAdvised)agi).Introductions.Count;
            Assert.IsTrue(agi.GlobalsAdded == 1);

            IApplicationEventListener l = (IApplicationEventListener)factory.GetObject("validGlobals");

            agi = (IAddedGlobalInterface)l;
            Assert.IsTrue(agi.GlobalsAdded == -1);
            Assert.Throws <InvalidCastException>(() => factory.GetObject <IAddedGlobalInterface>("test1"));
        }
Exemplo n.º 20
0
        public void MakeSurePrototypeTargetIsNotNeedlesslyCreatedDuringInitialization_Unit()
        {
            GoodCommand    target = new GoodCommand();
            NopInterceptor advice = new NopInterceptor();

            IObjectFactory factory = A.Fake <IObjectFactory>();

            ProxyFactoryObject fac = new ProxyFactoryObject();

            fac.ProxyInterfaces  = new[] { typeof(ICommand).FullName };
            fac.IsSingleton      = false;
            fac.InterceptorNames = new[] { "advice", "prototype" };
            fac.ObjectFactory    = factory;

            A.CallTo(() => factory.IsSingleton("advice")).Returns(true);
            A.CallTo(() => factory.GetObject("advice")).Returns(advice);
            A.CallTo(() => factory.GetType("prototype")).Returns(target.GetType());
            A.CallTo(() => factory.GetObject("prototype")).Returns(target);

            fac.GetObject();
        }
Exemplo n.º 21
0
        public void PassEmptyInterceptorNamesArray_WithTargetThatImplementsAnInterfaceCanBeCastToSaidInterface()
        {
            IObjectFactory factory = A.Fake <IObjectFactory>();

            ProxyFactoryObject fac = new ProxyFactoryObject();

            fac.ProxyInterfaces = new string[] { };
            fac.Target          = new GoodCommand();
            fac.ObjectFactory   = factory;

            IAdvised advised = fac.GetObject() as IAdvised;

            Assert.IsNotNull(advised);

            ICommand cmd = fac.GetObject() as ICommand;

            Assert.IsNotNull(cmd);

            DoesntImplementAnyInterfaces obj = fac.GetObject() as DoesntImplementAnyInterfaces;

            Assert.IsNull(obj);
        }
        public void IsSingletonFalseReturnsNew_ProxyInstance_NotNewProxyTargetSource()
        {
            GoodCommand    target = new GoodCommand();
            IObjectFactory mock   = (IObjectFactory)mocks.CreateMock(typeof(IObjectFactory));

            Expect.Call(mock.GetObject("singleton")).Return(target).Repeat.Twice();
            mocks.ReplayAll();

            ProxyFactoryObject fac = new ProxyFactoryObject();

            fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName };
            fac.IsSingleton     = false;
            fac.TargetName      = "singleton";
            fac.ObjectFactory   = mock;
            fac.AddAdvice(new NopInterceptor());

            ICommand one = (ICommand)fac.GetObject();
            ICommand two = (ICommand)fac.GetObject();

            Assert.IsFalse(ReferenceEquals(one, two));

            mocks.VerifyAll();
        }
Exemplo n.º 23
0
        public void ProxiedObjectUnwrapsTargetInvocationException()
        {
            ProxyFactoryObject fac = new ProxyFactoryObject();

            fac.AddInterface(typeof(ICommand));
            fac.AddAdvice(new NopInterceptor());
            fac.Target = new BadCommand();

            ICommand cmd = (ICommand)fac.GetObject();

            try
            {
                cmd.Execute();
            }
            catch (NotImplementedException)
            {
                // this is good, we want this exception to bubble up...
            }
            catch (TargetInvocationException)
            {
                Assert.Fail("Must have unwrapped this.");
            }
        }
 public void PassRubbishNameToTheProxyInterfacesProperty()
 {
     ProxyFactoryObject fac = new ProxyFactoryObject();
     Assert.Throws<AopConfigException>(() => fac.ProxyInterfaces = new string[] { "Hey" });
 }
        public void IsSingletonTrueReturnsNew_ProxyInstance_NotNewProxyTargetSource()
        {
            GoodCommand target = new GoodCommand();
            IObjectFactory mock = (IObjectFactory)mocks.CreateMock(typeof(IObjectFactory));
            Expect.Call(mock.GetObject("singleton")).Return(target);
            mocks.ReplayAll();

            ProxyFactoryObject fac = new ProxyFactoryObject();
            fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName };
            fac.IsSingleton = true; // default, just being explicit...
            fac.TargetName = "singleton";
            fac.ObjectFactory = mock;
            fac.AddAdvice(new NopInterceptor());

            ICommand one = (ICommand)fac.GetObject();
            ICommand two = (ICommand)fac.GetObject();
            Assert.IsTrue(ReferenceEquals(one, two));

            mocks.VerifyAll();
        }
        public void SupportsTransparentProxyAsTarget()
        {
            AppDomain domain = null;
            try
            {
                AppDomainSetup setup = new AppDomainSetup();
                setup.ApplicationBase = Environment.CurrentDirectory;
                domain = AppDomain.CreateDomain("Spring", new Evidence(AppDomain.CurrentDomain.Evidence), setup);
                object command = domain.CreateInstanceAndUnwrap(GetType().Assembly.FullName, typeof(RemotableCommand).FullName);

                ProxyFactoryObject fac = new ProxyFactoryObject();
                fac.AddInterface(typeof(ICommand));
                fac.AddAdvice(new NopInterceptor());
                fac.Target = command;

                IAdvised advised = fac.GetObject() as IAdvised;
                Assert.IsNotNull(advised);

                ICommand cmd = fac.GetObject() as ICommand;
                Assert.IsNotNull(cmd);

                cmd.Execute();
            }
            finally
            {
                AppDomain.Unload(domain);
            }
        }
        public void MakeSurePrototypeTargetIsNotNeedlesslyCreatedDuringInitialization_Unit()
        {
            GoodCommand target = new GoodCommand();
            NopInterceptor advice = new NopInterceptor();

            MockRepository mocks = new MockRepository();
            IObjectFactory factory = (IObjectFactory) mocks.CreateMock(typeof(IObjectFactory));

            ProxyFactoryObject fac = new ProxyFactoryObject();
            fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName };
            fac.IsSingleton = false;
            fac.InterceptorNames = new string[] { "advice", "prototype" };
            fac.ObjectFactory = factory;

            //            using (mocks.Record())
            {
                using (mocks.Unordered())
                {
                    Expect.Call(factory.IsSingleton("advice")).Return(true);
                    Expect.Call(factory.GetObject("advice")).Return(advice);
                    Expect.Call(factory.GetType("prototype")).Return(target.GetType());
                    Expect.Call(factory.GetObject("prototype")).Return(target);
                }
            }
            mocks.ReplayAll();

            //            using(mocks.Playback())
            {
                fac.GetObject();
            }
            mocks.VerifyAll();
        }
Exemplo n.º 28
0
        public void CanAddAndRemoveIntroductionsOnSingleton()
        {
            try
            {
                ITimeStamped ts = (ITimeStamped)factory.GetObject("test1");
                Assert.Fail("Shouldn't implement ITimeStamped before manipulation");
            }
            catch (InvalidCastException)
            {
            }

            ProxyFactoryObject config = (ProxyFactoryObject)factory.GetObject("&test1");
            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("test1");

            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("test1");
                Assert.Fail("Should no longer implement ITimeStamped");
            }
            catch (InvalidCastException)
            {
                // expected...
            }

            // Now check non-effect of removing interceptor that isn't there
            oldCount = config.Advisors.Count;
            config.RemoveAdvice(new DebugAdvice());
            Assert.IsTrue(config.Advisors.Count == oldCount);

            ITestObject it = (ITestObject)ts2;
            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);
        }
        public void ProxiedObjectUnwrapsTargetInvocationException()
        {
            ProxyFactoryObject fac = new ProxyFactoryObject();
            fac.AddInterface(typeof(ICommand));
            fac.AddAdvice(new NopInterceptor());
            fac.Target = new BadCommand();

            ICommand cmd = (ICommand)fac.GetObject();
            try
            {
                cmd.Execute();
            }
            catch (NotImplementedException)
            {
                // this is good, we want this exception to bubble up...
            }
            catch (TargetInvocationException)
            {
                Assert.Fail("Must have unwrapped this.");
            }
        }
 public void PassClassNotInterfaceNameToTheProxyInterfacesProperty()
 {
     ProxyFactoryObject fac = new ProxyFactoryObject();
     Assert.Throws<AopConfigException>(() => fac.ProxyInterfaces = new string[] { typeof(GoodCommand).FullName });
 }
        public void PassNullElementListToTheProxyInterfacesProperty()
        {
            ProxyFactoryObject fac = new ProxyFactoryObject();

            fac.ProxyInterfaces = new string[] { null };
        }
        public void PassRubbishNameToTheProxyInterfacesProperty()
        {
            ProxyFactoryObject fac = new ProxyFactoryObject();

            fac.ProxyInterfaces = new string[] { "Hey" };
        }
        public void SingletonProxyWithPrototypeTargetCreatesTargetOnlyOnce()
        {
            try
            {
                RootObjectDefinition advice = new RootObjectDefinition(typeof(NopInterceptor));
                // prototype target...
                RootObjectDefinition target = new RootObjectDefinition(typeof(InstantiationCountingCommand), false);

                DefaultListableObjectFactory ctx = new DefaultListableObjectFactory();
                ctx.RegisterObjectDefinition("advice", advice);
                ctx.RegisterObjectDefinition("prototype", target);

                ProxyFactoryObject fac = new ProxyFactoryObject();
                fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName };
                fac.IsSingleton = true;
                fac.InterceptorNames = new string[] { "advice", "prototype" };
                fac.ObjectFactory = ctx;

                Assert.AreEqual(0, InstantiationCountingCommand.NumberOfInstantiations, "First Call");
                fac.GetObject();
                Assert.AreEqual(1, InstantiationCountingCommand.NumberOfInstantiations, "Second Call");
                fac.GetObject();
                Assert.AreEqual(1, InstantiationCountingCommand.NumberOfInstantiations, "Third Call");
            }

            finally
            {
                InstantiationCountingCommand.NumberOfInstantiations = 0;
            }
        }
 public void PassClassNotInterfaceNameToTheProxyInterfacesProperty()
 {
     ProxyFactoryObject fac = new ProxyFactoryObject();
     fac.ProxyInterfaces = new string[] { typeof(GoodCommand).FullName };
 }
        public void NullNameInInterceptorNamesArrayThrowAopConfigException()
        {
            IObjectFactory factory = (IObjectFactory) mocks.CreateMock(typeof(IObjectFactory));

            ProxyFactoryObject fac = new ProxyFactoryObject();
            fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName };
            fac.IsSingleton = false;
            fac.InterceptorNames = new string[] { null, null };
            fac.ObjectFactory = factory;
            try
            {
                fac.GetObject();
                Assert.Fail();
            }
            catch (AopConfigException)
            {}
        }
Exemplo n.º 36
0
        public void PassNullToTheProxyInterfacesProperty()
        {
            ProxyFactoryObject fac = new ProxyFactoryObject();

            Assert.Throws <AopConfigException>(() => fac.ProxyInterfaces = null);
        }
Exemplo n.º 37
0
        public void PassRubbishNameToTheProxyInterfacesProperty()
        {
            ProxyFactoryObject fac = new ProxyFactoryObject();

            Assert.Throws <AopConfigException>(() => fac.ProxyInterfaces = new string[] { "Hey" });
        }
        public void TargetAtEndOfInterceptorList()
        {
            GoodCommand target = new GoodCommand();
            NopInterceptor advice = new NopInterceptor();

            IObjectFactory mock = (IObjectFactory) mocks.CreateMock(typeof(IObjectFactory));
            Expect.Call(mock.GetObject("advice")).Return(advice);
            Expect.Call(mock.GetObject("singleton")).Return(target);
            Expect.Call(mock.GetType("singleton")).Return(typeof(GoodCommand));
            mocks.ReplayAll();

            ProxyFactoryObject fac = new ProxyFactoryObject();
            fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName };
            fac.IsSingleton = true; // default, just being explicit...
            fac.InterceptorNames = new string[] { "advice", "singleton" };
            fac.ObjectFactory = mock;

            ICommand one = (ICommand)fac.GetObject();
            ICommand two = (ICommand)fac.GetObject();
            Assert.IsTrue(ReferenceEquals(one, two));
            one.Execute();
            Assert.AreEqual(1, advice.Count);
            two.Execute();
            Assert.AreEqual(2, advice.Count);

            mocks.VerifyAll();
        }
Exemplo n.º 39
0
        public void PassClassNotInterfaceNameToTheProxyInterfacesProperty()
        {
            ProxyFactoryObject fac = new ProxyFactoryObject();

            Assert.Throws <AopConfigException>(() => fac.ProxyInterfaces = new string[] { typeof(GoodCommand).FullName });
        }
 public void PassRubbishNameToTheProxyInterfacesProperty()
 {
     ProxyFactoryObject fac = new ProxyFactoryObject();
     fac.ProxyInterfaces = new string[] { "Hey" };
 }
Exemplo n.º 41
0
        public void PassNullElementListToTheProxyInterfacesProperty()
        {
            ProxyFactoryObject fac = new ProxyFactoryObject();

            Assert.Throws <AopConfigException>(() => fac.ProxyInterfaces = new string[] { null });
        }
 public void PassNullToTheProxyInterfacesProperty()
 {
     ProxyFactoryObject fac = new ProxyFactoryObject();
     fac.ProxyInterfaces = null;
 }
        public void MakeSurePrototypeTargetIsNotNeedlesslyCreatedDuringInitialization_Integration()
        {
            try
            {
                RootObjectDefinition advice = new RootObjectDefinition(typeof(NopInterceptor));
                // prototype target...
                RootObjectDefinition target = new RootObjectDefinition(typeof(InstantiationCountingCommand), false);

                DefaultListableObjectFactory ctx = new DefaultListableObjectFactory();
                ctx.RegisterObjectDefinition("advice", advice);
                ctx.RegisterObjectDefinition("prototype", target);

                ProxyFactoryObject fac = new ProxyFactoryObject();
                fac.ProxyInterfaces = new string[] { typeof(ICommand).FullName };
                fac.IsSingleton = false;
                fac.InterceptorNames = new string[] { "advice", "prototype" };
                fac.ObjectFactory = ctx;

                Assert.AreEqual(0, InstantiationCountingCommand.NumberOfInstantiations,
                                "Prototype target instance is being (needlessly) created during PFO initialization.");
                fac.GetObject();
                Assert.AreEqual(1, InstantiationCountingCommand.NumberOfInstantiations, "Expected 1 inst");
                fac.GetObject();
                Assert.AreEqual(2, InstantiationCountingCommand.NumberOfInstantiations);
            }
            finally
            {
                InstantiationCountingCommand.NumberOfInstantiations = 0;
            }
        }
 public void PassNullElementListToTheProxyInterfacesProperty()
 {
     ProxyFactoryObject fac = new ProxyFactoryObject();
     fac.ProxyInterfaces = new string[] { null };
 }
 public void PassNullElementListToTheProxyInterfacesProperty()
 {
     ProxyFactoryObject fac = new ProxyFactoryObject();
     Assert.Throws<AopConfigException>(() => fac.ProxyInterfaces = new string[] { null });
 }
        public void PassEmptyInterceptorNamesArray_WithTargetThatImplementsAnInterfaceCanBeCastToSaidInterface()
        {
            IObjectFactory factory = (IObjectFactory) mocks.CreateMock(typeof(IObjectFactory));

            ProxyFactoryObject fac = new ProxyFactoryObject();
            fac.ProxyInterfaces = new string[] { };
            fac.Target = new GoodCommand();
            fac.ObjectFactory = factory;

            IAdvised advised = fac.GetObject() as IAdvised;
            Assert.IsNotNull(advised);

            ICommand cmd = fac.GetObject() as ICommand;
            Assert.IsNotNull(cmd);

            DoesntImplementAnyInterfaces obj = fac.GetObject() as DoesntImplementAnyInterfaces;
            Assert.IsNull(obj);
        }
 public void PassNullToTheProxyInterfacesProperty()
 {
     ProxyFactoryObject fac = new ProxyFactoryObject();
     Assert.Throws<AopConfigException>(() => fac.ProxyInterfaces = null);
 }
 private ProxyFactoryObject CreateFrozenProxyFactory()
 {
     ProxyFactoryObject fac = new ProxyFactoryObject();
     fac.AddInterface(typeof(ITestObject));
     fac.IsFrozen = true;
     fac.AddAdvisor(new PointcutForVoid()); // this is ok, no proxy created yet
     fac.GetObject();
     return fac;
 }