From 9a40021f18bc0493c247d5fe29b05d5d76a9c05a Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sat, 13 Aug 2011 15:38:45 +0000 Subject: [PATCH] [SPR-8386][SPR-8387] Refined logging regarding detection of default resource locations and default configuration classes. --- .../support/AbstractContextLoader.java | 16 +++++++++----- .../support/DelegatingSmartContextLoader.java | 21 +++++++++---------- .../DelegatingSmartContextLoaderTests.java | 4 ++++ .../src/test/resources/log4j.xml | 8 ++++--- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java b/org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java index 77ce7fbb79..c4fcf8a221 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java @@ -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; } /** diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/support/DelegatingSmartContextLoader.java b/org.springframework.test/src/main/java/org/springframework/test/context/support/DelegatingSmartContextLoader.java index c68c10a68a..2915cf96a0 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/support/DelegatingSmartContextLoader.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/support/DelegatingSmartContextLoader.java @@ -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)); } diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/support/DelegatingSmartContextLoaderTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/support/DelegatingSmartContextLoaderTests.java index 4b53ea00da..dc9121085b 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/support/DelegatingSmartContextLoaderTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/support/DelegatingSmartContextLoaderTests.java @@ -172,6 +172,10 @@ public class DelegatingSmartContextLoaderTests { return new String("foo"); } } + + static class NotAConfigClass { + + } } static class ImproperDuplicateDefaultXmlAndConfigClassTestCase { diff --git a/org.springframework.test/src/test/resources/log4j.xml b/org.springframework.test/src/test/resources/log4j.xml index d9a255f08a..326b5d0d2e 100644 --- a/org.springframework.test/src/test/resources/log4j.xml +++ b/org.springframework.test/src/test/resources/log4j.xml @@ -21,14 +21,16 @@ + - + -- GitLab