Exemple #1
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));
        }
Exemple #2
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));
        }