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 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.");
        }
Exemple #3
0
        public void ValueHolderToStringsNicely()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            values.AddGenericArgumentValue(1, typeof(int).FullName);
            ConstructorArgumentValues.ValueHolder vh = values.GetGenericArgumentValue(typeof(int));
            Assert.AreEqual("'1' [System.Int32]", vh.ToString());
        }
Exemple #4
0
        public void AddGenericArgumentValue()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            values.AddGenericArgumentValue(DBNull.Value);
            Assert.IsFalse(values.Empty, "Added one value, but the collection is sayin' it's empty.");
            Assert.AreEqual(1, values.ArgumentCount, "Added one value, but the collection ain't sayin' that it's got a single element in it.");
            Assert.AreEqual(1, values.GenericArgumentValues.Count, "Added one generic value, but the collection of indexed values ain't sayin' that it's got a single element in it.");
        }
Exemple #5
0
        public void Instantiation()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            Assert.IsNotNull(values.GenericArgumentValues, "The 'GenericArgumentValues' property was not initialised.");
            Assert.IsNotNull(values.IndexedArgumentValues, "The 'IndexedArgumentValues' property was not initialised.");
            Assert.IsNotNull(values.NamedArgumentValues, "The 'NamedArgumentValues' property was not initialised.");
            Assert.AreEqual(0, values.ArgumentCount, "There were some arguments in a newly initialised instance.");
            Assert.IsTrue(values.Empty, "A newly initialised instance was not initially empty.");
        }
Exemple #6
0
        public void AddNamedArgument()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            values.AddNamedArgumentValue("foo", "sball");
            Assert.AreEqual(1, values.NamedArgumentValues.Count, "Added one named argument but it doesn't seem to have been added to the named arguments collection.");
            Assert.AreEqual(1, values.ArgumentCount, "Added one named argument but it doesn't seem to be reflected in the overall argument count.");
            ConstructorArgumentValues.ValueHolder arg = values.GetNamedArgumentValue("foo");
            Assert.IsNotNull(arg, "The named argument previously added could not be pulled from the ctor arg collection.");
            Assert.AreEqual("sball", arg.Value, "The value of the named argument passed in is not the same as the one that was pulled out.");
        }
Exemple #7
0
        public void AddRangeOfIndexedArgumentValues()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            values.AddIndexedArgumentValue(1, DBNull.Value);
            values.AddIndexedArgumentValue(2, "Foo");
            values.AddIndexedArgumentValue(3, 3);
            new ConstructorArgumentValues(values);
            Assert.IsFalse(values.Empty, "Added three indexed values(as a range), but the collection is sayin' it's empty.");
            Assert.AreEqual(3, values.ArgumentCount, "Added three indexed values(as a range), but the collection ain't sayin' that it's got 3 elements in it.");
            Assert.AreEqual(3, values.IndexedArgumentValues.Count, "Added three indexed values(as a range), but the collection of indexed values ain't sayin' that it's got 3 elements in it.");
        }
Exemple #8
0
        public void GetGeneric_Untyped_ArgumentValue()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();
            const string expectedValue       = "Rick";

            values.AddGenericArgumentValue(expectedValue);

            ConstructorArgumentValues.ValueHolder name = values.GetGenericArgumentValue(null, null);
            Assert.IsNotNull(name,
                             "Must get non-null valueholder back if no required type is specified.");
            Assert.AreEqual(expectedValue, name.Value);
        }
Exemple #9
0
        public void GetGenericArgumentValue()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            Assert.IsNull(values.GetGenericArgumentValue(typeof(object)), "Mmm... managed to get a non null instance back from an empty instance.");
            values.AddGenericArgumentValue(DBNull.Value, typeof(DBNull).FullName);
            Assert.IsNull(values.GetGenericArgumentValue(typeof(string)), "Mmm... managed to get a non null instance back from an instance that should have now't with the specified Type.");
            ConstructorArgumentValues.ValueHolder value =
                values.GetGenericArgumentValue(typeof(DBNull));
            Assert.IsNotNull(value, "Stored a value of a specified Type, but got null when retrieving it using said Type.");
            Assert.AreSame(DBNull.Value, value.Value, "The value stored at the specified index was not the exact same instance as was added.");
        }
Exemple #10
0
        public void GetGeneric_Untyped_ArgumentValueWithOnlyStronglyTypedValuesInTheCtorValueList()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();
            const string expectedValue       = "Rick";

            values.AddGenericArgumentValue(expectedValue, typeof(string).FullName);

            ConstructorArgumentValues.ValueHolder name = values.GetGenericArgumentValue(null, null);
            Assert.IsNull(name,
                          "Must get null valueholder back if no required type is specified but only " +
                          "strongly typed values are present in the ctor values list.");
        }
Exemple #11
0
        public void NamedArgumentsAreCaseInsensitive()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            values.AddNamedArgumentValue("foo", "sball");
            Assert.AreEqual(1, values.NamedArgumentValues.Count, "Added one named argument but it doesn't seem to have been added to the named arguments collection.");
            Assert.AreEqual(1, values.ArgumentCount, "Added one named argument but it doesn't seem to be reflected in the overall argument count.");
            Assert.IsTrue(values.ContainsNamedArgument("FOo"), "Mmm, the ContainsNamedArgument() method eveidently IS case sensitive (which is wrong).");
            ConstructorArgumentValues.ValueHolder arg = values.GetNamedArgumentValue("fOo");
            Assert.IsNotNull(arg, "The named argument previously added could not be pulled from the ctor arg collection.");
            Assert.AreEqual("sball", arg.Value, "The value of the named argument passed in is not the same as the one that was pulled out.");
        }
Exemple #12
0
        public void AddAllFromOther()
        {
            ConstructorArgumentValues other = new ConstructorArgumentValues();

            other.AddIndexedArgumentValue(1, DBNull.Value);
            other.AddIndexedArgumentValue(2, "Foo");
            other.AddIndexedArgumentValue(3, 3);

            ConstructorArgumentValues values = new ConstructorArgumentValues();

            values.AddAll(other);
            Assert.AreEqual(other.ArgumentCount, values.ArgumentCount,
                            "Must have been the same since one was filled up with the values in the other.");
        }
        /// <summary>
        /// Traverse the given ObjectDefinition object and the MutablePropertyValues
        /// and ConstructorArgumentValues contained in them.
        /// </summary>
        /// <param name="definition">The object definition to traverse.</param>
        public virtual void VisitObjectDefinition(IObjectDefinition definition)
        {
            VisitObjectTypeName(definition);
            VisitPropertyValues(definition);

            ConstructorArgumentValues cas = definition.ConstructorArgumentValues;

            if (cas != null)
            {
                VisitIndexedArgumentValues(cas.IndexedArgumentValues);
                VisitNamedArgumentValues(cas.NamedArgumentValues);
                VisitGenericArgumentValues(cas.GenericArgumentValues);
            }
        }
Exemple #14
0
        public void GetIndexedArgumentValue()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            Assert.IsNull(values.GetIndexedArgumentValue(0, typeof(object)), "Mmm... managed to get a non null instance back from an empty instance.");
            values.AddIndexedArgumentValue(16, DBNull.Value, typeof(DBNull).FullName);
            Assert.IsNull(values.GetIndexedArgumentValue(0, typeof(object)), "Mmm... managed to get a non null instance back from an instance that should have now't at the specified index.");
            ConstructorArgumentValues.ValueHolder value =
                values.GetIndexedArgumentValue(16, typeof(DBNull));
            Assert.IsNotNull(value, "Stored a value at a specified index, but got null when retrieving it.");
            Assert.AreSame(DBNull.Value, value.Value, "The value stored at the specified index was not the exact same instance as was added.");
            ConstructorArgumentValues.ValueHolder wrongValue =
                values.GetIndexedArgumentValue(16, typeof(string));
            Assert.IsNull(wrongValue, "Stored a value at a specified index, and got it (or rather something) back when retrieving it with the wrong Type specified.");
        }
Exemple #15
0
        public void GetArgumentValue()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            Assert.IsNull(values.GetArgumentValue(0, typeof(object)), "Mmm... managed to get a non null instance back from an empty instance.");
            values.AddGenericArgumentValue(DBNull.Value, typeof(DBNull).FullName);
            values.AddNamedArgumentValue("foo", DBNull.Value);
            values.AddIndexedArgumentValue(16, DBNull.Value, typeof(DBNull).FullName);
            Assert.IsNull(values.GetArgumentValue(100, typeof(string)), "Mmm... managed to get a non null instance back from an instance that should have now't with the specified Type.");
            ConstructorArgumentValues.ValueHolder value =
                values.GetArgumentValue(-3, typeof(DBNull));
            Assert.IsNotNull(value, "Stored a value of a specified Type at a specified index, but got null when retrieving it using the wrong index but the correct Type.");
            Assert.AreSame(DBNull.Value, value.Value, "The retrieved value was not the exact same instance as was added.");

            value = values.GetArgumentValue("foo", typeof(DBNull));
            Assert.IsNotNull(value, "Stored a value of a specified Type under a name, but got null when retrieving it using the wrong name but the correct Type.");
            Assert.AreSame(DBNull.Value, value.Value, "The retrieved value was not the exact same instance as was added.");
        }
 /// <summary>
 /// Copy all given argument values into this object.
 /// </summary>
 /// <param name="other">
 /// The <see cref="Oragon.Spring.Objects.Factory.Config.ConstructorArgumentValues"/>
 /// to be used to populate this instance.
 /// </param>
 public void AddAll(ConstructorArgumentValues other)
 {
     if (other != null)
     {
         foreach (ValueHolder o in other.GenericArgumentValues)
         {
             GenericArgumentValues.Add(o);
         }
         foreach (KeyValuePair <int, ValueHolder> entry in other.IndexedArgumentValues)
         {
             ValueHolder vh = entry.Value;
             if (vh != null)
             {
                 AddOrMergeIndexedArgumentValues(entry.Key, vh.Copy());
             }
         }
         foreach (KeyValuePair <string, object> entry in other.NamedArgumentValues)
         {
             AddOrMergeNamedArgumentValues(entry.Key, entry.Value);
             //NamedArgumentValues.Add(entry.Key, entry.Value);
         }
     }
 }
Exemple #17
0
        public void GetGenericArgumentValueIgnoresAlreadyUsedValues()
        {
            ISet used = new ListSet();

            ConstructorArgumentValues values = new ConstructorArgumentValues();

            values.AddGenericArgumentValue(1);
            values.AddGenericArgumentValue(2);
            values.AddGenericArgumentValue(3);

            Type intType = typeof(int);

            ConstructorArgumentValues.ValueHolder one = values.GetGenericArgumentValue(intType, used);
            Assert.AreEqual(1, one.Value);
            used.Add(one);
            ConstructorArgumentValues.ValueHolder two = values.GetGenericArgumentValue(intType, used);
            Assert.AreEqual(2, two.Value);
            used.Add(two);
            ConstructorArgumentValues.ValueHolder three = values.GetGenericArgumentValue(intType, used);
            Assert.AreEqual(3, three.Value);
            used.Add(three);
            ConstructorArgumentValues.ValueHolder four = values.GetGenericArgumentValue(intType, used);
            Assert.IsNull(four);
        }
 /// <summary>
 /// Creates a new instance of the
 /// <see cref="Oragon.Spring.Objects.Factory.Config.ConstructorArgumentValues"/>
 /// class.
 /// </summary>
 /// <param name="other">
 /// The <see cref="Oragon.Spring.Objects.Factory.Config.ConstructorArgumentValues"/>
 /// to be used to populate this instance.
 /// </param>
 public ConstructorArgumentValues(ConstructorArgumentValues other)
 {
     AddAll(other);
 }
Exemple #19
0
        public void AddNamedArgumentWithEmptyStringName()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            Assert.Throws <ArgumentNullException>(() => values.AddNamedArgumentValue(string.Empty, 1));
        }
Exemple #20
0
        public void AddNamedArgumentWithWhitespaceStringName()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            Assert.Throws <ArgumentNullException>(() => values.AddNamedArgumentValue(Environment.NewLine + "  ", 1));
        }
        public void SunnyDay()
        {
            StaticApplicationContext ac = new StaticApplicationContext();


            MutablePropertyValues pvs = new MutablePropertyValues();

            pvs.Add("age", "${age}");
            RootObjectDefinition def
                = new RootObjectDefinition("${fqn}", new ConstructorArgumentValues(), pvs);

            ac.RegisterObjectDefinition("tb3", def);



            pvs = new MutablePropertyValues();
            pvs.Add("age", "${age}");
            pvs.Add("name", "name${var}${");
            pvs.Add("spouse", new RuntimeObjectReference("${ref}"));
            ac.RegisterSingleton("tb1", typeof(TestObject), pvs);

            ConstructorArgumentValues cas = new ConstructorArgumentValues();

            cas.AddIndexedArgumentValue(1, "${age}");
            cas.AddGenericArgumentValue("${var}name${age}");

            pvs = new MutablePropertyValues();
            ArrayList friends = new ManagedList();

            friends.Add("na${age}me");
            friends.Add(new RuntimeObjectReference("${ref}"));
            pvs.Add("friends", friends);

            ISet someSet = new ManagedSet();

            someSet.Add("na${age}me");
            someSet.Add(new RuntimeObjectReference("${ref}"));
            pvs.Add("someSet", someSet);

            IDictionary someDictionary = new ManagedDictionary();

            someDictionary["key1"] = new RuntimeObjectReference("${ref}");
            someDictionary["key2"] = "${age}name";
            MutablePropertyValues innerPvs = new MutablePropertyValues();

            someDictionary["key3"] = new RootObjectDefinition(typeof(TestObject), innerPvs);
            someDictionary["key4"] = new ChildObjectDefinition("tb1", innerPvs);
            pvs.Add("someMap", someDictionary);

            RootObjectDefinition definition = new RootObjectDefinition(typeof(TestObject), cas, pvs);

            ac.DefaultListableObjectFactory.RegisterObjectDefinition("tb2", definition);

            pvs = new MutablePropertyValues();
            pvs.Add("Properties", "<spring-config><add key=\"age\" value=\"98\"/><add key=\"var\" value=\"${m}var\"/><add key=\"ref\" value=\"tb2\"/><add key=\"m\" value=\"my\"/><add key=\"fqn\" value=\"Oragon.Spring.Objects.TestObject, Oragon.Spring.Core.Tests\"/></spring-config>");
            ac.RegisterSingleton("configurer", typeof(PropertyPlaceholderConfigurer), pvs);
            ac.Refresh();

            TestObject tb1 = (TestObject)ac.GetObject("tb1");
            TestObject tb2 = (TestObject)ac.GetObject("tb2");
            TestObject tb3 = (TestObject)ac.GetObject("tb3");

            Assert.AreEqual(98, tb1.Age);
            Assert.AreEqual(98, tb2.Age);
            Assert.AreEqual(98, tb3.Age);
            Assert.AreEqual("namemyvar${", tb1.Name);
            Assert.AreEqual("myvarname98", tb2.Name);
            Assert.AreEqual(tb2, tb1.Spouse);
            Assert.AreEqual(2, tb2.Friends.Count);
            IEnumerator ie = tb2.Friends.GetEnumerator();

            ie.MoveNext();
            Assert.AreEqual("na98me", ie.Current);
            ie.MoveNext();
            Assert.AreEqual(tb2, ie.Current);
            Assert.AreEqual(2, tb2.SomeSet.Count);
            Assert.IsTrue(tb2.SomeSet.Contains("na98me"));
            Assert.IsTrue(tb2.SomeSet.Contains(tb2));
            Assert.AreEqual(4, tb2.SomeMap.Count);
            Assert.AreEqual(tb2, tb2.SomeMap["key1"]);
            Assert.AreEqual("98name", tb2.SomeMap["key2"]);
            TestObject inner1 = (TestObject)tb2.SomeMap["key3"];
            TestObject inner2 = (TestObject)tb2.SomeMap["key4"];

            Assert.AreEqual(0, inner1.Age);
            Assert.AreEqual(null, inner1.Name);
            Assert.AreEqual(98, inner2.Age);
            Assert.AreEqual("namemyvar${", inner2.Name);
        }
Exemple #22
0
        public void AddAllDoesntChokeOnNullArgument()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            values.AddAll(null);
        }
Exemple #23
0
        public void AddNamedArgumentWithNullName()
        {
            ConstructorArgumentValues values = new ConstructorArgumentValues();

            Assert.Throws <ArgumentNullException>(() => values.AddNamedArgumentValue(null, 1));
        }