public void Matches_MatchingMethodNameWithOverloadAndBadTypeIdentifiers()
        {
            ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");

            methodOverride.AddTypeIdentifier(GetType().FullName);
            Assert.IsFalse(methodOverride.Matches(typeof(Executor).GetMethod("Execute", new Type[] { typeof(object) })));
        }
        /// <summary>
        /// Reimplements the supplied <paramref name="method"/> by delegating to
        /// another <see cref="Spring.Objects.Factory.Support.IMethodReplacer"/>
        /// looked up in an enclosing IoC container.
        /// </summary>
        /// <param name="target">
        /// The instance whose <paramref name="method"/> is to be
        /// (re)implemented.
        /// </param>
        /// <param name="method">
        /// The method that is to be (re)implemented.
        /// </param>
        /// <param name="arguments">The target method's arguments.</param>
        /// <returns>
        /// The result of the delegated call to the looked up
        /// <see cref="Spring.Objects.Factory.Support.IMethodReplacer"/>.
        /// </returns>
        public override object Implement(object target, MethodInfo method, object[] arguments)
        {
            ReplacedMethodOverride ovr = (ReplacedMethodOverride)GetOverride(method);
            IMethodReplacer        methodReplacement = (IMethodReplacer)GetObject(ovr.MethodReplacerObjectName);

            return(methodReplacement.Implement(target, method, arguments));
        }
        public void Matches_MatchingMethodNameNoOverload()
        {
            ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Administer", "replacer");

            methodOverride.IsOverloaded = false;
            Assert.IsTrue(methodOverride.Matches(typeof(Executor).GetMethod("Administer")));
        }
示例#4
0
        public void SunnyDayPath()
        {
            IObjectFactory mockFactory = (IObjectFactory)mocks.CreateMock(typeof(IObjectFactory));
            IConfigurableObjectDefinition mockDefinition = (IConfigurableObjectDefinition)mocks.CreateMock(typeof(IConfigurableObjectDefinition));
            IMethodReplacer mockReplacer = (IMethodReplacer)mocks.CreateMock(typeof(IMethodReplacer));


            const string ReplacerObjectName = "replacer";

            Expect.Call(mockFactory.GetObject(ReplacerObjectName)).Return(mockReplacer);

            ReplacedMethodOverride ovr       = new ReplacedMethodOverride("SunnyDayPath", ReplacerObjectName);
            MethodOverrides        overrides = new MethodOverrides();

            overrides.Add(ovr);
            Expect.Call(mockDefinition.MethodOverrides).Return(overrides);

            Expect.Call(mockReplacer.Implement(null, null, null)).IgnoreArguments().Return(null);
            mocks.ReplayAll();

            DelegatingMethodReplacer replacer = new DelegatingMethodReplacer(mockDefinition, mockFactory);
            MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();

            replacer.Implement(this, method, new object[] {});

            mocks.VerifyAll();
        }
        public void Matches_RequiresAllTypeIdentifiers()
        {
            ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");

            methodOverride.AddTypeIdentifier(typeof(object).FullName);
            Assert.IsFalse(methodOverride.Matches(typeof(Executor).GetMethod("Execute",
                                                                             new Type[] { typeof(object), typeof(object) })));
        }
        public void SunnyDayPath()
        {
            IObjectFactory mockFactory = (IObjectFactory) mocks.CreateMock(typeof(IObjectFactory));
            IConfigurableObjectDefinition mockDefinition = (IConfigurableObjectDefinition) mocks.CreateMock(typeof(IConfigurableObjectDefinition));
            IMethodReplacer mockReplacer = (IMethodReplacer) mocks.CreateMock(typeof(IMethodReplacer));

            const string ReplacerObjectName = "replacer";
            Expect.Call(mockFactory.GetObject(ReplacerObjectName)).Return(mockReplacer);

            ReplacedMethodOverride ovr = new ReplacedMethodOverride("SunnyDayPath", ReplacerObjectName);
            MethodOverrides overrides = new MethodOverrides();
            overrides.Add(ovr);
            Expect.Call(mockDefinition.MethodOverrides).Return(overrides);

            Expect.Call(mockReplacer.Implement(null, null, null)).IgnoreArguments().Return(null);
            mocks.ReplayAll();

            DelegatingMethodReplacer replacer = new DelegatingMethodReplacer(mockDefinition, mockFactory);
            MethodInfo method = (MethodInfo) MethodBase.GetCurrentMethod();
            replacer.Implement(this, method, new object[] {});

            mocks.VerifyAll();
        }
        public void SunnyDayPath()
        {
            IObjectFactory mockFactory = A.Fake <IObjectFactory>();
            IConfigurableObjectDefinition mockDefinition = A.Fake <IConfigurableObjectDefinition>();
            IMethodReplacer mockReplacer = A.Fake <IMethodReplacer>();


            const string ReplacerObjectName = "replacer";

            A.CallTo(() => mockFactory.GetObject(ReplacerObjectName)).Returns(mockReplacer);

            ReplacedMethodOverride ovr       = new ReplacedMethodOverride("SunnyDayPath", ReplacerObjectName);
            MethodOverrides        overrides = new MethodOverrides();

            overrides.Add(ovr);
            A.CallTo(() => mockDefinition.MethodOverrides).Returns(overrides);

            A.CallTo(() => mockReplacer.Implement(null, null, null)).WithAnyArguments().Returns(null);

            DelegatingMethodReplacer replacer = new DelegatingMethodReplacer(mockDefinition, mockFactory);
            MethodInfo method = (MethodInfo)MethodBase.GetCurrentMethod();

            replacer.Implement(this, method, new object[] {});
        }
		public void Matches_AllTypeIdentifiers()
		{
			ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");
			methodOverride.AddTypeIdentifier(typeof (object).FullName);
			methodOverride.AddTypeIdentifier(typeof (object).FullName);
			Assert.IsTrue(methodOverride.Matches(typeof (Executor).GetMethod("Execute",
			                                                                 new Type[] {typeof (object), typeof (object)})));
		}
		public void Matches_MatchingMethodNameWithOverloadAndBadTypeIdentifiers()
		{
			ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");
			methodOverride.AddTypeIdentifier(GetType().FullName);
			Assert.IsFalse(methodOverride.Matches(typeof (Executor).GetMethod("Execute", new Type[] {typeof (object)})));
		}
		public void Matches_MatchingMethodNameWithOverload()
		{
			ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");
			Assert.IsTrue(methodOverride.Matches(typeof (Executor).GetMethod("Execute", new Type[] {})));
		}
		public void Matches_MatchingMethodNameNoOverload()
		{
			ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Administer", "replacer");
			methodOverride.IsOverloaded = false;
			Assert.IsTrue(methodOverride.Matches(typeof (Executor).GetMethod("Administer")));
		}
		public void Matches_TotallyDifferentMethodName()
		{
			ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");
			Assert.IsFalse(methodOverride.Matches(typeof (Executor).GetMethod("Administer")));
		}
 /// <summary>
 /// Parse <see cref="ObjectDefinitionConstants.ReplacedMethodElement"/> element and add parsed element to <paramref name="overrides"/>
 /// </summary>
 protected void ParseReplacedMethodElement(
     string name, MethodOverrides overrides, XmlElement element, ParserContext parserContext)
 {
     string methodName = GetAttributeValue(element, ObjectDefinitionConstants.ReplacedMethodNameAttribute);
     string targetReplacerObjectName = GetAttributeValue(element, ObjectDefinitionConstants.ReplacedMethodReplacerNameAttribute);
     if (StringUtils.IsNullOrEmpty(methodName))
     {
         throw new ObjectDefinitionStoreException(
             parserContext.ReaderContext.Resource, name,
             string.Format("The '{0}' attribute is required for the '{1}' element.",
                           ObjectDefinitionConstants.ReplacedMethodNameAttribute, ObjectDefinitionConstants.ReplacedMethodElement));
     }
     if (StringUtils.IsNullOrEmpty(targetReplacerObjectName))
     {
         throw new ObjectDefinitionStoreException(
             parserContext.ReaderContext.Resource, name,
             string.Format("The '{0}' attribute is required for the '{1}' element.",
                           ObjectDefinitionConstants.ReplacedMethodReplacerNameAttribute, ObjectDefinitionConstants.ReplacedMethodElement));
     }
     ReplacedMethodOverride theOverride = new ReplacedMethodOverride(methodName, targetReplacerObjectName);
     foreach (XmlNode node in this.SelectNodes(element, ObjectDefinitionConstants.ReplacedMethodArgumentTypeElement))
     {
         XmlElement argElement = (XmlElement)node;
         string match = GetAttributeValue(argElement, ObjectDefinitionConstants.ReplacedMethodArgumentTypeMatchAttribute);
         if (StringUtils.IsNullOrEmpty(match))
         {
             throw new ObjectDefinitionStoreException(
                 parserContext.ReaderContext.Resource, name,
                 string.Format("The '{0}' attribute is required for the '{1}' element.",
                               ObjectDefinitionConstants.ReplacedMethodArgumentTypeMatchAttribute, ObjectDefinitionConstants.ReplacedMethodArgumentTypeElement));
         }
         theOverride.AddTypeIdentifier(match);
     }
     overrides.Add(theOverride);
 }
		public void MatchesWithNullMethod()
		{
			ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");
            Assert.Throws<ArgumentNullException>(() => methodOverride.Matches(null));
		}
示例#15
0
        public void MatchesWithNullMethod()
        {
            ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");

            Assert.Throws <ArgumentNullException>(() => methodOverride.Matches(null));
        }
        public void ToStringWorks()
        {
            ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");

            Assert.AreEqual(typeof(ReplacedMethodOverride).Name + " for method 'Execute'; will call object 'replacer'.", methodOverride.ToString());
        }
		public void MatchesWithNullMethod()
		{
			ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");
			methodOverride.Matches(null);
		}
示例#18
0
		public void SunnyDayReplaceMethod_WithReplacerThatReturnsVoid()
		{
			RootObjectDefinition replacerDef = new RootObjectDefinition(typeof (DoNothingReplacer));

			RootObjectDefinition managerDef = new RootObjectDefinition(typeof (NewsFeedManagerThatReturnsVoid));
			ReplacedMethodOverride theOverride = new ReplacedMethodOverride("DoSomething", "replacer");
			managerDef.MethodOverrides.Add(theOverride);

			DefaultListableObjectFactory factory = new DefaultListableObjectFactory();
			factory.RegisterObjectDefinition("manager", managerDef);
			factory.RegisterObjectDefinition("replacer", replacerDef);
			NewsFeedManagerThatReturnsVoid manager = (NewsFeedManagerThatReturnsVoid) factory["manager"];
			manager.DoSomething();
		}
        public void Matches_MatchingMethodNameWithOverload()
        {
            ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");

            Assert.IsTrue(methodOverride.Matches(typeof(Executor).GetMethod("Execute", new Type[] {})));
        }
示例#20
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));
		}
        public void Matches_TotallyDifferentMethodName()
        {
            ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");

            Assert.IsFalse(methodOverride.Matches(typeof(Executor).GetMethod("Administer")));
        }
		public void ToStringWorks()
		{
			ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");
			Assert.AreEqual(typeof(ReplacedMethodOverride).Name + " for method 'Execute'; will call object 'replacer'.", methodOverride.ToString());
		}
        public void MatchesWithNullMethod()
        {
            ReplacedMethodOverride methodOverride = new ReplacedMethodOverride("Execute", "replacer");

            methodOverride.Matches(null);
        }