From 1dbce34fb9f2671fc79628f7410a64fb22c033aa Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Sat, 10 Aug 2024 23:56:01 +0200 Subject: [PATCH] GH-757 - Fix test context cache key contributions. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both ModuleContextCustomizer and ModuleTypeExcludeFilter contribute to the calculation of the context configuration which the Spring Test Context Framework uses to decide whether it's necessary to create new ApplicationContext instances. Both of them previously used a Supplier to calculate equals(…) and hashCode() which -- by definition -- does not result in the same result even when created with identical input. We now rather use the source class instance eventually backing the ModuleTestExecution, as that is the internal cache key in turn. --- .../test/ModuleContextCustomizerFactory.java | 58 ++++++++++--------- .../test/ModuleTypeExcludeFilter.java | 10 +++- .../ModuleContextCustomizerUnitTests.java | 40 +++++++++++++ .../ModuleTypeExcludeFilterUnitTests.java | 39 +++++++++++++ 4 files changed, 117 insertions(+), 30 deletions(-) create mode 100644 spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleContextCustomizerUnitTests.java create mode 100644 spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleTypeExcludeFilterUnitTests.java diff --git a/spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleContextCustomizerFactory.java b/spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleContextCustomizerFactory.java index 0574f809..93c72a81 100644 --- a/spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleContextCustomizerFactory.java +++ b/spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleContextCustomizerFactory.java @@ -62,9 +62,12 @@ class ModuleContextCustomizerFactory implements ContextCustomizerFactory { private static final Logger LOGGER = LoggerFactory.getLogger(ModuleContextCustomizer.class); private final Supplier execution; + private final Class source; + + ModuleContextCustomizer(Class testClass) { - private ModuleContextCustomizer(Class testClass) { this.execution = ModuleTestExecution.of(testClass); + this.source = testClass; } /* @@ -143,6 +146,32 @@ class ModuleContextCustomizerFactory implements ContextCustomizerFactory { LOGGER.info(""); } + /* + * (non-Javadoc) + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + + if (!(obj instanceof ModuleContextCustomizer that)) { + return false; + } + + return Objects.equals(this.source, that.source); + } + + /* + * (non-Javadoc) + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + return Objects.hashCode(source); + } + private static void logHeadline(String headline) { logHeadline(headline, () -> {}); } @@ -153,33 +182,6 @@ class ModuleContextCustomizerFactory implements ContextCustomizerFactory { LOGGER.info(headline); additional.run(); } - - /* - * (non-Javadoc) - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - - if (this == obj) { - return true; - } - - if (!(obj instanceof ModuleContextCustomizer that)) { - return false; - } - - return Objects.equals(execution, that.execution); - } - - /* - * (non-Javadoc) - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - return Objects.hash(execution); - } } /** diff --git a/spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTypeExcludeFilter.java b/spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTypeExcludeFilter.java index 38835722..330c0e9f 100644 --- a/spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTypeExcludeFilter.java +++ b/spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTypeExcludeFilter.java @@ -22,6 +22,7 @@ import java.util.function.Supplier; import org.springframework.boot.context.TypeExcludeFilter; import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.util.Assert; /** * @author Oliver Drotbohm @@ -29,9 +30,14 @@ import org.springframework.core.type.classreading.MetadataReaderFactory; class ModuleTypeExcludeFilter extends TypeExcludeFilter { private final Supplier execution; + private final Class source; public ModuleTypeExcludeFilter(Class testClass) { + + Assert.notNull(testClass, "Test class must not be null!"); + this.execution = ModuleTestExecution.of(testClass); + this.source = testClass; } /* @@ -58,7 +64,7 @@ class ModuleTypeExcludeFilter extends TypeExcludeFilter { return false; } - return Objects.equals(execution, that.execution); + return Objects.equals(source, that.source); } /* @@ -67,6 +73,6 @@ class ModuleTypeExcludeFilter extends TypeExcludeFilter { */ @Override public int hashCode() { - return Objects.hash(execution); + return Objects.hash(source); } } diff --git a/spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleContextCustomizerUnitTests.java b/spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleContextCustomizerUnitTests.java new file mode 100644 index 00000000..20fbf570 --- /dev/null +++ b/spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleContextCustomizerUnitTests.java @@ -0,0 +1,40 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.modulith.test; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.springframework.modulith.test.ModuleContextCustomizerFactory.ModuleContextCustomizer; + +/** + * Unit tests for {@link ModuleTypeExcludeFilter}. + * + * @author Oliver Drotbohm + */ +class ModuleContextCustomizerUnitTests { + + @Test + void instancesForSameTargetTypeAreEqual() { + + var left = new ModuleContextCustomizer(Object.class); + var right = new ModuleContextCustomizer(Object.class); + + assertThat(left).isEqualTo(right); + assertThat(right).isEqualTo(left); + assertThat(left).hasSameHashCodeAs(right); + } +} diff --git a/spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleTypeExcludeFilterUnitTests.java b/spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleTypeExcludeFilterUnitTests.java new file mode 100644 index 00000000..0186b55c --- /dev/null +++ b/spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleTypeExcludeFilterUnitTests.java @@ -0,0 +1,39 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.modulith.test; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; + +/** + * Unit tests for {@link ModuleTypeExcludeFilter}. + * + * @author Oliver Drotbohm + */ +class ModuleTypeExcludeFilterUnitTests { + + @Test + void instancesForSameTargetTypeAreEqual() { + + var left = new ModuleTypeExcludeFilter(Object.class); + var right = new ModuleTypeExcludeFilter(Object.class); + + assertThat(left).isEqualTo(right); + assertThat(right).isEqualTo(left); + assertThat(left).hasSameHashCodeAs(right); + } +}