Skip to content

Commit 12eb9d7

Browse files
committed
[SPR-8387] Fleshing out unit tests for DelegatingSmartContextLoader.
1 parent cc725d7 commit 12eb9d7

File tree

4 files changed

+53
-16
lines changed

4 files changed

+53
-16
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
*/
5050
public class MergedContextConfiguration {
5151

52-
private static final String[] EMPTY_STRING_ARRAY = new String[] {};
53-
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[] {};
52+
private static final String[] EMPTY_STRING_ARRAY = new String[0];
53+
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
5454

5555
private final Class<?> testClass;
5656

@@ -85,6 +85,13 @@ private static String[] processActiveProfiles(String[] activeProfiles) {
8585
return StringUtils.toStringArray(sortedProfilesSet);
8686
}
8787

88+
/**
89+
* Generate a null-safe {@link String} representation of the supplied {@link ContextLoader}.
90+
*/
91+
private static String nullSafeToString(ContextLoader contextLoader) {
92+
return contextLoader == null ? "null" : contextLoader.getClass().getName();
93+
}
94+
8895
/**
8996
* Generate a context <em>key</em> from the supplied values.
9097
*/
@@ -94,7 +101,7 @@ private static String generateContextKey(String[] locations, Class<?>[] classes,
94101
String locationsKey = ObjectUtils.nullSafeToString(locations);
95102
String classesKey = ObjectUtils.nullSafeToString(classes);
96103
String activeProfilesKey = ObjectUtils.nullSafeToString(activeProfiles);
97-
String contextLoaderKey = contextLoader == null ? "null" : contextLoader.getClass().getName();
104+
String contextLoaderKey = nullSafeToString(contextLoader);
98105

99106
return String.format("locations = %s, classes = %s, activeProfiles = %s, contextLoader = %s", locationsKey,
100107
classesKey, activeProfilesKey, contextLoaderKey);
@@ -187,7 +194,7 @@ public String toString() {
187194
.append("locations", ObjectUtils.nullSafeToString(this.locations))//
188195
.append("classes", ObjectUtils.nullSafeToString(this.classes))//
189196
.append("activeProfiles", ObjectUtils.nullSafeToString(this.activeProfiles))//
190-
.append("contextLoader", this.contextLoader)//
197+
.append("contextLoader", nullSafeToString(contextLoader))//
191198
.append("contextKey", this.contextKey)//
192199
.toString();
193200
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void processContextConfiguration(ContextConfigurationAttributes configAtt
9595
* TODO Document default supports(MergedContextConfiguration) implementation.
9696
*/
9797
public boolean supports(MergedContextConfiguration mergedConfig) {
98-
return !ObjectUtils.isEmpty(mergedConfig.getLocations());
98+
return !ObjectUtils.isEmpty(mergedConfig.getLocations()) && ObjectUtils.isEmpty(mergedConfig.getClasses());
9999
}
100100

101101
// --- ContextLoader -------------------------------------------------------

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,20 @@ public boolean supports(MergedContextConfiguration mergedConfig) {
119119
public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
120120

121121
for (SmartContextLoader loader : candidates) {
122-
if (logger.isDebugEnabled()) {
123-
logger.debug(String.format("Delegating to %s to load context from [%s].", loader.getClass().getName(),
124-
mergedConfig));
125-
}
126122

127123
// Ask each loader if it can load a context from the mergedConfig.
128124
// If a loader can, let it; otherwise, continue iterating over all
129125
// remaining candidates.
130126
if (loader.supports(mergedConfig)) {
127+
if (logger.isDebugEnabled()) {
128+
logger.debug(String.format("Delegating to %s to load context from [%s].",
129+
loader.getClass().getName(), mergedConfig));
130+
}
131131
return loader.loadContext(mergedConfig);
132132
}
133133
}
134134

135-
throw new IllegalStateException(String.format("None of the candidate SmartContextLoaders %s "
135+
throw new IllegalStateException(String.format("None of the SmartContextLoader candidates %s "
136136
+ "was able to load an ApplicationContext from [%s].", candidates, mergedConfig));
137137
}
138138

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

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
package org.springframework.test.context.support;
1818

19+
import static org.junit.Assert.assertFalse;
20+
import static org.junit.Assert.assertTrue;
21+
1922
import org.junit.Test;
23+
import org.springframework.test.context.MergedContextConfiguration;
2024

2125
/**
2226
* Unit tests for {@link DelegatingSmartContextLoader}.
@@ -26,14 +30,17 @@
2630
*/
2731
public class DelegatingSmartContextLoaderTests {
2832

29-
private DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader();
33+
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
34+
private static final String[] EMPTY_STRING_ARRAY = new String[0];
35+
36+
private final DelegatingSmartContextLoader loader = new DelegatingSmartContextLoader();
3037

3138

3239
// --- SmartContextLoader --------------------------------------------------
3340

3441
@Test
3542
public void generatesDefaults() {
36-
// TODO test generateDefaults().
43+
assertTrue(loader.generatesDefaults());
3744
}
3845

3946
@Test
@@ -42,8 +49,31 @@ public void processContextConfiguration() {
4249
}
4350

4451
@Test
45-
public void supports() {
46-
// TODO test supports().
52+
public void doesNotSupportEmptyConfig() {
53+
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
54+
EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
55+
assertFalse(loader.supports(mergedConfig));
56+
}
57+
58+
@Test
59+
public void doesNotSupportLocationsAndConfigurationClasses() {
60+
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(),
61+
new String[] { "foo.xml" }, new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
62+
assertFalse(loader.supports(mergedConfig));
63+
}
64+
65+
@Test
66+
public void supportsLocations() {
67+
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(),
68+
new String[] { "foo.xml" }, EMPTY_CLASS_ARRAY, EMPTY_STRING_ARRAY, loader);
69+
assertTrue(loader.supports(mergedConfig));
70+
}
71+
72+
@Test
73+
public void supportsConfigurationClasses() {
74+
MergedContextConfiguration mergedConfig = new MergedContextConfiguration(getClass(), EMPTY_STRING_ARRAY,
75+
new Class<?>[] { getClass() }, EMPTY_STRING_ARRAY, loader);
76+
assertTrue(loader.supports(mergedConfig));
4777
}
4878

4979
@Test
@@ -55,12 +85,12 @@ public void loadContext() {
5585

5686
@Test(expected = UnsupportedOperationException.class)
5787
public void processLocations() {
58-
loader.processLocations(getClass(), new String[0]);
88+
loader.processLocations(getClass(), EMPTY_STRING_ARRAY);
5989
}
6090

6191
@Test(expected = UnsupportedOperationException.class)
6292
public void loadContextFromLocations() throws Exception {
63-
loader.loadContext(new String[0]);
93+
loader.loadContext(EMPTY_STRING_ARRAY);
6494
}
6595

6696
}

0 commit comments

Comments
 (0)