[SPR-8386][SPR-8387] Refined logging regarding detection of default resource locations and default configuration classes.
This commit is contained in:
parent
bfbd7def33
commit
9a40021f18
|
@ -131,17 +131,23 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
|
|||
String suffix = getResourceSuffix();
|
||||
Assert.hasText(suffix, "Resource suffix must not be empty");
|
||||
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()) {
|
||||
logger.info(String.format("Could not detect default resource locations for test class [%s]: "
|
||||
+ "classpath resource [%s] does not exist.", clazz.getName(), resourcePath));
|
||||
logger.info(String.format("Detected default resource location \"%s\" for test class [%s].",
|
||||
prefixedResourcePath, clazz.getName()));
|
||||
}
|
||||
return EMPTY_STRING_ARRAY;
|
||||
return new String[] { prefixedResourcePath };
|
||||
}
|
||||
|
||||
// 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 {
|
|||
|
||||
private static void delegateProcessing(SmartContextLoader loader, ContextConfigurationAttributes configAttributes) {
|
||||
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));
|
||||
}
|
||||
loader.processContextConfiguration(configAttributes);
|
||||
|
@ -77,7 +77,7 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
|
|||
if (configAttributes.hasLocations() && configAttributes.hasClasses()) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"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
|
||||
|
@ -98,14 +98,14 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
|
|||
|
||||
if (xmlLoaderDetectedDefaults) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
if (configAttributes.hasClasses()) {
|
||||
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));
|
||||
}
|
||||
|
||||
|
@ -115,28 +115,28 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
|
|||
if (configAttributes.hasClasses()) {
|
||||
if (logger.isInfoEnabled()) {
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
if (!xmlLoaderDetectedDefaults && configAttributes.hasLocations()) {
|
||||
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));
|
||||
}
|
||||
|
||||
// If neither loader detected defaults, throw an exception.
|
||||
if (!configAttributes.hasResources()) {
|
||||
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));
|
||||
}
|
||||
|
||||
if (configAttributes.hasLocations() && configAttributes.hasClasses()) {
|
||||
String message = String.format(
|
||||
"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);
|
||||
logger.error(message);
|
||||
throw new IllegalStateException(message);
|
||||
|
@ -157,15 +157,14 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
|
|||
for (SmartContextLoader loader : candidates) {
|
||||
if (supports(loader, mergedConfig)) {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("Delegating to %s to load context from [%s].", name(loader),
|
||||
mergedConfig));
|
||||
logger.debug(String.format("Delegating to %s to load context from %s.", name(loader), mergedConfig));
|
||||
}
|
||||
return loader.loadContext(mergedConfig);
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
|
|
|
@ -172,6 +172,10 @@ public class DelegatingSmartContextLoaderTests {
|
|||
return new String("foo");
|
||||
}
|
||||
}
|
||||
|
||||
static class NotAConfigClass {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static class ImproperDuplicateDefaultXmlAndConfigClassTestCase {
|
||||
|
|
|
@ -21,14 +21,16 @@
|
|||
<logger name="org.springframework.test.context.ContextLoaderUtils">
|
||||
<level value="warn" />
|
||||
</logger>
|
||||
<!--
|
||||
<logger name="org.springframework.test.context.support.AbstractGenericContextLoader">
|
||||
<level value="warn" />
|
||||
<level value="info" />
|
||||
</logger>
|
||||
<logger name="org.springframework.test.context.support.AnnotationConfigContextLoader">
|
||||
<level value="warn" />
|
||||
<level value="info" />
|
||||
</logger>
|
||||
-->
|
||||
<logger name="org.springframework.test.context.support">
|
||||
<level value="warn" />
|
||||
<level value="fatal" />
|
||||
</logger>
|
||||
|
||||
<!-- Root Logger -->
|
||||
|
|
Loading…
Reference in New Issue