[SPR-6184] Introduced ResourceType enum for context loaders; documented tests.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2009 the original author or authors.
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -79,7 +79,7 @@ public @interface ContextConfiguration {
|
||||
* defined by an annotated superclass. Thus, subclasses have the option of
|
||||
* <em>extending</em> the list of resource locations. In the following
|
||||
* example, the {@link org.springframework.context.ApplicationContext ApplicationContext}
|
||||
* for <code>ExtendedTest</code> will be loaded from
|
||||
* for <code>ExtendedTest</code> will be loaded from
|
||||
* "base-context.xml" <strong>and</strong>
|
||||
* "extended-context.xml", in that order. Beans defined in
|
||||
* "extended-context.xml" may therefore override those defined in
|
||||
@@ -106,8 +106,8 @@ public @interface ContextConfiguration {
|
||||
* {@link org.springframework.context.ApplicationContext ApplicationContext}.
|
||||
* <p>If not specified, the loader will be inherited from the first superclass
|
||||
* which is annotated with <code>@ContextConfiguration</code> and specifies
|
||||
* an explicit loader. If no class in the hierarchy specifies an explicit
|
||||
* loader, a default loader will be used instead.
|
||||
* an explicit loader. If no class in the hierarchy specifies an explicit
|
||||
* loader, a default loader will be used instead.
|
||||
* <p>The default concrete implementation chosen at runtime will be
|
||||
* {@link org.springframework.test.context.support.GenericXmlContextLoader GenericXmlContextLoader}.
|
||||
* Also check out {@link org.springframework.test.context.support.AbstractContextLoader AbstractContextLoader}'s
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.test.context.ResourceTypeAwareContextLoader.ResourceType;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -101,8 +102,9 @@ public abstract class ContextLoaderUtils {
|
||||
|
||||
Class<ContextConfiguration> annotationType = ContextConfiguration.class;
|
||||
Class<?> declaringClass = AnnotationUtils.findAnnotationDeclaringClass(annotationType, clazz);
|
||||
Assert.notNull(declaringClass, "Could not find an 'annotation declaring class' for annotation type ["
|
||||
+ annotationType + "] and class [" + clazz + "]");
|
||||
Assert.notNull(declaringClass, String.format(
|
||||
"Could not find an 'annotation declaring class' for annotation type [%s] and class [%s]", annotationType,
|
||||
clazz));
|
||||
|
||||
while (declaringClass != null) {
|
||||
ContextConfiguration contextConfiguration = declaringClass.getAnnotation(annotationType);
|
||||
@@ -166,7 +168,7 @@ public abstract class ContextLoaderUtils {
|
||||
Assert.notNull(clazz, "Class must not be null");
|
||||
|
||||
boolean processConfigurationClasses = (contextLoader instanceof ResourceTypeAwareContextLoader)
|
||||
&& ((ResourceTypeAwareContextLoader) contextLoader).supportsClassResources();
|
||||
&& ResourceType.CLASSES == ((ResourceTypeAwareContextLoader) contextLoader).getResourceType();
|
||||
LocationsResolver locationsResolver = processConfigurationClasses ? classNameLocationsResolver
|
||||
: resourcePathLocationsResolver;
|
||||
|
||||
@@ -212,9 +214,10 @@ public abstract class ContextLoaderUtils {
|
||||
|
||||
if (!ObjectUtils.isEmpty(valueLocations) && !ObjectUtils.isEmpty(locations)) {
|
||||
String msg = String.format(
|
||||
"Test class [%s] has been configured with @ContextConfiguration's 'value' [%s] and 'locations' [%s] attributes. Only one declaration of resource locations is permitted per @ContextConfiguration annotation.",
|
||||
declaringClass, ObjectUtils.nullSafeToString(valueLocations),
|
||||
ObjectUtils.nullSafeToString(locations));
|
||||
"Test class [%s] has been configured with @ContextConfiguration's 'value' [%s] "
|
||||
+ "and 'locations' [%s] attributes. Only one declaration of resource "
|
||||
+ "locations is permitted per @ContextConfiguration annotation.", declaringClass,
|
||||
ObjectUtils.nullSafeToString(valueLocations), ObjectUtils.nullSafeToString(locations));
|
||||
ContextLoaderUtils.logger.error(msg);
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
|
||||
@@ -25,18 +25,34 @@ package org.springframework.test.context;
|
||||
public interface ResourceTypeAwareContextLoader extends ContextLoader {
|
||||
|
||||
/**
|
||||
* @return <code>true</code> if this <code>ContextLoader</code> supports
|
||||
* String-based resource locations
|
||||
* @see ContextConfiguration#locations()
|
||||
* @see ContextConfiguration#value()
|
||||
* TODO Document ResourceType.
|
||||
*/
|
||||
boolean supportsStringResources();
|
||||
public static enum ResourceType {
|
||||
|
||||
/**
|
||||
* String-based resource locations.
|
||||
*
|
||||
* @see ContextConfiguration#locations()
|
||||
* @see ContextConfiguration#value()
|
||||
*/
|
||||
LOCATIONS,
|
||||
|
||||
/**
|
||||
* Configuration classes.
|
||||
*
|
||||
* @see ContextConfiguration#classes()
|
||||
*/
|
||||
CLASSES;
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @return <code>true</code> if this <code>ContextLoader</code> supports
|
||||
* Class-based resource locations
|
||||
* @see ContextConfiguration#classes()
|
||||
* Get the application context {@link ResourceType} supported by this
|
||||
* <code>ContextLoader</code>.
|
||||
*
|
||||
* @return the context resource type supported by this ContextLoader
|
||||
*/
|
||||
boolean supportsClassResources();
|
||||
ResourceType getResourceType();
|
||||
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ public abstract class AbstractContextLoader implements ResourceTypeAwareContextL
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates the default classpath resource locations array based on the
|
||||
* Generate the default classpath resource locations array based on the
|
||||
* supplied class.
|
||||
* <p>For example, if the supplied class is <code>com.example.MyTest</code>,
|
||||
* the generated locations will contain a single string with a value of
|
||||
@@ -142,21 +142,11 @@ public abstract class AbstractContextLoader implements ResourceTypeAwareContextL
|
||||
protected abstract String getResourceSuffix();
|
||||
|
||||
/**
|
||||
* TODO Document supportsStringResources() implementation.
|
||||
*
|
||||
* @return <code>true</code>
|
||||
* The default implementation returns {@link ResourceType#LOCATIONS}.
|
||||
* <p>Can be overridden by subclasses.
|
||||
*/
|
||||
public boolean supportsStringResources() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Document supportsClassResources() implementations.
|
||||
*
|
||||
* @return <code>false</code>
|
||||
*/
|
||||
public boolean supportsClassResources() {
|
||||
return false;
|
||||
public ResourceType getResourceType() {
|
||||
return ResourceType.LOCATIONS;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -128,19 +128,11 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
|
||||
}
|
||||
|
||||
/**
|
||||
* @return <code>true</code>
|
||||
* Returns {@link ResourceType#CLASSES}.
|
||||
*/
|
||||
@Override
|
||||
public boolean supportsClassResources() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return <code>false</code>
|
||||
*/
|
||||
@Override
|
||||
public boolean supportsStringResources() {
|
||||
return false;
|
||||
public ResourceType getResourceType() {
|
||||
return ResourceType.CLASSES;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,7 +21,15 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunnerAppCtxTest
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
/**
|
||||
* TODO [SPR-6184] Document tests.
|
||||
* Integration tests that verify support for configuration classes in
|
||||
* the Spring TestContext Framework.
|
||||
*
|
||||
* <p>Furthermore, by extending {@link SpringJUnit4ClassRunnerAppCtxTests},
|
||||
* this class also verifies support for several basic features of the
|
||||
* Spring TestContext Framework. See JavaDoc in
|
||||
* <code>SpringJUnit4ClassRunnerAppCtxTests</code> for details.
|
||||
*
|
||||
* <p>Configuration will be loaded from {@link PojoAndStringConfig}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
|
||||
@@ -21,7 +21,8 @@ import org.junit.runners.Suite;
|
||||
import org.junit.runners.Suite.SuiteClasses;
|
||||
|
||||
/**
|
||||
* TODO [SPR-6184] Document tests.
|
||||
* JUnit test suite for annotation-driven <em>configuration class</em>
|
||||
* support in the Spring TestContext Framework.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
@@ -33,5 +34,5 @@ AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.class,//
|
||||
DefaultConfigClassBaseTests.class,//
|
||||
DefaultConfigClassInheritedTests.class //
|
||||
})
|
||||
public class AnnotationConfigSuiteTests {
|
||||
public class AnnotationConfigTestSuite {
|
||||
}
|
||||
@@ -28,7 +28,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||
|
||||
/**
|
||||
* TODO [SPR-6184] Document tests.
|
||||
* Integration tests that verify support for configuration classes in
|
||||
* the Spring TestContext Framework.
|
||||
*
|
||||
* <p>Configuration will be loaded from {@link DefaultConfigClassBaseTestsConfig}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* TODO [SPR-6184] Document configuration class.
|
||||
* ApplicationContext configuration class for {@link DefaultConfigClassBaseTests}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
|
||||
@@ -25,7 +25,11 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
/**
|
||||
* TODO [SPR-6184] Document tests.
|
||||
* Integration tests that verify support for configuration classes in
|
||||
* the Spring TestContext Framework.
|
||||
*
|
||||
* <p>Configuration will be loaded from {@link DefaultConfigClassBaseTestsConfig}
|
||||
* and {@link DefaultConfigClassInheritedTestsConfig}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
|
||||
@@ -21,7 +21,7 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* TODO [SPR-6184] Document configuration class.
|
||||
* ApplicationContext configuration class for {@link DefaultConfigClassInheritedTests}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
|
||||
@@ -22,7 +22,12 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* TODO [SPR-6184] Document configuration class.
|
||||
* ApplicationContext configuration class for various integration tests.
|
||||
*
|
||||
* <p>The beans defined in this configuration class map directly to the
|
||||
* beans defined in <code>SpringJUnit4ClassRunnerAppCtxTests-context.xml</code>.
|
||||
* Consequently, the application contexts loaded from these two sources
|
||||
* should be identical with regard to bean definitions.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
|
||||
Reference in New Issue
Block a user