|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.context; |
| 18 | + |
| 19 | +import java.lang.annotation.Documented; |
| 20 | +import java.lang.annotation.ElementType; |
| 21 | +import java.lang.annotation.Inherited; |
| 22 | +import java.lang.annotation.Retention; |
| 23 | +import java.lang.annotation.RetentionPolicy; |
| 24 | +import java.lang.annotation.Target; |
| 25 | + |
| 26 | +/** |
| 27 | + * {@code @NestedTestConfiguration} is a type-level annotation that is used to |
| 28 | + * configure how Spring test configuration annotations are processed within |
| 29 | + * enclosing class hierarchies (i.e., for <em>inner</em> test classes). |
| 30 | + * |
| 31 | + * <p>If {@code @NestedTestConfiguration} is not <em>present</em> or |
| 32 | + * <em>meta-present</em> on a test class, configuration from the test class will |
| 33 | + * not propagate to inner test classes (see {@link EnclosingConfiguration#OVERRIDE}). |
| 34 | + * Consequently, inner test classes will have to declare their own Spring test |
| 35 | + * configuration annotations. If you wish for an inner test class to inherit |
| 36 | + * configuration from its enclosing class, annotate either the inner test class |
| 37 | + * or the enclosing class with |
| 38 | + * {@code @NestedTestConfiguration(EnclosingConfiguration.INHERIT)}. Note that |
| 39 | + * a {@code @NestedTestConfiguration(...)} declaration is inherited within the |
| 40 | + * superclass hierarchy as well as within the enclosing class hierarchy. Thus, |
| 41 | + * there is no need to redeclare the annotation unless you wish to switch the |
| 42 | + * mode. |
| 43 | + * |
| 44 | + * <p>This annotation may be used as a <em>meta-annotation</em> to create custom |
| 45 | + * <em>composed annotations</em>. |
| 46 | + * |
| 47 | + * <p>As of Spring Framework 5.3, the use of this annotation typically only makes |
| 48 | + * sense in conjunction with {@link org.junit.jupiter.api.Nested @Nested} test |
| 49 | + * classes in JUnit Jupiter. |
| 50 | + * |
| 51 | + * @author Sam Brannen |
| 52 | + * @since 5.3 |
| 53 | + * @see EnclosingConfiguration#INHERIT |
| 54 | + * @see EnclosingConfiguration#OVERRIDE |
| 55 | + * @see ContextConfiguration @ContextConfiguration |
| 56 | + * @see ContextHierarchy @ContextHierarchy |
| 57 | + * @see ActiveProfiles @ActiveProfiles |
| 58 | + * @see TestPropertySource @TestPropertySource |
| 59 | + */ |
| 60 | +@Target({ElementType.TYPE, ElementType.METHOD}) |
| 61 | +@Retention(RetentionPolicy.RUNTIME) |
| 62 | +@Documented |
| 63 | +@Inherited |
| 64 | +public @interface NestedTestConfiguration { |
| 65 | + |
| 66 | + /** |
| 67 | + * Configures the {@link EnclosingConfiguration} mode. |
| 68 | + */ |
| 69 | + EnclosingConfiguration value(); |
| 70 | + |
| 71 | + |
| 72 | + /** |
| 73 | + * Enumeration of <em>modes</em> that dictate how test configuration from |
| 74 | + * enclosing classes is processed for inner test classes. |
| 75 | + */ |
| 76 | + enum EnclosingConfiguration { |
| 77 | + |
| 78 | + /** |
| 79 | + * Indicates that test configuration for an inner test class should be |
| 80 | + * <em>inherited</em> from its {@linkplain Class#getEnclosingClass() |
| 81 | + * enclosing class}, as if the enclosing class were a superclass. |
| 82 | + */ |
| 83 | + INHERIT, |
| 84 | + |
| 85 | + /** |
| 86 | + * Indicates that test configuration for an inner test class should |
| 87 | + * <em>override</em> configuration from its |
| 88 | + * {@linkplain Class#getEnclosingClass() enclosing class}. |
| 89 | + */ |
| 90 | + OVERRIDE |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | +} |
0 commit comments