Browse Source

[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.

pull/7/head
Sam Brannen 14 years ago
parent
commit
a77cf0f652
  1. 124
      org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java
  2. 195
      org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java
  3. 53
      org.springframework.test/src/test/java/org/springframework/test/context/support/AnnotationConfigContextLoaderTests.java
  4. 6
      org.springframework.test/src/test/java/org/springframework/test/context/support/ContextConfigurationInnerClassTestCase.java
  5. 35
      org.springframework.test/src/test/java/org/springframework/test/context/support/FinalConfigInnerClassTestCase.java
  6. 38
      org.springframework.test/src/test/java/org/springframework/test/context/support/MultipleStaticConfigurationClassesTestCase.java
  7. 40
      org.springframework.test/src/test/java/org/springframework/test/context/support/NonStaticConfigInnerClassesTestCase.java
  8. 7
      org.springframework.test/src/test/java/org/springframework/test/context/support/PlainVanillaFooConfigInnerClassTestCase.java
  9. 36
      org.springframework.test/src/test/java/org/springframework/test/context/support/PrivateConfigInnerClassTestCase.java

124
org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractGenericContextLoader.java

@ -24,23 +24,36 @@ import org.springframework.context.ConfigurableApplicationContext; @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) {
}

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

@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.test.context.support;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
@ -31,81 +32,81 @@ import org.springframework.util.Assert; @@ -31,81 +32,81 @@ 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() {
return true;
private boolean isDefaultConfigurationClassCandidate(Class<?> clazz) {
return clazz != null && isStaticNonPrivateAndNonFinal(clazz) && clazz.isAnnotationPresent(Configuration.class);
}
/**
* Returns &quot;$ContextConfiguration</code>&quot;; 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)
* TODO Document generatesDefaults().
*/
protected String getConfigurationClassNameSuffix() {
return "$ContextConfiguration";
// TODO Consider moving to SmartContextLoader SPI.
protected boolean generatesDefaults() {
return true;
}
/**
@ -113,40 +114,57 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader @@ -113,40 +114,57 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
*/
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()]);
}
// --- AbstractContextLoader -----------------------------------------------
/**
* TODO Document overridden createBeanDefinitionReader().
* <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 BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context) {
protected String[] modifyLocations(Class<?> clazz, String... locations) {
throw new UnsupportedOperationException(
"AnnotationConfigContextLoader does not support the createBeanDefinitionReader(GenericApplicationContext) method");
"AnnotationConfigContextLoader does not support the modifyLocations(Class, String...) method");
}
/**
* TODO Document overridden generateDefaultLocations().
* <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) {
@ -155,21 +173,62 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader @@ -155,21 +173,62 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader
}
/**
* 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#getResourceSuffix
* @throws UnsupportedOperationException
*/
@Override
protected String[] modifyLocations(Class<?> clazz, String... locations) {
protected String getResourceSuffix() {
throw new UnsupportedOperationException(
"AnnotationConfigContextLoader does not support the modifyLocations(Class, String...) method");
"AnnotationConfigContextLoader does not support the getResourceSuffix() method");
}
// --- AbstractGenericContextLoader ----------------------------------------
/**
* TODO Document overridden getResourceSuffix().
* 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 String getResourceSuffix() {
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 getResourceSuffix() method");
"AnnotationConfigContextLoader does not support the createBeanDefinitionReader(GenericApplicationContext) method");
}
}

53
org.springframework.test/src/test/java/org/springframework/test/context/support/AnnotationConfigContextLoaderTests.java

@ -32,31 +32,50 @@ public class AnnotationConfigContextLoaderTests { @@ -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);
}
}

6
org.springframework.test/src/test/java/org/springframework/test/context/support/ContextConfigurationInnerClassTestCase.java

@ -16,7 +16,6 @@ @@ -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 { @@ -30,11 +29,6 @@ public class ContextConfigurationInnerClassTestCase {
@Configuration
static class ContextConfiguration {
@Bean
public String foo() {
return "foo";
}
}
}

35
org.springframework.test/src/test/java/org/springframework/test/context/support/FinalConfigInnerClassTestCase.java

@ -0,0 +1,35 @@ @@ -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 {
}
}

38
org.springframework.test/src/test/java/org/springframework/test/context/support/MultipleStaticConfigurationClassesTestCase.java

@ -0,0 +1,38 @@ @@ -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 {
}
}

40
org.springframework.test/src/test/java/org/springframework/test/context/support/NonStaticConfigInnerClassesTestCase.java

@ -0,0 +1,40 @@ @@ -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 {
}
}

7
org.springframework.test/src/test/java/org/springframework/test/context/support/PlainVanillaFooConfigInnerClassTestCase.java

@ -16,8 +16,6 @@ @@ -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 { @@ -29,11 +27,6 @@ public class PlainVanillaFooConfigInnerClassTestCase {
// Intentionally NOT annotated with @Configuration
static class FooConfig {
@Bean
public String foo() {
return "foo";
}
}
}

36
org.springframework.test/src/test/java/org/springframework/test/context/support/PrivateConfigInnerClassTestCase.java

@ -0,0 +1,36 @@ @@ -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…
Cancel
Save