Skip to content

Commit 9aee155

Browse files
committed
GH-1262 - Avoid referring to the source class in ModuleTypeExcludeFilter.equals(…) and ….hashCode().
MTEF's equals(…) and hashCode() methods is depended on by the application context caching facilities of Spring Framework's test context framework. Previously, it referred to the class under test in those methods, thus effectively disabling the cache, even if the application module test bootstrap setup was identical but used on different test classes. We now avoid this by solely referring to the equals(…) and hashCode() methods of ModuleTestExecution, which in turn only considers the base package and the @ApplicationModuleTest annotation.
1 parent a666948 commit 9aee155

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTestExecution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ private ModuleTestExecution(ApplicationModuleTest annotation, ApplicationModules
105105

106106
public static Supplier<ModuleTestExecution> of(Class<?> type) {
107107

108-
return () -> {
108+
return SingletonSupplier.of(() -> {
109109

110110
var annotation = AnnotatedElementUtils.findMergedAnnotation(type, ApplicationModuleTest.class);
111111
var packageName = type.getPackage().getName();
@@ -120,7 +120,7 @@ public static Supplier<ModuleTestExecution> of(Class<?> type) {
120120

121121
return EXECUTIONS.computeIfAbsent(new Key(module.getBasePackage().getName(), annotation),
122122
it -> new ModuleTestExecution(annotation, modules, module));
123-
};
123+
});
124124
}
125125

126126
/**

spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTypeExcludeFilter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@
2525
import org.springframework.util.Assert;
2626

2727
/**
28+
* A {@link TypeExcludeFilter} that only selects types included in a {@link ModuleTestExecution}, i.e. from the modules
29+
* included in a particular test run.
30+
*
2831
* @author Oliver Drotbohm
2932
*/
3033
class ModuleTypeExcludeFilter extends TypeExcludeFilter {
3134

3235
private final Supplier<ModuleTestExecution> execution;
33-
private final Class<?> source;
3436

3537
public ModuleTypeExcludeFilter(Class<?> testClass) {
3638

3739
Assert.notNull(testClass, "Test class must not be null!");
3840

3941
this.execution = ModuleTestExecution.of(testClass);
40-
this.source = testClass;
4142
}
4243

4344
/*
@@ -64,7 +65,7 @@ public boolean equals(Object obj) {
6465
return false;
6566
}
6667

67-
return Objects.equals(source, that.source);
68+
return Objects.equals(execution.get(), that.execution.get());
6869
}
6970

7071
/*
@@ -73,6 +74,6 @@ public boolean equals(Object obj) {
7374
*/
7475
@Override
7576
public int hashCode() {
76-
return Objects.hash(source);
77+
return Objects.hash(execution.get());
7778
}
7879
}

spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleTypeExcludeFilterUnitTests.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,45 @@
2626
*/
2727
class ModuleTypeExcludeFilterUnitTests {
2828

29-
@Test
29+
@Test // GH-725, GH-1216
3030
void instancesForSameTargetTypeAreEqual() {
3131

32-
var left = new ModuleTypeExcludeFilter(Object.class);
33-
var right = new ModuleTypeExcludeFilter(Object.class);
32+
var left = new ModuleTypeExcludeFilter(First.class);
33+
var right = new ModuleTypeExcludeFilter(First.class);
3434

3535
assertThat(left).isEqualTo(right);
3636
assertThat(right).isEqualTo(left);
3737
assertThat(left).hasSameHashCodeAs(right);
3838
}
39+
40+
@Test // GH-1216
41+
void instancesForDifferentTargetButSameTestSetupTypeAreEqual() {
42+
43+
var left = new ModuleTypeExcludeFilter(First.class);
44+
var right = new ModuleTypeExcludeFilter(Second.class);
45+
46+
assertThat(left).isEqualTo(right);
47+
assertThat(right).isEqualTo(left);
48+
assertThat(left).hasSameHashCodeAs(right);
49+
}
50+
51+
@Test // GH-1216
52+
void instancesForDifferentTargetAndDifferentTestSetupTypeAreNotEqual() {
53+
54+
var left = new ModuleTypeExcludeFilter(Second.class);
55+
var right = new ModuleTypeExcludeFilter(Third.class);
56+
57+
assertThat(left).isNotEqualTo(right);
58+
assertThat(right).isNotEqualTo(left);
59+
assertThat(left).doesNotHaveSameHashCodeAs(right);
60+
}
61+
62+
@ApplicationModuleTest
63+
static class First {}
64+
65+
@ApplicationModuleTest
66+
static class Second {}
67+
68+
@ApplicationModuleTest(extraIncludes = "foo")
69+
static class Third {}
3970
}

0 commit comments

Comments
 (0)