Support @Configuration as meta-annotation in the TCF

Spring Framework 4.0 introduced support for using test-related
annotations as meta-annotations in the Spring TestContext Framework
(TCF) in order to create custom composed annotations within a test
suite; however, the detection of default @Configuration classes in test
classes was not updated to search for @Configuration declared as a
meta-annotation. Specifically, AnnotationConfigContextLoaderUtils
invokes Class.isAnnotated() which only searches for annotations
declared directly on the class in question.

This commit addresses this issue by refactoring the
isDefaultConfigurationClassCandidate() method in
AnnotationConfigContextLoaderUtils so that it uses
AnnotationUtils.findAnnotation() instead of Class.isAnnotated() for
detecting the presence of the @Configuration annotation, either
directly or as a meta-annotation.

Issue: SPR-12659
This commit is contained in:
Sam Brannen
2015-01-23 22:13:04 +01:00
parent c5c32ec206
commit 2d918380f0
2 changed files with 93 additions and 2 deletions

View File

@@ -24,6 +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.test.context.SmartContextLoader;
import org.springframework.util.Assert;
@@ -95,13 +96,14 @@ public abstract class AnnotationConfigContextLoaderUtils {
* <li>must not be {@code private}</li>
* <li>must not be {@code final}</li>
* <li>must be {@code static}</li>
* <li>must be annotated with {@code @Configuration}</li>
* <li>must be annotated or meta-annotated with {@code @Configuration}</li>
* </ul>
* @param clazz the class to check
* @return {@code true} if the supplied class meets the candidate criteria
*/
private static boolean isDefaultConfigurationClassCandidate(Class<?> clazz) {
return (clazz != null && isStaticNonPrivateAndNonFinal(clazz) && clazz.isAnnotationPresent(Configuration.class));
return (clazz != null && isStaticNonPrivateAndNonFinal(clazz) &&
(AnnotationUtils.findAnnotation(clazz, Configuration.class) != null));
}
private static boolean isStaticNonPrivateAndNonFinal(Class<?> clazz) {