Introduce TestContextAnnotationUtils to avoid package cycles

This commit introduces TestContextAnnotationUtils as a replacement for
MetaAnnotationUtils, with dedicated support for honoring the new
@NestedTestConfiguration annotation and related annotation search
semantics.

MetaAnnotationUtils has been reverted to its previous scope and is now
deprecated.

See gh-19930
This commit is contained in:
Sam Brannen
2020-10-23 14:24:33 +02:00
parent 1ec6843913
commit 8d86d61f9f
16 changed files with 740 additions and 382 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.util;
package org.springframework.test.context;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -22,21 +22,20 @@ import java.lang.annotation.RetentionPolicy;
import org.junit.jupiter.api.Test;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptor;
import static org.springframework.test.context.TestContextAnnotationUtils.findAnnotationDescriptor;
/**
* Unit tests for {@link MetaAnnotationUtils} that verify support for overridden
* meta-annotation attributes.
* Unit tests for {@link TestContextAnnotationUtils} that verify support for
* overridden meta-annotation attributes.
*
* <p>See <a href="https://jira.spring.io/browse/SPR-10181">SPR-10181</a>.
*
* @author Sam Brannen
* @since 4.0
* @see MetaAnnotationUtilsTests
* @see TestContextAnnotationUtilsTests
*/
class OverriddenMetaAnnotationAttributesTests {

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.test.util;
package org.springframework.test.context;
import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
@@ -32,26 +32,24 @@ import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.NestedTestConfiguration;
import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
import org.springframework.test.util.MetaAnnotationUtils.UntypedAnnotationDescriptor;
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
import org.springframework.test.context.TestContextAnnotationUtils.UntypedAnnotationDescriptor;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.context.NestedTestConfiguration.EnclosingConfiguration.OVERRIDE;
import static org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptor;
import static org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes;
import static org.springframework.test.util.MetaAnnotationUtils.searchEnclosingClass;
import static org.springframework.test.context.TestContextAnnotationUtils.findAnnotationDescriptor;
import static org.springframework.test.context.TestContextAnnotationUtils.findAnnotationDescriptorForTypes;
import static org.springframework.test.context.TestContextAnnotationUtils.searchEnclosingClass;
/**
* Unit tests for {@link MetaAnnotationUtils}.
* Unit tests for {@link TestContextAnnotationUtils}.
*
* @author Sam Brannen
* @since 4.0
* @see OverriddenMetaAnnotationAttributesTests
*/
class MetaAnnotationUtilsTests {
class TestContextAnnotationUtilsTests {
@Nested
@DisplayName("searchEnclosingClass() tests")
@@ -398,7 +396,7 @@ class MetaAnnotationUtilsTests {
assertThat(descriptor.getRootDeclaringClass()).isEqualTo(startClass);
assertThat(descriptor.getAnnotationType()).isEqualTo(annotationType);
assertThat(((ContextConfiguration) descriptor.getAnnotation()).value()).isEqualTo(new Class<?>[] {});
assertThat(descriptor.getAnnotationAttributes().getClassArray("classes")).isEqualTo(new Class<?>[] {MetaAnnotationUtilsTests.class});
assertThat(descriptor.getAnnotationAttributes().getClassArray("classes")).isEqualTo(new Class<?>[] {TestContextAnnotationUtilsTests.class});
assertThat(descriptor.getComposedAnnotation()).isNotNull();
assertThat(descriptor.getComposedAnnotationType()).isEqualTo(MetaConfig.class);
}
@@ -619,7 +617,7 @@ class MetaAnnotationUtilsTests {
class MetaConfigWithDefaultAttributesTestCase {
}
@MetaConfig(classes = MetaAnnotationUtilsTests.class)
@MetaConfig(classes = TestContextAnnotationUtilsTests.class)
class MetaConfigWithOverriddenAttributesTestCase {
}