Skip to content

Commit 4cbe2ae

Browse files
committed
[SPR-8387] Introduced supports(MergedContextConfiguration) method in the SmartContextLoader SPI; updated existing loaders accordingly; and fleshed out implementation of and tests for the new DelegatingSmartContextLoader.
1 parent 19fc200 commit 4cbe2ae

19 files changed

+432
-28
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ public interface SmartContextLoader extends ContextLoader {
9595
*/
9696
void processContextConfiguration(ContextConfigurationAttributes configAttributes);
9797

98+
/**
99+
* TODO Document supports(MergedContextConfiguration).
100+
*
101+
* @param mergedConfig
102+
* @return
103+
*/
104+
boolean supports(MergedContextConfiguration mergedConfig);
105+
98106
/**
99107
* Loads a new {@link ApplicationContext context} based on the supplied
100108
* {@link MergedContextConfiguration merged context configuration},

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.springframework.core.io.support.ResourcePatternUtils;
2424
import org.springframework.test.context.ContextConfigurationAttributes;
2525
import org.springframework.test.context.ContextLoader;
26+
import org.springframework.test.context.MergedContextConfiguration;
2627
import org.springframework.test.context.SmartContextLoader;
2728
import org.springframework.util.Assert;
2829
import org.springframework.util.ClassUtils;
@@ -90,6 +91,13 @@ public void processContextConfiguration(ContextConfigurationAttributes configAtt
9091
configAttributes.setLocations(processedLocations);
9192
}
9293

94+
/**
95+
* TODO Document default supports(MergedContextConfiguration) implementation.
96+
*/
97+
public boolean supports(MergedContextConfiguration mergedConfig) {
98+
return !ObjectUtils.isEmpty(mergedConfig.getLocations());
99+
}
100+
93101
// --- ContextLoader -------------------------------------------------------
94102

95103
/**

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ public void processContextConfiguration(ContextConfigurationAttributes configAtt
8181
}
8282
}
8383

84+
/**
85+
* TODO Document overridden supports(MergedContextConfiguration) implementation.
86+
*/
87+
@Override
88+
public boolean supports(MergedContextConfiguration mergedConfig) {
89+
return ObjectUtils.isEmpty(mergedConfig.getLocations()) && !ObjectUtils.isEmpty(mergedConfig.getClasses());
90+
}
91+
8492
// --- AnnotationConfigContextLoader ---------------------------------------
8593

8694
private boolean isStaticNonPrivateAndNonFinal(Class<?> clazz) {

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

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.test.context.ContextLoader;
2727
import org.springframework.test.context.MergedContextConfiguration;
2828
import org.springframework.test.context.SmartContextLoader;
29+
import org.springframework.util.ObjectUtils;
2930

3031
/**
3132
* TODO Document DelegatingSmartContextLoader.
@@ -62,6 +63,11 @@ public boolean generatesDefaults() {
6263
* TODO Document processContextConfiguration() implementation.
6364
*/
6465
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
66+
67+
final String[] originalLocations = configAttributes.getLocations();
68+
final Class<?>[] originalClasses = configAttributes.getClasses();
69+
final boolean emptyResources = ObjectUtils.isEmpty(originalLocations) && ObjectUtils.isEmpty(originalClasses);
70+
6571
for (SmartContextLoader loader : candidates) {
6672
if (logger.isDebugEnabled()) {
6773
logger.debug(String.format("Delegating to loader [%s] to process context configuration [%s].",
@@ -73,7 +79,9 @@ public void processContextConfiguration(ContextConfigurationAttributes configAtt
7379
// If the original locations and classes are not empty, there's no
7480
// need to bother with default generation checks; just let each
7581
// loader process the configuration.
76-
//
82+
if (!emptyResources) {
83+
loader.processContextConfiguration(configAttributes);
84+
}
7785
// Otherwise, if a loader claims to generate defaults, let it
7886
// process the configuration, and then verify that it actually did
7987
// generate defaults.
@@ -83,9 +91,26 @@ public void processContextConfiguration(ContextConfigurationAttributes configAtt
8391
// 1) stop iterating
8492
// 2) mark the current loader as the winning candidate (?)
8593
// 3) log an info message.
94+
else {
95+
if (loader.generatesDefaults()) {
96+
loader.processContextConfiguration(configAttributes);
97+
}
98+
}
99+
}
100+
// If any loader claims to generate defaults but none actually did,
101+
// throw an exception.
102+
}
86103

87-
loader.processContextConfiguration(configAttributes);
104+
/**
105+
* TODO Document supports(MergedContextConfiguration) implementation.
106+
*/
107+
public boolean supports(MergedContextConfiguration mergedConfig) {
108+
for (SmartContextLoader loader : candidates) {
109+
if (loader.supports(mergedConfig)) {
110+
return true;
111+
}
88112
}
113+
return false;
89114
}
90115

91116
/**
@@ -99,21 +124,16 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) t
99124
loader.getClass().getName(), mergedConfig));
100125
}
101126

102-
// TODO Implement loadContext(MergedContextConfiguration).
103-
//
104-
// Ask each loader if it _can_ load a context from the mergedConfig.
127+
// Ask each loader if it can load a context from the mergedConfig.
105128
// If a loader can, let it; otherwise, continue iterating over all
106129
// remaining candidates.
107-
//
108-
// If no candidate can load a context from the mergedConfig, throw
109-
// an exception.
130+
if (loader.supports(mergedConfig)) {
131+
return loader.loadContext(mergedConfig);
132+
}
110133
}
111134

112-
// TODO Implement delegation logic.
113-
//
114-
// Proof of concept: ensuring that hard-coded delegation to
115-
// GenericXmlContextLoader works "as is".
116-
return candidates.get(0).loadContext(mergedConfig);
135+
throw new IllegalStateException(String.format("None of the candidate SmartContextLoaders [%s] "
136+
+ "was able to load an ApplicationContext from [%s].", candidates, mergedConfig));
117137
}
118138

119139
// --- ContextLoader -------------------------------------------------------

org.springframework.test/src/test/java/org/springframework/test/context/junit4/SpringJUnit4SuiteTests.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@
2626
import org.springframework.test.context.junit4.annotation.BeanOverridingExplicitConfigClassesInheritedTests;
2727
import org.springframework.test.context.junit4.annotation.DefaultConfigClassesBaseTests;
2828
import org.springframework.test.context.junit4.annotation.DefaultConfigClassesInheritedTests;
29+
import org.springframework.test.context.junit4.annotation.DefaultLoaderBeanOverridingDefaultConfigClassesInheritedTests;
30+
import org.springframework.test.context.junit4.annotation.DefaultLoaderBeanOverridingExplicitConfigClassesInheritedTests;
31+
import org.springframework.test.context.junit4.annotation.DefaultLoaderDefaultConfigClassesBaseTests;
32+
import org.springframework.test.context.junit4.annotation.DefaultLoaderDefaultConfigClassesInheritedTests;
33+
import org.springframework.test.context.junit4.annotation.DefaultLoaderExplicitConfigClassesBaseTests;
34+
import org.springframework.test.context.junit4.annotation.DefaultLoaderExplicitConfigClassesInheritedTests;
35+
import org.springframework.test.context.junit4.annotation.ExplicitConfigClassesBaseTests;
2936
import org.springframework.test.context.junit4.annotation.ExplicitConfigClassesInheritedTests;
3037
import org.springframework.test.context.junit4.orm.HibernateSessionFlushingTests;
3138
import org.springframework.test.context.junit4.profile.annotation.DefaultProfileAnnotationConfigTests;
@@ -58,12 +65,19 @@
5865
DefaultConfigClassesBaseTests.class,//
5966
DefaultConfigClassesInheritedTests.class,//
6067
BeanOverridingDefaultConfigClassesInheritedTests.class,//
68+
ExplicitConfigClassesBaseTests.class,//
6169
ExplicitConfigClassesInheritedTests.class,//
6270
BeanOverridingExplicitConfigClassesInheritedTests.class,//
71+
DefaultLoaderDefaultConfigClassesBaseTests.class,//
72+
DefaultLoaderDefaultConfigClassesInheritedTests.class,//
73+
DefaultLoaderBeanOverridingDefaultConfigClassesInheritedTests.class,//
74+
DefaultLoaderExplicitConfigClassesBaseTests.class,//
75+
DefaultLoaderExplicitConfigClassesInheritedTests.class,//
76+
DefaultLoaderBeanOverridingExplicitConfigClassesInheritedTests.class,//
6377
DefaultProfileAnnotationConfigTests.class,//
64-
DevProfileAnnotationConfigTests.class, //
78+
DevProfileAnnotationConfigTests.class,//
6579
DefaultProfileXmlConfigTests.class,//
66-
DevProfileXmlConfigTests.class, //
80+
DevProfileXmlConfigTests.class,//
6781
ExpectedExceptionSpringRunnerTests.class,//
6882
TimedSpringRunnerTests.class,//
6983
RepeatedSpringRunnerTests.class,//

org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import org.springframework.test.context.ContextConfiguration;
2020
import org.springframework.test.context.junit4.SpringJUnit4ClassRunnerAppCtxTests;
21-
import org.springframework.test.context.support.AnnotationConfigContextLoader;
2221

2322
/**
2423
* Integration tests that verify support for configuration classes in
@@ -34,7 +33,7 @@
3433
* @author Sam Brannen
3534
* @since 3.1
3635
*/
37-
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = PojoAndStringConfig.class, inheritLocations = false)
36+
@ContextConfiguration(classes = PojoAndStringConfig.class, inheritLocations = false)
3837
public class AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
3938
/* all tests are in the parent class. */
4039
}

org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigTestSuite.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,15 @@
3434
DefaultConfigClassesBaseTests.class,//
3535
DefaultConfigClassesInheritedTests.class,//
3636
BeanOverridingDefaultConfigClassesInheritedTests.class,//
37+
ExplicitConfigClassesBaseTests.class,//
38+
ExplicitConfigClassesInheritedTests.class,//
3739
BeanOverridingExplicitConfigClassesInheritedTests.class,//
38-
ExplicitConfigClassesInheritedTests.class //
40+
DefaultLoaderDefaultConfigClassesBaseTests.class,//
41+
DefaultLoaderDefaultConfigClassesInheritedTests.class,//
42+
DefaultLoaderBeanOverridingDefaultConfigClassesInheritedTests.class,//
43+
DefaultLoaderExplicitConfigClassesBaseTests.class,//
44+
DefaultLoaderExplicitConfigClassesInheritedTests.class,//
45+
DefaultLoaderBeanOverridingExplicitConfigClassesInheritedTests.class //
3946
})
4047
public class AnnotationConfigTestSuite {
4148
}

org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/BeanOverridingDefaultConfigClassesInheritedTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
* Integration tests that verify support for configuration classes in
3030
* the Spring TestContext Framework.
3131
*
32-
* <p>Configuration will be loaded from {@link DefaultConfigClassesBaseTestsConfig}
33-
* and {@link BeanOverridingDefaultConfigClassesInheritedTestsConfig}.
32+
* <p>Configuration will be loaded from {@link DefaultConfigClassesBaseTests.ContextConfiguration}
33+
* and {@link BeanOverridingDefaultConfigClassesInheritedTests.ContextConfiguration}.
3434
*
3535
* @author Sam Brannen
3636
* @since 3.1

org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/BeanOverridingExplicitConfigClassesInheritedTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
* Integration tests that verify support for configuration classes in
2727
* the Spring TestContext Framework.
2828
*
29-
* <p>Configuration will be loaded from {@link DefaultConfigClassesBaseTestsConfig}
30-
* and {@link BeanOverridingDefaultConfigClassesInheritedTestsConfig}.
29+
* <p>Configuration will be loaded from {@link DefaultConfigClassesBaseTests.ContextConfiguration}
30+
* and {@link BeanOverridingDefaultConfigClassesInheritedTests.ContextConfiguration}.
3131
*
3232
* @author Sam Brannen
3333
* @since 3.1

org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassesBaseTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* Integration tests that verify support for configuration classes in
3434
* the Spring TestContext Framework.
3535
*
36-
* <p>Configuration will be loaded from {@link DefaultConfigClassesBaseTestsConfig}.
36+
* <p>Configuration will be loaded from {@link DefaultConfigClassesBaseTests.ContextConfiguration}.
3737
*
3838
* @author Sam Brannen
3939
* @since 3.1

0 commit comments

Comments
 (0)