提交 9a40021f 编写于 作者: S Sam Brannen

[SPR-8386][SPR-8387] Refined logging regarding detection of default resource...

[SPR-8386][SPR-8387] Refined logging regarding detection of default resource locations and default configuration classes.
上级 bfbd7def
...@@ -131,17 +131,23 @@ public abstract class AbstractContextLoader implements SmartContextLoader { ...@@ -131,17 +131,23 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
String suffix = getResourceSuffix(); String suffix = getResourceSuffix();
Assert.hasText(suffix, "Resource suffix must not be empty"); Assert.hasText(suffix, "Resource suffix must not be empty");
String resourcePath = SLASH + ClassUtils.convertClassNameToResourcePath(clazz.getName()) + suffix; String resourcePath = SLASH + ClassUtils.convertClassNameToResourcePath(clazz.getName()) + suffix;
String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath;
ClassPathResource classPathResource = new ClassPathResource(resourcePath, clazz);
if (!new ClassPathResource(resourcePath, clazz).exists()) { if (classPathResource.exists()) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info(String.format("Could not detect default resource locations for test class [%s]: " logger.info(String.format("Detected default resource location \"%s\" for test class [%s].",
+ "classpath resource [%s] does not exist.", clazz.getName(), resourcePath)); prefixedResourcePath, clazz.getName()));
} }
return EMPTY_STRING_ARRAY; return new String[] { prefixedResourcePath };
} }
// else // else
return new String[] { ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath }; if (logger.isInfoEnabled()) {
logger.info(String.format("Could not detect default resource locations for test class [%s]: "
+ "%s does not exist.", clazz.getName(), classPathResource));
}
return EMPTY_STRING_ARRAY;
} }
/** /**
......
...@@ -54,7 +54,7 @@ public class DelegatingSmartContextLoader implements SmartContextLoader { ...@@ -54,7 +54,7 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
private static void delegateProcessing(SmartContextLoader loader, ContextConfigurationAttributes configAttributes) { private static void delegateProcessing(SmartContextLoader loader, ContextConfigurationAttributes configAttributes) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Delegating to %s to process context configuration [%s].", name(loader), logger.debug(String.format("Delegating to %s to process context configuration %s.", name(loader),
configAttributes)); configAttributes));
} }
loader.processContextConfiguration(configAttributes); loader.processContextConfiguration(configAttributes);
...@@ -77,7 +77,7 @@ public class DelegatingSmartContextLoader implements SmartContextLoader { ...@@ -77,7 +77,7 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
if (configAttributes.hasLocations() && configAttributes.hasClasses()) { if (configAttributes.hasLocations() && configAttributes.hasClasses()) {
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
"Cannot process locations AND configuration classes for context " "Cannot process locations AND configuration classes for context "
+ "configuration [%s]; configure one or the other, but not both.", configAttributes)); + "configuration %s; configure one or the other, but not both.", configAttributes));
} }
// If the original locations or classes were not empty, there's no // If the original locations or classes were not empty, there's no
...@@ -98,14 +98,14 @@ public class DelegatingSmartContextLoader implements SmartContextLoader { ...@@ -98,14 +98,14 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
if (xmlLoaderDetectedDefaults) { if (xmlLoaderDetectedDefaults) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info(String.format("%s detected default locations for context configuration [%s].", logger.info(String.format("%s detected default locations for context configuration %s.",
name(xmlLoader), configAttributes)); name(xmlLoader), configAttributes));
} }
} }
if (configAttributes.hasClasses()) { if (configAttributes.hasClasses()) {
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
"%s should NOT have detected default configuration classes for context configuration [%s].", "%s should NOT have detected default configuration classes for context configuration %s.",
name(xmlLoader), configAttributes)); name(xmlLoader), configAttributes));
} }
...@@ -115,28 +115,28 @@ public class DelegatingSmartContextLoader implements SmartContextLoader { ...@@ -115,28 +115,28 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
if (configAttributes.hasClasses()) { if (configAttributes.hasClasses()) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info(String.format( logger.info(String.format(
"%s detected default configuration classes for context configuration [%s].", "%s detected default configuration classes for context configuration %s.",
name(annotationLoader), configAttributes)); name(annotationLoader), configAttributes));
} }
} }
if (!xmlLoaderDetectedDefaults && configAttributes.hasLocations()) { if (!xmlLoaderDetectedDefaults && configAttributes.hasLocations()) {
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
"%s should NOT have detected default locations for context configuration [%s].", "%s should NOT have detected default locations for context configuration %s.",
name(annotationLoader), configAttributes)); name(annotationLoader), configAttributes));
} }
// If neither loader detected defaults, throw an exception. // If neither loader detected defaults, throw an exception.
if (!configAttributes.hasResources()) { if (!configAttributes.hasResources()) {
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
"Neither %s nor %s was able to detect defaults for context configuration [%s].", name(xmlLoader), "Neither %s nor %s was able to detect defaults for context configuration %s.", name(xmlLoader),
name(annotationLoader), configAttributes)); name(annotationLoader), configAttributes));
} }
if (configAttributes.hasLocations() && configAttributes.hasClasses()) { if (configAttributes.hasLocations() && configAttributes.hasClasses()) {
String message = String.format( String message = String.format(
"Configuration error: both default locations AND default configuration classes " "Configuration error: both default locations AND default configuration classes "
+ "were detected for context configuration [%s]; configure one or the other, but not both.", + "were detected for context configuration %s; configure one or the other, but not both.",
configAttributes); configAttributes);
logger.error(message); logger.error(message);
throw new IllegalStateException(message); throw new IllegalStateException(message);
...@@ -157,15 +157,14 @@ public class DelegatingSmartContextLoader implements SmartContextLoader { ...@@ -157,15 +157,14 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
for (SmartContextLoader loader : candidates) { for (SmartContextLoader loader : candidates) {
if (supports(loader, mergedConfig)) { if (supports(loader, mergedConfig)) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Delegating to %s to load context from [%s].", name(loader), logger.debug(String.format("Delegating to %s to load context from %s.", name(loader), mergedConfig));
mergedConfig));
} }
return loader.loadContext(mergedConfig); return loader.loadContext(mergedConfig);
} }
} }
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
"Neither %s nor %s was able to load an ApplicationContext from [%s].", name(xmlLoader), "Neither %s nor %s was able to load an ApplicationContext from %s.", name(xmlLoader),
name(annotationLoader), mergedConfig)); name(annotationLoader), mergedConfig));
} }
......
...@@ -172,6 +172,10 @@ public class DelegatingSmartContextLoaderTests { ...@@ -172,6 +172,10 @@ public class DelegatingSmartContextLoaderTests {
return new String("foo"); return new String("foo");
} }
} }
static class NotAConfigClass {
}
} }
static class ImproperDuplicateDefaultXmlAndConfigClassTestCase { static class ImproperDuplicateDefaultXmlAndConfigClassTestCase {
......
...@@ -21,14 +21,16 @@ ...@@ -21,14 +21,16 @@
<logger name="org.springframework.test.context.ContextLoaderUtils"> <logger name="org.springframework.test.context.ContextLoaderUtils">
<level value="warn" /> <level value="warn" />
</logger> </logger>
<!--
<logger name="org.springframework.test.context.support.AbstractGenericContextLoader"> <logger name="org.springframework.test.context.support.AbstractGenericContextLoader">
<level value="warn" /> <level value="info" />
</logger> </logger>
<logger name="org.springframework.test.context.support.AnnotationConfigContextLoader"> <logger name="org.springframework.test.context.support.AnnotationConfigContextLoader">
<level value="warn" /> <level value="info" />
</logger> </logger>
-->
<logger name="org.springframework.test.context.support"> <logger name="org.springframework.test.context.support">
<level value="warn" /> <level value="fatal" />
</logger> </logger>
<!-- Root Logger --> <!-- Root Logger -->
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册