[SPR-8387] Fleshed out the implementation of DelegatingSmartContextLoader and corresponding tests.

This commit is contained in:
Sam Brannen
2011-07-17 17:16:12 +00:00
parent 9d7bc31f0d
commit f874ed9790
6 changed files with 171 additions and 53 deletions

View File

@@ -102,6 +102,10 @@ public class ContextConfigurationAttributes {
*/
public ContextConfigurationAttributes(Class<?> declaringClass, String[] locations, Class<?>[] classes,
boolean inheritLocations, Class<? extends ContextLoader> contextLoaderClass) {
Assert.notNull(declaringClass, "declaringClass must not be null");
Assert.notNull(contextLoaderClass, "contextLoaderClass must not be null");
this.declaringClass = declaringClass;
this.locations = locations;
this.classes = classes;
@@ -115,7 +119,7 @@ public class ContextConfigurationAttributes {
* @return the declaring class; never <code>null</code>
*/
public Class<?> getDeclaringClass() {
return this.declaringClass;
return declaringClass;
}
/**
@@ -130,7 +134,7 @@ public class ContextConfigurationAttributes {
* @see #setLocations()
*/
public String[] getLocations() {
return this.locations;
return locations;
}
/**
@@ -153,7 +157,7 @@ public class ContextConfigurationAttributes {
* @see #setClasses()
*/
public Class<?>[] getClasses() {
return this.classes;
return classes;
}
/**
@@ -184,7 +188,7 @@ public class ContextConfigurationAttributes {
* @see ContextConfiguration#inheritLocations
*/
public boolean isInheritLocations() {
return this.inheritLocations;
return inheritLocations;
}
/**
@@ -194,7 +198,7 @@ public class ContextConfigurationAttributes {
* @see ContextConfiguration#loader
*/
public Class<? extends ContextLoader> getContextLoaderClass() {
return this.contextLoaderClass;
return contextLoaderClass;
}
/**
@@ -204,11 +208,11 @@ public class ContextConfigurationAttributes {
@Override
public String toString() {
return new ToStringCreator(this)//
.append("declaringClass", this.declaringClass)//
.append("locations", ObjectUtils.nullSafeToString(this.locations))//
.append("classes", ObjectUtils.nullSafeToString(this.classes))//
.append("inheritLocations", this.inheritLocations)//
.append("contextLoaderClass", this.contextLoaderClass)//
.append("declaringClass", declaringClass.getName())//
.append("locations", ObjectUtils.nullSafeToString(locations))//
.append("classes", ObjectUtils.nullSafeToString(classes))//
.append("inheritLocations", inheritLocations)//
.append("contextLoaderClass", contextLoaderClass.getName())//
.toString();
}

View File

@@ -136,7 +136,7 @@ public class MergedContextConfiguration {
* <code>MergedContextConfiguration</code>.
*/
public Class<?> getTestClass() {
return this.testClass;
return testClass;
}
/**
@@ -144,7 +144,7 @@ public class MergedContextConfiguration {
* {@link #getTestClass() test class}.
*/
public String[] getLocations() {
return this.locations;
return locations;
}
/**
@@ -152,7 +152,7 @@ public class MergedContextConfiguration {
* {@link #getTestClass() test class}.
*/
public Class<?>[] getClasses() {
return this.classes;
return classes;
}
/**
@@ -160,7 +160,7 @@ public class MergedContextConfiguration {
* {@link #getTestClass() test class}.
*/
public String[] getActiveProfiles() {
return this.activeProfiles;
return activeProfiles;
}
/**
@@ -168,7 +168,7 @@ public class MergedContextConfiguration {
* {@link #getTestClass() test class}.
*/
public ContextLoader getContextLoader() {
return this.contextLoader;
return contextLoader;
}
/**
@@ -180,7 +180,7 @@ public class MergedContextConfiguration {
* that was loaded using properties of this <code>MergedContextConfiguration</code>.
*/
public String getContextKey() {
return this.contextKey;
return contextKey;
}
/**
@@ -190,12 +190,12 @@ public class MergedContextConfiguration {
@Override
public String toString() {
return new ToStringCreator(this)//
.append("testClass", this.testClass)//
.append("locations", ObjectUtils.nullSafeToString(this.locations))//
.append("classes", ObjectUtils.nullSafeToString(this.classes))//
.append("activeProfiles", ObjectUtils.nullSafeToString(this.activeProfiles))//
.append("testClass", testClass)//
.append("locations", ObjectUtils.nullSafeToString(locations))//
.append("classes", ObjectUtils.nullSafeToString(classes))//
.append("activeProfiles", ObjectUtils.nullSafeToString(activeProfiles))//
.append("contextLoader", nullSafeToString(contextLoader))//
.append("contextKey", this.contextKey)//
.append("contextKey", contextKey)//
.toString();
}

View File

@@ -37,7 +37,8 @@ import org.springframework.context.ApplicationContext;
* {@link #processContextConfiguration(ContextConfigurationAttributes) processContextConfiguration()}
* should be merged for all classes in the hierarchy of the root test class and
* then supplied to {@link #loadContext(MergedContextConfiguration) loadContext()}.
* Even though <code>SmartContextLoader</code> extends <code>ContextLoader</code>,
*
* <p>Even though <code>SmartContextLoader</code> extends <code>ContextLoader</code>,
* clients should favor <code>SmartContextLoader</code>-specific methods over those
* defined in <code>ContextLoader</code>, particularly because a
* <code>SmartContextLoader</code> may choose not to support methods defined in
@@ -63,9 +64,8 @@ public interface SmartContextLoader extends ContextLoader {
/**
* Determines if this <code>SmartContextLoader</code> generates default resource
* locations or
* {@link org.springframework.context.annotation.Configuration configuration classes}
* if the <code>locations</code> or <code>classes</code>
* locations or {@link org.springframework.context.annotation.Configuration
* configuration classes} if the <code>locations</code> or <code>classes</code>
* present in the {@link ContextConfigurationAttributes} provided to
* {@link #processContextConfiguration()} are <code>null</code> or empty.
* <p>Returning a value of <code>true</code> signals not only that this

View File

@@ -66,34 +66,50 @@ public class DelegatingSmartContextLoader implements SmartContextLoader {
final boolean originallyHadResources = configAttributes.hasResources();
for (SmartContextLoader loader : candidates) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Potentially delegating to %s to process context configuration [%s].",
loader.getClass().getName(), configAttributes));
}
// If the original locations and classes were not empty, there's no
// need to bother with default generation checks; just let each
// loader process the configuration.
if (originallyHadResources) {
loader.processContextConfiguration(configAttributes);
}
// Otherwise, if the loader claims to generate defaults, let it
// process the configuration.
else if (loader.generatesDefaults()) {
loader.processContextConfiguration(configAttributes);
if (configAttributes.hasResources() && logger.isInfoEnabled()) {
logger.info(String.format("SmartContextLoader candidate %s "
+ "generated defaults for context configuration [%s].", loader, configAttributes));
// If the original locations and classes were not empty, there's no
// need to bother with default generation checks; just let each
// loader process the configuration.
if (originallyHadResources) {
for (SmartContextLoader loader : candidates) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("Delegating to %s to process context configuration [%s].",
loader.getClass().getName(), configAttributes));
}
loader.processContextConfiguration(configAttributes);
}
}
else if (generatesDefaults()) {
for (SmartContextLoader loader : candidates) {
boolean defaultResourcesAlreadyGenerated = configAttributes.hasResources();
// If defaults haven't already been generated and the loader
// claims to generate defaults, let it process the
// configuration.
if (!defaultResourcesAlreadyGenerated && loader.generatesDefaults()) {
if (logger.isDebugEnabled()) {
logger.debug(String.format(
"Delegating to %s to generate defaults for context configuration [%s].",
loader.getClass().getName(), configAttributes));
}
// If any loader claims to generate defaults but none actually did,
// throw an exception.
if (generatesDefaults() && !originallyHadResources && !configAttributes.hasResources()) {
throw new IllegalStateException(String.format("None of the SmartContextLoader candidates %s "
+ "was able to generate defaults for context configuration [%s].", candidates, configAttributes));
loader.processContextConfiguration(configAttributes);
if (configAttributes.hasResources()) {
if (logger.isInfoEnabled()) {
logger.info(String.format("SmartContextLoader candidate %s "
+ "generated defaults for context configuration [%s].", loader, configAttributes));
}
}
}
}
// If any loader claims to generate defaults but none actually did,
// throw an exception.
if (!configAttributes.hasResources()) {
throw new IllegalStateException(
String.format("None of the SmartContextLoader candidates %s "
+ "was able to generate defaults for context configuration [%s].", candidates,
configAttributes));
}
}
}