Remove @BootstrapWith from @WebAppConfiguration

Update @WebAppConfiguration so that it no longer directly specifies
a TestContextBootstrapper. This allows third parties to use the
annotation in combination with their own bootstrapper.

BootstrapUtils now provides the detection logic for when
WebTestContextBootstrapper should be used.

Issue: SPR-13991
This commit is contained in:
Phillip Webb
2016-02-26 13:41:56 -08:00
committed by Sam Brannen
parent 0d12b84022
commit 504779e210
4 changed files with 112 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -24,6 +24,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.MultiValueMap;
@@ -43,6 +44,10 @@ abstract class BootstrapUtils {
private static final String DEFAULT_BOOTSTRAP_CONTEXT_CLASS_NAME = "org.springframework.test.context.support.DefaultBootstrapContext";
private static final String DEFAULT_WEB_BOOTSTRAP_CONTEXT_CLASS_NAME = "org.springframework.test.context.web.WebTestContextBootstrapper";
private static final String WEB_APP_CONFIGURATION_ANNOTATION = "org.springframework.test.context.web.WebAppConfiguration";
private static final String DEFAULT_CACHE_AWARE_CONTEXT_LOADER_DELEGATE_CLASS_NAME = "org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate";
private static final String DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME = "org.springframework.test.context.support.DefaultTestContextBootstrapper";
@@ -117,31 +122,15 @@ abstract class BootstrapUtils {
* @param bootstrapContext the bootstrap context to use
* @return a fully configured {@code TestContextBootstrapper}
*/
@SuppressWarnings("unchecked")
static TestContextBootstrapper resolveTestContextBootstrapper(BootstrapContext bootstrapContext) {
Class<?> testClass = bootstrapContext.getTestClass();
Class<? extends TestContextBootstrapper> clazz = null;
Class<?> clazz = null;
try {
MultiValueMap<String, Object> attributesMultiMap = AnnotatedElementUtils.getAllAnnotationAttributes(
testClass, BootstrapWith.class.getName());
List<Object> values = (attributesMultiMap == null ? null : attributesMultiMap.get(AnnotationUtils.VALUE));
if (values != null) {
if (values.size() != 1) {
String msg = String.format(
"Configuration error: found multiple declarations of @BootstrapWith on test class [%s] with values %s",
testClass.getName(), values);
throw new IllegalStateException(msg);
}
clazz = (Class<? extends TestContextBootstrapper>) values.get(0);
clazz = resolveExplicitTestContextBootstrapper(testClass);
if (clazz == null) {
clazz = resolveDefaultTestContextBootstrapper(testClass);
}
else {
clazz = (Class<? extends TestContextBootstrapper>) ClassUtils.forName(
DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME, BootstrapUtils.class.getClassLoader());
}
if (logger.isDebugEnabled()) {
logger.debug(String.format("Instantiating TestContextBootstrapper for test class [%s] from class [%s]",
testClass.getName(), clazz.getName()));
@@ -152,15 +141,35 @@ abstract class BootstrapUtils {
return testContextBootstrapper;
}
catch (Throwable t) {
if (t instanceof IllegalStateException) {
throw (IllegalStateException) t;
catch (Throwable ex) {
if (ex instanceof IllegalStateException) {
throw (IllegalStateException) ex;
}
throw new IllegalStateException("Could not load TestContextBootstrapper [" + clazz
+ "]. Specify @BootstrapWith's 'value' attribute "
+ "or make the default bootstrapper class available.", t);
+ "or make the default bootstrapper class available.", ex);
}
}
private static Class<?> resolveExplicitTestContextBootstrapper(Class<?> testClass) {
MultiValueMap<String, Object> attributesMultiMap = AnnotatedElementUtils.getAllAnnotationAttributes(
testClass, BootstrapWith.class.getName());
List<Object> values = (attributesMultiMap == null ? null : attributesMultiMap.get(AnnotationUtils.VALUE));
if (values == null) {
return null;
}
Assert.state(values.size() == 1, String.format("Configuration error: found multiple declarations of "
+ "@BootstrapWith on test class [%s] with values %s", testClass.getName(), values));
return (Class<?>) values.get(0);
}
private static Class<?> resolveDefaultTestContextBootstrapper(Class<?> testClass)
throws Exception {
ClassLoader classLoader = BootstrapUtils.class.getClassLoader();
if (AnnotatedElementUtils.isAnnotated(testClass, WEB_APP_CONFIGURATION_ANNOTATION)) {
return ClassUtils.forName(DEFAULT_WEB_BOOTSTRAP_CONTEXT_CLASS_NAME, classLoader);
}
return ClassUtils.forName(DEFAULT_TEST_CONTEXT_BOOTSTRAPPER_CLASS_NAME, classLoader);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -23,8 +23,6 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.test.context.BootstrapWith;
/**
* {@code @WebAppConfiguration} is a class-level annotation that is used to
* declare that the {@code ApplicationContext} loaded for an integration test
@@ -53,7 +51,6 @@ import org.springframework.test.context.BootstrapWith;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@BootstrapWith(WebTestContextBootstrapper.class)
public @interface WebAppConfiguration {
/**