added checks+tests for GetObject(name,arguments) referring to IFactoryObject definitions
This commit is contained in:
@@ -1347,13 +1347,21 @@ namespace Spring.Objects.Factory.Support
|
||||
|
||||
// check validity of the usage of the args parameter; this can
|
||||
// only be used for prototypes constructed via a factory method...
|
||||
if (arguments != null && arguments.Length > 0)
|
||||
if (arguments != null)
|
||||
{
|
||||
if (mergedObjectDefinition.IsSingleton)
|
||||
{
|
||||
throw new ObjectDefinitionStoreException( "Cannot specify arguments in the GetObject () method when "
|
||||
+ "referring to a singleton object definition." );
|
||||
}
|
||||
|
||||
if (mergedObjectDefinition.HasObjectType
|
||||
&& typeof( IFactoryObject ).IsAssignableFrom( mergedObjectDefinition.ObjectType ))
|
||||
{
|
||||
throw new ObjectDefinitionStoreException( "Cannot specify arguments in the GetObject () method when "
|
||||
+ "referring to a factory object definition." );
|
||||
}
|
||||
|
||||
//MLP lets skip this check for now.
|
||||
/*
|
||||
else if (StringUtils.IsNullOrEmpty(mergedObjectDefinition.FactoryMethodName))
|
||||
@@ -1671,7 +1679,7 @@ namespace Spring.Objects.Factory.Support
|
||||
/// <see cref="Spring.Objects.Factory.IObjectFactory.GetObject(string)"/>.
|
||||
public object GetObject( string name )
|
||||
{
|
||||
return GetObjectInternal( name, typeof( object ), null, false );
|
||||
return GetObjectInternal( name, null, null, false );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1720,7 +1728,7 @@ namespace Spring.Objects.Factory.Support
|
||||
/// </exception>
|
||||
public object GetObject( string name, object[] arguments )
|
||||
{
|
||||
return GetObjectInternal( name, typeof( object ), arguments, false );
|
||||
return GetObjectInternal( name, null, arguments, false );
|
||||
|
||||
// string objectName = TransformedObjectName(name);
|
||||
// object instance = null;
|
||||
|
||||
@@ -915,6 +915,44 @@ namespace Spring.Objects.Factory
|
||||
Assert.AreEqual(expectedName, sing.Name, "Dependency was not resolved pulling manually registered instance out of the factory using factory method instantiation.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetObjectWithArgsOnFactoryObject()
|
||||
{
|
||||
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
|
||||
{
|
||||
// DummyFactory produces a TestObject
|
||||
RootObjectDefinition factoryObjectDef = new RootObjectDefinition(typeof(DummyFactory));
|
||||
factoryObjectDef.IsSingleton = true;
|
||||
lof.RegisterObjectDefinition("factoryObject", factoryObjectDef);
|
||||
|
||||
// verify preconditions
|
||||
TestObject to = lof.GetObject("factoryObject", null, null) as TestObject;
|
||||
Assert.IsNotNull(to);
|
||||
Assert.AreEqual(25, to.Age);
|
||||
Assert.AreEqual(DummyFactory.SINGLETON_NAME, to.Name);
|
||||
|
||||
try
|
||||
{
|
||||
to = lof.GetObject("factoryObject", new object[] {}) as TestObject;
|
||||
Assert.Fail("should throw ObjectDefinitionStoreException");
|
||||
}
|
||||
catch (ObjectDefinitionStoreException ex)
|
||||
{
|
||||
Assert.IsTrue( ex.Message.IndexOf("Cannot specify arguments in the GetObject () method when referring to a factory object definition") > -1);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
to = lof.GetObject("factoryObject", new object[] { "Mark", "35" }) as TestObject;
|
||||
Assert.Fail("should throw ObjectDefinitionStoreException");
|
||||
}
|
||||
catch (ObjectDefinitionStoreException ex)
|
||||
{
|
||||
Assert.IsTrue( ex.Message.IndexOf("Cannot specify arguments in the GetObject () method when referring to a factory object definition") > -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test(Description = "http://opensource.atlassian.com/projects/spring/browse/SPRNET-368")]
|
||||
public void GetObjectWithCtorArgsAndCtorAutowiring()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user