From 64ef66396b8b7cfa1996c02135eaad9dc51ac1fa Mon Sep 17 00:00:00 2001 From: eeichinger Date: Tue, 2 Dec 2008 20:38:20 +0000 Subject: [PATCH] fixed SPRNET-1114 --- ...ctDependencyInjectionSpringContextTests.cs | 23 ------------- .../NUnit/AbstractSpringContextTests.cs | 30 +++++++++++++++-- ...endencyInjectionSpringContextTestsTests.cs | 32 +++++++++++++++---- 3 files changed, 53 insertions(+), 32 deletions(-) diff --git a/src/Spring/Spring.Testing.NUnit/Testing/NUnit/AbstractDependencyInjectionSpringContextTests.cs b/src/Spring/Spring.Testing.NUnit/Testing/NUnit/AbstractDependencyInjectionSpringContextTests.cs index db1a4726..f426b520 100644 --- a/src/Spring/Spring.Testing.NUnit/Testing/NUnit/AbstractDependencyInjectionSpringContextTests.cs +++ b/src/Spring/Spring.Testing.NUnit/Testing/NUnit/AbstractDependencyInjectionSpringContextTests.cs @@ -78,7 +78,6 @@ namespace Spring.Testing.NUnit /// Aleksandar Seovic (.NET) 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() {} - /// - /// Controls, whether the instance will - /// be registered/unregistered with the global before and after each test. - /// Defaults to true. - /// - public bool RegisterContextWithContextRegistry - { - get { return registerContextWithContextRegistry; } - set { registerContextWithContextRegistry = value; } - } - /// /// 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(); - } - } } /// diff --git a/src/Spring/Spring.Testing.NUnit/Testing/NUnit/AbstractSpringContextTests.cs b/src/Spring/Spring.Testing.NUnit/Testing/NUnit/AbstractSpringContextTests.cs index 99dcae2f..9e4e45dd 100644 --- a/src/Spring/Spring.Testing.NUnit/Testing/NUnit/AbstractSpringContextTests.cs +++ b/src/Spring/Spring.Testing.NUnit/Testing/NUnit/AbstractSpringContextTests.cs @@ -65,6 +65,11 @@ namespace Spring.Testing.NUnit contextKeyToContextMap.Clear(); } + /// + /// Indicates, whether context instances should be automatically registered with the global . + /// + private bool registerContextWithContextRegistry = true; + /// /// Logger available to subclasses. /// @@ -78,6 +83,17 @@ namespace Spring.Testing.NUnit logger = LogManager.GetLogger(GetType()); } + /// + /// Controls, whether application context instances will + /// be registered/unregistered with the global . + /// Defaults to true. + /// + public bool RegisterContextWithContextRegistry + { + get { return registerContextWithContextRegistry; } + set { registerContextWithContextRegistry = value; } + } + /// /// 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 /// protected bool HasCachedContext(object contextKey) { - return contextKeyToContextMap.Contains(contextKey); + string keyString = ContextKeyString(contextKey); + return contextKeyToContextMap.Contains(keyString); } /// @@ -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); + } } /// @@ -174,7 +198,7 @@ namespace Spring.Testing.NUnit { ctx = LoadContext(key); } - contextKeyToContextMap[keyString] = ctx; + AddContext(key, ctx); } return ctx; } diff --git a/test/Spring/Spring.Testing.NUnit.Tests/Testing/NUnit/AbstractDependencyInjectionSpringContextTestsTests.cs b/test/Spring/Spring.Testing.NUnit.Tests/Testing/NUnit/AbstractDependencyInjectionSpringContextTestsTests.cs index b62eb00c..82060a1b 100644 --- a/test/Spring/Spring.Testing.NUnit.Tests/Testing/NUnit/AbstractDependencyInjectionSpringContextTestsTests.cs +++ b/test/Spring/Spring.Testing.NUnit.Tests/Testing/NUnit/AbstractDependencyInjectionSpringContextTestsTests.cs @@ -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);