public void UsingCustomMarkers()
        {
            RootObjectDefinition def = new RootObjectDefinition();

            def.ObjectType = typeof(TestObject);
            ConstructorArgumentValues args = new ConstructorArgumentValues();

            args.AddNamedArgumentValue("name", "#hope.floats#");
            def.ConstructorArgumentValues = args;

            NameValueCollection properties   = new NameValueCollection();
            const string        expectedName = "Rick";

            properties.Add("hope.floats", expectedName);

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

            A.CallTo(() => mock.GetObjectDefinitionNames(false)).Returns(new string[] { "foo" });
            A.CallTo(() => mock.GetObjectDefinition(null, false)).WithAnyArguments().Returns(def);

            PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();

            cfg.PlaceholderPrefix = cfg.PlaceholderSuffix = "#";
            cfg.Properties        = properties;
            cfg.PostProcessObjectFactory(mock);

            A.CallTo(() => mock.AddEmbeddedValueResolver(null)).WithAnyArguments().MustHaveHappened();

            Assert.AreEqual(expectedName,
                            def.ConstructorArgumentValues.GetNamedArgumentValue("name").Value,
                            "Named argument placeholder value was not replaced.");
        }
        /// <summary>
        /// Apply the given properties to the supplied
        /// <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>.
        /// </summary>
        /// <param name="factory">
        /// The <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>
        /// used by the application context.
        /// </param>
        /// <param name="props">The properties to apply.</param>
        /// <exception cref="Spring.Objects.ObjectsException">
        /// If an error occured.
        /// </exception>
        protected override void ProcessProperties(IConfigurableListableObjectFactory factory, NameValueCollection props)
        {
            PlaceholderResolveHandlerAdapter resolveAdapter = new PlaceholderResolveHandlerAdapter(this, props);
            ObjectDefinitionVisitor          visitor        = new ObjectDefinitionVisitor(resolveAdapter.ParseAndResolveVariables);

            IList <string> objectDefinitionNames = factory.GetObjectDefinitionNames(includeAncestors);

            for (int i = 0; i < objectDefinitionNames.Count; ++i)
            {
                string            name       = objectDefinitionNames[i];
                IObjectDefinition definition = factory.GetObjectDefinition(name, includeAncestors);

                if (definition == null)
                {
                    logger.ErrorFormat("'{0}' can't be found in factorys'  '{1}' object definition (includeAncestor {2})",
                                       name, factory, includeAncestors);
                    continue;
                }

                try
                {
                    visitor.VisitObjectDefinition(definition);
                }
                catch (ObjectDefinitionStoreException ex)
                {
                    throw new ObjectDefinitionStoreException(
                              definition.ResourceDescription, name, ex.Message);
                }
            }

            factory.AddEmbeddedValueResolver(resolveAdapter);
        }
        public void IncludingAncestors()
        {
            const string          defName     = "foo";
            const string          placeholder = "${name}";
            MutablePropertyValues pvs         = new MutablePropertyValues();


            const string theProperty = "name";

            pvs.Add(theProperty, placeholder);
            RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), pvs);

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

            A.CallTo(() => mock.GetObjectDefinitionNames(true)).Returns(new string[] { defName });
            A.CallTo(() => mock.GetObjectDefinition(defName, true)).Returns(def);

            PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();

            cfg.IncludeAncestors = true;

            NameValueCollection defaultProperties = new NameValueCollection();
            const string        expectedName      = "Rick Evans";

            defaultProperties.Add(theProperty, expectedName);
            cfg.Properties = defaultProperties;
            cfg.PostProcessObjectFactory(mock);
            Assert.AreEqual(expectedName, def.PropertyValues.GetPropertyValue(theProperty).Value,
                            "Property placeholder value was not replaced with the resolved value.");

            A.CallTo(() => mock.AddEmbeddedValueResolver(A <IStringValueResolver> ._)).MustHaveHappened();
        }
        public void WithIgnoreUnresolvablePlaceholder()
        {
            const string          defName     = "foo";
            const string          placeholder = "${name}";
            TestObject            foo         = new TestObject(placeholder, 30);
            MutablePropertyValues pvs         = new MutablePropertyValues();

            pvs.Add("name", placeholder);
            RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), pvs);

            IConfigurableListableObjectFactory mock = mocks.StrictMock <IConfigurableListableObjectFactory>();

            Expect.Call(mock.GetObjectDefinitionNames(false)).Return(new string [] { defName });
            Expect.Call(mock.GetObjectDefinition(defName, false)).Return(def);
            Expect.Call(() => mock.AddEmbeddedValueResolver(null)).IgnoreArguments();
            mocks.ReplayAll();

            PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();

            cfg.IgnoreUnresolvablePlaceholders = true;
            cfg.PostProcessObjectFactory(mock);
            Assert.AreEqual(placeholder, foo.Name);

            mocks.VerifyAll();
        }
        public void ReplacesNamedCtorArgument()
        {
            RootObjectDefinition def = new RootObjectDefinition();

            def.ObjectType = typeof(TestObject);
            ConstructorArgumentValues args = new ConstructorArgumentValues();

            args.AddNamedArgumentValue("name", "${hope.floats}");
            def.ConstructorArgumentValues = args;

            NameValueCollection properties   = new NameValueCollection();
            const string        expectedName = "Rick";

            properties.Add("hope.floats", expectedName);

            IConfigurableListableObjectFactory mock = mocks.StrictMock <IConfigurableListableObjectFactory>();

            Expect.Call(mock.GetObjectDefinitionNames(false)).Return(new string[] { "foo" });
            Expect.Call(mock.GetObjectDefinition(null, false)).IgnoreArguments().Return(def);
            Expect.Call(delegate { mock.AddEmbeddedValueResolver(null); }).IgnoreArguments();
            mocks.ReplayAll();

            PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();

            cfg.Properties = properties;
            cfg.PostProcessObjectFactory(mock);

            mocks.VerifyAll();

            Assert.AreEqual(expectedName,
                            def.ConstructorArgumentValues.GetNamedArgumentValue("name").Value,
                            "Named argument placeholder value was not replaced.");
        }
        public void WithDefaultProperties()
        {
            const string          defName     = "foo";
            const string          placeholder = "${name}";
            MutablePropertyValues pvs         = new MutablePropertyValues();

            const string theProperty = "name";

            pvs.Add(theProperty, placeholder);
            RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), pvs);

            IConfigurableListableObjectFactory mock = mocks.StrictMock <IConfigurableListableObjectFactory>();

            Expect.Call(mock.GetObjectDefinitionNames(false)).Return(new string [] { defName });
            Expect.Call(mock.GetObjectDefinition(defName, false)).Return(def);
            Expect.Call(() => mock.AddEmbeddedValueResolver(null)).IgnoreArguments();
            mocks.ReplayAll();

            PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
            NameValueCollection           defaultProperties = new NameValueCollection();
            const string expectedName = "Rick Evans";

            defaultProperties.Add(theProperty, expectedName);
            cfg.Properties = defaultProperties;
            cfg.PostProcessObjectFactory(mock);
            Assert.AreEqual(expectedName, def.PropertyValues.GetPropertyValue(theProperty).Value,
                            "Property placeholder value was not replaced with the resolved value.");

            mocks.VerifyAll();
        }
Beispiel #7
0
        /// <summary>
        /// Apply the given properties to the supplied
        /// <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>.
        /// </summary>
        /// <param name="factory">
        /// The <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>
        /// used by the application context.
        /// </param>
        /// <param name="props">The properties to apply.</param>
        /// <exception cref="Spring.Objects.ObjectsException">
        /// If an error occured.
        /// </exception>
        protected override void ProcessProperties(IConfigurableListableObjectFactory factory, NameValueCollection props)
        {
            PlaceholderResolveHandlerAdapter resolveAdapter = new PlaceholderResolveHandlerAdapter(this, props);
            ObjectDefinitionVisitor          visitor        = new ObjectDefinitionVisitor(resolveAdapter.ParseAndResolveVariables);

            IList <string> objectDefinitionNames = factory.GetObjectDefinitionNames();

            for (int i = 0; i < objectDefinitionNames.Count; ++i)
            {
                string            name       = objectDefinitionNames[i];
                IObjectDefinition definition = factory.GetObjectDefinition(name);
                try
                {
                    visitor.VisitObjectDefinition(definition);
                }
                catch (ObjectDefinitionStoreException ex)
                {
                    throw new ObjectDefinitionStoreException(
                              definition.ResourceDescription, name, ex.Message);
                }
            }

            factory.AddEmbeddedValueResolver(resolveAdapter);
        }
        /// <summary>
		/// Apply the given properties to the supplied
		/// <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>.
		/// </summary>
		/// <param name="factory">
		/// The <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>
		/// used by the application context.
		/// </param>
		/// <param name="props">The properties to apply.</param>
		/// <exception cref="Spring.Objects.ObjectsException">
		/// If an error occured.
		/// </exception>
		protected override void ProcessProperties(IConfigurableListableObjectFactory factory, NameValueCollection props)
		{
            PlaceholderResolveHandlerAdapter resolveAdapter = new PlaceholderResolveHandlerAdapter(this, props);
            ObjectDefinitionVisitor visitor = new ObjectDefinitionVisitor(resolveAdapter.ParseAndResolveVariables);

			IList<string> objectDefinitionNames = factory.GetObjectDefinitionNames();
			for (int i = 0; i < objectDefinitionNames.Count; ++i)
			{
				string name = objectDefinitionNames[i];
				IObjectDefinition definition = factory.GetObjectDefinition(name);
				try
				{
                    visitor.VisitObjectDefinition(definition);
				}
				catch (ObjectDefinitionStoreException ex)
				{
					throw new ObjectDefinitionStoreException(
						definition.ResourceDescription, name, ex.Message);
				}
			}

            factory.AddEmbeddedValueResolver(resolveAdapter);
		}
	    /// <summary>
	    /// Apply the given properties to the supplied
	    /// <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>.
	    /// </summary>
	    /// <param name="factory">
	    /// The <see cref="Spring.Objects.Factory.Config.IConfigurableListableObjectFactory"/>
	    /// used by the application context.
	    /// </param>
	    /// <param name="props">The properties to apply.</param>
	    /// <exception cref="Spring.Objects.ObjectsException">
	    /// If an error occured.
	    /// </exception>
	    protected override void ProcessProperties(IConfigurableListableObjectFactory factory, NameValueCollection props)
	    {
	        PlaceholderResolveHandlerAdapter resolveAdapter = new PlaceholderResolveHandlerAdapter(this, props);
	        ObjectDefinitionVisitor visitor = new ObjectDefinitionVisitor(resolveAdapter.ParseAndResolveVariables);

	        IList<string> objectDefinitionNames = factory.GetObjectDefinitionNames(includeAncestors);
	        for (int i = 0; i < objectDefinitionNames.Count; ++i)
	        {
	            string name = objectDefinitionNames[i];
	            IObjectDefinition definition = factory.GetObjectDefinition(name, includeAncestors);

	            if (definition == null)
	            {
	                logger.ErrorFormat("'{0}' can't be found in factorys'  '{1}' object definition (includeAncestor {2})",
	                                   name, factory, includeAncestors);
	                continue;
	            }

	            try
	            {
	                visitor.VisitObjectDefinition(definition);
	            }
	            catch (ObjectDefinitionStoreException ex)
	            {
	                throw new ObjectDefinitionStoreException(
	                    definition.ResourceDescription, name, ex.Message);
	            }
	        }

	        factory.AddEmbeddedValueResolver(resolveAdapter);
	    }