[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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user