Merge branch '3.1.x'

Closes gh-37925
This commit is contained in:
Andy Wilkinson 2023-10-18 11:19:47 +01:00
commit 029c28d923
2 changed files with 43 additions and 1 deletions

View File

@ -16,8 +16,10 @@
package org.springframework.boot.test.context;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;
@ -34,6 +36,7 @@ import org.springframework.boot.context.annotation.DeterminableImports;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
@ -232,7 +235,19 @@ class ImportsContextCustomizer implements ContextCustomizer {
.withAnnotationFilter(this::isFilteredAnnotation)
.from(testClass);
Set<Object> determinedImports = determineImports(annotations, testClass);
this.key = (determinedImports != null) ? determinedImports : synthesize(annotations);
if (determinedImports == null) {
this.key = Collections.unmodifiableSet(synthesize(annotations));
}
else {
Set<Object> key = new HashSet<>();
key.addAll(determinedImports);
Set<Annotation> componentScanning = annotations.stream()
.filter((annotation) -> annotation.getType().equals(ComponentScan.class))
.map(MergedAnnotation::synthesize)
.collect(Collectors.toSet());
key.addAll(componentScanning);
this.key = Collections.unmodifiableSet(key);
}
}
private boolean isFilteredAnnotation(String typeName) {

View File

@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
@ -74,6 +75,20 @@ class ImportsContextCustomizerFactoryTests {
assertThat(customizer3).isEqualTo(customizer4);
}
@Test
void contextCustomizerEqualsAndHashCodeConsidersComponentScan() {
ContextCustomizer customizer1 = this.factory
.createContextCustomizer(TestWithImportAndComponentScanOfSomePackage.class, null);
ContextCustomizer customizer2 = this.factory
.createContextCustomizer(TestWithImportAndComponentScanOfSomePackage.class, null);
ContextCustomizer customizer3 = this.factory
.createContextCustomizer(TestWithImportAndComponentScanOfAnotherPackage.class, null);
assertThat(customizer1.hashCode()).isEqualTo(customizer2.hashCode());
assertThat(customizer1).isEqualTo(customizer2);
assertThat(customizer3.hashCode()).isNotEqualTo(customizer2.hashCode()).isNotEqualTo(customizer1.hashCode());
assertThat(customizer3).isNotEqualTo(customizer2).isNotEqualTo(customizer1);
}
@Test
void getContextCustomizerWhenClassHasBeanMethodsShouldThrowException() {
assertThatIllegalStateException()
@ -105,6 +120,18 @@ class ImportsContextCustomizerFactoryTests {
}
@Import(ImportedBean.class)
@ComponentScan("some.package")
static class TestWithImportAndComponentScanOfSomePackage {
}
@Import(ImportedBean.class)
@ComponentScan("another.package")
static class TestWithImportAndComponentScanOfAnotherPackage {
}
@MetaImport
static class TestWithMetaImport {