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();
        }
Exemple #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);
        }
Exemple #3
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));
        }
Exemple #4
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();
        }
        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();
        }
        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();
        }
        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();
        }
        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();
        }