Skip to content

Commit 9a40021

Browse files
committed
[SPR-8386][SPR-8387] Refined logging regarding detection of default resource locations and default configuration classes.
1 parent bfbd7de commit 9a40021

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,23 @@ protected String[] generateDefaultLocations(Class<?> clazz) {
131131
String suffix = getResourceSuffix();
132132
Assert.hasText(suffix, "Resource suffix must not be empty");
133133
String resourcePath = SLASH + ClassUtils.convertClassNameToResourcePath(clazz.getName()) + suffix;
134+
String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath;
135+
ClassPathResource classPathResource = new ClassPathResource(resourcePath, clazz);
134136

135-
if (!new ClassPathResource(resourcePath, clazz).exists()) {
137+
if (classPathResource.exists()) {
136138
if (logger.isInfoEnabled()) {
137-
logger.info(String.format("Could not detect default resource locations for test class [%s]: "
138-
+ "classpath resource [%s] does not exist.", clazz.getName(), resourcePath));
139+
logger.info(String.format("Detected default resource location \"%s\" for test class [%s].",
140+
prefixedResourcePath, clazz.getName()));
139141
}
140-
return EMPTY_STRING_ARRAY;
142+
return new String[] { prefixedResourcePath };
141143
}
142144

143145
// else
144-
return new String[] { ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath };
146+
if (logger.isInfoEnabled()) {
147+
logger.info(String.format("Could not detect default resource locations for test class [%s]: "
148+
+ "%s does not exist.", clazz.getName(), classPathResource));
149+
}
150+
return EMPTY_STRING_ARRAY;
145151
}
146152

147153
/**

org.springframework.test/src/main/java/org/springframework/test/context/support/DelegatingSmartContextLoader.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ private static String name(SmartContextLoader loader) {
5454

5555
private static void delegateProcessing(SmartContextLoader loader, ContextConfigurationAttributes configAttributes) {
5656
if (logger.isDebugEnabled()) {
57-
logger.debug(String.format("Delegating to %s to process context configuration [%s].", name(loader),
57+
logger.debug(String.format("Delegating to %s to process context configuration %s.", name(loader),
5858
configAttributes));
5959
}
6060
loader.processContextConfiguration(configAttributes);
@@ -77,7 +77,7 @@ public void processContextConfiguration(final ContextConfigurationAttributes con
7777
if (configAttributes.hasLocations() && configAttributes.hasClasses()) {
7878
throw new IllegalStateException(String.format(
7979
"Cannot process locations AND configuration classes for context "
80-
+ "configuration [%s]; configure one or the other, but not both.", configAttributes));
80+
+ "configuration %s; configure one or the other, but not both.", configAttributes));
8181
}
8282

8383
// If the original locations or classes were not empty, there's no
@@ -98,14 +98,14 @@ else if (configAttributes.hasClasses()) {
9898

9999
if (xmlLoaderDetectedDefaults) {
100100
if (logger.isInfoEnabled()) {
101-
logger.info(String.format("%s detected default locations for context configuration [%s].",
101+
logger.info(String.format("%s detected default locations for context configuration %s.",
102102
name(xmlLoader), configAttributes));
103103
}
104104
}
105105

106106
if (configAttributes.hasClasses()) {
107107
throw new IllegalStateException(String.format(
108-
"%s should NOT have detected default configuration classes for context configuration [%s].",
108+
"%s should NOT have detected default configuration classes for context configuration %s.",
109109
name(xmlLoader), configAttributes));
110110
}
111111

@@ -115,28 +115,28 @@ else if (configAttributes.hasClasses()) {
115115
if (configAttributes.hasClasses()) {
116116
if (logger.isInfoEnabled()) {
117117
logger.info(String.format(
118-
"%s detected default configuration classes for context configuration [%s].",
118+
"%s detected default configuration classes for context configuration %s.",
119119
name(annotationLoader), configAttributes));
120120
}
121121
}
122122

123123
if (!xmlLoaderDetectedDefaults && configAttributes.hasLocations()) {
124124
throw new IllegalStateException(String.format(
125-
"%s should NOT have detected default locations for context configuration [%s].",
125+
"%s should NOT have detected default locations for context configuration %s.",
126126
name(annotationLoader), configAttributes));
127127
}
128128

129129
// If neither loader detected defaults, throw an exception.
130130
if (!configAttributes.hasResources()) {
131131
throw new IllegalStateException(String.format(
132-
"Neither %s nor %s was able to detect defaults for context configuration [%s].", name(xmlLoader),
132+
"Neither %s nor %s was able to detect defaults for context configuration %s.", name(xmlLoader),
133133
name(annotationLoader), configAttributes));
134134
}
135135

136136
if (configAttributes.hasLocations() && configAttributes.hasClasses()) {
137137
String message = String.format(
138138
"Configuration error: both default locations AND default configuration classes "
139-
+ "were detected for context configuration [%s]; configure one or the other, but not both.",
139+
+ "were detected for context configuration %s; configure one or the other, but not both.",
140140
configAttributes);
141141
logger.error(message);
142142
throw new IllegalStateException(message);
@@ -157,15 +157,14 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
157157
for (SmartContextLoader loader : candidates) {
158158
if (supports(loader, mergedConfig)) {
159159
if (logger.isDebugEnabled()) {
160-
logger.debug(String.format("Delegating to %s to load context from [%s].", name(loader),
161-
mergedConfig));
160+
logger.debug(String.format("Delegating to %s to load context from %s.", name(loader), mergedConfig));
162161
}
163162
return loader.loadContext(mergedConfig);
164163
}
165164
}
166165

167166
throw new IllegalStateException(String.format(
168-
"Neither %s nor %s was able to load an ApplicationContext from [%s].", name(xmlLoader),
167+
"Neither %s nor %s was able to load an ApplicationContext from %s.", name(xmlLoader),
169168
name(annotationLoader), mergedConfig));
170169
}
171170

org.springframework.test/src/test/java/org/springframework/test/context/support/DelegatingSmartContextLoaderTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ public String foo() {
172172
return new String("foo");
173173
}
174174
}
175+
176+
static class NotAConfigClass {
177+
178+
}
175179
}
176180

177181
static class ImproperDuplicateDefaultXmlAndConfigClassTestCase {

org.springframework.test/src/test/resources/log4j.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,16 @@
2121
<logger name="org.springframework.test.context.ContextLoaderUtils">
2222
<level value="warn" />
2323
</logger>
24+
<!--
2425
<logger name="org.springframework.test.context.support.AbstractGenericContextLoader">
25-
<level value="warn" />
26+
<level value="info" />
2627
</logger>
2728
<logger name="org.springframework.test.context.support.AnnotationConfigContextLoader">
28-
<level value="warn" />
29+
<level value="info" />
2930
</logger>
31+
-->
3032
<logger name="org.springframework.test.context.support">
31-
<level value="warn" />
33+
<level value="fatal" />
3234
</logger>
3335

3436
<!-- Root Logger -->

0 commit comments

Comments
 (0)