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 = (IConfigurableListableObjectFactory) mocks.CreateMock(typeof (IConfigurableListableObjectFactory)); Expect.Call(mock.GetObjectDefinitionNames()).Return(new string[] {"foo"}); Expect.Call(mock.GetObjectDefinition(null)).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 = (IConfigurableListableObjectFactory)mocks.CreateMock(typeof(IConfigurableListableObjectFactory)); Expect.Call(mock.GetObjectDefinitionNames()).Return(new string[] { "foo" }); Expect.Call(mock.GetObjectDefinition(null)).IgnoreArguments().Return(def); 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 = 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.Properties = properties; try { cfg.PostProcessObjectFactory(mock); Assert.Fail("Should have raised an ObjectDefinitionStoreException by this point."); } catch (ObjectDefinitionStoreException) { } }
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."); }
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 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 = (IConfigurableListableObjectFactory)mocks.CreateMock(typeof(IConfigurableListableObjectFactory)); Expect.Call(mock.GetObjectDefinitionNames()).Return(new string[] { "foo" }); Expect.Call(mock.GetObjectDefinition(null)).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 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."); }
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."); }
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 GetArgumentValueIgnoresAlreadyUsedValues() { ISet used = new ListSet(); ConstructorArgumentValues values = new ConstructorArgumentValues(); values.AddGenericArgumentValue(1); values.AddNamedArgumentValue("2", 2); values.AddIndexedArgumentValue(3, 3); Type intType = typeof(int); ConstructorArgumentValues.ValueHolder one = values.GetArgumentValue(10, string.Empty, intType, used); Assert.AreEqual(1, one.Value); used.Add(one); ConstructorArgumentValues.ValueHolder two = values.GetArgumentValue(10, "2", intType, used); Assert.AreEqual(2, two.Value); used.Add(two); ConstructorArgumentValues.ValueHolder three = values.GetArgumentValue(3, string.Empty, intType, used); Assert.AreEqual(3, three.Value); used.Add(three); ConstructorArgumentValues.ValueHolder four = values.GetArgumentValue(10, string.Empty, intType, used); Assert.IsNull(four); }
public void CreateObjectWithMixOfNamedAndIndexedCtorArguments() { string expectedName = "Bingo"; int expectedAge = 1023; ConstructorArgumentValues values = new ConstructorArgumentValues(); values.AddNamedArgumentValue("age", expectedAge); values.AddIndexedArgumentValue(0, expectedName); RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), values, new MutablePropertyValues()); DefaultListableObjectFactory fac = new DefaultListableObjectFactory(); fac.RegisterObjectDefinition("foo", def); ITestObject foo = fac["foo"] as ITestObject; Assert.IsNotNull(foo, "Couldn't pull manually registered instance out of the factory."); Assert.AreEqual(expectedName, foo.Name, "Dependency 'name' was not resolved an indexed ctor arg."); Assert.AreEqual(expectedAge, foo.Age, "Dependency 'age' was not resolved using a named ctor arg."); }
public void CreateObjectWithMixOfNamedAndIndexedAndAutowiredCtorArguments() { string expectedCompany = "Griffin's Foosball Arcade"; MutablePropertyValues autoProps = new MutablePropertyValues(); autoProps.Add(new PropertyValue("Company", expectedCompany)); RootObjectDefinition autowired = new RootObjectDefinition(typeof(NestedTestObject), autoProps); string expectedName = "Bingo"; int expectedAge = 1023; ConstructorArgumentValues values = new ConstructorArgumentValues(); values.AddNamedArgumentValue("age", expectedAge); values.AddIndexedArgumentValue(0, expectedName); RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), values, new MutablePropertyValues()); def.AutowireMode = AutoWiringMode.Constructor; DefaultListableObjectFactory fac = new DefaultListableObjectFactory(); fac.RegisterObjectDefinition("foo", def); fac.RegisterObjectDefinition("doctor", autowired); ITestObject foo = fac["foo"] as ITestObject; Assert.IsNotNull(foo, "Couldn't pull manually registered instance out of the factory."); Assert.AreEqual(expectedName, foo.Name, "Dependency 'name' was not resolved an indexed ctor arg."); Assert.AreEqual(expectedAge, foo.Age, "Dependency 'age' was not resolved using a named ctor arg."); Assert.AreEqual(expectedCompany, foo.Doctor.Company, "Dependency 'doctor.Company' was not resolved using autowiring."); }
public void AddNamedArgumentWithNullName() { ConstructorArgumentValues values = new ConstructorArgumentValues(); Assert.Throws<ArgumentNullException>(() => values.AddNamedArgumentValue(null, 1)); }
public void CreateObjectWithCtorArgsOverrided() { using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory()) { ConstructorArgumentValues arguments = new ConstructorArgumentValues(); arguments.AddNamedArgumentValue("age", 27); arguments.AddNamedArgumentValue("name", "Bruno"); RootObjectDefinition singleton = new RootObjectDefinition(typeof(TestObject), arguments, new MutablePropertyValues()); singleton.IsSingleton = true; lof.RegisterObjectDefinition("singleton", singleton); TestObject to = lof.CreateObject("singleton", typeof(TestObject), new object[] { "Mark", 35 }) as TestObject; Assert.IsNotNull(to); Assert.AreEqual(35, to.Age); Assert.AreEqual("Mark", to.Name); TestObject to2 = lof.CreateObject("singleton", null, null) as TestObject; Assert.IsNotNull(to2); Assert.AreEqual(27, to2.Age); Assert.AreEqual("Bruno", to2.Name); } }
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 AddNamedArgumentWithEmptyStringName() { ConstructorArgumentValues values = new ConstructorArgumentValues(); Assert.Throws<ArgumentNullException>(() => values.AddNamedArgumentValue(string.Empty, 1)); }
public void AddNamedArgumentWithEmptyStringName() { ConstructorArgumentValues values = new ConstructorArgumentValues(); values.AddNamedArgumentValue(string.Empty, 1); }
/// <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 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 = (IConfigurableListableObjectFactory) mocks.CreateMock(typeof (IConfigurableListableObjectFactory)); Expect.Call(mock.GetObjectDefinitionNames()).Return(new string[] {"foo"}); Expect.Call(mock.GetObjectDefinition(null)).IgnoreArguments().Return(def); 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 AddNamedArgumentWithWhitespaceStringName() { ConstructorArgumentValues values = new ConstructorArgumentValues(); values.AddNamedArgumentValue(Environment.NewLine + " ", 1); }
public void GetArgumentValueIgnoresAlreadyUsedValues() { ISet used = new ListSet(); ConstructorArgumentValues values = new ConstructorArgumentValues(); values.AddGenericArgumentValue(1); values.AddNamedArgumentValue("2", 2); values.AddIndexedArgumentValue(3, 3); Type intType = typeof (int); ConstructorArgumentValues.ValueHolder one = values.GetArgumentValue(10, string.Empty, intType, used); Assert.AreEqual(1, one.Value); used.Add(one); ConstructorArgumentValues.ValueHolder two = values.GetArgumentValue(10, "2", intType, used); Assert.AreEqual(2, two.Value); used.Add(two); ConstructorArgumentValues.ValueHolder three = values.GetArgumentValue(3, string.Empty, intType, used); Assert.AreEqual(3, three.Value); used.Add(three); ConstructorArgumentValues.ValueHolder four = values.GetArgumentValue(10, string.Empty, intType, used); Assert.IsNull(four); }
public void AddNamedArgumentFromAotherCtorArgCollection() { ConstructorArgumentValues values = new ConstructorArgumentValues(); values.AddNamedArgumentValue("foo", "sball"); ConstructorArgumentValues copy = new ConstructorArgumentValues(values); Assert.AreEqual(1, copy.NamedArgumentValues.Count, "Added one named argument but it doesn't seem to have been added to the named arguments collection."); Assert.AreEqual(1, copy.ArgumentCount, "Added one named argument but it doesn't seem to be reflected in the overall argument count."); ConstructorArgumentValues.ValueHolder arg = copy.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."); }
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."); }
public void CreateObjectWithMixOfIndexedAndTwoNamedSameTypeCtorArguments() { // this object will be passed in as a named constructor argument string expectedCompany = "Griffin's Foosball Arcade"; MutablePropertyValues autoProps = new MutablePropertyValues(); autoProps.Add(new PropertyValue("Company", expectedCompany)); RootObjectDefinition autowired = new RootObjectDefinition(typeof(NestedTestObject), autoProps); // this object will be passed in as a named constructor argument string expectedLawyersCompany = "Pollack, Pounce, & Pulverise"; MutablePropertyValues lawyerProps = new MutablePropertyValues(); lawyerProps.Add(new PropertyValue("Company", expectedLawyersCompany)); RootObjectDefinition lawyer = new RootObjectDefinition(typeof(NestedTestObject), lawyerProps); // this simple string object will be passed in as an indexed constructor argument string expectedName = "Bingo"; // this simple integer object will be passed in as a named constructor argument int expectedAge = 1023; ConstructorArgumentValues values = new ConstructorArgumentValues(); // lets mix the order up a little... values.AddNamedArgumentValue("age", expectedAge); values.AddIndexedArgumentValue(0, expectedName); values.AddNamedArgumentValue("doctor", new RuntimeObjectReference("a_doctor")); values.AddNamedArgumentValue("lawyer", new RuntimeObjectReference("a_lawyer")); RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), values, new MutablePropertyValues()); DefaultListableObjectFactory fac = new DefaultListableObjectFactory(); // the object we're attempting to resolve... fac.RegisterObjectDefinition("foo", def); // the object that will be looked up and passed as a named parameter to a ctor call... fac.RegisterObjectDefinition("a_doctor", autowired); // another object that will be looked up and passed as a named parameter to a ctor call... fac.RegisterObjectDefinition("a_lawyer", lawyer); ITestObject foo = fac["foo"] as ITestObject; Assert.IsNotNull(foo, "Couldn't pull manually registered instance out of the factory."); Assert.AreEqual(expectedName, foo.Name, "Dependency 'name' was not resolved an indexed ctor arg."); Assert.AreEqual(expectedAge, foo.Age, "Dependency 'age' was not resolved using a named ctor arg."); Assert.AreEqual(expectedCompany, foo.Doctor.Company, "Dependency 'doctor.Company' was not resolved using autowiring."); Assert.AreEqual(expectedLawyersCompany, foo.Lawyer.Company, "Dependency 'lawyer.Company' was not resolved using another named ctor arg."); }
public void AddNamedArgumentWithNullName() { ConstructorArgumentValues values = new ConstructorArgumentValues(); Assert.Throws <ArgumentNullException>(() => values.AddNamedArgumentValue(null, 1)); }
/// <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); } } }
public void AddNamedArgumentWithWhitespaceStringName() { ConstructorArgumentValues values = new ConstructorArgumentValues(); Assert.Throws <ArgumentNullException>(() => values.AddNamedArgumentValue(Environment.NewLine + " ", 1)); }
public void AddNamedArgumentWithEmptyStringName() { ConstructorArgumentValues values = new ConstructorArgumentValues(); values.AddNamedArgumentValue(string.Empty, 1); }
public void AddNamedArgumentWithWhitespaceStringName() { ConstructorArgumentValues values = new ConstructorArgumentValues(); Assert.Throws<ArgumentNullException>(() => values.AddNamedArgumentValue(Environment.NewLine + " ", 1)); }
public void AddNamedArgumentWithEmptyStringName() { ConstructorArgumentValues values = new ConstructorArgumentValues(); Assert.Throws <ArgumentNullException>(() => values.AddNamedArgumentValue(string.Empty, 1)); }
public void AddNamedArgumentWithNullName() { ConstructorArgumentValues values = new ConstructorArgumentValues(); values.AddNamedArgumentValue(null, 1); }
public void AddNamedArgumentWithWhitespaceStringName() { ConstructorArgumentValues values = new ConstructorArgumentValues(); values.AddNamedArgumentValue(Environment.NewLine + " ", 1); }
public void ProxyTransparentProxy() { DefaultListableObjectFactory of = new DefaultListableObjectFactory(); ConstructorArgumentValues ctorArgs = new ConstructorArgumentValues(); ctorArgs.AddNamedArgumentValue("objectType", typeof(ITestObject)); of.RegisterObjectDefinition("bar", new RootObjectDefinition(typeof(TransparentProxyFactory), ctorArgs, null)); TestAutoProxyCreator apc = new TestAutoProxyCreator(of); of.AddObjectPostProcessor(apc); ITestObject o = of.GetObject("bar") as ITestObject; Assert.IsTrue(AopUtils.IsAopProxy(o)); // ensure interceptors get called o.Foo(); Assert.AreEqual(1, apc.NopInterceptor.Count); IAdvised advised = (IAdvised) o; // ensure target was called object target = advised.TargetSource.GetTarget(); Assert.AreEqual(1, TransparentProxyFactory.GetRealProxy(target).Count); }
public void AddNamedArgumentWithNullName() { ConstructorArgumentValues values = new ConstructorArgumentValues(); values.AddNamedArgumentValue(null, 1); }
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."); }