sprnet-1027, sprnet-899, sprnet-840

added more unit tests for new IObjectFactory.CreateObject() method
This commit is contained in:
eeichinger
2008-10-06 11:50:20 +00:00
parent 83098fe045
commit b25755d9df
10 changed files with 638 additions and 389 deletions

View File

@@ -1325,6 +1325,16 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\ISharedStateAware.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\ISharedStateFactory.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\MutablePropertyValues.cs"
SubType = "Code"
@@ -1715,6 +1725,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Factory\Config\SharedStateAwareProcessor.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Factory\Config\SmartInstantiationAwareObjectPostProcessor.cs"
SubType = "Code"
@@ -2067,6 +2082,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Support\AbstractSharedStateFactory.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Support\AbstractWiringEventHandlerValue.cs"
SubType = "Code"
@@ -2082,6 +2102,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Support\ByTypeSharedStateFactory.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Support\InstanceEventHandlerValue.cs"
SubType = "Code"

View File

@@ -60,6 +60,7 @@
<DebugType>full</DebugType>
<ErrorReport>prompt</ErrorReport>
<WarningsAsErrors>1591</WarningsAsErrors>
<RunCodeAnalysis>false</RunCodeAnalysis>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>..\..\..\build\VS.Net.2008\Spring.Core\Release\</OutputPath>

View File

@@ -437,7 +437,7 @@
/>
<File
RelPath = "Web\Support\PageHandlerFactory.cs"
SubType = "ASPXCodeBehind"
SubType = "Code"
BuildAction = "Compile"
/>
<File

View File

@@ -48,7 +48,7 @@ namespace Spring.Aop.Framework.DynamicProxy
{
DerivedTestObject testObject = new DerivedTestObject();
IList interfaces = new ArrayList(testObject.GetType().GetInterfaces());
Assert.AreEqual(8, interfaces.Count, "Incorrect number of interfaces");
Assert.AreEqual(9, interfaces.Count, "Incorrect number of interfaces");
Assert.IsTrue(interfaces.Contains(typeof (ITestObject)), "Does not contain ITestObject");
Assert.IsTrue(interfaces.Contains(typeof (IOther)), "Does not contain IOther");
}

View File

@@ -446,7 +446,7 @@ namespace Spring.Aop.Framework
// Extend to get new interface
TestObjectSubclass raw = new TestObjectSubclass();
ProxyFactory factory = new ProxyFactory(raw);
Assert.AreEqual(7, factory.Interfaces.Length, "Found correct number of interfaces");
Assert.AreEqual(8, factory.Interfaces.Length, "Found correct number of interfaces");
//System.out.println("Proxied interfaces are " + StringUtils.arrayToDelimitedString(factory.getProxiedInterfaces(), ","));
ITestObject tb = (ITestObject) factory.GetProxy();
Assert.IsTrue(tb is IOther, "Picked up secondary interface");

View File

@@ -22,7 +22,6 @@
using System;
using System.Collections;
using System.Web.UI;
using NUnit.Framework;
using Rhino.Mocks;
using Spring.Objects.Factory.Support;
@@ -179,7 +178,7 @@ namespace Spring.Objects.Factory.Config
private static IDisposable Record( MockRepository mocks )
{
#if !NET_1_1
#if NET_2_0
return mocks.Record();
#else
return new RecordModeChanger(mocks);
@@ -188,14 +187,14 @@ namespace Spring.Objects.Factory.Config
private static IDisposable Playback( MockRepository mocks )
{
#if !NET_1_1
#if NET_2_0
return mocks.Playback();
#else
return new PlaybackModeChanger(mocks);
#endif
}
#if NET_1_1
#if !NET_2_0
private class RecordModeChanger : IDisposable
{
private MockRepository _mocks;

View File

@@ -30,6 +30,7 @@ using Spring.Core.TypeConversion;
using Spring.Objects.Factory.Config;
using Spring.Objects.Factory.Support;
using Spring.Objects.Factory.Xml;
using Spring.Objects.Support;
#endregion
@@ -1016,6 +1017,179 @@ namespace Spring.Objects.Factory
}
}
[Test(Description = "http://opensource.atlassian.com/projects/spring/browse/SPRNET-368")]
public void CreateObjectWithCtorArgsAndCtorAutowiring()
{
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
{
RootObjectDefinition prototype = new RootObjectDefinition(typeof(TestObject));
prototype.IsSingleton = false;
lof.RegisterObjectDefinition("prototype", prototype);
TestObject to = lof.CreateObject("prototype", typeof(TestObject), new object[] { "Bruno", 26, new NestedTestObject("Home") }) as TestObject;
Assert.IsNotNull(to);
Assert.AreEqual(26, to.Age);
Assert.AreEqual("Bruno", to.Name);
Assert.AreEqual("Home", to.Doctor.Company);
}
}
[Test]
public void CreateObjectWithCtorArgsOnPrototype()
{
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
{
RootObjectDefinition prototype = new RootObjectDefinition(typeof(TestObject));
prototype.IsSingleton = false;
lof.RegisterObjectDefinition("prototype", prototype);
TestObject to = lof.CreateObject("prototype", typeof(TestObject), new object[] { "Mark", 35 }) as TestObject;
Assert.IsNotNull(to);
Assert.AreEqual(35, to.Age);
Assert.AreEqual("Mark", to.Name);
}
}
[Test]
[Ignore("Ordering must now be strict when providing array of arguments for ctors")]
public void CreateObjectWithCtorArgsOnPrototypeOutOfOrderArgs()
{
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
{
RootObjectDefinition prototype
= new RootObjectDefinition(typeof(TestObject));
prototype.IsSingleton = false;
lof.RegisterObjectDefinition("prototype", prototype);
try
{
TestObject to2 = lof.CreateObject("prototype", typeof(TestObject), new object[] { 35, "Mark" }) as TestObject;
Assert.IsNotNull(to2);
Assert.AreEqual(35, to2.Age);
Assert.AreEqual("Mark", to2.Name);
}
catch (ObjectCreationException ex)
{
Assert.IsTrue(ex.Message.IndexOf("'Object of type 'System.Int32' cannot be converted to type 'System.String'") >= 0);
}
}
}
[Test]
public void CreateObjectWithCtorArgsOnSingleton()
{
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
{
RootObjectDefinition singleton
= new RootObjectDefinition(typeof(TestObject));
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);
}
}
[Test]
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);
}
}
[Test]
public void CreateObjectDoesNotConfigure()
{
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
{
MutablePropertyValues properties = new MutablePropertyValues();
properties.Add(new PropertyValue("Age", 27));
properties.Add(new PropertyValue("Name", "Bruno"));
RootObjectDefinition singleton = new RootObjectDefinition(typeof(TestObject), null, properties);
singleton.IsSingleton = true;
lof.RegisterObjectDefinition("singleton", singleton);
TestObject to2 = lof.CreateObject("singleton", typeof(TestObject), null) as TestObject;
Assert.IsNotNull(to2);
// no props set
Assert.AreEqual(0, to2.Age);
Assert.AreEqual(null, to2.Name);
// no object postprocessors executed!
Assert.AreEqual(null, to2.ObjectName);
Assert.AreEqual(null, to2.ObjectFactory);
Assert.AreEqual(null, to2.SharedState);
}
}
[Test]
public void CreateObjectDoesAffectContainerManagedSingletons()
{
using (DefaultListableObjectFactory lof = new DefaultListableObjectFactory())
{
lof.AddObjectPostProcessor(new SharedStateAwareProcessor(new ISharedStateFactory[] { new ByTypeSharedStateFactory() }, Int32.MaxValue ));
MutablePropertyValues properties = new MutablePropertyValues();
properties.Add(new PropertyValue("Age", 27));
properties.Add(new PropertyValue("Name", "Bruno"));
RootObjectDefinition singleton = new RootObjectDefinition(typeof(TestObject), null, properties);
singleton.IsSingleton = true;
lof.RegisterObjectDefinition("singleton", singleton);
// a call to GetObject() results in a normally configured instance
TestObject to = lof.GetObject("singleton") as TestObject;
Assert.IsNotNull(to);
// props set
Assert.AreEqual(27, to.Age);
Assert.AreEqual("Bruno", to.Name);
// object postprocessors executed!
Assert.AreEqual("singleton", to.ObjectName);
Assert.AreEqual(lof, to.ObjectFactory);
Assert.IsNotNull(to.SharedState);
// altough configured as singleton, calling CreateObject prevents the instance from being cached
// otherwise the container could not guarantee the results of calling GetObject() to other clients.
TestObject to2 = lof.CreateObject("singleton", typeof(TestObject), null) as TestObject;
Assert.IsNotNull(to2);
// no props set
Assert.AreEqual(0, to2.Age);
Assert.AreEqual(null, to2.Name);
// no object postprocessors executed!
Assert.AreEqual(null, to2.ObjectName);
Assert.AreEqual(null, to2.ObjectFactory);
Assert.AreEqual(null, to2.SharedState);
// a call to GetObject() results in a normally configured instance
TestObject to3 = lof.GetObject("singleton") as TestObject;
Assert.IsNotNull(to3);
// props set
Assert.AreEqual(27, to3.Age);
Assert.AreEqual("Bruno", to3.Name);
// object postprocessors executed!
Assert.AreEqual("singleton", to3.ObjectName);
Assert.AreEqual(lof, to3.ObjectFactory);
Assert.IsNotNull(to3.SharedState);
}
}
[Test]
public void CreateObjectWithAllNamedCtorArguments()
{

View File

@@ -41,7 +41,7 @@ namespace Spring.Objects
/// </summary>
/// <author>Rod Johnson</author>
/// <author>Mark Pollack (.NET)</author>
public class TestObject : MarshalByRefObject, ITestObject, IObjectFactoryAware, IComparable, IOther, IApplicationContextAware, IObjectNameAware, IInitializingObject
public class TestObject : MarshalByRefObject, ITestObject, IObjectFactoryAware, IComparable, IOther, IApplicationContextAware, IObjectNameAware, IInitializingObject, ISharedStateAware
{
#region Event testing members
@@ -381,6 +381,7 @@ namespace Spring.Objects
private Size size;
private bool initCompleted;
private IDictionary sharedState;
#endregion
#region Constructor (s) / Destructor
@@ -582,6 +583,12 @@ namespace Spring.Objects
}
#endregion
public IDictionary SharedState
{
get { return sharedState; }
set { sharedState = value; }
}
}
#region Inner Class : TestObjectConverter

View File

@@ -1394,6 +1394,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Factory\Config\SharedStateAwareProcessorTests.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Factory\Config\SpecialFolderVariableSourceTests.cs"
SubType = "Code"
@@ -1599,6 +1604,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Support\AbstractSharedStateFactoryTests.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Support\ArgumentConvertingMethodInvokerTests.cs"
SubType = "Code"
@@ -1609,6 +1619,11 @@
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Support\ByTypeSharedStateProviderTests.cs"
SubType = "Code"
BuildAction = "Compile"
/>
<File
RelPath = "Objects\Support\InstanceEventHandlerValueTests.cs"
SubType = "Code"