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 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 = mocks.StrictMock <IConfigurableListableObjectFactory>();

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

            PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();

            cfg.PlaceholderPrefix = cfg.PlaceholderSuffix = "#";
            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 ChokesOnCircularReferenceToPlaceHolder()
        {
            RootObjectDefinition def = new RootObjectDefinition();

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

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

            NameValueCollection properties   = new NameValueCollection();
            const string        expectedName = "ba${foo}r";

            properties.Add("foo", expectedName);

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

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

            PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();

            cfg.Properties = properties;
            try
            {
                cfg.PostProcessObjectFactory(mock);
                Assert.Fail("Should have raised an ObjectDefinitionStoreException by this point.");
            }
            catch (ObjectDefinitionStoreException)
            {
            }
            mocks.VerifyAll();
        }
        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 = mocks.StrictMock <IConfigurableListableObjectFactory>();

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

            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.");

            mocks.VerifyAll();
        }
        public void MismatchBetweenNumberOfConfigNamesAndNumberOfLocations()
        {
            PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();

            cfg.Locations      = new IResource[] { mocks.StrictMock <IResource>() }; // will never get to the point where we check the validity
            cfg.ConfigSections = new string[] { "", "" };
            Assert.Throws <ObjectInitializationException>(() => cfg.PostProcessObjectFactory((IConfigurableListableObjectFactory)mocks.DynamicMock(typeof(IConfigurableListableObjectFactory))));
        }
        public void ViaXMLAndConfigSection()
        {
            IResource        resource         = new ReadOnlyXmlTestResource("PropertyResourceConfigurerTests.xml", GetType());
            XmlObjectFactory xbf              = new XmlObjectFactory(resource);
            PropertyPlaceholderConfigurer ppc = (PropertyPlaceholderConfigurer)xbf.GetObject("ConfigSectionPlaceholderConfigurer");

            Assert.IsNotNull(ppc);
            ppc.PostProcessObjectFactory(xbf);

            Assert.AreEqual("name from section", ((TestObject)xbf.GetObject("Test3")).Name);
            Assert.AreEqual("name from sectiongroup/section", ((TestObject)xbf.GetObject("Test4")).Name);
        }
        public void ViaXML()
        {
            IResource        resource         = new ReadOnlyXmlTestResource("PropertyResourceConfigurerTests.xml", GetType());
            XmlObjectFactory xbf              = new XmlObjectFactory(resource);
            PropertyPlaceholderConfigurer ppc = (PropertyPlaceholderConfigurer)xbf.GetObject("PlaceholderConfigurer");

            Assert.IsNotNull(ppc);
            ppc.PostProcessObjectFactory(xbf);
            TestObject to = (TestObject)xbf.GetObject("Test1");

            Assert.AreEqual("A DefName", to.Name);
        }
        public void OneConfigNameIsOKForLotsOfLocations()
        {
            PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
            IResource mock = mocks.StrictMock <IResource>();

            Expect.Call(mock.Exists).Return(true);
            Expect.Call(mock.InputStream).Throw(new FileNotFoundException());
            mocks.ReplayAll();

            cfg.Locations      = new IResource [] { mock };
            cfg.ConfigSections = new string[] { "" };
            Assert.Throws <ObjectsException>(() => cfg.PostProcessObjectFactory((IConfigurableListableObjectFactory)mocks.DynamicMock(typeof(IConfigurableListableObjectFactory))));
        }
        public void ChokesOnBadResourceLocationIfIgnoreBadResourcesFlagNotSetToTrue()
        {
            PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();

            cfg.IgnoreResourceNotFound = false;
            IResource mock = mocks.StrictMock <IResource>();

            Expect.Call(mock.Exists).Return(false);
            mocks.ReplayAll();

            cfg.Locations      = new IResource [] { mock };
            cfg.ConfigSections = new string[] { "" };
            Assert.Throws <ObjectInitializationException>(() => cfg.PostProcessObjectFactory((IConfigurableListableObjectFactory)mocks.DynamicMock(typeof(IConfigurableListableObjectFactory))));
        }
        public void DoesNotChokeOnBadResourceLocationIfIgnoreBadResourcesFlagSetToTrue()
        {
            PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();

            cfg.IgnoreResourceNotFound = true;
            IResource mockResource = mocks.StrictMock <IResource>();

            Expect.Call(mockResource.Exists).Return(false);
            cfg.Location       = mockResource;
            cfg.ConfigSections = new string[] { "" };
            IConfigurableListableObjectFactory mockFactory = (IConfigurableListableObjectFactory)mocks.DynamicMock(typeof(IConfigurableListableObjectFactory));

            Expect.Call(mockFactory.GetObjectDefinitionNames(false)).Return(new string[] {});
            mocks.ReplayAll();

            cfg.PostProcessObjectFactory(mockFactory);

            mocks.VerifyAll();
        }