From 5d0c5f76987ed4e9cf3172df3b4fa9acbb64e576 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 8 Apr 2011 22:57:45 +0000 Subject: [PATCH] [SPR-6184] Introduced ResourceType enum for context loaders; documented tests. git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4199 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../test/context/ContextConfiguration.java | 8 ++--- .../test/context/ContextLoaderUtils.java | 15 ++++---- .../ResourceTypeAwareContextLoader.java | 34 ++++++++++++++----- .../support/AbstractContextLoader.java | 20 +++-------- .../AnnotationConfigContextLoader.java | 14 ++------ ...figSpringJUnit4ClassRunnerAppCtxTests.java | 10 +++++- ...ts.java => AnnotationConfigTestSuite.java} | 5 +-- .../DefaultConfigClassBaseTests.java | 5 ++- .../DefaultConfigClassBaseTestsConfig.java | 2 +- .../DefaultConfigClassInheritedTests.java | 6 +++- ...efaultConfigClassInheritedTestsConfig.java | 2 +- .../annotation/PojoAndStringConfig.java | 7 +++- 12 files changed, 75 insertions(+), 53 deletions(-) rename org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/{AnnotationConfigSuiteTests.java => AnnotationConfigTestSuite.java} (87%) diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/ContextConfiguration.java b/org.springframework.test/src/main/java/org/springframework/test/context/ContextConfiguration.java index b2728c83bb1..c84abc94059 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/ContextConfiguration.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/ContextConfiguration.java @@ -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 * extending the list of resource locations. In the following * example, the {@link org.springframework.context.ApplicationContext ApplicationContext} - * for ExtendedTest will be loaded from + * for ExtendedTest will be loaded from * "base-context.xml" and * "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}. *

If not specified, the loader will be inherited from the first superclass * which is annotated with @ContextConfiguration 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. *

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 diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java b/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java index bbf848f0afc..8ca43ff18a0 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/ContextLoaderUtils.java @@ -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 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); } diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/ResourceTypeAwareContextLoader.java b/org.springframework.test/src/main/java/org/springframework/test/context/ResourceTypeAwareContextLoader.java index a3874d9c0ef..b0254975683 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/ResourceTypeAwareContextLoader.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/ResourceTypeAwareContextLoader.java @@ -25,18 +25,34 @@ package org.springframework.test.context; public interface ResourceTypeAwareContextLoader extends ContextLoader { /** - * @return true if this ContextLoader 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 true if this ContextLoader supports - * Class-based resource locations - * @see ContextConfiguration#classes() + * Get the application context {@link ResourceType} supported by this + * ContextLoader. + * + * @return the context resource type supported by this ContextLoader */ - boolean supportsClassResources(); + ResourceType getResourceType(); } diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java b/org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java index 27882beab64..ff0328e8837 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/support/AbstractContextLoader.java @@ -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. *

For example, if the supplied class is com.example.MyTest, * 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 true + * The default implementation returns {@link ResourceType#LOCATIONS}. + *

Can be overridden by subclasses. */ - public boolean supportsStringResources() { - return true; - } - - /** - * TODO Document supportsClassResources() implementations. - * - * @return false - */ - public boolean supportsClassResources() { - return false; + public ResourceType getResourceType() { + return ResourceType.LOCATIONS; } } diff --git a/org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java b/org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java index a8f23ee3ebc..5503f5f739d 100644 --- a/org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java +++ b/org.springframework.test/src/main/java/org/springframework/test/context/support/AnnotationConfigContextLoader.java @@ -128,19 +128,11 @@ public class AnnotationConfigContextLoader extends AbstractGenericContextLoader } /** - * @return true + * Returns {@link ResourceType#CLASSES}. */ @Override - public boolean supportsClassResources() { - return true; - } - - /** - * @return false - */ - @Override - public boolean supportsStringResources() { - return false; + public ResourceType getResourceType() { + return ResourceType.CLASSES; } } diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java index 4b2b7eb0673..7c7cef5c544 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSpringJUnit4ClassRunnerAppCtxTests.java @@ -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. + * + *

Furthermore, by extending {@link SpringJUnit4ClassRunnerAppCtxTests}, + * this class also verifies support for several basic features of the + * Spring TestContext Framework. See JavaDoc in + * SpringJUnit4ClassRunnerAppCtxTests for details. + * + *

Configuration will be loaded from {@link PojoAndStringConfig}. * * @author Sam Brannen * @since 3.1 diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSuiteTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigTestSuite.java similarity index 87% rename from org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSuiteTests.java rename to org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigTestSuite.java index 7c5445634b6..926c5cc7cf5 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigSuiteTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/AnnotationConfigTestSuite.java @@ -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 configuration class + * 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 { } diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassBaseTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassBaseTests.java index 7da86c1f439..0a994176b33 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassBaseTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassBaseTests.java @@ -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. + * + *

Configuration will be loaded from {@link DefaultConfigClassBaseTestsConfig}. * * @author Sam Brannen * @since 3.1 diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassBaseTestsConfig.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassBaseTestsConfig.java index 3df61e7ccb8..56466b5bf44 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassBaseTestsConfig.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassBaseTestsConfig.java @@ -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 diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassInheritedTests.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassInheritedTests.java index 0c0011a5bfd..8a2b37cfd5b 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassInheritedTests.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassInheritedTests.java @@ -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. + * + *

Configuration will be loaded from {@link DefaultConfigClassBaseTestsConfig} + * and {@link DefaultConfigClassInheritedTestsConfig}. * * @author Sam Brannen * @since 3.1 diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassInheritedTestsConfig.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassInheritedTestsConfig.java index f47420b2f3a..a1261d4ec0b 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassInheritedTestsConfig.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/DefaultConfigClassInheritedTestsConfig.java @@ -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 diff --git a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/PojoAndStringConfig.java b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/PojoAndStringConfig.java index 440f5cbc33c..e04c1c07378 100644 --- a/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/PojoAndStringConfig.java +++ b/org.springframework.test/src/test/java/org/springframework/test/context/junit4/annotation/PojoAndStringConfig.java @@ -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. + * + *

The beans defined in this configuration class map directly to the + * beans defined in SpringJUnit4ClassRunnerAppCtxTests-context.xml. + * Consequently, the application contexts loaded from these two sources + * should be identical with regard to bean definitions. * * @author Sam Brannen * @since 3.1