From 08063accf6ad04bd3408a54399fd0d158c553a3e Mon Sep 17 00:00:00 2001 From: eeichinger Date: Thu, 9 Oct 2008 19:41:34 +0000 Subject: [PATCH] added checks+tests for GetObject(name,arguments) referring to IFactoryObject definitions --- .../Factory/Support/AbstractObjectFactory.cs | 14 +++++-- .../DefaultListableObjectFactoryTests.cs | 38 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs b/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs index 7eba6642..d543441e 100644 --- a/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs +++ b/src/Spring/Spring.Core/Objects/Factory/Support/AbstractObjectFactory.cs @@ -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 /// . public object GetObject( string name ) { - return GetObjectInternal( name, typeof( object ), null, false ); + return GetObjectInternal( name, null, null, false ); } /// @@ -1720,7 +1728,7 @@ namespace Spring.Objects.Factory.Support /// 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; diff --git a/test/Spring/Spring.Core.Tests/Objects/Factory/DefaultListableObjectFactoryTests.cs b/test/Spring/Spring.Core.Tests/Objects/Factory/DefaultListableObjectFactoryTests.cs index 1343f924..60a51ffa 100644 --- a/test/Spring/Spring.Core.Tests/Objects/Factory/DefaultListableObjectFactoryTests.cs +++ b/test/Spring/Spring.Core.Tests/Objects/Factory/DefaultListableObjectFactoryTests.cs @@ -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() {