[SPR-8395][SPR-8386] AnnotationConfigContextLoader now generates a list of default configuration classes by finding all non-private, non-final, static, inner classes of the test class that are annotated with @Configuration; updated JavaDoc in AbstractGenericContextLoader and AnnotationConfigContextLoader to reflect changes resulting from the SmartContextLoader integration.
This commit is contained in:
parent
c070a4b0a4
commit
a77cf0f652
|
|
@ -24,23 +24,36 @@ import org.springframework.context.ConfigurableApplicationContext;
|
|||
import org.springframework.context.annotation.AnnotationConfigUtils;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.SmartContextLoader;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Abstract, generic extension of {@link AbstractContextLoader} which loads a
|
||||
* {@link GenericApplicationContext} from the <em>locations</em> provided to
|
||||
* {@link #loadContext loadContext()}.
|
||||
* Abstract, generic extension of {@link AbstractContextLoader} that loads a
|
||||
* {@link GenericApplicationContext}.
|
||||
*
|
||||
* <ul>
|
||||
* <li>If instances of concrete subclasses are invoked via the
|
||||
* {@link org.springframework.test.context.ContextLoader ContextLoader} SPI, the
|
||||
* context will be loaded from the <em>locations</em> provided to
|
||||
* {@link #loadContext(String...)}.</li>
|
||||
* <li>If instances of concrete subclasses are invoked via the
|
||||
* {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}
|
||||
* SPI, the context will be loaded from the {@link MergedContextConfiguration}
|
||||
* provided to {@link #loadContext(MergedContextConfiguration)}. In such cases, a
|
||||
* <code>SmartContextLoader</code> will decide whether to load the context from
|
||||
* <em>locations</em> or
|
||||
* {@link org.springframework.context.annotation.Configuration configuration classes}.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>Concrete subclasses must provide an appropriate implementation of
|
||||
* {@link #createBeanDefinitionReader createBeanDefinitionReader()},
|
||||
* potentially overriding {@link #loadBeanDefinitions loadBeanDefinitions()}
|
||||
* in addition.
|
||||
* as well.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Juergen Hoeller
|
||||
* @since 2.5
|
||||
* @see #loadContext
|
||||
* @see #loadContext(MergedContextConfiguration)
|
||||
* @see #loadContext(String...)
|
||||
*/
|
||||
public abstract class AbstractGenericContextLoader extends AbstractContextLoader {
|
||||
|
||||
|
|
@ -48,9 +61,31 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
|
|||
|
||||
|
||||
/**
|
||||
* TODO Document loadContext(MergedContextConfiguration).
|
||||
*
|
||||
* @see SmartContextLoader#loadContext(MergedContextConfiguration)
|
||||
* Load a Spring ApplicationContext from the supplied {@link MergedContextConfiguration}.
|
||||
* <p>Implementation details:
|
||||
* <ul>
|
||||
* <li>Creates a {@link GenericApplicationContext} instance.</li>
|
||||
* <li>Sets the <em>active bean definition profiles</em> from the supplied
|
||||
* <code>MergedContextConfiguration</code> in the
|
||||
* {@link org.springframework.core.env.Environment Environment} of the context.</li>
|
||||
* <li>Calls {@link #prepareContext(GenericApplicationContext)} to
|
||||
* prepare the context.</li>
|
||||
* <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to
|
||||
* allow for customizing the context's <code>DefaultListableBeanFactory</code>.</li>
|
||||
* <li>Delegates to {@link #loadBeanDefinitions()} to populate the context
|
||||
* from the configuration locations or classes in the supplied
|
||||
* <code>MergedContextConfiguration</code>.</li>
|
||||
* <li>Delegates to {@link AnnotationConfigUtils} for
|
||||
* {@link AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
|
||||
* annotation configuration processors.</li>
|
||||
* <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow
|
||||
* for customizing the context before it is refreshed.</li>
|
||||
* <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
|
||||
* context and registers a JVM shutdown hook for it.</li>
|
||||
* </ul>
|
||||
* @return a new application context
|
||||
* @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration)
|
||||
* @see GenericApplicationContext
|
||||
*/
|
||||
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception {
|
||||
if (logger.isDebugEnabled()) {
|
||||
|
|
@ -70,7 +105,7 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
|
|||
}
|
||||
|
||||
/**
|
||||
* Loads a Spring ApplicationContext from the supplied <code>locations</code>.
|
||||
* Load a Spring ApplicationContext from the supplied <code>locations</code>.
|
||||
* <p>Implementation details:
|
||||
* <ul>
|
||||
* <li>Creates a {@link GenericApplicationContext} instance.</li>
|
||||
|
|
@ -78,19 +113,24 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
|
|||
* prepare the context.</li>
|
||||
* <li>Calls {@link #customizeBeanFactory(DefaultListableBeanFactory)} to
|
||||
* allow for customizing the context's <code>DefaultListableBeanFactory</code>.</li>
|
||||
* <li>TODO Update/revert documentation... Delegates to {@link #loadBeanDefinitions(GenericApplicationContext, String...)}
|
||||
* to populate the context from the specified config locations.</li>
|
||||
* <li>Delegates to {@link #createBeanDefinitionReader()} to create a
|
||||
* {@link BeanDefinitionReader} which is then used to populate the context
|
||||
* from the specified config locations.</li>
|
||||
* <li>Delegates to {@link AnnotationConfigUtils} for
|
||||
* {@link AnnotationConfigUtils#registerAnnotationConfigProcessors registering}
|
||||
* annotation configuration processors.</li>
|
||||
* <li>Calls {@link #customizeContext(GenericApplicationContext)} to allow
|
||||
* for customizing the context before it is refreshed.</li>
|
||||
* <li>{@link ConfigurableApplicationContext#refresh() Refreshes} the
|
||||
* <li>{@link ConfigurableApplicationContext#refresh Refreshes} the
|
||||
* context and registers a JVM shutdown hook for it.</li>
|
||||
* </ul>
|
||||
* <p><b>Note</b>: this method does not provide a means to set active bean definition
|
||||
* profiles for the loaded context. See {@link #loadContext(MergedContextConfiguration)}
|
||||
* for an alternative.
|
||||
* @return a new application context
|
||||
* @see org.springframework.test.context.ContextLoader#loadContext
|
||||
* @see GenericApplicationContext
|
||||
* @see #loadContext(MergedContextConfiguration)
|
||||
*/
|
||||
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception {
|
||||
if (logger.isDebugEnabled()) {
|
||||
|
|
@ -110,58 +150,65 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
|
|||
|
||||
/**
|
||||
* Prepare the {@link GenericApplicationContext} created by this <code>ContextLoader</code>.
|
||||
* Called <i>before</> bean definitions are read.
|
||||
* Called <i>before</i> bean definitions are read.
|
||||
* <p>The default implementation is empty. Can be overridden in subclasses to
|
||||
* customize GenericApplicationContext's standard settings.
|
||||
* @param context the context for which the BeanDefinitionReader should be created
|
||||
* @see #loadContext
|
||||
* @see org.springframework.context.support.GenericApplicationContext#setResourceLoader
|
||||
* @see org.springframework.context.support.GenericApplicationContext#setId
|
||||
* customize <code>GenericApplicationContext</code>'s standard settings.
|
||||
* @param context the context that should be prepared
|
||||
* @see #loadContext(MergedContextConfiguration)
|
||||
* @see #loadContext(String...)
|
||||
* @see GenericApplicationContext#setAllowBeanDefinitionOverriding
|
||||
* @see GenericApplicationContext#setResourceLoader
|
||||
* @see GenericApplicationContext#setId
|
||||
*/
|
||||
protected void prepareContext(GenericApplicationContext context) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Customize the internal bean factory of the ApplicationContext created by this <code>ContextLoader</code>.
|
||||
* Customize the internal bean factory of the ApplicationContext created by
|
||||
* this <code>ContextLoader</code>.
|
||||
* <p>The default implementation is empty but can be overridden in subclasses
|
||||
* to customize DefaultListableBeanFactory's standard settings.
|
||||
* to customize <code>DefaultListableBeanFactory</code>'s standard settings.
|
||||
* @param beanFactory the bean factory created by this <code>ContextLoader</code>
|
||||
* @see #loadContext
|
||||
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding(boolean)
|
||||
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowEagerClassLoading(boolean)
|
||||
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences(boolean)
|
||||
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping(boolean)
|
||||
* @see #loadContext(MergedContextConfiguration)
|
||||
* @see #loadContext(String...)
|
||||
* @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
|
||||
* @see DefaultListableBeanFactory#setAllowEagerClassLoading
|
||||
* @see DefaultListableBeanFactory#setAllowCircularReferences
|
||||
* @see DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping
|
||||
*/
|
||||
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Load bean definitions into the supplied {@link GenericApplicationContext context}
|
||||
* from the specified resource locations.
|
||||
* from the configuration locations or classes in the supplied
|
||||
* <code>MergedContextConfiguration</code>.</li>
|
||||
* <p>The default implementation delegates to the {@link BeanDefinitionReader}
|
||||
* returned by {@link #createBeanDefinitionReader} to
|
||||
* returned by {@link #createBeanDefinitionReader()} to
|
||||
* {@link BeanDefinitionReader#loadBeanDefinitions(String) load} the
|
||||
* bean definitions.
|
||||
* <p>Subclasses must provide an appropriate implementation of
|
||||
* {@link #createBeanDefinitionReader}. Alternatively subclasses may
|
||||
* provide a <em>no-op</em> implementation of {@link #createBeanDefinitionReader}
|
||||
* {@link #createBeanDefinitionReader()}. Alternatively subclasses may
|
||||
* provide a <em>no-op</em> implementation of {@code createBeanDefinitionReader()}
|
||||
* and override this method to provide a custom strategy for loading or
|
||||
* registering bean definitions.
|
||||
* @param context the context into which the bean definitions should be loaded
|
||||
* @param mergedConfig TODO Document mergedConfig parameter.
|
||||
* @param mergedConfig the merged context configuration
|
||||
* @since 3.1
|
||||
* @see #loadContext
|
||||
* @see #loadContext(MergedContextConfiguration)
|
||||
*/
|
||||
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
|
||||
createBeanDefinitionReader(context).loadBeanDefinitions(mergedConfig.getLocations());
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method for creating a new {@link BeanDefinitionReader} for
|
||||
* loading bean definitions into the supplied {@link GenericApplicationContext context}.
|
||||
* @param context the context for which the BeanDefinitionReader should be created
|
||||
* @return a BeanDefinitionReader for the supplied context
|
||||
* Factory method for creating a new {@link BeanDefinitionReader} for loading
|
||||
* bean definitions into the supplied {@link GenericApplicationContext context}.
|
||||
* @param context the context for which the <code>BeanDefinitionReader</code>
|
||||
* should be created
|
||||
* @return a <code>BeanDefinitionReader</code> for the supplied context
|
||||
* @see #loadContext(String...)
|
||||
* @see #loadBeanDefinitions
|
||||
* @see BeanDefinitionReader
|
||||
*/
|
||||
protected abstract BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context);
|
||||
|
|
@ -169,11 +216,12 @@ public abstract class AbstractGenericContextLoader extends AbstractContextLoader
|
|||
/**
|
||||
* Customize the {@link GenericApplicationContext} created by this
|
||||
* <code>ContextLoader</code> <i>after</i> bean definitions have been
|
||||
* loaded into the context but before the context is refreshed.
|
||||
* loaded into the context but <i>before</i> the context is refreshed.
|
||||
* <p>The default implementation is empty but can be overridden in subclasses
|
||||
* to customize the application context.
|
||||
* @param context the newly created application context
|
||||
* @see #loadContext
|
||||
* @see #loadContext(MergedContextConfiguration)
|
||||
* @see #loadContext(String...)
|
||||
*/
|
||||
protected void customizeContext(GenericApplicationContext context) {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
|
@ -31,131 +32,124 @@ import org.springframework.util.Assert;
|
|||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* Concrete implementation of {@link AbstractGenericContextLoader} which
|
||||
* registers bean definitions from
|
||||
* Concrete implementation of {@link AbstractGenericContextLoader} that loads
|
||||
* bean definitions from
|
||||
* {@link org.springframework.context.annotation.Configuration configuration classes}.
|
||||
*
|
||||
* <p>Note: <code>AnnotationConfigContextLoader</code> supports
|
||||
* {@link org.springframework.context.annotation.Configuration configuration classes}
|
||||
* rather than the String-based resource locations defined by the legacy
|
||||
* {@link org.springframework.test.context.ContextLoader ContextLoader} API. Thus,
|
||||
* although <code>AnnotationConfigContextLoader</code> extends
|
||||
* <code>AbstractGenericContextLoader</code>, <code>AnnotationConfigContextLoader</code>
|
||||
* does <em>not</em> support any String-based methods defined by
|
||||
* <code>AbstractContextLoader</code> or <code>AbstractGenericContextLoader</code>.
|
||||
* Consequently, <code>AnnotationConfigContextLoader</code> should chiefly be
|
||||
* considered a {@link org.springframework.test.context.SmartContextLoader SmartContextLoader}
|
||||
* rather than a {@link org.springframework.test.context.ContextLoader ContextLoader}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
* @see #generateDefaultConfigurationClasses
|
||||
* @see #loadBeanDefinitions
|
||||
*/
|
||||
public class AnnotationConfigContextLoader extends AbstractGenericContextLoader {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(AnnotationConfigContextLoader.class);
|
||||
|
||||
|
||||
// --- SmartContextLoader -----------------------------------------------
|
||||
|
||||
/**
|
||||
* TODO Document overridden processContextConfiguration().
|
||||
*
|
||||
* @see org.springframework.test.context.SmartContextLoader#processContextConfiguration
|
||||
* @see #generatesDefaults
|
||||
* @see #generateDefaultConfigurationClasses
|
||||
*/
|
||||
public void processContextConfiguration(ContextConfigurationAttributes configAttributes) {
|
||||
if (ObjectUtils.isEmpty(configAttributes.getClasses()) && isGenerateDefaultClasses()) {
|
||||
if (ObjectUtils.isEmpty(configAttributes.getClasses()) && generatesDefaults()) {
|
||||
Class<?>[] defaultConfigClasses = generateDefaultConfigurationClasses(configAttributes.getDeclaringClass());
|
||||
configAttributes.setClasses(defaultConfigClasses);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Update documentation regarding SmartContextLoader SPI.
|
||||
*
|
||||
* <p>
|
||||
* Registers {@link org.springframework.context.annotation.Configuration configuration classes}
|
||||
* in the supplied {@link GenericApplicationContext context} from the specified
|
||||
* class names.
|
||||
*
|
||||
* <p>Each class name must be the <em>fully qualified class name</em> of an
|
||||
* annotated configuration class, component, or feature specification. An
|
||||
* {@link AnnotatedBeanDefinitionReader} is used to register the appropriate
|
||||
* bean definitions.
|
||||
*
|
||||
* <p>Note that this method does not call {@link #createBeanDefinitionReader}
|
||||
* since <code>AnnotatedBeanDefinitionReader</code> is not an instance of
|
||||
* {@link BeanDefinitionReader}.
|
||||
*
|
||||
* @param context the context in which the configuration classes should be registered
|
||||
* @param classNames the names of configuration classes to register in the context
|
||||
* @throws IllegalArgumentException if a supplied class name does not represent a class
|
||||
*/
|
||||
@Override
|
||||
protected void loadBeanDefinitions(GenericApplicationContext context,
|
||||
MergedContextConfiguration mergedContextConfiguration) {
|
||||
Class<?>[] configClasses = mergedContextConfiguration.getClasses();
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Registering configuration classes: " + ObjectUtils.nullSafeToString(configClasses));
|
||||
}
|
||||
new AnnotatedBeanDefinitionReader(context).register(configClasses);
|
||||
// --- AnnotationConfigContextLoader ---------------------------------------
|
||||
|
||||
private boolean isStaticNonPrivateAndNonFinal(Class<?> clazz) {
|
||||
Assert.notNull(clazz, "Class must not be null");
|
||||
int modifiers = clazz.getModifiers();
|
||||
return (Modifier.isStatic(modifiers) && !Modifier.isPrivate(modifiers) && !Modifier.isFinal(modifiers));
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Document isGenerateDefaultClasses().
|
||||
* <p>
|
||||
* TODO Consider renaming to a generic boolean generatesDefaults() method and moving to SmartContextLoader.
|
||||
* Determine if the supplied {@link Class} meets the criteria for being considered
|
||||
* as a <em>default configuration class</em> candidate.
|
||||
* <p>Specifically, such candidates:
|
||||
* <ul>
|
||||
* <li>must not be <code>null</code></li>
|
||||
* <li>must not be <code>private</code></li>
|
||||
* <li>must not be <code>final</code></li>
|
||||
* <li>must be <code>static</code></li>
|
||||
* <li>must be annotated with {@code @Configuration}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param clazz the class the check
|
||||
* @return <code>true</code> if the supplied class meets the candidate criteria
|
||||
*/
|
||||
protected boolean isGenerateDefaultClasses() {
|
||||
private boolean isDefaultConfigurationClassCandidate(Class<?> clazz) {
|
||||
return clazz != null && isStaticNonPrivateAndNonFinal(clazz) && clazz.isAnnotationPresent(Configuration.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Document generatesDefaults().
|
||||
*/
|
||||
// TODO Consider moving to SmartContextLoader SPI.
|
||||
protected boolean generatesDefaults() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns "$ContextConfiguration</code>"; intended to be used
|
||||
* as a suffix to append to the name of the test class when generating
|
||||
* default configuration class names.
|
||||
*
|
||||
* <p>Note: the use of a dollar sign ($) signifies that the resulting
|
||||
* class name refers to a nested <code>static</code> class within the
|
||||
* test class.
|
||||
*
|
||||
* @see #generateDefaultConfigurationClasses(Class)
|
||||
*/
|
||||
protected String getConfigurationClassNameSuffix() {
|
||||
return "$ContextConfiguration";
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Document generateDefaultConfigurationClasses().
|
||||
*/
|
||||
protected Class<?>[] generateDefaultConfigurationClasses(Class<?> declaringClass) {
|
||||
Assert.notNull(declaringClass, "Declaring class must not be null");
|
||||
String suffix = getConfigurationClassNameSuffix();
|
||||
Assert.hasText(suffix, "Configuration class name suffix must not be empty");
|
||||
String className = declaringClass.getName() + suffix;
|
||||
|
||||
List<Class<?>> configClasses = new ArrayList<Class<?>>();
|
||||
try {
|
||||
Class<?> configClass = (Class<?>) getClass().getClassLoader().loadClass(className);
|
||||
if (configClass.isAnnotationPresent(Configuration.class)) {
|
||||
|
||||
for (Class<?> configClass : declaringClass.getDeclaredClasses()) {
|
||||
if (isDefaultConfigurationClassCandidate(configClass)) {
|
||||
configClasses.add(configClass);
|
||||
}
|
||||
else {
|
||||
logger.warn(String.format(
|
||||
"Found candidate configuration class [%s], but it is not annotated with @Configuration.", className));
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format(
|
||||
"Ignoring class [%s]; it must be static, non-private, non-final, and annotated "
|
||||
+ "with @Configuration to be considered a default configuration class.",
|
||||
configClass.getName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (ClassNotFoundException e) {
|
||||
|
||||
if (configClasses.isEmpty()) {
|
||||
logger.warn(String.format(
|
||||
"Cannot load @Configuration class with generated class name [%s]: class not found", className));
|
||||
"Test class [%s] does not declare any static, non-private, non-final, inner classes annotated "
|
||||
+ "with @Configuration that can be used as a default configuration class.", declaringClass));
|
||||
}
|
||||
|
||||
return configClasses.toArray(new Class<?>[configClasses.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Document overridden createBeanDefinitionReader().
|
||||
*/
|
||||
@Override
|
||||
protected BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context) {
|
||||
throw new UnsupportedOperationException(
|
||||
"AnnotationConfigContextLoader does not support the createBeanDefinitionReader(GenericApplicationContext) method");
|
||||
}
|
||||
// --- AbstractContextLoader -----------------------------------------------
|
||||
|
||||
/**
|
||||
* TODO Document overridden generateDefaultLocations().
|
||||
*/
|
||||
@Override
|
||||
protected String[] generateDefaultLocations(Class<?> clazz) {
|
||||
throw new UnsupportedOperationException(
|
||||
"AnnotationConfigContextLoader does not support the generateDefaultLocations(Class) method");
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO Document overridden modifyLocations().
|
||||
* <code>AnnotationConfigContextLoader</code> should be used as a
|
||||
* {@link org.springframework.test.context.SmartContextLoader SmartContextLoader},
|
||||
* not as a legacy {@link org.springframework.test.context.ContextLoader ContextLoader}.
|
||||
* Consequently, this method is not supported.
|
||||
*
|
||||
* @see AbstractContextLoader#modifyLocations
|
||||
* @throws UnsupportedOperationException
|
||||
*/
|
||||
@Override
|
||||
protected String[] modifyLocations(Class<?> clazz, String... locations) {
|
||||
|
|
@ -164,7 +158,28 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
|
|||
}
|
||||
|
||||
/**
|
||||
* TODO Document overridden getResourceSuffix().
|
||||
* <code>AnnotationConfigContextLoader</code> should be used as a
|
||||
* {@link org.springframework.test.context.SmartContextLoader SmartContextLoader},
|
||||
* not as a legacy {@link org.springframework.test.context.ContextLoader ContextLoader}.
|
||||
* Consequently, this method is not supported.
|
||||
*
|
||||
* @see AbstractContextLoader#generateDefaultLocations
|
||||
* @throws UnsupportedOperationException
|
||||
*/
|
||||
@Override
|
||||
protected String[] generateDefaultLocations(Class<?> clazz) {
|
||||
throw new UnsupportedOperationException(
|
||||
"AnnotationConfigContextLoader does not support the generateDefaultLocations(Class) method");
|
||||
}
|
||||
|
||||
/**
|
||||
* <code>AnnotationConfigContextLoader</code> should be used as a
|
||||
* {@link org.springframework.test.context.SmartContextLoader SmartContextLoader},
|
||||
* not as a legacy {@link org.springframework.test.context.ContextLoader ContextLoader}.
|
||||
* Consequently, this method is not supported.
|
||||
*
|
||||
* @see AbstractContextLoader#getResourceSuffix
|
||||
* @throws UnsupportedOperationException
|
||||
*/
|
||||
@Override
|
||||
protected String getResourceSuffix() {
|
||||
|
|
@ -172,4 +187,48 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
|
|||
"AnnotationConfigContextLoader does not support the getResourceSuffix() method");
|
||||
}
|
||||
|
||||
// --- AbstractGenericContextLoader ----------------------------------------
|
||||
|
||||
/**
|
||||
* Register {@link org.springframework.context.annotation.Configuration configuration classes}
|
||||
* in the supplied {@link GenericApplicationContext context} from the classes
|
||||
* in the supplied {@link MergedContextConfiguration}.
|
||||
*
|
||||
* <p>Each class must represent an annotated configuration class or component. An
|
||||
* {@link AnnotatedBeanDefinitionReader} is used to register the appropriate
|
||||
* bean definitions.
|
||||
*
|
||||
* <p>Note that this method does not call {@link #createBeanDefinitionReader}
|
||||
* since <code>AnnotatedBeanDefinitionReader</code> is not an instance of
|
||||
* {@link BeanDefinitionReader}.
|
||||
*
|
||||
* @param context the context in which the configuration classes should be registered
|
||||
* @param mergedConfig the merged configuration from which the classes should be retrieved
|
||||
* @see AbstractGenericContextLoader#loadBeanDefinitions
|
||||
*/
|
||||
@Override
|
||||
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig) {
|
||||
Class<?>[] configClasses = mergedConfig.getClasses();
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Registering configuration classes: " + ObjectUtils.nullSafeToString(configClasses));
|
||||
}
|
||||
new AnnotatedBeanDefinitionReader(context).register(configClasses);
|
||||
}
|
||||
|
||||
/**
|
||||
* <code>AnnotationConfigContextLoader</code> should be used as a
|
||||
* {@link org.springframework.test.context.SmartContextLoader SmartContextLoader},
|
||||
* not as a legacy {@link org.springframework.test.context.ContextLoader ContextLoader}.
|
||||
* Consequently, this method is not supported.
|
||||
*
|
||||
* @see #loadBeanDefinitions
|
||||
* @see AbstractGenericContextLoader#createBeanDefinitionReader
|
||||
* @throws UnsupportedOperationException
|
||||
*/
|
||||
@Override
|
||||
protected BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context) {
|
||||
throw new UnsupportedOperationException(
|
||||
"AnnotationConfigContextLoader does not support the createBeanDefinitionReader(GenericApplicationContext) method");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,31 +32,50 @@ public class AnnotationConfigContextLoaderTests {
|
|||
private final AnnotationConfigContextLoader contextLoader = new AnnotationConfigContextLoader();
|
||||
|
||||
|
||||
// Developer's note: AnnotationConfigContextLoader currently generates a
|
||||
// default config class named exactly ContextConfiguration, which is a
|
||||
// static inner class of the test class itself.
|
||||
|
||||
@Test
|
||||
public void generateDefaultConfigurationClassesForAnnotatedInnerClassNamedContextConfiguration() {
|
||||
Class<?>[] defaultLocations = contextLoader.generateDefaultConfigurationClasses(ContextConfigurationInnerClassTestCase.class);
|
||||
assertNotNull(defaultLocations);
|
||||
assertEquals("ContextConfigurationInnerClassTestCase.ContextConfiguration should be found", 1,
|
||||
defaultLocations.length);
|
||||
public void generateDefaultConfigurationClassesForAnnotatedInnerClass() {
|
||||
Class<?>[] configClasses = contextLoader.generateDefaultConfigurationClasses(ContextConfigurationInnerClassTestCase.class);
|
||||
assertNotNull(configClasses);
|
||||
assertEquals("annotated static ContextConfiguration should be considered.", 1, configClasses.length);
|
||||
|
||||
configClasses = contextLoader.generateDefaultConfigurationClasses(AnnotatedFooConfigInnerClassTestCase.class);
|
||||
assertNotNull(configClasses);
|
||||
assertEquals("annotated static FooConfig should be considered.", 1, configClasses.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateDefaultConfigurationClassesForAnnotatedInnerClass() {
|
||||
Class<?>[] defaultLocations = contextLoader.generateDefaultConfigurationClasses(AnnotatedFooConfigInnerClassTestCase.class);
|
||||
assertNotNull(defaultLocations);
|
||||
assertEquals("AnnotatedFooConfigInnerClassTestCase.FooConfig should NOT be found", 0, defaultLocations.length);
|
||||
public void generateDefaultConfigurationClassesForMultipleAnnotatedInnerClasses() {
|
||||
Class<?>[] configClasses = contextLoader.generateDefaultConfigurationClasses(MultipleStaticConfigurationClassesTestCase.class);
|
||||
assertNotNull(configClasses);
|
||||
assertEquals("multiple annotated static classes should be considered.", 2, configClasses.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateDefaultConfigurationClassesForNonAnnotatedInnerClass() {
|
||||
Class<?>[] defaultLocations = contextLoader.generateDefaultConfigurationClasses(PlainVanillaFooConfigInnerClassTestCase.class);
|
||||
assertNotNull(defaultLocations);
|
||||
assertEquals("PlainVanillaFooConfigInnerClassTestCase.FooConfig should NOT be found", 0,
|
||||
defaultLocations.length);
|
||||
Class<?>[] configClasses = contextLoader.generateDefaultConfigurationClasses(PlainVanillaFooConfigInnerClassTestCase.class);
|
||||
assertNotNull(configClasses);
|
||||
assertEquals("non-annotated static FooConfig should NOT be considered.", 0, configClasses.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateDefaultConfigurationClassesForFinalAnnotatedInnerClass() {
|
||||
Class<?>[] configClasses = contextLoader.generateDefaultConfigurationClasses(FinalConfigInnerClassTestCase.class);
|
||||
assertNotNull(configClasses);
|
||||
assertEquals("final annotated static Config should NOT be considered.", 0, configClasses.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateDefaultConfigurationClassesForPrivateAnnotatedInnerClass() {
|
||||
Class<?>[] configClasses = contextLoader.generateDefaultConfigurationClasses(PrivateConfigInnerClassTestCase.class);
|
||||
assertNotNull(configClasses);
|
||||
assertEquals("private annotated inner classes should NOT be considered.", 0, configClasses.length);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateDefaultConfigurationClassesForNonStaticAnnotatedInnerClass() {
|
||||
Class<?>[] configClasses = contextLoader.generateDefaultConfigurationClasses(NonStaticConfigInnerClassesTestCase.class);
|
||||
assertNotNull(configClasses);
|
||||
assertEquals("non-static annotated inner classes should NOT be considered.", 0, configClasses.length);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
|
|
@ -30,11 +29,6 @@ public class ContextConfigurationInnerClassTestCase {
|
|||
|
||||
@Configuration
|
||||
static class ContextConfiguration {
|
||||
|
||||
@Bean
|
||||
public String foo() {
|
||||
return "foo";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Not an actual <em>test case</em>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
* @see AnnotationConfigContextLoaderTests
|
||||
*/
|
||||
public class FinalConfigInnerClassTestCase {
|
||||
|
||||
// Intentionally FINAL.
|
||||
@Configuration
|
||||
static final class Config {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Copyright 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Not an actual <em>test case</em>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
* @see AnnotationConfigContextLoaderTests
|
||||
*/
|
||||
public class MultipleStaticConfigurationClassesTestCase {
|
||||
|
||||
@Configuration
|
||||
static class ConfigA {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class ConfigB {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Not an actual <em>test case</em>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
* @see AnnotationConfigContextLoaderTests
|
||||
*/
|
||||
public class NonStaticConfigInnerClassesTestCase {
|
||||
|
||||
// Intentionally not static
|
||||
@Configuration
|
||||
class FooConfig {
|
||||
}
|
||||
|
||||
// Intentionally not static
|
||||
@Configuration
|
||||
class BarConfig {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -16,8 +16,6 @@
|
|||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* Not an actual <em>test case</em>.
|
||||
*
|
||||
|
|
@ -29,11 +27,6 @@ public class PlainVanillaFooConfigInnerClassTestCase {
|
|||
|
||||
// Intentionally NOT annotated with @Configuration
|
||||
static class FooConfig {
|
||||
|
||||
@Bean
|
||||
public String foo() {
|
||||
return "foo";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Copyright 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.test.context.support;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Not an actual <em>test case</em>.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 3.1
|
||||
* @see AnnotationConfigContextLoaderTests
|
||||
*/
|
||||
public class PrivateConfigInnerClassTestCase {
|
||||
|
||||
// Intentionally private
|
||||
@SuppressWarnings("unused")
|
||||
@Configuration
|
||||
private static class PrivateConfig {
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue