ContextRegistry.GetContext(string name) should throw an exception if no context has been registered under that name. [SPRNET-991]

This commit is contained in:
bbaia
2008-08-05 18:29:52 +00:00
parent 4fce21df8d
commit b69f1331aa
2 changed files with 28 additions and 14 deletions

View File

@@ -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

View File

@@ -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]