Skip to content

Commit b8624b4

Browse files
committed
[SPR-8387] Introduced hasResources() in ContextConfigurationAttributes; plus minor polishing.
1 parent d2e6f82 commit b8624b4

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

org.springframework.test/src/main/java/org/springframework/test/context/ContextConfigurationAttributes.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public class ContextConfigurationAttributes {
5555
*
5656
* @throws IllegalStateException if both the locations and value attributes have been declared
5757
*/
58-
static String[] resolveLocations(Class<?> declaringClass, ContextConfiguration contextConfiguration) {
58+
private static String[] resolveLocations(Class<?> declaringClass, ContextConfiguration contextConfiguration) {
5959
Assert.notNull(declaringClass, "declaringClass must not be null");
6060

6161
String[] locations = contextConfiguration.locations();
@@ -165,6 +165,18 @@ public void setClasses(Class<?>[] classes) {
165165
this.classes = classes;
166166
}
167167

168+
/**
169+
* Determine if this {@code ContextConfigurationAttributes} instance has
170+
* either path-based resource locations or class-based resources.
171+
* @return <code>true</code> if neither the {@link #getLocations() locations}
172+
* nor the {@link #getClasses() classes} array is empty
173+
* @see #getLocations()
174+
* @see #getClasses()
175+
*/
176+
public boolean hasResources() {
177+
return !ObjectUtils.isEmpty(getLocations()) && !ObjectUtils.isEmpty(getClasses());
178+
}
179+
168180
/**
169181
* Get the <code>inheritLocations</code> flag that was declared via
170182
* {@link ContextConfiguration @ContextConfiguration}.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,16 @@ protected Class<?>[] generateDefaultConfigurationClasses(Class<?> declaringClass
138138

139139
List<Class<?>> configClasses = new ArrayList<Class<?>>();
140140

141-
for (Class<?> configClass : declaringClass.getDeclaredClasses()) {
142-
if (isDefaultConfigurationClassCandidate(configClass)) {
143-
configClasses.add(configClass);
141+
for (Class<?> candidate : declaringClass.getDeclaredClasses()) {
142+
if (isDefaultConfigurationClassCandidate(candidate)) {
143+
configClasses.add(candidate);
144144
}
145145
else {
146146
if (logger.isDebugEnabled()) {
147147
logger.debug(String.format(
148148
"Ignoring class [%s]; it must be static, non-private, non-final, and annotated "
149149
+ "with @Configuration to be considered a default configuration class.",
150-
configClass.getName()));
150+
candidate.getName()));
151151
}
152152
}
153153
}

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

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.springframework.test.context.MergedContextConfiguration;
2828
import org.springframework.test.context.SmartContextLoader;
2929
import org.springframework.util.Assert;
30-
import org.springframework.util.ObjectUtils;
3130

3231
/**
3332
* TODO Document DelegatingSmartContextLoader.
@@ -60,38 +59,30 @@ public boolean generatesDefaults() {
6059
return false;
6160
}
6261

63-
/**
64-
* TODO Document emptyResources().
65-
*/
66-
private boolean emptyResources(ContextConfigurationAttributes configAttributes) {
67-
return ObjectUtils.isEmpty(configAttributes.getLocations())
68-
&& ObjectUtils.isEmpty(configAttributes.getClasses());
69-
}
70-
7162
/**
7263
* TODO Document processContextConfiguration() implementation.
7364
*/
7465
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
7566

76-
final boolean emptyResources = emptyResources(configAttributes);
67+
final boolean hasResources = configAttributes.hasResources();
7768

7869
for (SmartContextLoader loader : candidates) {
7970
if (logger.isDebugEnabled()) {
80-
logger.debug(String.format("Delegating to %s to process context configuration [%s].",
71+
logger.debug(String.format("Potentially delegating to %s to process context configuration [%s].",
8172
loader.getClass().getName(), configAttributes));
8273
}
8374

8475
// If the original locations and classes were not empty, there's no
8576
// need to bother with default generation checks; just let each
8677
// loader process the configuration.
87-
if (!emptyResources) {
78+
if (hasResources) {
8879
loader.processContextConfiguration(configAttributes);
8980
}
9081
// Otherwise, if the loader claims to generate defaults, let it
9182
// process the configuration.
9283
else if (loader.generatesDefaults()) {
9384
loader.processContextConfiguration(configAttributes);
94-
if (!emptyResources(configAttributes) && logger.isInfoEnabled()) {
85+
if (configAttributes.hasResources() && logger.isInfoEnabled()) {
9586
logger.info(String.format("SmartContextLoader candidate %s "
9687
+ "generated defaults for context configuration [%s].", loader, configAttributes));
9788
}
@@ -100,7 +91,7 @@ else if (loader.generatesDefaults()) {
10091

10192
// If any loader claims to generate defaults but none actually did,
10293
// throw an exception.
103-
if (generatesDefaults() && emptyResources(configAttributes)) {
94+
if (generatesDefaults() && !configAttributes.hasResources()) {
10495
throw new IllegalStateException(String.format("None of the SmartContextLoader candidates %s "
10596
+ "was able to generate defaults for context configuration [%s].", candidates, configAttributes));
10697
}
@@ -128,10 +119,8 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
128119
Assert.notNull(mergedConfig, "mergedConfig must not be null");
129120

130121
for (SmartContextLoader loader : candidates) {
131-
132122
// Ask each loader if it can load a context from the mergedConfig.
133-
// If a loader can, let it; otherwise, continue iterating over all
134-
// remaining candidates.
123+
// If it can, let it; otherwise, keep iterating.
135124
if (loader.supports(mergedConfig)) {
136125
if (logger.isDebugEnabled()) {
137126
logger.debug(String.format("Delegating to %s to load context from [%s].",

0 commit comments

Comments
 (0)