diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoader.java b/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoader.java index 5c125c717d3..3371b880e98 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoader.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoader.java @@ -19,8 +19,7 @@ package org.springframework.test.context; import org.springframework.context.ApplicationContext; /** - * Strategy interface for loading an - * {@link ApplicationContext application context}. + * Strategy interface for loading an {@link ApplicationContext application context}. * *
Clients of a ContextLoader should call * {@link #processLocations(Class,String...) processLocations()} prior to @@ -34,6 +33,7 @@ import org.springframework.context.ApplicationContext; * *
Spring provides the following out-of-the-box implementations: *
true if this ContextLoader supports
+ * String-based resource locations
+ * @see ContextConfiguration#locations()
+ * @see ContextConfiguration#value()
+ */
+ boolean supportsStringResources();
+
+ /**
+ * @return true if this ContextLoader supports
+ * Class-based resource locations
+ * @see ContextConfiguration#classes()
+ */
+ boolean supportsClassResources();
+
+}
diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java b/org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java
index 77dd4a901ec..e741f233417 100644
--- a/org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java
+++ b/org.springframework.test/src/main/java/org/springframework/test/context/TestContext.java
@@ -29,7 +29,6 @@ import org.springframework.context.ApplicationContext;
import org.springframework.core.AttributeAccessorSupport;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.style.ToStringCreator;
-import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
@@ -231,9 +230,9 @@ public class TestContext extends AttributeAccessorSupport {
// TODO [SPR-6184] Implement recursive search for configuration classes.
// This needs to integrate seamlessly (i.e., analogous yet mutually
- // exclusive) with the existing locations search. Furthermore, the
- // solution must not depend on an explicit ACCL check.
- if (contextLoader instanceof AnnotationConfigContextLoader) {
+ // exclusive) with the existing locations search.
+ if ((contextLoader instanceof ResourceTypeAwareContextLoader)
+ && ((ResourceTypeAwareContextLoader) contextLoader).supportsClassResources()) {
ContextConfiguration cc = declaringClass.getAnnotation(annotationType);
if (logger.isTraceEnabled()) {
@@ -317,11 +316,12 @@ public class TestContext extends AttributeAccessorSupport {
*/
public ApplicationContext getApplicationContext() {
synchronized (this.contextCache) {
- ApplicationContext context = this.contextCache.get(contextKeyString(this.locations));
+ String contextKeyString = contextKeyString(this.locations);
+ ApplicationContext context = this.contextCache.get(contextKeyString);
if (context == null) {
try {
context = loadApplicationContext();
- this.contextCache.put(contextKeyString(this.locations), context);
+ this.contextCache.put(contextKeyString, context);
}
catch (Exception ex) {
throw new IllegalStateException("Failed to load ApplicationContext", ex);
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 0db4bae0e9d..27882beab64 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
@@ -19,6 +19,7 @@ package org.springframework.test.context.support;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.test.context.ContextLoader;
+import org.springframework.test.context.ResourceTypeAwareContextLoader;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
@@ -37,7 +38,7 @@ import org.springframework.util.StringUtils;
* @see #generateDefaultLocations
* @see #modifyLocations
*/
-public abstract class AbstractContextLoader implements ContextLoader {
+public abstract class AbstractContextLoader implements ResourceTypeAwareContextLoader {
/**
* If the supplied locations are null or
@@ -58,8 +59,8 @@ public abstract class AbstractContextLoader implements ContextLoader {
* @see org.springframework.test.context.ContextLoader#processLocations
*/
public final String[] processLocations(Class> clazz, String... locations) {
- return (ObjectUtils.isEmpty(locations) && isGenerateDefaultLocations()) ?
- generateDefaultLocations(clazz) : modifyLocations(clazz, locations);
+ return (ObjectUtils.isEmpty(locations) && isGenerateDefaultLocations()) ? generateDefaultLocations(clazz)
+ : modifyLocations(clazz, locations);
}
/**
@@ -80,8 +81,8 @@ public abstract class AbstractContextLoader implements ContextLoader {
Assert.notNull(clazz, "Class must not be null");
String suffix = getResourceSuffix();
Assert.hasText(suffix, "Resource suffix must not be empty");
- return new String[] { ResourceUtils.CLASSPATH_URL_PREFIX + "/" +
- ClassUtils.convertClassNameToResourcePath(clazz.getName()) + suffix };
+ return new String[] { ResourceUtils.CLASSPATH_URL_PREFIX + "/"
+ + ClassUtils.convertClassNameToResourcePath(clazz.getName()) + suffix };
}
/**
@@ -119,7 +120,6 @@ public abstract class AbstractContextLoader implements ContextLoader {
return modifiedLocations;
}
-
/**
* Determine whether or not default resource locations should be
* generated if the locations provided to
@@ -141,4 +141,22 @@ public abstract class AbstractContextLoader implements ContextLoader {
*/
protected abstract String getResourceSuffix();
+ /**
+ * TODO Document supportsStringResources() implementation.
+ *
+ * @return true
+ */
+ public boolean supportsStringResources() {
+ return true;
+ }
+
+ /**
+ * TODO Document supportsClassResources() implementations.
+ *
+ * @return false
+ */
+ public boolean supportsClassResources() {
+ return false;
+ }
+
}
diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java b/org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java
index 6c13bb1095f..a8f23ee3ebc 100644
--- a/org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java
+++ b/org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java
@@ -127,4 +127,20 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
return "Config";
}
+ /**
+ * @return true
+ */
+ @Override
+ public boolean supportsClassResources() {
+ return true;
+ }
+
+ /**
+ * @return false
+ */
+ @Override
+ public boolean supportsStringResources() {
+ return false;
+ }
+
}
diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java
index 2252a9e2a95..50d1f3a3374 100644
--- a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java
+++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java
@@ -26,7 +26,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader;
* @author Sam Brannen
* @since 3.1
*/
-@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = AnnotationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig.class, inheritLocations = false)
+@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = PojoAndStringConfig.class)
public class AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests extends SpringJUnit4ClassRunnerAppCtxTests {
/* all tests are in the parent class. */
}
diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/PojoAndStringConfig.java
similarity index 95%
rename from org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig.java
rename to org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/PojoAndStringConfig.java
index e72c4c8edab..440f5cbc33c 100644
--- a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig.java
+++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/PojoAndStringConfig.java
@@ -28,7 +28,7 @@ import org.springframework.context.annotation.Configuration;
* @since 3.1
*/
@Configuration
-public class AnnotationConfigSpringJUnit4ClassRunnerAppCtxTestsConfig {
+public class PojoAndStringConfig {
@Bean
public Employee employee() {