fixed SPRNET-1114

This commit is contained in:
eeichinger
2008-12-02 20:38:20 +00:00
parent 085464919f
commit 64ef66396b
3 changed files with 53 additions and 32 deletions

View File

@@ -78,7 +78,6 @@ namespace Spring.Testing.NUnit
/// <author>Aleksandar Seovic (.NET)</author>
public abstract class AbstractDependencyInjectionSpringContextTests : AbstractSpringContextTests
{
private bool registerContextWithContextRegistry = true;
private bool populateProtectedVariables = false;
private AutoWiringMode autowireMode = AutoWiringMode.ByType;
private bool dependencyCheck = true;
@@ -100,17 +99,6 @@ namespace Spring.Testing.NUnit
public AbstractDependencyInjectionSpringContextTests()
{}
/// <summary>
/// Controls, whether the <see cref="applicationContext"/> instance will
/// be registered/unregistered with the global <see cref="ContextRegistry"/> before and after each test.
/// Defaults to <c>true</c>.
/// </summary>
public bool RegisterContextWithContextRegistry
{
get { return registerContextWithContextRegistry; }
set { registerContextWithContextRegistry = value; }
}
/// <summary>
/// Gets or sets a flag specifying whether to populate protected
/// variables of this test case.
@@ -179,10 +167,6 @@ namespace Spring.Testing.NUnit
public virtual void SetUp()
{
this.applicationContext = GetContext(ContextKey);
if (RegisterContextWithContextRegistry)
{
ContextRegistry.RegisterContext(this.applicationContext);
}
InjectDependencies();
try
{
@@ -366,13 +350,6 @@ namespace Spring.Testing.NUnit
{
logger.Error("OnTearDown error", ex);
}
finally
{
if (RegisterContextWithContextRegistry)
{
ContextRegistry.Clear();
}
}
}
/// <summary>

View File

@@ -65,6 +65,11 @@ namespace Spring.Testing.NUnit
contextKeyToContextMap.Clear();
}
/// <summary>
/// Indicates, whether context instances should be automatically registered with the global <see cref="ContextRegistry"/>.
/// </summary>
private bool registerContextWithContextRegistry = true;
/// <summary>
/// Logger available to subclasses.
/// </summary>
@@ -78,6 +83,17 @@ namespace Spring.Testing.NUnit
logger = LogManager.GetLogger(GetType());
}
/// <summary>
/// Controls, whether application context instances will
/// be registered/unregistered with the global <see cref="ContextRegistry"/>.
/// Defaults to <c>true</c>.
/// </summary>
public bool RegisterContextWithContextRegistry
{
get { return registerContextWithContextRegistry; }
set { registerContextWithContextRegistry = value; }
}
/// <summary>
/// Set custom locations dirty. This will cause them to be reloaded
/// from the cache before the next test case is executed.
@@ -112,7 +128,8 @@ namespace Spring.Testing.NUnit
/// </returns>
protected bool HasCachedContext(object contextKey)
{
return contextKeyToContextMap.Contains(contextKey);
string keyString = ContextKeyString(contextKey);
return contextKeyToContextMap.Contains(keyString);
}
/// <summary>
@@ -151,7 +168,14 @@ namespace Spring.Testing.NUnit
public void AddContext(object key, IConfigurableApplicationContext context)
{
AssertUtils.ArgumentNotNull(context, "context", "ApplicationContext must not be null");
contextKeyToContextMap[ContextKeyString(key)] = context;
string keyString = ContextKeyString(key);
contextKeyToContextMap.Add(keyString, context);
if (RegisterContextWithContextRegistry
&& !ContextRegistry.IsContextRegistered(context.Name))
{
ContextRegistry.RegisterContext(context);
}
}
/// <summary>
@@ -174,7 +198,7 @@ namespace Spring.Testing.NUnit
{
ctx = LoadContext(key);
}
contextKeyToContextMap[keyString] = ctx;
AddContext(key, ctx);
}
return ctx;
}

View File

@@ -13,6 +13,8 @@ namespace Spring.Testing.NUnit
{
private class TestAbstractDependencyInjectionSpringContextTests :AbstractDependencyInjectionSpringContextTests
{
public static readonly string[] CONFIGLOCATIONS = new string[] {"assembly://Spring.Testing.NUnit.Tests/Spring.Testing.NUnit/TestApplicationContext.xml"};
public TestAbstractDependencyInjectionSpringContextTests()
{}
@@ -28,8 +30,13 @@ namespace Spring.Testing.NUnit
protected override string[] ConfigLocations
{
get { return new string[] {"assembly://Spring.Testing.NUnit.Tests/Spring.Testing.NUnit/TestApplicationContext.xml"}; }
get { return CONFIGLOCATIONS; }
}
public new bool HasContextCached(object key)
{
return base.HasCachedContext(key);
}
}
private TestAbstractDependencyInjectionSpringContextTests fixtureInstance ;
@@ -41,14 +48,26 @@ namespace Spring.Testing.NUnit
}
[Test]
public void RegistersAndUnregistersWithContextRegistryByDefault()
public void RegistersWithContextRegistryByDefault()
{
fixtureInstance = new TestAbstractDependencyInjectionSpringContextTests();
Assert.IsTrue(fixtureInstance.RegisterContextWithContextRegistry);
}
[Test]
public void UnregistersFromContextRegistryWhenDirty()
{
fixtureInstance = new TestAbstractDependencyInjectionSpringContextTests();
Assert.IsTrue(fixtureInstance.RegisterContextWithContextRegistry);
fixtureInstance.SetUp();
Assert.IsTrue( ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name) );
fixtureInstance.TearDown();
Assert.IsFalse( ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name) );
Assert.IsTrue(ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name));
fixtureInstance.SetUp();
Assert.IsTrue(ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name));
fixtureInstance.SetDirty();
fixtureInstance.TearDown();
Assert.IsFalse(ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name));
}
[Test]
@@ -63,7 +82,6 @@ namespace Spring.Testing.NUnit
Assert.IsFalse( ContextRegistry.IsContextRegistered(fixtureInstance.ApplicationContext.Name) );
}
[Test]
public void CachesApplicationContexts()
{
@@ -71,6 +89,7 @@ namespace Spring.Testing.NUnit
fixtureInstance.SetUp();
Assert.IsNotNull(fixtureInstance.ApplicationContext);
Assert.AreEqual(1, fixtureInstance.LoadCount); // context has been loaded
Assert.IsTrue(fixtureInstance.HasContextCached(TestAbstractDependencyInjectionSpringContextTests.CONFIGLOCATIONS));
fixtureInstance.TearDown();
TestAbstractDependencyInjectionSpringContextTests otherFixtureInstance = new TestAbstractDependencyInjectionSpringContextTests(false);
@@ -78,9 +97,10 @@ namespace Spring.Testing.NUnit
Assert.IsNotNull(otherFixtureInstance.ApplicationContext);
Assert.AreEqual(0, otherFixtureInstance.LoadCount); // context was obtained from cache
Assert.AreSame(fixtureInstance.ApplicationContext, otherFixtureInstance.ApplicationContext);
otherFixtureInstance.SetDirty(); // dispose
otherFixtureInstance.SetDirty(); // purge cache and dispose cached instances
Assert.IsFalse(fixtureInstance.HasContextCached(TestAbstractDependencyInjectionSpringContextTests.CONFIGLOCATIONS));
otherFixtureInstance.TearDown();
otherFixtureInstance = new TestAbstractDependencyInjectionSpringContextTests(false);
otherFixtureInstance.SetUp();
Assert.IsNotNull(otherFixtureInstance.ApplicationContext);