[SPR-8386] AbstractContextLoader now adheres to the SmartContextLoader contract by verifying the existence of generated default resource locations.
This commit is contained in:
@@ -16,7 +16,10 @@
|
|||||||
|
|
||||||
package org.springframework.test.context.support;
|
package org.springframework.test.context.support;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.support.ResourcePatternUtils;
|
import org.springframework.core.io.support.ResourcePatternUtils;
|
||||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||||
import org.springframework.test.context.ContextLoader;
|
import org.springframework.test.context.ContextLoader;
|
||||||
@@ -29,9 +32,16 @@ import org.springframework.util.StringUtils;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract application context loader, which provides a basis for all concrete
|
* Abstract application context loader, which provides a basis for all concrete
|
||||||
* implementations of the {@link ContextLoader} strategy. Provides a
|
* implementations of the {@link ContextLoader} SPI. Provides a
|
||||||
* <em>Template Method</em> based approach for {@link #processLocations processing}
|
* <em>Template Method</em> based approach for {@link #processLocations processing}
|
||||||
* locations.
|
* resource locations.
|
||||||
|
*
|
||||||
|
* <p>As of Spring 3.1, <code>AbstractContextLoader</code> also provides a basis
|
||||||
|
* for all concrete implementations of the {@link SmartContextLoader} SPI. For
|
||||||
|
* backwards compatibility with the {@code ContextLoader} SPI,
|
||||||
|
* {@link #processContextConfiguration()} delegates to
|
||||||
|
* {@link #processLocations()}, and {@link #generatesDefaults()} delegates to
|
||||||
|
* {@link #isGenerateDefaultLocations()}.
|
||||||
*
|
*
|
||||||
* @author Sam Brannen
|
* @author Sam Brannen
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
@@ -41,14 +51,18 @@ import org.springframework.util.StringUtils;
|
|||||||
*/
|
*/
|
||||||
public abstract class AbstractContextLoader implements SmartContextLoader {
|
public abstract class AbstractContextLoader implements SmartContextLoader {
|
||||||
|
|
||||||
|
private static final Log logger = LogFactory.getLog(AbstractContextLoader.class);
|
||||||
|
|
||||||
|
private static final String[] EMPTY_STRING_ARRAY = new String[] {};
|
||||||
private static final String SLASH = "/";
|
private static final String SLASH = "/";
|
||||||
|
|
||||||
|
|
||||||
// --- SmartContextLoader -----------------------------------------------
|
// --- SmartContextLoader -----------------------------------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO Document generatesDefaults() implementation.
|
* For backwards compatibility with the {@link ContextLoader} SPI, the
|
||||||
*
|
* default implementation simply delegates to
|
||||||
|
* {@link #isGenerateDefaultLocations()}.
|
||||||
* @see org.springframework.test.context.SmartContextLoader#generatesDefaults()
|
* @see org.springframework.test.context.SmartContextLoader#generatesDefaults()
|
||||||
* @see #isGenerateDefaultLocations()
|
* @see #isGenerateDefaultLocations()
|
||||||
*/
|
*/
|
||||||
@@ -57,14 +71,22 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO Document processContextConfiguration() implementation.
|
* For backwards compatibility with the {@link ContextLoader} SPI, the
|
||||||
*
|
* default implementation simply delegates to {@link #processLocations()},
|
||||||
* @see #processLocations(Class, String...)
|
* passing it the {@link ContextConfigurationAttributes#getDeclaringClass()
|
||||||
|
* declaring class} and {@link ContextConfigurationAttributes#getLocations()
|
||||||
|
* resource locations} retrieved from the supplied
|
||||||
|
* {@link ContextConfigurationAttributes configuration attributes}. The
|
||||||
|
* processed locations are then
|
||||||
|
* {@link ContextConfigurationAttributes#setLocations(String[]) set} in
|
||||||
|
* the supplied configuration attributes.
|
||||||
|
* <p>Can be overridden in subclasses — for example, to process
|
||||||
|
* configuration classes instead of resource locations.
|
||||||
|
* @see #processLocations()
|
||||||
*/
|
*/
|
||||||
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
|
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
|
||||||
String[] processedLocations = processLocations(configAttributes.getDeclaringClass(),
|
String[] processedLocations = processLocations(configAttributes.getDeclaringClass(),
|
||||||
configAttributes.getLocations());
|
configAttributes.getLocations());
|
||||||
|
|
||||||
configAttributes.setLocations(processedLocations);
|
configAttributes.setLocations(processedLocations);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +94,7 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* If the supplied <code>locations</code> are <code>null</code> or
|
* If the supplied <code>locations</code> are <code>null</code> or
|
||||||
* <em>empty</em> and {@link #isGenerateDefaultLocations()} is
|
* <em>empty</em> and {@link #isGenerateDefaultLocations()} returns
|
||||||
* <code>true</code>, default locations will be
|
* <code>true</code>, default locations will be
|
||||||
* {@link #generateDefaultLocations(Class) generated} for the specified
|
* {@link #generateDefaultLocations(Class) generated} for the specified
|
||||||
* {@link Class class} and the configured
|
* {@link Class class} and the configured
|
||||||
@@ -83,10 +105,12 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
|
|||||||
* used when generating default locations
|
* used when generating default locations
|
||||||
* @param locations the unmodified locations to use for loading the
|
* @param locations the unmodified locations to use for loading the
|
||||||
* application context (can be <code>null</code> or empty)
|
* application context (can be <code>null</code> or empty)
|
||||||
* @return an array of application context resource locations
|
* @return a processed array of application context resource locations
|
||||||
* @see #generateDefaultLocations
|
* @see #isGenerateDefaultLocations()
|
||||||
* @see #modifyLocations
|
* @see #generateDefaultLocations()
|
||||||
* @see org.springframework.test.context.ContextLoader#processLocations
|
* @see #modifyLocations()
|
||||||
|
* @see org.springframework.test.context.ContextLoader#processLocations()
|
||||||
|
* @see #processContextConfiguration()
|
||||||
*/
|
*/
|
||||||
public final String[] processLocations(Class<?> clazz, String... locations) {
|
public final String[] processLocations(Class<?> clazz, String... locations) {
|
||||||
return (ObjectUtils.isEmpty(locations) && isGenerateDefaultLocations()) ? generateDefaultLocations(clazz)
|
return (ObjectUtils.isEmpty(locations) && isGenerateDefaultLocations()) ? generateDefaultLocations(clazz)
|
||||||
@@ -101,6 +125,11 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
|
|||||||
* "classpath:/com/example/MyTest<code><suffix></code>",
|
* "classpath:/com/example/MyTest<code><suffix></code>",
|
||||||
* where <code><suffix></code> is the value of the
|
* where <code><suffix></code> is the value of the
|
||||||
* {@link #getResourceSuffix() resource suffix} string.
|
* {@link #getResourceSuffix() resource suffix} string.
|
||||||
|
* <p>As of Spring 3.1, the implementation of this method adheres to the
|
||||||
|
* contract defined in the {@link SmartContextLoader} SPI. Specifically,
|
||||||
|
* this method will <em>preemptively</em> verify that the generated default
|
||||||
|
* location actually exists. If it does not exist, this method will log a
|
||||||
|
* warning and return an empty array.
|
||||||
* <p>Subclasses can override this method to implement a different
|
* <p>Subclasses can override this method to implement a different
|
||||||
* <em>default location generation</em> strategy.
|
* <em>default location generation</em> strategy.
|
||||||
* @param clazz the class for which the default locations are to be generated
|
* @param clazz the class for which the default locations are to be generated
|
||||||
@@ -111,12 +140,17 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
|
|||||||
Assert.notNull(clazz, "Class must not be null");
|
Assert.notNull(clazz, "Class must not be null");
|
||||||
String suffix = getResourceSuffix();
|
String suffix = getResourceSuffix();
|
||||||
Assert.hasText(suffix, "Resource suffix must not be empty");
|
Assert.hasText(suffix, "Resource suffix must not be empty");
|
||||||
|
String resourcePath = SLASH + ClassUtils.convertClassNameToResourcePath(clazz.getName()) + suffix;
|
||||||
|
|
||||||
// TODO Adhere to SmartContextLoader contract: verify existence of
|
if (!new ClassPathResource(resourcePath, clazz).exists()) {
|
||||||
// default and return an empty array if non-existent, in which case a
|
logger.warn(String.format(
|
||||||
// warning should be logged as well.
|
"Cannot generate default resource location for test class [%s]: classpath resource [%s] does not exist.",
|
||||||
return new String[] { ResourceUtils.CLASSPATH_URL_PREFIX + SLASH
|
clazz.getName(), resourcePath));
|
||||||
+ ClassUtils.convertClassNameToResourcePath(clazz.getName()) + suffix };
|
return EMPTY_STRING_ARRAY;
|
||||||
|
}
|
||||||
|
|
||||||
|
// else
|
||||||
|
return new String[] { ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath };
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -157,8 +191,7 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
|
|||||||
/**
|
/**
|
||||||
* Determine whether or not <em>default</em> resource locations should be
|
* Determine whether or not <em>default</em> resource locations should be
|
||||||
* generated if the <code>locations</code> provided to
|
* generated if the <code>locations</code> provided to
|
||||||
* {@link #processLocations(Class,String...) processLocations()} are
|
* {@link #processLocations()} are <code>null</code> or empty.
|
||||||
* <code>null</code> or empty.
|
|
||||||
* <p>Can be overridden by subclasses to change the default behavior.
|
* <p>Can be overridden by subclasses to change the default behavior.
|
||||||
* @return always <code>true</code> by default
|
* @return always <code>true</code> by default
|
||||||
*/
|
*/
|
||||||
@@ -171,7 +204,7 @@ public abstract class AbstractContextLoader implements SmartContextLoader {
|
|||||||
* locations when generating default locations.
|
* locations when generating default locations.
|
||||||
* <p>Must be implemented by subclasses.
|
* <p>Must be implemented by subclasses.
|
||||||
* @return the resource suffix; should not be <code>null</code> or empty
|
* @return the resource suffix; should not be <code>null</code> or empty
|
||||||
* @see #generateDefaultLocations(Class)
|
* @see #generateDefaultLocations()
|
||||||
*/
|
*/
|
||||||
protected abstract String getResourceSuffix();
|
protected abstract String getResourceSuffix();
|
||||||
|
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
|
|||||||
/**
|
/**
|
||||||
* TODO Document overridden processContextConfiguration().
|
* TODO Document overridden processContextConfiguration().
|
||||||
*
|
*
|
||||||
* @see org.springframework.test.context.SmartContextLoader#processContextConfiguration
|
* @see org.springframework.test.context.SmartContextLoader#processContextConfiguration()
|
||||||
* @see #generatesDefaults
|
* @see #generatesDefaults
|
||||||
* @see #generateDefaultConfigurationClasses
|
* @see #generateDefaultConfigurationClasses
|
||||||
*/
|
*/
|
||||||
@@ -102,7 +102,20 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO Document generateDefaultConfigurationClasses().
|
* TODO Complete JavaDoc for generateDefaultConfigurationClasses().
|
||||||
|
*
|
||||||
|
* <p>The implementation of this method adheres to the contract defined in the
|
||||||
|
* {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}
|
||||||
|
* SPI. Specifically, this method will <em>preemptively</em> verify that the
|
||||||
|
* generated default configuration classes exist <b>and</b> that such classes
|
||||||
|
* comply with the constraints required of {@link Configuration @Configuration}
|
||||||
|
* class implementations. If a candidate configuration class does meet these
|
||||||
|
* requirements, this method will log a warning and potentially return an empty
|
||||||
|
* array.
|
||||||
|
*
|
||||||
|
* @param declaringClass the test class that declared
|
||||||
|
* {@link org.springframework.test.context.ContextConfiguration @ContextConfiguration}
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
protected Class<?>[] generateDefaultConfigurationClasses(Class<?> declaringClass) {
|
protected Class<?>[] generateDefaultConfigurationClasses(Class<?> declaringClass) {
|
||||||
Assert.notNull(declaringClass, "Declaring class must not be null");
|
Assert.notNull(declaringClass, "Declaring class must not be null");
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
|
||||||
|
|
||||||
|
<!-- intentionally empty: only needed so that the ContextLoader can find this file -->
|
||||||
|
|
||||||
|
</beans>
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
|
||||||
|
|
||||||
|
<!-- intentionally empty: only needed so that the ContextLoader can find this file -->
|
||||||
|
|
||||||
|
</beans>
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2007 the original author or authors.
|
* Copyright 2002-2011 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -27,7 +27,6 @@ import org.junit.Test;
|
|||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.Parameterized;
|
import org.junit.runners.Parameterized;
|
||||||
import org.junit.runners.Parameterized.Parameters;
|
import org.junit.runners.Parameterized.Parameters;
|
||||||
|
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.ContextLoader;
|
import org.springframework.test.context.ContextLoader;
|
||||||
@@ -63,35 +62,41 @@ public class GenericXmlContextLoaderResourceLocationsTests {
|
|||||||
@Parameters
|
@Parameters
|
||||||
public static Collection<Object[]> contextConfigurationLocationsData() {
|
public static Collection<Object[]> contextConfigurationLocationsData() {
|
||||||
@ContextConfiguration
|
@ContextConfiguration
|
||||||
class ClasspathDefaultLocationsTestCase {
|
class ClasspathNonExistentDefaultLocationsTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ContextConfiguration(locations = { "context1.xml", "context2.xml" })
|
@ContextConfiguration
|
||||||
|
class ClasspathExistentDefaultLocationsTestCase {
|
||||||
|
}
|
||||||
|
|
||||||
|
@ContextConfiguration({ "context1.xml", "context2.xml" })
|
||||||
class ImplicitClasspathLocationsTestCase {
|
class ImplicitClasspathLocationsTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ContextConfiguration(locations = { "classpath:context.xml" })
|
@ContextConfiguration("classpath:context.xml")
|
||||||
class ExplicitClasspathLocationsTestCase {
|
class ExplicitClasspathLocationsTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ContextConfiguration(locations = { "file:/testing/directory/context.xml" })
|
@ContextConfiguration("file:/testing/directory/context.xml")
|
||||||
class ExplicitFileLocationsTestCase {
|
class ExplicitFileLocationsTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ContextConfiguration(locations = { "http://example.com/context.xml" })
|
@ContextConfiguration("http://example.com/context.xml")
|
||||||
class ExplicitUrlLocationsTestCase {
|
class ExplicitUrlLocationsTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ContextConfiguration(locations = { "context1.xml", "classpath:context2.xml", "/context3.xml",
|
@ContextConfiguration({ "context1.xml", "classpath:context2.xml", "/context3.xml",
|
||||||
"file:/testing/directory/context.xml", "http://example.com/context.xml" })
|
"file:/testing/directory/context.xml", "http://example.com/context.xml" })
|
||||||
class ExplicitMixedPathTypesLocationsTestCase {
|
class ExplicitMixedPathTypesLocationsTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
return Arrays.asList(new Object[][] {
|
return Arrays.asList(new Object[][] {
|
||||||
|
|
||||||
|
{ ClasspathNonExistentDefaultLocationsTestCase.class, new String[] {} },
|
||||||
|
|
||||||
{
|
{
|
||||||
ClasspathDefaultLocationsTestCase.class,
|
ClasspathExistentDefaultLocationsTestCase.class,
|
||||||
new String[] { "classpath:/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests$1ClasspathDefaultLocationsTestCase-context.xml" } },
|
new String[] { "classpath:/org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests$1ClasspathExistentDefaultLocationsTestCase-context.xml" } },
|
||||||
|
|
||||||
{
|
{
|
||||||
ImplicitClasspathLocationsTestCase.class,
|
ImplicitClasspathLocationsTestCase.class,
|
||||||
@@ -118,7 +123,7 @@ public class GenericXmlContextLoaderResourceLocationsTests {
|
|||||||
|
|
||||||
final ContextConfiguration contextConfig = this.testClass.getAnnotation(ContextConfiguration.class);
|
final ContextConfiguration contextConfig = this.testClass.getAnnotation(ContextConfiguration.class);
|
||||||
final ContextLoader contextLoader = new GenericXmlContextLoader();
|
final ContextLoader contextLoader = new GenericXmlContextLoader();
|
||||||
final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig, "locations");
|
final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
|
||||||
final String[] processedLocations = contextLoader.processLocations(this.testClass, configuredLocations);
|
final String[] processedLocations = contextLoader.processLocations(this.testClass, configuredLocations);
|
||||||
|
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
@@ -129,8 +134,7 @@ public class GenericXmlContextLoaderResourceLocationsTests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
assertArrayEquals("Verifying locations for test [" + this.testClass + "].", this.expectedLocations,
|
assertArrayEquals("Verifying locations for test [" + this.testClass + "].", this.expectedLocations,
|
||||||
processedLocations);
|
processedLocations);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user