From 00630899213e8a8dcf555ef68eeff8e4adfcb803 Mon Sep 17 00:00:00 2001 From: eeichinger Date: Tue, 5 Aug 2008 23:58:48 +0000 Subject: [PATCH] reset ConfigurationSystem before each ContextRegistryTests test --- .../Context/Support/ContextRegistryTests.cs | 200 ++++++++++-------- .../HookableContextHandler.cs | 36 +++- .../Spring.Core.Tests.dll-1.1.config | 2 +- 3 files changed, 146 insertions(+), 92 deletions(-) diff --git a/test/Spring/Spring.Core.Tests/Context/Support/ContextRegistryTests.cs b/test/Spring/Spring.Core.Tests/Context/Support/ContextRegistryTests.cs index 94de9e46..714aacb3 100644 --- a/test/Spring/Spring.Core.Tests/Context/Support/ContextRegistryTests.cs +++ b/test/Spring/Spring.Core.Tests/Context/Support/ContextRegistryTests.cs @@ -21,6 +21,8 @@ #region Imports using System; +using System.Configuration; +using System.Reflection; using System.Xml; using NUnit.Framework; @@ -28,23 +30,53 @@ using NUnit.Framework; using Spring.Objects; using Spring.Proxy; using Spring.Objects.Factory.Support; +using Spring.Reflection.Dynamic; #endregion namespace Spring.Context.Support { - /// - /// Unit tests for the ContextRegistry class. - /// - /// Rick Evans - [TestFixture] - public sealed class ContextRegistryTests - { - [SetUp] - public void SetUp() - { - ContextRegistry.Clear(); - } + /// + /// Unit tests for the ContextRegistry class. + /// + /// Rick Evans + [TestFixture] + public sealed class ContextRegistryTests + { + [SetUp] + public void SetUp() + { + ContextRegistry.Clear(); + ResetConfigurationSystem(); + } + + private static void ResetConfigurationSystem() + { +#if NET_2_0 + FieldInfo initStateRef = typeof(ConfigurationManager).GetField("s_initState",BindingFlags.NonPublic|BindingFlags.Static); + object notStarted = Activator.CreateInstance(initStateRef.FieldType); + initStateRef.SetValue(null,notStarted); +#endif +#if NET_1_1 + FieldInfo initStateRef = typeof(ConfigurationSettings).GetField("_initState",BindingFlags.NonPublic|BindingFlags.Static); + object notStarted = Activator.CreateInstance(initStateRef.FieldType); + initStateRef.SetValue(null,notStarted); +#endif +#if NET_1_0 + FieldInfo initStateRef = typeof(ConfigurationSettings).GetField("_configurationInitialized",BindingFlags.NonPublic|BindingFlags.Static); + FieldInfo configSystemRef = typeof(ConfigurationSettings).GetField("_configSystem",BindingFlags.NonPublic|BindingFlags.Static); + initStateRef.SetValue(null,false); + configSystemRef.SetValue(null,null); +#endif + } + + /// + /// This handler simulates an undefined configuration section + /// + private static object GetNullSection(object parent, object context, XmlNode section) + { + return null; + } /// /// This handler simulates calls to ContextRegistry during context creation @@ -54,41 +86,36 @@ namespace Spring.Context.Support return ContextRegistry.GetContext(); // this must fail! } -#if NET_2_0 [Test] public void ThrowsInvalidOperationExceptionOnRecursiveCallsToGetContext() { - HookableContextHandler.CreateContextFromSectionHandler prevInst = HookableContextHandler.SetSectionHandler( - new HookableContextHandler.CreateContextFromSectionHandler(GetContextRecursive)); - try + using (new HookableContextHandler.Guard(new HookableContextHandler.CreateContextFromSectionHandler(GetContextRecursive))) { - ContextRegistry.GetContext("somename"); - Assert.Fail("Should throw an exception"); - } - catch(Exception ex) - { - InvalidOperationException rootCause = ex.GetBaseException() as InvalidOperationException; - Assert.IsNotNull(rootCause); - Assert.AreEqual("root context is currently in creation.", rootCause.Message.Substring(0, 38)); - } - finally - { - HookableContextHandler.SetSectionHandler(prevInst); + try + { + ContextRegistry.GetContext("somename"); + Assert.Fail("Should throw an exception"); + } + catch (ConfigurationException ex) + { + InvalidOperationException rootCause = ex.GetBaseException() as InvalidOperationException; + Assert.IsNotNull(rootCause); + Assert.AreEqual("root context is currently in creation.", rootCause.Message.Substring(0, 38)); + } } } -#endif - [Test] - public void RegisterRootContext() - { - MockApplicationContext ctx = new MockApplicationContext(); - ContextRegistry.RegisterContext(ctx); - IApplicationContext context = ContextRegistry.GetContext(); - Assert.IsNotNull(context, - "Root context is null even though a context has been registered."); - Assert.IsTrue(Object.ReferenceEquals(ctx, context), - "Root context was not the same as the first context registered (it must be)."); - } + [Test] + public void RegisterRootContext() + { + MockApplicationContext ctx = new MockApplicationContext(); + ContextRegistry.RegisterContext(ctx); + IApplicationContext context = ContextRegistry.GetContext(); + Assert.IsNotNull(context, + "Root context is null even though a context has been registered."); + Assert.IsTrue(Object.ReferenceEquals(ctx, context), + "Root context was not the same as the first context registered (it must be)."); + } [Test] public void RegisterNamedRootContext() @@ -103,50 +130,53 @@ namespace Spring.Context.Support "Root context name is different even though the root context has been registered under the lookup name."); } - [Test] - public void RegisterNamedContext() - { - const string ctxName = "bingo"; - MockApplicationContext ctx = new MockApplicationContext(ctxName); - ContextRegistry.RegisterContext(ctx); - IApplicationContext context = ContextRegistry.GetContext(ctxName); - Assert.IsNotNull(context, - "Named context is null even though a context has been registered under the lookup name."); - Assert.IsTrue(Object.ReferenceEquals(ctx, context), - "Named context was not the same as the registered context (it must be)."); - } - - [Test] - [ExpectedException(typeof(ArgumentException))] - public void GetContextWithNullName() - { - ContextRegistry.GetContext(null); - } - - [Test] - [ExpectedException(typeof(ArgumentException))] - public void GetContextWithEmptyName() - { - ContextRegistry.GetContext(""); - } + [Test] + public void RegisterNamedContext() + { + const string ctxName = "bingo"; + MockApplicationContext ctx = new MockApplicationContext(ctxName); + ContextRegistry.RegisterContext(ctx); + IApplicationContext context = ContextRegistry.GetContext(ctxName); + Assert.IsNotNull(context, + "Named context is null even though a context has been registered under the lookup name."); + Assert.IsTrue(Object.ReferenceEquals(ctx, context), + "Named context was not the same as the registered context (it must be)."); + } [Test] - [Ignore("How can we test that one ???")] + [ExpectedException(typeof(ArgumentException))] + public void GetContextWithNullName() + { + ContextRegistry.GetContext(null); + } + + [Test] + [ExpectedException(typeof(ArgumentException))] + public void GetContextWithEmptyName() + { + ContextRegistry.GetContext(""); + } + + [Test] +// [Ignore("How can we test that one ???")] [ExpectedException(typeof(ApplicationContextException), ExpectedMessage = "No context registered. Use the 'RegisterContext' method or the 'spring/context' section from your configuration file.")] public void GetRootContextNotRegisteredThrowsException() { - IApplicationContext context = ContextRegistry.GetContext(); + using (new HookableContextHandler.Guard(new HookableContextHandler.CreateContextFromSectionHandler(GetNullSection))) + { + IApplicationContext context = ContextRegistry.GetContext(); + } } - [Test] - [ExpectedException(typeof(ApplicationContextException), + [Test] + [ExpectedException(typeof(ApplicationContextException), ExpectedMessage = "No context registered under name 'bingo'. Use the 'RegisterContext' method or the 'spring/context' section from your configuration file.")] - public void GetContextByNameNotRegisteredThrowsException() - { - IApplicationContext context = ContextRegistry.GetContext("bingo"); - } + public void GetContextByNameNotRegisteredThrowsException() + { + IApplicationContext context = ContextRegistry.GetContext("bingo"); + } [Test] public void ClearWithDynamicProxies() @@ -190,14 +220,14 @@ namespace Spring.Context.Support } #endif - [Test(Description="SPRNET-105")] - [ExpectedException(typeof(ApplicationContextException))] - public void ChokesIfChildContextRegisteredUnderNameOfAnExistingContext() - { - MockApplicationContext original = new MockApplicationContext("original"); - ContextRegistry.RegisterContext(original); - MockApplicationContext duplicate = new MockApplicationContext("original"); - ContextRegistry.RegisterContext(duplicate); - } - } -} \ No newline at end of file + [Test(Description = "SPRNET-105")] + [ExpectedException(typeof(ApplicationContextException))] + public void ChokesIfChildContextRegisteredUnderNameOfAnExistingContext() + { + MockApplicationContext original = new MockApplicationContext("original"); + ContextRegistry.RegisterContext(original); + MockApplicationContext duplicate = new MockApplicationContext("original"); + ContextRegistry.RegisterContext(duplicate); + } + } +} diff --git a/test/Spring/Spring.Core.Tests/HookableContextHandler.cs b/test/Spring/Spring.Core.Tests/HookableContextHandler.cs index 65c81363..4b226a2a 100644 --- a/test/Spring/Spring.Core.Tests/HookableContextHandler.cs +++ b/test/Spring/Spring.Core.Tests/HookableContextHandler.cs @@ -33,8 +33,32 @@ namespace Spring /// Replace the original context handler with this hookable version for testing ContextRegistry /// /// Erich Eichinger - public class HookableContextHandler : ContextHandler, IConfigurationSectionHandler + public class HookableContextHandler : IConfigurationSectionHandler { + private IConfigurationSectionHandler baseHandler = new ContextHandler(); + + /// + /// May be used to wrap codeblocks within using(new Guard(new CreateContextFromSectionHandler(MyTestSectionHandler))) { ... } + /// + public class Guard : IDisposable + { + private readonly CreateContextFromSectionHandler _prevInst; + + /// + /// Initializes a new instance of the class. + /// + public Guard(CreateContextFromSectionHandler sectionHandler) + { + NUnit.Framework.Assert.IsNotNull(sectionHandler); + _prevInst = SetSectionHandler(sectionHandler); + } + + public void Dispose() + { + SetSectionHandler(_prevInst); + } + } + public delegate object CreateContextFromSectionHandler(object parent, object configContext, XmlNode section); private static CreateContextFromSectionHandler s_callback; @@ -52,24 +76,24 @@ namespace Spring } /// - ///Creates a configuration section handler. + ///Creates a configuration section. /// /// /// - ///The created section handler object. + ///The created section object. /// /// ///Parent object. ///Section XML node. ///Configuration context object.2 - new virtual public object Create(object parent, object configContext, XmlNode section) - { + public object Create(object parent, object configContext, XmlNode section) + { if (s_callback != null) { return s_callback(parent, configContext, section); } - return base.Create(parent, configContext, section); + return baseHandler.Create(parent, configContext, section); } } } \ No newline at end of file diff --git a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.dll-1.1.config b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.dll-1.1.config index f476f22b..6424829a 100644 --- a/test/Spring/Spring.Core.Tests/Spring.Core.Tests.dll-1.1.config +++ b/test/Spring/Spring.Core.Tests/Spring.Core.Tests.dll-1.1.config @@ -39,7 +39,7 @@ limitations under the License. -
+