Skip to content

Commit 767444c

Browse files
committed
Merge branch '4.0.x'
Closes spring-projectsgh-50013
2 parents 24a8053 + 9fdb2db commit 767444c

File tree

2 files changed

+118
-18
lines changed

2 files changed

+118
-18
lines changed

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

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@
1818

1919
import java.lang.annotation.Annotation;
2020
import java.lang.reflect.Constructor;
21+
import java.util.ArrayList;
2122
import java.util.Collections;
2223
import java.util.HashSet;
2324
import java.util.LinkedHashSet;
25+
import java.util.List;
2426
import java.util.Set;
2527
import java.util.stream.Collectors;
2628

@@ -53,6 +55,7 @@
5355
import org.springframework.core.type.AnnotationMetadata;
5456
import org.springframework.test.context.ContextCustomizer;
5557
import org.springframework.test.context.MergedContextConfiguration;
58+
import org.springframework.test.context.TestContextAnnotationUtils;
5659
import org.springframework.util.ReflectionUtils;
5760

5861
/**
@@ -66,17 +69,30 @@
6669
*/
6770
class ImportsContextCustomizer implements ContextCustomizer {
6871

69-
private static final String TEST_CLASS_NAME_ATTRIBUTE = "testClassName";
72+
private static final String TEST_CLASS_NAMES_ATTRIBUTE = "testClassNames";
7073

71-
private final String testClassName;
74+
private final String[] testClassNames;
7275

7376
private final ContextCustomizerKey key;
7477

7578
ImportsContextCustomizer(Class<?> testClass) {
76-
this.testClassName = testClass.getName();
79+
this.testClassNames = collectClassNames(testClass);
7780
this.key = new ContextCustomizerKey(testClass);
7881
}
7982

83+
private static String[] collectClassNames(Class<?> source) {
84+
List<String> classNames = new ArrayList<>();
85+
collectClassNames(source, classNames);
86+
return classNames.toArray(new String[0]);
87+
}
88+
89+
private static void collectClassNames(Class<?> source, List<String> classNames) {
90+
classNames.add(source.getName());
91+
if (TestContextAnnotationUtils.searchEnclosingClass(source)) {
92+
collectClassNames(source.getEnclosingClass(), classNames);
93+
}
94+
}
95+
8096
@Override
8197
public void customizeContext(ConfigurableApplicationContext context,
8298
MergedContextConfiguration mergedContextConfiguration) {
@@ -90,13 +106,13 @@ private void registerCleanupPostProcessor(BeanDefinitionRegistry registry, Annot
90106
BeanDefinition definition = registerBean(registry, reader, ImportsCleanupPostProcessor.BEAN_NAME,
91107
ImportsCleanupPostProcessor.class);
92108
definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
93-
definition.getConstructorArgumentValues().addIndexedArgumentValue(0, this.testClassName);
109+
definition.getConstructorArgumentValues().addIndexedArgumentValue(0, this.testClassNames);
94110
}
95111

96112
private void registerImportsConfiguration(BeanDefinitionRegistry registry, AnnotatedBeanDefinitionReader reader) {
97113
BeanDefinition definition = registerBean(registry, reader, ImportsConfiguration.BEAN_NAME,
98114
ImportsConfiguration.class);
99-
definition.setAttribute(TEST_CLASS_NAME_ATTRIBUTE, this.testClassName);
115+
definition.setAttribute(TEST_CLASS_NAMES_ATTRIBUTE, this.testClassNames);
100116
}
101117

102118
private BeanDefinitionRegistry getBeanDefinitionRegistry(ApplicationContext context) {
@@ -169,8 +185,8 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
169185
@Override
170186
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
171187
BeanDefinition definition = this.beanFactory.getBeanDefinition(ImportsConfiguration.BEAN_NAME);
172-
Object testClassName = definition.getAttribute(TEST_CLASS_NAME_ATTRIBUTE);
173-
return (testClassName != null) ? new String[] { (String) testClassName } : NO_IMPORTS;
188+
Object testClassNames = definition.getAttribute(TEST_CLASS_NAMES_ATTRIBUTE);
189+
return (testClassNames != null) ? (String[]) testClassNames : NO_IMPORTS;
174190
}
175191

176192
}
@@ -184,10 +200,10 @@ static class ImportsCleanupPostProcessor implements BeanDefinitionRegistryPostPr
184200

185201
static final String BEAN_NAME = ImportsCleanupPostProcessor.class.getName();
186202

187-
private final String testClassName;
203+
private final String[] testClassNames;
188204

189-
ImportsCleanupPostProcessor(String testClassName) {
190-
this.testClassName = testClassName;
205+
ImportsCleanupPostProcessor(String[] testClassNames) {
206+
this.testClassNames = testClassNames;
191207
}
192208

193209
@Override
@@ -196,15 +212,15 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
196212

197213
@Override
198214
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
215+
for (String testClassName : this.testClassNames) {
216+
removeBean(testClassName, registry);
217+
}
218+
removeBean(ImportsConfiguration.BEAN_NAME, registry);
219+
}
220+
221+
private void removeBean(String beanName, BeanDefinitionRegistry registry) {
199222
try {
200-
String[] names = registry.getBeanDefinitionNames();
201-
for (String name : names) {
202-
BeanDefinition definition = registry.getBeanDefinition(name);
203-
if (this.testClassName.equals(definition.getBeanClassName())) {
204-
registry.removeBeanDefinition(name);
205-
}
206-
}
207-
registry.removeBeanDefinition(ImportsConfiguration.BEAN_NAME);
223+
registry.removeBeanDefinition(beanName);
208224
}
209225
catch (NoSuchBeanDefinitionException ex) {
210226
// Ignore
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2012-present 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.boot.test.context;
18+
19+
import org.junit.jupiter.api.Nested;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.context.SpringBootTestImportTests.ImportedByContainingTests;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.context.annotation.Import;
26+
import org.springframework.core.env.Environment;
27+
import org.springframework.test.context.TestPropertySource;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link SpringBootTest @SpringBootTest} with {@link Import @Import}.
33+
*
34+
* @author Andy Wilkinson
35+
*/
36+
@TestPropertySource(properties = "a=alpha")
37+
@Import(ImportedByContainingTests.class)
38+
class SpringBootTestImportTests {
39+
40+
@Nested
41+
@SpringBootTest(classes = Config.class)
42+
@Import(ImportedByNestedTests.class)
43+
class NestedTests {
44+
45+
@Autowired(required = false)
46+
private ImportedByContainingTests importedByContainingTests;
47+
48+
@Autowired(required = false)
49+
private ImportedByNestedTests importedByNestedTests;
50+
51+
@Autowired
52+
private Environment environment;
53+
54+
@Test
55+
void sameClassImportIsHonored() {
56+
assertThat(this.importedByNestedTests).isNotNull();
57+
}
58+
59+
@Test
60+
void containingClassImportIsHonored() {
61+
assertThat(this.importedByContainingTests).isNotNull();
62+
}
63+
64+
@Test
65+
void containingClassTestPropertySourceIsHonored() {
66+
assertThat(this.environment.getProperty("a")).isEqualTo("alpha");
67+
}
68+
69+
}
70+
71+
@Configuration(proxyBeanMethods = false)
72+
static class Config {
73+
74+
}
75+
76+
static class ImportedByNestedTests {
77+
78+
}
79+
80+
static class ImportedByContainingTests {
81+
82+
}
83+
84+
}

0 commit comments

Comments
 (0)