Switch the default @NestedTestConfiguration mode to INHERIT.
See gh-19930
This commit is contained in:
@@ -30,13 +30,13 @@ import java.lang.annotation.Target;
|
||||
*
|
||||
* <p>If {@code @NestedTestConfiguration} is not <em>present</em> or
|
||||
* <em>meta-present</em> on a test class, configuration from the test class will
|
||||
* not propagate to inner test classes (see {@link EnclosingConfiguration#OVERRIDE}).
|
||||
* Consequently, inner test classes will have to declare their own Spring test
|
||||
* configuration annotations. If you wish for an inner test class to inherit
|
||||
* configuration from its enclosing class, annotate either the inner test class
|
||||
* or the enclosing class with
|
||||
* {@code @NestedTestConfiguration(EnclosingConfiguration.INHERIT)}. Note that
|
||||
* a {@code @NestedTestConfiguration(...)} declaration is inherited within the
|
||||
* propagate to inner test classes (see {@link EnclosingConfiguration#INHERIT}).
|
||||
* If {@code @NestedTestConfiguration(OVERRIDE)} is used to switch the mode,
|
||||
* inner test classes will have to declare their own Spring test configuration
|
||||
* annotations. If you wish to explicitly configure the mode, annotate either
|
||||
* the inner test class or the enclosing class with
|
||||
* {@code @NestedTestConfiguration(...}. Note that a
|
||||
* {@code @NestedTestConfiguration(...)} declaration is inherited within the
|
||||
* superclass hierarchy as well as within the enclosing class hierarchy. Thus,
|
||||
* there is no need to redeclare the annotation unless you wish to switch the
|
||||
* mode.
|
||||
|
||||
@@ -68,8 +68,8 @@ import org.springframework.util.ObjectUtils;
|
||||
*/
|
||||
public abstract class MetaAnnotationUtils {
|
||||
|
||||
private static final ConcurrentLruCache<Class<?>, SearchStrategy> cachedSearchStrategies =
|
||||
new ConcurrentLruCache<>(32, MetaAnnotationUtils::lookUpSearchStrategy);
|
||||
private static final ConcurrentLruCache<Class<?>, EnclosingConfiguration> cachedSearchStrategies =
|
||||
new ConcurrentLruCache<>(32, MetaAnnotationUtils::lookUpEnclosingConfiguration);
|
||||
|
||||
|
||||
/**
|
||||
@@ -301,35 +301,29 @@ public abstract class MetaAnnotationUtils {
|
||||
* class should be searched
|
||||
* @since 5.3
|
||||
* @see ClassUtils#isInnerClass(Class)
|
||||
* @see #getSearchStrategy(Class)
|
||||
*/
|
||||
public static boolean searchEnclosingClass(Class<?> clazz) {
|
||||
return (ClassUtils.isInnerClass(clazz) &&
|
||||
getSearchStrategy(clazz) == SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES);
|
||||
getEnclosingConfiguration(clazz) == EnclosingConfiguration.INHERIT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the {@link SearchStrategy} for the supplied class.
|
||||
* Get the {@link EnclosingConfiguration} mode for the supplied class.
|
||||
* @param clazz the class for which the search strategy should be resolved
|
||||
* @return the resolved search strategy
|
||||
* @since 5.3
|
||||
*/
|
||||
private static SearchStrategy getSearchStrategy(Class<?> clazz) {
|
||||
private static EnclosingConfiguration getEnclosingConfiguration(Class<?> clazz) {
|
||||
return cachedSearchStrategies.get(clazz);
|
||||
}
|
||||
|
||||
private static SearchStrategy lookUpSearchStrategy(Class<?> clazz) {
|
||||
EnclosingConfiguration enclosingConfiguration =
|
||||
MergedAnnotations.from(clazz, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES)
|
||||
private static EnclosingConfiguration lookUpEnclosingConfiguration(Class<?> clazz) {
|
||||
// TODO Make the default EnclosingConfiguration mode globally configurable via SpringProperties.
|
||||
return MergedAnnotations.from(clazz, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES)
|
||||
.stream(NestedTestConfiguration.class)
|
||||
.map(mergedAnnotation -> mergedAnnotation.getEnum("value", EnclosingConfiguration.class))
|
||||
.findFirst()
|
||||
.orElse(EnclosingConfiguration.OVERRIDE);
|
||||
// TODO Switch the default EnclosingConfiguration mode to INHERIT.
|
||||
// TODO Make the default EnclosingConfiguration mode globally configurable via SpringProperties.
|
||||
return (enclosingConfiguration == EnclosingConfiguration.INHERIT ?
|
||||
SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES :
|
||||
SearchStrategy.TYPE_HIERARCHY);
|
||||
.orElse(EnclosingConfiguration.INHERIT);
|
||||
}
|
||||
|
||||
private static void assertNonEmptyAnnotationTypeArray(Class<?>[] annotationTypes, String message) {
|
||||
@@ -505,10 +499,12 @@ public abstract class MetaAnnotationUtils {
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Set<T> findAllLocalMergedAnnotations() {
|
||||
Class<T> annotationType = (Class<T>) getAnnotationType();
|
||||
SearchStrategy searchStrategy = getSearchStrategy(getRootDeclaringClass());
|
||||
SearchStrategy searchStrategy =
|
||||
(getEnclosingConfiguration(getRootDeclaringClass()) == EnclosingConfiguration.INHERIT ?
|
||||
SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES :
|
||||
SearchStrategy.TYPE_HIERARCHY);
|
||||
return MergedAnnotations.from(getRootDeclaringClass(), searchStrategy, RepeatableContainers.none())
|
||||
.stream(annotationType)
|
||||
.stream((Class<T>) getAnnotationType())
|
||||
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
|
||||
.collect(MergedAnnotationCollectors.toAnnotationSet());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user