Skip to content

Commit

Permalink
Merge branch '3.0.x' into 3.1.x
Browse files Browse the repository at this point in the history
Closes gh-37924
  • Loading branch information
wilkinsona committed Oct 18, 2023
2 parents 60a0b7e + ece239f commit 8f18b11
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Expand Up @@ -23,6 +23,7 @@
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
Expand All @@ -36,6 +37,7 @@
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;
Expand Down Expand Up @@ -237,7 +239,18 @@ static class ContextCustomizerKey {
Set<Class<?>> seen = new HashSet<>();
collectClassAnnotations(testClass, annotations, seen);
Set<Object> determinedImports = determineImports(annotations, testClass);
this.key = Collections.unmodifiableSet((determinedImports != null) ? determinedImports : annotations);
if (determinedImports == null) {
this.key = Collections.unmodifiableSet(annotations);
}
else {
Set<Object> key = new HashSet<>();
key.addAll(determinedImports);
Set<Annotation> componentScanning = annotations.stream()
.filter(ComponentScan.class::isInstance)
.collect(Collectors.toSet());
key.addAll(componentScanning);
this.key = Collections.unmodifiableSet(key);
}
}

private void collectClassAnnotations(Class<?> classType, Set<Annotation> annotations, Set<Class<?>> seen) {
Expand Down
Expand Up @@ -23,6 +23,7 @@

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;
Expand Down Expand Up @@ -74,6 +75,20 @@ void contextCustomizerEqualsAndHashCode() {
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()
Expand Down Expand Up @@ -105,6 +120,18 @@ static class TestWithImport {

}

@Import(ImportedBean.class)
@ComponentScan("some.package")
static class TestWithImportAndComponentScanOfSomePackage {

}

@Import(ImportedBean.class)
@ComponentScan("another.package")
static class TestWithImportAndComponentScanOfAnotherPackage {

}

@MetaImport
static class TestWithMetaImport {

Expand Down

0 comments on commit 8f18b11

Please sign in to comment.