From b69f1331aaa0f460674b7e3ba5230613030e3b54 Mon Sep 17 00:00:00 2001 From: bbaia Date: Tue, 5 Aug 2008 18:29:52 +0000 Subject: [PATCH] ContextRegistry.GetContext(string name) should throw an exception if no context has been registered under that name. [SPRNET-991] --- .../Context/Support/ContextRegistry.cs | 23 +++++++++++-------- .../Context/Support/ContextRegistryTests.cs | 19 +++++++++++---- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/Spring/Spring.Core/Context/Support/ContextRegistry.cs b/src/Spring/Spring.Core/Context/Support/ContextRegistry.cs index 96e74c39..81bf9198 100644 --- a/src/Spring/Spring.Core/Context/Support/ContextRegistry.cs +++ b/src/Spring/Spring.Core/Context/Support/ContextRegistry.cs @@ -163,6 +163,11 @@ namespace Spring.Context.Support lock (syncRoot) { InitializeContextIfNeeded(); + if (rootContextName == null) + { + throw new ApplicationContextException( + "No context registered. Use the 'RegisterContext' method or the 'spring/context' section from your configuration file."); + } return GetContext(rootContextName); } } @@ -195,21 +200,19 @@ namespace Spring.Context.Support { InitializeContextIfNeeded(); IApplicationContext ctx = (IApplicationContext)instance.contextMap[name]; + if (ctx == null) + { + throw new ApplicationContextException(String.Format( + "No context registered under name '{0}'. Use the 'RegisterContext' method or the 'spring/context' section from your configuration file.", + name)); + } #region Instrumentation if (log.IsDebugEnabled) { - if (ctx == null) - { - log.Debug(String.Format( - "No context registered under name '{0}'.", name)); - } - else - { - log.Debug(String.Format( - "Returning context '{0}' registered under name '{1}'.", ctx, name)); - } + log.Debug(String.Format( + "Returning context '{0}' registered under name '{1}'.", ctx, name)); } #endregion diff --git a/test/Spring/Spring.Core.Tests/Context/Support/ContextRegistryTests.cs b/test/Spring/Spring.Core.Tests/Context/Support/ContextRegistryTests.cs index 89e8df81..af19dbaa 100644 --- a/test/Spring/Spring.Core.Tests/Context/Support/ContextRegistryTests.cs +++ b/test/Spring/Spring.Core.Tests/Context/Support/ContextRegistryTests.cs @@ -61,7 +61,8 @@ namespace Spring.Context.Support new HookableContextHandler.CreateContextFromSectionHandler(GetContextRecursive)); try { - ContextRegistry.GetContext("somename"); + ContextRegistry.GetContext("somename"); + Assert.Fail("Should throw an exception"); } catch(Exception ex) { @@ -127,12 +128,22 @@ namespace Spring.Context.Support 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(); + } + + [Test] - public void GetContextNotRegisteredReturnsNull() + [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"); - Assert.IsNull(context, - "Named context is not null even though a context has not been registered under the lookup name."); } [Test]