private void InitFactory(DefaultListableObjectFactory factory)
        {
            Console.WriteLine("init factory");
            RootObjectDefinition tee = new RootObjectDefinition(typeof(Tee), true);
            tee.IsLazyInit = true;
            ConstructorArgumentValues teeValues = new ConstructorArgumentValues();
            teeValues.AddGenericArgumentValue("test");
            tee.ConstructorArgumentValues = teeValues;

            RootObjectDefinition bar = new RootObjectDefinition(typeof(BBar), false);
            ConstructorArgumentValues barValues = new ConstructorArgumentValues();
            barValues.AddGenericArgumentValue(new RuntimeObjectReference("tee"));
            barValues.AddGenericArgumentValue(5);
            bar.ConstructorArgumentValues = barValues;

            RootObjectDefinition foo = new RootObjectDefinition(typeof(FFoo), false);
            MutablePropertyValues fooValues = new MutablePropertyValues();
            fooValues.Add("i", 5);
            fooValues.Add("bar", new RuntimeObjectReference("bar"));
            fooValues.Add("copy", new RuntimeObjectReference("bar"));
            fooValues.Add("s", "test");
            foo.PropertyValues = fooValues;

            factory.RegisterObjectDefinition("foo", foo);
            factory.RegisterObjectDefinition("bar", bar);
            factory.RegisterObjectDefinition("tee", tee);
        }
        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());
        }
        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.");
        }
		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);
		}
        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.");
        }
        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);
        }
        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.");
        }
		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);
		}
        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);
        }
        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.");
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Creates an error message action based on the specified message element.
        /// </summary>
        /// <param name="message">The message element.</param>
        /// <param name="parserContext">The parser helper.</param>
        /// <returns>The error message action definition.</returns>
        private static IObjectDefinition ParseErrorMessageAction(XmlElement message, ParserContext parserContext)
        {
            string messageId = GetAttributeValue(message, MessageConstants.IdAttribute);
            string[] providers = GetAttributeValue(message, MessageConstants.ProvidersAttribute).Split(',');
            ArrayList parameters = new ArrayList();

            foreach (XmlElement param in message.ChildNodes)
            {
                IExpression paramExpression = Expression.Parse(GetAttributeValue(param, MessageConstants.ParameterValueAttribute));
                parameters.Add(paramExpression);
            }

            string typeName = "Spring.Validation.Actions.ErrorMessageAction, Spring.Core";
            ConstructorArgumentValues ctorArgs = new ConstructorArgumentValues();
            ctorArgs.AddGenericArgumentValue(messageId);
            ctorArgs.AddGenericArgumentValue(providers);

            string when = GetAttributeValue(message, ValidatorDefinitionConstants.WhenAttribute);
            MutablePropertyValues properties = new MutablePropertyValues();
            if (StringUtils.HasText(when))
            {
                properties.Add("When", when);
            }
            if (parameters.Count > 0)
            {
                properties.Add("Parameters", parameters.ToArray(typeof(IExpression)));
            }

            IConfigurableObjectDefinition action =
                parserContext.ReaderContext.ObjectDefinitionFactory.CreateObjectDefinition(typeName, null, parserContext.ReaderContext.Reader.Domain);
            action.ConstructorArgumentValues = ctorArgs;
            action.PropertyValues = properties;

            return action;
        }
		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.");
		}
		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.");
		}
		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.");
		}
        /// <summary>
        /// Parse a constructor-arg element.
        /// </summary>
        /// <param name="name">
        /// The name of the object (definition) associated with the ctor arg.
        /// </param>
        /// <param name="arguments">
        /// The list of constructor args associated with the object (definition).
        /// </param>
        /// <param name="element">
        /// The name of the element containing the ctor arg definition.
        /// </param>
        /// <param name="parserContext">
        /// The namespace-aware parser.
        /// </param>
        protected virtual void ParseConstructorArgElement(
            string name, ConstructorArgumentValues arguments, XmlElement element, ParserContext parserContext)
        {
            object val = ParsePropertyValue(element, name, parserContext);
            string indexAttr = GetAttributeValue(element, ObjectDefinitionConstants.IndexAttribute);
            string typeAttr = GetAttributeValue(element, ObjectDefinitionConstants.TypeAttribute);
            string nameAttr = GetAttributeValue(element, ObjectDefinitionConstants.ArgumentNameAttribute);

            // only one of the 'index' or 'name' attributes can be present
            if (StringUtils.HasText(indexAttr)
                && StringUtils.HasText(nameAttr))
            {
                throw new ObjectDefinitionStoreException(
                    parserContext.ReaderContext.Resource, name,
                    "Only one of the 'index' or 'name' attributes can be present per constructor argument.");
            }
            if (StringUtils.HasText(indexAttr))
            {
                try
                {
                    int index = int.Parse(indexAttr, CultureInfo.CurrentCulture);
                    if (index < 0)
                    {
                        throw new ObjectDefinitionStoreException(
                            parserContext.ReaderContext.Resource, name,
                            "'index' cannot be lower than 0");
                    }
                    if (StringUtils.HasText(typeAttr))
                    {
                        arguments.AddIndexedArgumentValue(index, val, typeAttr);
                    }
                    else
                    {
                        arguments.AddIndexedArgumentValue(index, val);
                    }
                }
                catch (FormatException)
                {
                    throw new ObjectDefinitionStoreException(
                        parserContext.ReaderContext.Resource, name,
                        "Attribute 'index' of tag 'constructor-arg' must be an integer value.");
                }
            }
            else if (StringUtils.HasText(nameAttr))
            {
                if (StringUtils.HasText(typeAttr))
                {
                    if (log.IsWarnEnabled)
                    {
                        log.Warn("The 'type' attribute is redundant when the 'name' attribute has been used on a constructor argument element.");
                    }
                }
                arguments.AddNamedArgumentValue(nameAttr, val);
            }
            else
            {
                if (StringUtils.HasText(typeAttr))
                {
                    arguments.AddGenericArgumentValue(val, typeAttr);
                }
                else
                {
                    arguments.AddGenericArgumentValue(val);
                }
            }
        }
        /// <summary>
        /// Resolves the <see cref="Spring.Objects.Factory.Config.ConstructorArgumentValues"/>
        /// of the supplied <paramref name="definition"/>.
        /// </summary>
        /// <param name="objectName">The name of the object that is being resolved by this factory.</param>
        /// <param name="definition">The rod.</param>
        /// <param name="wrapper">The wrapper.</param>
        /// <param name="cargs">The cargs.</param>
        /// <param name="resolvedValues">Where the resolved constructor arguments will be placed.</param>
        /// <returns>
        /// The minimum number of arguments that any constructor for the supplied
        /// <paramref name="definition"/> must have.
        /// </returns>
        /// <remarks>
        /// 	<p>
        /// 'Resolve' can be taken to mean that all of the <paramref name="definition"/>s
        /// constructor arguments is resolved into a concrete object that can be plugged
        /// into one of the <paramref name="definition"/>s constructors. Runtime object
        /// references to other objects in this (or a parent) factory are resolved,
        /// type conversion is performed, etc.
        /// </p>
        /// 	<p>
        /// These resolved values are plugged into the supplied
        /// <paramref name="resolvedValues"/> object, because we wouldn't want to touch
        /// the <paramref name="definition"/>s constructor arguments in case it (or any of
        /// its constructor arguments) is a prototype object definition.
        /// </p>
        /// 	<p>
        /// This method is also used for handling invocations of static factory methods.
        /// </p>
        /// </remarks>
        private int ResolveConstructorArguments(string objectName, RootObjectDefinition definition, ObjectWrapper wrapper,
                                                ConstructorArgumentValues cargs,
                                                ConstructorArgumentValues resolvedValues)
        {
//            ObjectDefinitionValueResolver valueResolver = new ObjectDefinitionValueResolver(objectFactory);
            int minNrOfArgs = cargs.ArgumentCount;

            foreach (KeyValuePair<int, ConstructorArgumentValues.ValueHolder> entry in cargs.IndexedArgumentValues)
            {
                int index = Convert.ToInt32(entry.Key);
                if (index < 0)
                {
                    throw new ObjectCreationException(definition.ResourceDescription, objectName,
                                                      "Invalid constructor agrument index: " + index);
                }
                if (index > minNrOfArgs)
                {
                    minNrOfArgs = index + 1;
                }
                ConstructorArgumentValues.ValueHolder valueHolder = entry.Value;
                string argName = "constructor argument with index " + index;
                object resolvedValue =
                    valueResolver.ResolveValueIfNecessary(objectName, definition, argName, valueHolder.Value);
                resolvedValues.AddIndexedArgumentValue(index, resolvedValue,
                                                       StringUtils.HasText(valueHolder.Type)
                                                           ? TypeResolutionUtils.ResolveType(valueHolder.Type).
                                                                 AssemblyQualifiedName
                                                           : null);
            }

            foreach (ConstructorArgumentValues.ValueHolder valueHolder in definition.ConstructorArgumentValues.GenericArgumentValues)
            {
                string argName = "constructor argument";
                object resolvedValue =
                    valueResolver.ResolveValueIfNecessary(objectName, definition, argName, valueHolder.Value);
                resolvedValues.AddGenericArgumentValue(resolvedValue,
                                                       StringUtils.HasText(valueHolder.Type)
                                                           ? TypeResolutionUtils.ResolveType(valueHolder.Type).
                                                                 AssemblyQualifiedName
                                                           : null);
            }
            foreach (KeyValuePair<string, object> namedArgumentEntry in definition.ConstructorArgumentValues.NamedArgumentValues)
            {
                string argumentName = namedArgumentEntry.Key;
                string syntheticArgumentName = "constructor argument with name " + argumentName;
                ConstructorArgumentValues.ValueHolder valueHolder =
                    (ConstructorArgumentValues.ValueHolder)namedArgumentEntry.Value;
                object resolvedValue =
                    valueResolver.ResolveValueIfNecessary(objectName, definition, syntheticArgumentName, valueHolder.Value);
                resolvedValues.AddNamedArgumentValue(argumentName, resolvedValue);
            }
            return minNrOfArgs;
        }
        public void DoubleBooleanAutowire()
        {
            RootObjectDefinition def = new RootObjectDefinition(typeof(DoubleBooleanConstructorObject));
            ConstructorArgumentValues args = new ConstructorArgumentValues();
            args.AddGenericArgumentValue(true, "bool");
            def.ConstructorArgumentValues = args;
            def.AutowireMode = AutoWiringMode.Constructor;
            def.IsSingleton = true;

            DefaultListableObjectFactory fac = new DefaultListableObjectFactory();
            fac.RegisterObjectDefinition("foo", def);

            fac.GetObject("foo");
        }
        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=\"Spring.Objects.TestObject, 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);
        }
		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());
		}
		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.");
		}
        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=\"Spring.Objects.TestObject, 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);
        }
Ejemplo n.º 22
0
        private IObjectDefinition ParseExceptionAction(XmlElement element, ParserContext parserContext)
        {
            string typeName = "Spring.Validation.Actions.ExceptionAction, Spring.Core";
            string throwExpression = GetAttributeValue(element, ValidatorDefinitionConstants.ThrowAttribute);

            
            ConstructorArgumentValues ctorArgs = new ConstructorArgumentValues();
            ctorArgs.AddGenericArgumentValue(throwExpression);

            string when = GetAttributeValue(element, ValidatorDefinitionConstants.WhenAttribute);
            MutablePropertyValues properties = new MutablePropertyValues();
            if (StringUtils.HasText(when))
            {
                properties.Add("When", when);
            }

            IConfigurableObjectDefinition action =
                parserContext.ReaderContext.ObjectDefinitionFactory.CreateObjectDefinition(typeName, null, parserContext.ReaderContext.Reader.Domain);
            action.ConstructorArgumentValues = ctorArgs;
            action.PropertyValues = properties;

            return action;
        }
        public void DoubleBooleanAutowire()
        {
            RootObjectDefinition def = new RootObjectDefinition(typeof(DoubleBooleanConstructorObject));
            ConstructorArgumentValues args = new ConstructorArgumentValues();
            args.AddGenericArgumentValue(true, "bool");
            def.ConstructorArgumentValues = args;
            def.AutowireMode = AutoWiringMode.Constructor;
            def.IsSingleton = true;

            DefaultListableObjectFactory fac = new DefaultListableObjectFactory();
            fac.RegisterObjectDefinition("foo", def);

            Assert.Throws<UnsatisfiedDependencyException>(() => fac.GetObject("foo"), 
                "Error creating object with name 'foo' : Unsatisfied dependency " +
                "expressed through constructor argument with index 1 of type [System.Boolean] : " +
                "No unique object of type [System.Boolean] is defined : Unsatisfied dependency of type [System.Boolean]: expected at least 1 matching object to wire the [b2] parameter on the constructor of object [foo]");
        }