Consistent use of AnnotatedElementUtils.findMergedAnnotation/hasAnnotation
Issue: SPR-13440
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 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.
|
||||
@@ -21,13 +21,12 @@ import java.lang.reflect.Method;
|
||||
import org.apache.commons.logging.Log;
|
||||
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.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.springframework.core.annotation.AnnotationUtils.*;
|
||||
|
||||
/**
|
||||
* General utility methods for working with <em>profile values</em>.
|
||||
*
|
||||
@@ -49,12 +48,10 @@ public abstract class ProfileValueUtils {
|
||||
* {@link ProfileValueSourceConfiguration
|
||||
* @ProfileValueSourceConfiguration} annotation and instantiates a new
|
||||
* instance of that type.
|
||||
* <p>
|
||||
* If {@link ProfileValueSourceConfiguration
|
||||
* <p>If {@link ProfileValueSourceConfiguration
|
||||
* @ProfileValueSourceConfiguration} is not present on the specified
|
||||
* class or if a custom {@link ProfileValueSource} is not declared, the
|
||||
* default {@link SystemProfileValueSource} will be returned instead.
|
||||
*
|
||||
* @param testClass The test class for which the ProfileValueSource should
|
||||
* be retrieved
|
||||
* @return the configured (or default) ProfileValueSource for the specified
|
||||
@@ -66,10 +63,10 @@ public abstract class ProfileValueUtils {
|
||||
Assert.notNull(testClass, "testClass must not be null");
|
||||
|
||||
Class<ProfileValueSourceConfiguration> annotationType = ProfileValueSourceConfiguration.class;
|
||||
ProfileValueSourceConfiguration config = findAnnotation(testClass, annotationType);
|
||||
ProfileValueSourceConfiguration config = AnnotatedElementUtils.findMergedAnnotation(testClass, annotationType);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Retrieved @ProfileValueSourceConfiguration [" + config + "] for test class ["
|
||||
+ testClass.getName() + "]");
|
||||
logger.debug("Retrieved @ProfileValueSourceConfiguration [" + config + "] for test class [" +
|
||||
testClass.getName() + "]");
|
||||
}
|
||||
|
||||
Class<? extends ProfileValueSource> profileValueSourceType;
|
||||
@@ -80,8 +77,8 @@ public abstract class ProfileValueUtils {
|
||||
profileValueSourceType = (Class<? extends ProfileValueSource>) AnnotationUtils.getDefaultValue(annotationType);
|
||||
}
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Retrieved ProfileValueSource type [" + profileValueSourceType + "] for class ["
|
||||
+ testClass.getName() + "]");
|
||||
logger.debug("Retrieved ProfileValueSource type [" + profileValueSourceType + "] for class [" +
|
||||
testClass.getName() + "]");
|
||||
}
|
||||
|
||||
ProfileValueSource profileValueSource;
|
||||
@@ -92,10 +89,10 @@ public abstract class ProfileValueUtils {
|
||||
try {
|
||||
profileValueSource = profileValueSourceType.newInstance();
|
||||
}
|
||||
catch (Exception e) {
|
||||
catch (Exception ex) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Could not instantiate a ProfileValueSource of type [" + profileValueSourceType
|
||||
+ "] for class [" + testClass.getName() + "]: using default.", e);
|
||||
logger.warn("Could not instantiate a ProfileValueSource of type [" + profileValueSourceType +
|
||||
"] for class [" + testClass.getName() + "]: using default.", ex);
|
||||
}
|
||||
profileValueSource = SystemProfileValueSource.getInstance();
|
||||
}
|
||||
@@ -108,16 +105,14 @@ public abstract class ProfileValueUtils {
|
||||
* Determine if the supplied {@code testClass} is <em>enabled</em> in
|
||||
* the current environment, as specified by the {@link IfProfileValue
|
||||
* @IfProfileValue} annotation at the class level.
|
||||
* <p>
|
||||
* Defaults to {@code true} if no {@link IfProfileValue
|
||||
* <p>Defaults to {@code true} if no {@link IfProfileValue
|
||||
* @IfProfileValue} annotation is declared.
|
||||
*
|
||||
* @param testClass the test class
|
||||
* @return {@code true} if the test is <em>enabled</em> in the current
|
||||
* environment
|
||||
*/
|
||||
public static boolean isTestEnabledInThisEnvironment(Class<?> testClass) {
|
||||
IfProfileValue ifProfileValue = findAnnotation(testClass, IfProfileValue.class);
|
||||
IfProfileValue ifProfileValue = AnnotatedElementUtils.findMergedAnnotation(testClass, IfProfileValue.class);
|
||||
return isTestEnabledInThisEnvironment(retrieveProfileValueSource(testClass), ifProfileValue);
|
||||
}
|
||||
|
||||
@@ -127,10 +122,8 @@ public abstract class ProfileValueUtils {
|
||||
* @IfProfileValue} annotation, which may be declared on the test
|
||||
* method itself or at the class level. Class-level usage overrides
|
||||
* method-level usage.
|
||||
* <p>
|
||||
* Defaults to {@code true} if no {@link IfProfileValue
|
||||
* <p>Defaults to {@code true} if no {@link IfProfileValue
|
||||
* @IfProfileValue} annotation is declared.
|
||||
*
|
||||
* @param testMethod the test method
|
||||
* @param testClass the test class
|
||||
* @return {@code true} if the test is <em>enabled</em> in the current
|
||||
@@ -146,10 +139,8 @@ public abstract class ProfileValueUtils {
|
||||
* @IfProfileValue} annotation, which may be declared on the test
|
||||
* method itself or at the class level. Class-level usage overrides
|
||||
* method-level usage.
|
||||
* <p>
|
||||
* Defaults to {@code true} if no {@link IfProfileValue
|
||||
* <p>Defaults to {@code true} if no {@link IfProfileValue
|
||||
* @IfProfileValue} annotation is declared.
|
||||
*
|
||||
* @param profileValueSource the ProfileValueSource to use to determine if
|
||||
* the test is enabled
|
||||
* @param testMethod the test method
|
||||
@@ -160,11 +151,11 @@ public abstract class ProfileValueUtils {
|
||||
public static boolean isTestEnabledInThisEnvironment(ProfileValueSource profileValueSource, Method testMethod,
|
||||
Class<?> testClass) {
|
||||
|
||||
IfProfileValue ifProfileValue = findAnnotation(testClass, IfProfileValue.class);
|
||||
IfProfileValue ifProfileValue = AnnotatedElementUtils.findMergedAnnotation(testClass, IfProfileValue.class);
|
||||
boolean classLevelEnabled = isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue);
|
||||
|
||||
if (classLevelEnabled) {
|
||||
ifProfileValue = findAnnotation(testMethod, IfProfileValue.class);
|
||||
ifProfileValue = AnnotatedElementUtils.findMergedAnnotation(testMethod, IfProfileValue.class);
|
||||
return isTestEnabledInThisEnvironment(profileValueSource, ifProfileValue);
|
||||
}
|
||||
|
||||
@@ -175,7 +166,6 @@ public abstract class ProfileValueUtils {
|
||||
* Determine if the {@code value} (or one of the {@code values})
|
||||
* in the supplied {@link IfProfileValue @IfProfileValue} annotation is
|
||||
* <em>enabled</em> in the current environment.
|
||||
*
|
||||
* @param profileValueSource the ProfileValueSource to use to determine if
|
||||
* the test is enabled
|
||||
* @param ifProfileValue the annotation to introspect; may be
|
||||
@@ -195,8 +185,8 @@ public abstract class ProfileValueUtils {
|
||||
String[] annotatedValues = ifProfileValue.values();
|
||||
if (StringUtils.hasLength(ifProfileValue.value())) {
|
||||
if (annotatedValues.length > 0) {
|
||||
throw new IllegalArgumentException("Setting both the 'value' and 'values' attributes "
|
||||
+ "of @IfProfileValue is not allowed: choose one or the other.");
|
||||
throw new IllegalArgumentException("Setting both the 'value' and 'values' attributes " +
|
||||
"of @IfProfileValue is not allowed: choose one or the other.");
|
||||
}
|
||||
annotatedValues = new String[] { ifProfileValue.value() };
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -19,7 +19,6 @@ package org.springframework.test.annotation;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
|
||||
/**
|
||||
* Collection of utility methods for working with Spring's core testing annotations.
|
||||
@@ -52,7 +51,7 @@ public class TestAnnotationUtils {
|
||||
* not annotated with {@code @Repeat}
|
||||
*/
|
||||
public static int getRepeatCount(Method method) {
|
||||
Repeat repeat = AnnotationUtils.findAnnotation(method, Repeat.class);
|
||||
Repeat repeat = AnnotatedElementUtils.findMergedAnnotation(method, Repeat.class);
|
||||
if (repeat == null) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
@@ -22,7 +22,7 @@ import java.lang.reflect.Method;
|
||||
import org.junit.AssumptionViolatedException;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.test.annotation.IfProfileValue;
|
||||
import org.springframework.test.annotation.ProfileValueUtils;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -64,6 +64,7 @@ public class ProfileValueChecker extends Statement {
|
||||
this.testMethod = testMethod;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine if the test specified by arguments to the
|
||||
* {@linkplain #ProfileValueChecker constructor} is <em>enabled</em> in
|
||||
@@ -83,17 +84,17 @@ public class ProfileValueChecker extends Statement {
|
||||
public void evaluate() throws Throwable {
|
||||
if (this.testMethod == null) {
|
||||
if (!ProfileValueUtils.isTestEnabledInThisEnvironment(this.testClass)) {
|
||||
Annotation ann = AnnotationUtils.findAnnotation(this.testClass, IfProfileValue.class);
|
||||
throw new AssumptionViolatedException(
|
||||
String.format("Profile configured via [%s] is not enabled in this environment for test class [%s].",
|
||||
Annotation ann = AnnotatedElementUtils.findMergedAnnotation(this.testClass, IfProfileValue.class);
|
||||
throw new AssumptionViolatedException(String.format(
|
||||
"Profile configured via [%s] is not enabled in this environment for test class [%s].",
|
||||
ann, this.testClass.getName()));
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (!ProfileValueUtils.isTestEnabledInThisEnvironment(this.testMethod, this.testClass)) {
|
||||
throw new AssumptionViolatedException(String.format(
|
||||
"Profile configured via @IfProfileValue is not enabled in this environment for test method [%s].",
|
||||
this.testMethod));
|
||||
"Profile configured via @IfProfileValue is not enabled in this environment for test method [%s].",
|
||||
this.testMethod));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,8 +31,8 @@ import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.BeanInstantiationException;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.io.support.SpringFactoriesLoader;
|
||||
import org.springframework.test.context.BootstrapContext;
|
||||
import org.springframework.test.context.CacheAwareContextLoaderDelegate;
|
||||
@@ -278,7 +278,7 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
|
||||
return buildDefaultMergedContextConfiguration(testClass, cacheAwareContextLoaderDelegate);
|
||||
}
|
||||
|
||||
if (AnnotationUtils.findAnnotation(testClass, ContextHierarchy.class) != null) {
|
||||
if (AnnotatedElementUtils.findMergedAnnotation(testClass, ContextHierarchy.class) != null) {
|
||||
Map<String, List<ContextConfigurationAttributes>> hierarchyMap = ContextLoaderUtils.buildContextHierarchyMap(testClass);
|
||||
MergedContextConfiguration parentConfig = null;
|
||||
MergedContextConfiguration mergedConfig = null;
|
||||
|
||||
@@ -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,7 +24,7 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.test.context.SmartContextLoader;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -103,7 +103,7 @@ public abstract class AnnotationConfigContextLoaderUtils {
|
||||
*/
|
||||
private static boolean isDefaultConfigurationClassCandidate(Class<?> clazz) {
|
||||
return (clazz != null && isStaticNonPrivateAndNonFinal(clazz) &&
|
||||
(AnnotationUtils.findAnnotation(clazz, Configuration.class) != null));
|
||||
AnnotatedElementUtils.hasAnnotation(clazz, Configuration.class));
|
||||
}
|
||||
|
||||
private static boolean isStaticNonPrivateAndNonFinal(Class<?> clazz) {
|
||||
|
||||
@@ -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.
|
||||
@@ -95,11 +95,10 @@ abstract class ContextLoaderUtils {
|
||||
@SuppressWarnings("unchecked")
|
||||
static List<List<ContextConfigurationAttributes>> resolveContextHierarchyAttributes(Class<?> testClass) {
|
||||
Assert.notNull(testClass, "Class must not be null");
|
||||
Assert.state(findAnnotation(testClass, ContextHierarchy.class) != null, "@ContextHierarchy must be present");
|
||||
|
||||
final Class<ContextConfiguration> contextConfigType = ContextConfiguration.class;
|
||||
final Class<ContextHierarchy> contextHierarchyType = ContextHierarchy.class;
|
||||
final List<List<ContextConfigurationAttributes>> hierarchyAttributes = new ArrayList<List<ContextConfigurationAttributes>>();
|
||||
Class<ContextConfiguration> contextConfigType = ContextConfiguration.class;
|
||||
Class<ContextHierarchy> contextHierarchyType = ContextHierarchy.class;
|
||||
List<List<ContextConfigurationAttributes>> hierarchyAttributes = new ArrayList<List<ContextConfigurationAttributes>>();
|
||||
|
||||
UntypedAnnotationDescriptor desc =
|
||||
findAnnotationDescriptorForTypes(testClass, contextConfigType, contextHierarchyType);
|
||||
@@ -124,7 +123,7 @@ abstract class ContextLoaderUtils {
|
||||
throw new IllegalStateException(msg);
|
||||
}
|
||||
|
||||
final List<ContextConfigurationAttributes> configAttributesList = new ArrayList<ContextConfigurationAttributes>();
|
||||
List<ContextConfigurationAttributes> configAttributesList = new ArrayList<ContextConfigurationAttributes>();
|
||||
|
||||
if (contextConfigDeclaredLocally) {
|
||||
ContextConfiguration contextConfiguration = AnnotationUtils.synthesizeAnnotation(
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.annotation.BeanFactoryAnnotationUtils;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.test.annotation.Commit;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.TestContext;
|
||||
@@ -44,9 +45,6 @@ import org.springframework.util.Assert;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
|
||||
import static org.springframework.core.annotation.AnnotationUtils.getAnnotation;
|
||||
|
||||
/**
|
||||
* {@code TestExecutionListener} that provides support for executing tests
|
||||
* within <em>test-managed transactions</em> by honoring Spring's
|
||||
@@ -181,8 +179,8 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
|
||||
transactionAttribute);
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug("Explicit transaction definition [" + transactionAttribute + "] found for test context "
|
||||
+ testContext);
|
||||
logger.debug("Explicit transaction definition [" + transactionAttribute + "] found for test context " +
|
||||
testContext);
|
||||
}
|
||||
|
||||
if (transactionAttribute.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NOT_SUPPORTED) {
|
||||
@@ -193,8 +191,8 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
|
||||
|
||||
if (tm == null) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"Failed to retrieve PlatformTransactionManager for @Transactional test for test context %s.",
|
||||
testContext));
|
||||
"Failed to retrieve PlatformTransactionManager for @Transactional test for test context %s.",
|
||||
testContext));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,8 +253,10 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
|
||||
}
|
||||
}
|
||||
catch (InvocationTargetException ex) {
|
||||
logger.error("Exception encountered while executing @BeforeTransaction methods for test context "
|
||||
+ testContext + ".", ex.getTargetException());
|
||||
if (logger.isErrorEnabled()) {
|
||||
logger.error("Exception encountered while executing @BeforeTransaction methods for test context " +
|
||||
testContext + ".", ex.getTargetException());
|
||||
}
|
||||
ReflectionUtils.rethrowException(ex.getTargetException());
|
||||
}
|
||||
}
|
||||
@@ -286,15 +286,15 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
|
||||
if (afterTransactionException == null) {
|
||||
afterTransactionException = targetException;
|
||||
}
|
||||
logger.error("Exception encountered while executing @AfterTransaction method [" + method
|
||||
+ "] for test context " + testContext, targetException);
|
||||
logger.error("Exception encountered while executing @AfterTransaction method [" + method +
|
||||
"] for test context " + testContext, targetException);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
if (afterTransactionException == null) {
|
||||
afterTransactionException = ex;
|
||||
}
|
||||
logger.error("Exception encountered while executing @AfterTransaction method [" + method
|
||||
+ "] for test context " + testContext, ex);
|
||||
logger.error("Exception encountered while executing @AfterTransaction method [" + method +
|
||||
"] for test context " + testContext, ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,20 +317,18 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
|
||||
* @see #getTransactionManager(TestContext)
|
||||
*/
|
||||
protected PlatformTransactionManager getTransactionManager(TestContext testContext, String qualifier) {
|
||||
// look up by type and qualifier from @Transactional
|
||||
// Look up by type and qualifier from @Transactional
|
||||
if (StringUtils.hasText(qualifier)) {
|
||||
try {
|
||||
// Use autowire-capable factory in order to support extended qualifier
|
||||
// matching (only exposed on the internal BeanFactory, not on the
|
||||
// ApplicationContext).
|
||||
// Use autowire-capable factory in order to support extended qualifier matching
|
||||
// (only exposed on the internal BeanFactory, not on the ApplicationContext).
|
||||
BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
|
||||
|
||||
return BeanFactoryAnnotationUtils.qualifiedBeanOfType(bf, PlatformTransactionManager.class, qualifier);
|
||||
}
|
||||
catch (RuntimeException ex) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn(
|
||||
String.format(
|
||||
logger.warn(String.format(
|
||||
"Caught exception while retrieving transaction manager with qualifier '%s' for test context %s",
|
||||
qualifier, testContext), ex);
|
||||
}
|
||||
@@ -376,7 +374,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
|
||||
@SuppressWarnings("deprecation")
|
||||
protected final boolean isDefaultRollback(TestContext testContext) throws Exception {
|
||||
Class<?> testClass = testContext.getTestClass();
|
||||
Rollback rollback = findAnnotation(testClass, Rollback.class);
|
||||
Rollback rollback = AnnotatedElementUtils.findMergedAnnotation(testClass, Rollback.class);
|
||||
boolean rollbackPresent = (rollback != null);
|
||||
TransactionConfigurationAttributes txConfigAttributes = retrieveConfigurationAttributes(testContext);
|
||||
|
||||
@@ -411,21 +409,22 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
|
||||
*/
|
||||
protected final boolean isRollback(TestContext testContext) throws Exception {
|
||||
boolean rollback = isDefaultRollback(testContext);
|
||||
Rollback rollbackAnnotation = findAnnotation(testContext.getTestMethod(), Rollback.class);
|
||||
Rollback rollbackAnnotation =
|
||||
AnnotatedElementUtils.findMergedAnnotation(testContext.getTestMethod(), Rollback.class);
|
||||
if (rollbackAnnotation != null) {
|
||||
boolean rollbackOverride = rollbackAnnotation.value();
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format(
|
||||
"Method-level @Rollback(%s) overrides default rollback [%s] for test context %s.",
|
||||
rollbackOverride, rollback, testContext));
|
||||
"Method-level @Rollback(%s) overrides default rollback [%s] for test context %s.",
|
||||
rollbackOverride, rollback, testContext));
|
||||
}
|
||||
rollback = rollbackOverride;
|
||||
}
|
||||
else {
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format(
|
||||
"No method-level @Rollback override: using default rollback [%s] for test context %s.", rollback,
|
||||
testContext));
|
||||
"No method-level @Rollback override: using default rollback [%s] for test context %s.",
|
||||
rollback, testContext));
|
||||
}
|
||||
}
|
||||
return rollback;
|
||||
@@ -466,7 +465,7 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
|
||||
List<Method> results = new ArrayList<Method>();
|
||||
for (Class<?> current : getSuperClasses(clazz)) {
|
||||
for (Method method : current.getDeclaredMethods()) {
|
||||
Annotation annotation = getAnnotation(method, annotationType);
|
||||
Annotation annotation = AnnotationUtils.getAnnotation(method, annotationType);
|
||||
if (annotation != null && !isShadowed(method, results)) {
|
||||
results.add(method);
|
||||
}
|
||||
@@ -537,19 +536,18 @@ public class TransactionalTestExecutionListener extends AbstractTestExecutionLis
|
||||
if (this.configurationAttributes == null) {
|
||||
Class<?> clazz = testContext.getTestClass();
|
||||
|
||||
TransactionConfiguration txConfig = AnnotatedElementUtils.findMergedAnnotation(clazz,
|
||||
TransactionConfiguration.class);
|
||||
TransactionConfiguration txConfig =
|
||||
AnnotatedElementUtils.findMergedAnnotation(clazz, TransactionConfiguration.class);
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("Retrieved @TransactionConfiguration [%s] for test class [%s].",
|
||||
txConfig, clazz.getName()));
|
||||
txConfig, clazz.getName()));
|
||||
}
|
||||
|
||||
TransactionConfigurationAttributes configAttributes = (txConfig == null ? defaultTxConfigAttributes
|
||||
: new TransactionConfigurationAttributes(txConfig.transactionManager(), txConfig.defaultRollback()));
|
||||
|
||||
TransactionConfigurationAttributes configAttributes = (txConfig == null ? defaultTxConfigAttributes :
|
||||
new TransactionConfigurationAttributes(txConfig.transactionManager(), txConfig.defaultRollback()));
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format("Using TransactionConfigurationAttributes %s for test class [%s].",
|
||||
configAttributes, clazz.getName()));
|
||||
configAttributes, clazz.getName()));
|
||||
}
|
||||
this.configurationAttributes = configAttributes;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.core.Conventions;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.mock.web.MockServletContext;
|
||||
@@ -33,7 +33,6 @@ import org.springframework.test.context.TestContext;
|
||||
import org.springframework.test.context.TestExecutionListener;
|
||||
import org.springframework.test.context.support.AbstractTestExecutionListener;
|
||||
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
@@ -58,7 +57,7 @@ import org.springframework.web.context.request.ServletWebRequest;
|
||||
* <p>Note that {@code ServletTestExecutionListener} is enabled by default but
|
||||
* generally takes no action if the {@linkplain TestContext#getTestClass() test
|
||||
* class} is not annotated with {@link WebAppConfiguration @WebAppConfiguration}.
|
||||
* See the Javadoc for individual methods in this class for details.
|
||||
* See the javadocs for individual methods in this class for details.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @author Phillip Webb
|
||||
@@ -71,45 +70,41 @@ public class ServletTestExecutionListener extends AbstractTestExecutionListener
|
||||
* whether or not the {@code ServletTestExecutionListener} should {@linkplain
|
||||
* RequestContextHolder#resetRequestAttributes() reset} Spring Web's
|
||||
* {@code RequestContextHolder} in {@link #afterTestMethod(TestContext)}.
|
||||
*
|
||||
* <p>Permissible values include {@link Boolean#TRUE} and {@link Boolean#FALSE}.
|
||||
*/
|
||||
public static final String RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE = Conventions.getQualifiedAttributeName(
|
||||
ServletTestExecutionListener.class, "resetRequestContextHolder");
|
||||
ServletTestExecutionListener.class, "resetRequestContextHolder");
|
||||
|
||||
/**
|
||||
* Attribute name for a {@link TestContext} attribute which indicates that
|
||||
* {@code ServletTestExecutionListener} has already populated Spring Web's
|
||||
* {@code RequestContextHolder}.
|
||||
*
|
||||
* <p>Permissible values include {@link Boolean#TRUE} and {@link Boolean#FALSE}.
|
||||
*/
|
||||
public static final String POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE = Conventions.getQualifiedAttributeName(
|
||||
ServletTestExecutionListener.class, "populatedRequestContextHolder");
|
||||
ServletTestExecutionListener.class, "populatedRequestContextHolder");
|
||||
|
||||
/**
|
||||
* Attribute name for a request attribute which indicates that the
|
||||
* {@link MockHttpServletRequest} stored in the {@link RequestAttributes}
|
||||
* in Spring Web's {@link RequestContextHolder} was created by the TestContext
|
||||
* framework.
|
||||
*
|
||||
* <p>Permissible values include {@link Boolean#TRUE} and {@link Boolean#FALSE}.
|
||||
* @since 4.2
|
||||
*/
|
||||
public static final String CREATED_BY_THE_TESTCONTEXT_FRAMEWORK = Conventions.getQualifiedAttributeName(
|
||||
ServletTestExecutionListener.class, "createdByTheTestContextFramework");
|
||||
ServletTestExecutionListener.class, "createdByTheTestContextFramework");
|
||||
|
||||
/**
|
||||
* Attribute name for a {@link TestContext} attribute which indicates that that
|
||||
* the {@code ServletTestExecutionListener} should be activated. When not set to
|
||||
* {@code true}, activation occurs when the {@linkplain TestContext#getTestClass()
|
||||
* test class} is annotated with {@link WebAppConfiguration @WebAppConfiguration}.
|
||||
*
|
||||
* <p>Permissible values include {@link Boolean#TRUE} and {@link Boolean#FALSE}.
|
||||
* @since 4.3
|
||||
*/
|
||||
public static final String ACTIVATE_LISTENER = Conventions.getQualifiedAttributeName(
|
||||
ServletTestExecutionListener.class, "activateListener");
|
||||
ServletTestExecutionListener.class, "activateListener");
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ServletTestExecutionListener.class);
|
||||
|
||||
@@ -181,8 +176,8 @@ public class ServletTestExecutionListener extends AbstractTestExecutionListener
|
||||
}
|
||||
|
||||
private boolean isActivated(TestContext testContext) {
|
||||
return (Boolean.TRUE.equals(testContext.getAttribute(ACTIVATE_LISTENER))
|
||||
|| AnnotationUtils.findAnnotation(testContext.getTestClass(), WebAppConfiguration.class) != null);
|
||||
return (Boolean.TRUE.equals(testContext.getAttribute(ACTIVATE_LISTENER)) ||
|
||||
AnnotatedElementUtils.hasAnnotation(testContext.getTestClass(), WebAppConfiguration.class));
|
||||
}
|
||||
|
||||
private boolean alreadyPopulatedRequestContextHolder(TestContext testContext) {
|
||||
@@ -199,14 +194,16 @@ public class ServletTestExecutionListener extends AbstractTestExecutionListener
|
||||
if (context instanceof WebApplicationContext) {
|
||||
WebApplicationContext wac = (WebApplicationContext) context;
|
||||
ServletContext servletContext = wac.getServletContext();
|
||||
Assert.state(servletContext instanceof MockServletContext, String.format(
|
||||
"The WebApplicationContext for test context %s must be configured with a MockServletContext.",
|
||||
testContext));
|
||||
if (!(servletContext instanceof MockServletContext)) {
|
||||
throw new IllegalStateException(String.format(
|
||||
"The WebApplicationContext for test context %s must be configured with a MockServletContext.",
|
||||
testContext));
|
||||
}
|
||||
|
||||
if (logger.isDebugEnabled()) {
|
||||
logger.debug(String.format(
|
||||
"Setting up MockHttpServletRequest, MockHttpServletResponse, ServletWebRequest, and RequestContextHolder for test context %s.",
|
||||
testContext));
|
||||
"Setting up MockHttpServletRequest, MockHttpServletResponse, ServletWebRequest, and RequestContextHolder for test context %s.",
|
||||
testContext));
|
||||
}
|
||||
|
||||
MockServletContext mockServletContext = (MockServletContext) servletContext;
|
||||
|
||||
@@ -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.
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
package org.springframework.test.context.web;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.test.context.ContextLoader;
|
||||
import org.springframework.test.context.MergedContextConfiguration;
|
||||
import org.springframework.test.context.TestContextBootstrapper;
|
||||
@@ -45,12 +45,12 @@ public class WebTestContextBootstrapper extends DefaultTestContextBootstrapper {
|
||||
*/
|
||||
@Override
|
||||
protected Class<? extends ContextLoader> getDefaultContextLoaderClass(Class<?> testClass) {
|
||||
if (AnnotationUtils.findAnnotation(testClass, WebAppConfiguration.class) != null) {
|
||||
if (AnnotatedElementUtils.findMergedAnnotation(testClass, WebAppConfiguration.class) != null) {
|
||||
return WebDelegatingSmartContextLoader.class;
|
||||
}
|
||||
|
||||
// else...
|
||||
return super.getDefaultContextLoaderClass(testClass);
|
||||
else {
|
||||
return super.getDefaultContextLoaderClass(testClass);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,14 +61,14 @@ public class WebTestContextBootstrapper extends DefaultTestContextBootstrapper {
|
||||
*/
|
||||
@Override
|
||||
protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) {
|
||||
WebAppConfiguration webAppConfiguration = AnnotationUtils.findAnnotation(mergedConfig.getTestClass(),
|
||||
WebAppConfiguration.class);
|
||||
WebAppConfiguration webAppConfiguration =
|
||||
AnnotatedElementUtils.findMergedAnnotation(mergedConfig.getTestClass(), WebAppConfiguration.class);
|
||||
if (webAppConfiguration != null) {
|
||||
return new WebMergedContextConfiguration(mergedConfig, webAppConfiguration.value());
|
||||
}
|
||||
|
||||
// else...
|
||||
return mergedConfig;
|
||||
else {
|
||||
return mergedConfig;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user