Beispiel #1
0
        public void SunnyDayReplaceMethod_WithArgumentAcceptingReplacer()
        {
            RootObjectDefinition replacerDef = new RootObjectDefinition(typeof(NewsFeedFactory));

            RootObjectDefinition   managerDef  = new RootObjectDefinition(typeof(NewsFeedManagerWith_Replace_MethodThatTakesArguments));
            ReplacedMethodOverride theOverride = new ReplacedMethodOverride("CreateNewsFeed", "replacer");

            // we must specify parameter type fragments...
            theOverride.AddTypeIdentifier(typeof(string).FullName);
            managerDef.MethodOverrides.Add(theOverride);

            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();

            factory.RegisterObjectDefinition("manager", managerDef);
            factory.RegisterObjectDefinition("replacer", replacerDef);
            NewsFeedManagerWith_Replace_MethodThatTakesArguments manager = (NewsFeedManagerWith_Replace_MethodThatTakesArguments)factory["manager"];
            NewsFeed feed1 = manager.CreateNewsFeed("So sad... to be all alone in the world");

            Assert.IsNotNull(feed1, "The CreateNewsFeed() method is not being replaced.");
            Assert.AreEqual("So sad... to be all alone in the world", feed1.Name);
            NewsFeed feed2 = manager.CreateNewsFeed("Oh Muzzy!");

            // NewsFeedFactory always yields a new NewsFeed (see class definition below)...
            Assert.IsFalse(ReferenceEquals(feed1, feed2));
        }
Beispiel #2
0
        public void LookupMethodWithNullMethodInstantiatedWithCtorArg()
        {
            RootObjectDefinition feedDef = new RootObjectDefinition(typeof(NewsFeed));

            feedDef.IsSingleton = false;
            feedDef.PropertyValues.Add("name", "Bingo");

            RootObjectDefinition managerDef = new RootObjectDefinition(typeof(ReturnsNullNewsFeedManager));

            managerDef.MethodOverrides.Add(new LookupMethodOverride("CreateNewsFeed", "feed"));
            managerDef.ConstructorArgumentValues.AddNamedArgumentValue("name", "Bingo");

            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();

            factory.RegisterObjectDefinition("manager", managerDef);
            factory.RegisterObjectDefinition("feed", feedDef);
            ReturnsNullNewsFeedManager manager = (ReturnsNullNewsFeedManager)factory["manager"];

            Assert.AreEqual("Bingo", manager.Name);
            NewsFeed feed1 = manager.CreateNewsFeed();

            Assert.IsNotNull(feed1, "The CreateNewsFeed() method is not being replaced.");
            NewsFeed feed2 = manager.CreateNewsFeed();

            // assert that the object (prototype) is definitely being looked up each time...
            Assert.IsFalse(ReferenceEquals(feed1, feed2));
        }
Beispiel #3
0
        public void LookupMethodWithNullMethod()
        {
            try
            {
                RootObjectDefinition feedDef = new RootObjectDefinition(typeof(NewsFeed));
                feedDef.IsSingleton = false;
                feedDef.PropertyValues.Add("name", "Bingo");

                RootObjectDefinition managerDef = new RootObjectDefinition(typeof(ReturnsNullNewsFeedManager));
                managerDef.MethodOverrides.Add(new LookupMethodOverride("CreateNewsFeed", "feed"));

                DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
                factory.RegisterObjectDefinition("manager", managerDef);
                factory.RegisterObjectDefinition("feed", feedDef);
                INewsFeedManager manager = (INewsFeedManager)factory["manager"];
                NewsFeed         feed1   = manager.CreateNewsFeed();
                Assert.IsNotNull(feed1, "The CreateNewsFeed() method is not being replaced.");
                NewsFeed feed2 = manager.CreateNewsFeed();
                // assert that the object (prototype) is definitely being looked up each time...
                Assert.IsFalse(ReferenceEquals(feed1, feed2));
            }
            catch (Exception ex)
            {
                Console.Out.WriteLine("ex = {0}", ex);
            }
        }
Beispiel #4
0
        public void LookupMethodMultiple()
        {
            RootObjectDefinition feedDef = new RootObjectDefinition(typeof(NewsFeed));

            feedDef.IsSingleton = false;
            feedDef.PropertyValues.Add("name", "Bingo");

            RootObjectDefinition testObjectDef = new RootObjectDefinition(typeof(TestObject));

            testObjectDef.IsSingleton = false;
            testObjectDef.PropertyValues.Add("name", "Miki Nakatani");

            RootObjectDefinition managerDef = new RootObjectDefinition(typeof(TestObjectAndNewsFeedFactory));

            managerDef.MethodOverrides.Add(new LookupMethodOverride("CreateNewsFeed", "feed"));
            managerDef.MethodOverrides.Add(new LookupMethodOverride("CreateTestObject", "test"));

            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();

            factory.RegisterObjectDefinition("manager", managerDef);
            factory.RegisterObjectDefinition("feed", feedDef);
            factory.RegisterObjectDefinition("test", testObjectDef);
            TestObjectAndNewsFeedFactory manager = (TestObjectAndNewsFeedFactory)factory["manager"];

            INewsFeedManager newsFeedManager = manager;
            NewsFeed         feed1           = newsFeedManager.CreateNewsFeed();

            Assert.IsNotNull(feed1, "The CreateNewsFeed() method is not being replaced.");
            NewsFeed feed2 = newsFeedManager.CreateNewsFeed();

            // assert that the object (prototype) is definitely being looked up each time...
            Assert.IsFalse(ReferenceEquals(feed1, feed2));

            ITestObjectFactory toFactory = manager;
            ITestObject        to1       = toFactory.CreateTestObject();

            Assert.IsNotNull(to1, "The CreateTestObject() method is not being replaced.");
            ITestObject to2 = toFactory.CreateTestObject();

            // assert that the object (prototype) is definitely being looked up each time...
            Assert.IsFalse(ReferenceEquals(to1, to2));
        }
Beispiel #5
0
        public void SunnyDayReplaceMethod_WithProtectedVirtual()
        {
            RootObjectDefinition replacerDef = new RootObjectDefinition(typeof(NewsFeedFactory));

            RootObjectDefinition managerDef = new RootObjectDefinition(typeof(ProtectedReturnsNullNewsFeedManagerWithVirtualMethod));

            managerDef.MethodOverrides.Add(new ReplacedMethodOverride("CreateNewsFeed", "replacer"));

            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();

            factory.RegisterObjectDefinition("manager", managerDef);
            factory.RegisterObjectDefinition("replacer", replacerDef);
            ProtectedReturnsNullNewsFeedManagerWithVirtualMethod manager = (ProtectedReturnsNullNewsFeedManagerWithVirtualMethod)factory["manager"];
            NewsFeed feed1 = manager.GrabNewsFeed();

            Assert.IsNotNull(feed1, "The protected CreateNewsFeed() method is not being replaced.");
            Assert.AreEqual(NewsFeedFactory.DefaultName, feed1.Name);
            NewsFeed feed2 = manager.GrabNewsFeed();

            // NewsFeedFactory always yields a new NewsFeed (see class definition below)...
            Assert.IsFalse(ReferenceEquals(feed1, feed2));
        }
Beispiel #6
0
        public void LookupMethodWithVirtualProtectedMethod()
        {
            RootObjectDefinition feedDef = new RootObjectDefinition(typeof(NewsFeed));

            feedDef.IsSingleton = false;
            feedDef.PropertyValues.Add("name", "Bingo");

            RootObjectDefinition managerDef = new RootObjectDefinition(typeof(ProtectedReturnsNullNewsFeedManagerWithVirtualMethod));

            managerDef.MethodOverrides.Add(new LookupMethodOverride("CreateNewsFeed", "feed"));

            DefaultListableObjectFactory factory = new DefaultListableObjectFactory();

            factory.RegisterObjectDefinition("manager", managerDef);
            factory.RegisterObjectDefinition("feed", feedDef);
            ProtectedReturnsNullNewsFeedManagerWithVirtualMethod manager = (ProtectedReturnsNullNewsFeedManagerWithVirtualMethod)factory["manager"];
            NewsFeed feed1 = manager.GrabNewsFeed();

            Assert.IsNotNull(feed1, "The CreateNewsFeed() method is not being replaced.");
            NewsFeed feed2 = manager.GrabNewsFeed();

            // assert that the object (prototype) is definitely being looked up each time...
            Assert.IsFalse(ReferenceEquals(feed1, feed2));
        }