Skip to content

Commit 97ef8f3

Browse files
committed
Merge branch '3.0.x' into 3.1.x
Closes gh-36209
2 parents 13d5cda + ab7e4da commit 97ef8f3

File tree

2 files changed

+103
-2
lines changed

2 files changed

+103
-2
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/context/ImportsContextCustomizer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,6 +43,9 @@
4343
import org.springframework.context.support.AbstractApplicationContext;
4444
import org.springframework.core.Ordered;
4545
import org.springframework.core.annotation.AnnotationUtils;
46+
import org.springframework.core.annotation.MergedAnnotation;
47+
import org.springframework.core.annotation.MergedAnnotations;
48+
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
4649
import org.springframework.core.annotation.Order;
4750
import org.springframework.core.style.ToStringCreator;
4851
import org.springframework.core.type.AnnotationMetadata;
@@ -251,7 +254,9 @@ private void collectClassAnnotations(Class<?> classType, Set<Annotation> annotat
251254

252255
private void collectElementAnnotations(AnnotatedElement element, Set<Annotation> annotations,
253256
Set<Class<?>> seen) {
254-
for (Annotation annotation : element.getDeclaredAnnotations()) {
257+
for (MergedAnnotation<Annotation> mergedAnnotation : MergedAnnotations.from(element,
258+
SearchStrategy.DIRECT)) {
259+
Annotation annotation = mergedAnnotation.synthesize();
255260
if (!isIgnoredAnnotation(annotation)) {
256261
annotations.add(annotation);
257262
collectClassAnnotations(annotation.annotationType(), annotations, seen);

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/ImportsContextCustomizerTests.java

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.context.annotation.Configuration;
3434
import org.springframework.context.annotation.Import;
3535
import org.springframework.context.annotation.ImportSelector;
36+
import org.springframework.core.annotation.AliasFor;
3637
import org.springframework.core.type.AnnotationMetadata;
3738

3839
import static org.assertj.core.api.Assertions.assertThat;
@@ -41,6 +42,7 @@
4142
* Tests for {@link ImportsContextCustomizer}.
4243
*
4344
* @author Andy Wilkinson
45+
* @author Laurent Martelli
4446
*/
4547
class ImportsContextCustomizerTests {
4648

@@ -80,6 +82,30 @@ void customizersForTestClassesWithDifferentJUnitAnnotationsAreEqual() {
8082
.isEqualTo(new ImportsContextCustomizer(SecondJUnitAnnotatedTestClass.class));
8183
}
8284

85+
@Test
86+
void customizersForClassesWithDifferentImportsAreNotEqual() {
87+
assertThat(new ImportsContextCustomizer(FirstAnnotatedTestClass.class))
88+
.isNotEqualTo(new ImportsContextCustomizer(SecondAnnotatedTestClass.class));
89+
}
90+
91+
@Test
92+
void customizersForClassesWithDifferentMetaImportsAreNotEqual() {
93+
assertThat(new ImportsContextCustomizer(FirstMetaAnnotatedTestClass.class))
94+
.isNotEqualTo(new ImportsContextCustomizer(SecondMetaAnnotatedTestClass.class));
95+
}
96+
97+
@Test
98+
void customizersForClassesWithDifferentAliasedImportsAreNotEqual() {
99+
assertThat(new ImportsContextCustomizer(FirstAliasAnnotatedTestClass.class))
100+
.isNotEqualTo(new ImportsContextCustomizer(SecondAliasAnnotatedTestClass.class));
101+
}
102+
103+
@Test
104+
void importsCanBeScatteredOnMultipleAnnotations() {
105+
assertThat(new ImportsContextCustomizer(SingleImportAnnotationTestClass.class))
106+
.isEqualTo(new ImportsContextCustomizer(MultipleImportAnnotationTestClass.class));
107+
}
108+
83109
@Import(TestImportSelector.class)
84110
@Indicator1
85111
static class FirstImportSelectorAnnotatedClass {
@@ -152,6 +178,17 @@ static class SecondJUnitAnnotatedTestClass {
152178

153179
}
154180

181+
@Import({ FirstImportedClass.class, SecondImportedClass.class })
182+
static class SingleImportAnnotationTestClass {
183+
184+
}
185+
186+
@FirstMetaImport
187+
@Import(SecondImportedClass.class)
188+
static class MultipleImportAnnotationTestClass {
189+
190+
}
191+
155192
@Retention(RetentionPolicy.RUNTIME)
156193
@interface Indicator1 {
157194

@@ -162,6 +199,65 @@ static class SecondJUnitAnnotatedTestClass {
162199

163200
}
164201

202+
@Retention(RetentionPolicy.RUNTIME)
203+
@Import(AliasFor.class)
204+
public @interface AliasedImport {
205+
206+
@AliasFor(annotation = Import.class)
207+
Class<?>[] value();
208+
209+
}
210+
211+
@Retention(RetentionPolicy.RUNTIME)
212+
@Import(FirstImportedClass.class)
213+
public @interface FirstMetaImport {
214+
215+
}
216+
217+
@Retention(RetentionPolicy.RUNTIME)
218+
@Import(SecondImportedClass.class)
219+
public @interface SecondMetaImport {
220+
221+
}
222+
223+
static class FirstImportedClass {
224+
225+
}
226+
227+
static class SecondImportedClass {
228+
229+
}
230+
231+
@AliasedImport(FirstImportedClass.class)
232+
static class FirstAliasAnnotatedTestClass {
233+
234+
}
235+
236+
@AliasedImport(SecondImportedClass.class)
237+
static class SecondAliasAnnotatedTestClass {
238+
239+
}
240+
241+
@FirstMetaImport
242+
static class FirstMetaAnnotatedTestClass {
243+
244+
}
245+
246+
@SecondMetaImport
247+
static class SecondMetaAnnotatedTestClass {
248+
249+
}
250+
251+
@Import(FirstImportedClass.class)
252+
static class FirstAnnotatedTestClass {
253+
254+
}
255+
256+
@Import(SecondImportedClass.class)
257+
static class SecondAnnotatedTestClass {
258+
259+
}
260+
165261
static class TestImportSelector implements ImportSelector {
166262

167263
@Override

0 commit comments

Comments
 (0)