From 4f3180540cac20bc439c274573735cac910a8fd0 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Thu, 16 Jun 2022 15:35:28 +0100 Subject: [PATCH] Use Framework's MockSpringFactoriesLoader Closes gh-31406 --- .../mock/MockSpringFactoriesLoader.java | 137 ------------------ .../boot/testsupport/mock/package-info.java | 20 --- spring-boot-project/spring-boot/build.gradle | 1 + .../config/ConfigDataLoadersTests.java | 2 +- .../ConfigDataLocationResolversTests.java | 2 +- ...ataLocationRuntimeHintsRegistrarTests.java | 12 +- .../diagnostics/FailureAnalyzersTests.java | 2 +- 7 files changed, 13 insertions(+), 163 deletions(-) delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/mock/MockSpringFactoriesLoader.java delete mode 100644 spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/mock/package-info.java diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/mock/MockSpringFactoriesLoader.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/mock/MockSpringFactoriesLoader.java deleted file mode 100644 index 372dd2231b..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/mock/MockSpringFactoriesLoader.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright 2012-2022 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.boot.testsupport.mock; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.atomic.AtomicInteger; - -import org.springframework.core.io.support.SpringFactoriesLoader; - -/** - * Simple mock {@link SpringFactoriesLoader} implementation that can be used for testing - * purposes. - * - * @author Phillip Webb - * @since 3.0.0 - */ -public class MockSpringFactoriesLoader extends SpringFactoriesLoader { - - private final AtomicInteger sequence = new AtomicInteger(); - - private final Map> factories; - - private final Map implementations = new HashMap<>(); - - /** - * Create a new {@link MockSpringFactoriesLoader} instance with the default - * classloader. - */ - public MockSpringFactoriesLoader() { - this(null); - } - - /** - * Create a new {@link MockSpringFactoriesLoader} instance with the given classloader. - * @param classLoader the classloader to use - */ - public MockSpringFactoriesLoader(ClassLoader classLoader) { - this(classLoader, new LinkedHashMap<>()); - } - - protected MockSpringFactoriesLoader(ClassLoader classLoader, Map> factories) { - super(classLoader, factories); - this.factories = factories; - } - - @Override - @SuppressWarnings("unchecked") - protected T instantiateFactory(String implementationName, Class type, ArgumentResolver argumentResolver, - FailureHandler failureHandler) { - if (implementationName.startsWith("!")) { - Object implementation = this.implementations.get(implementationName); - if (implementation != null) { - return (T) implementation; - } - } - return super.instantiateFactory(implementationName, type, argumentResolver, failureHandler); - } - - /** - * Add factory implementations to this instance. - * @param factoryType the factory type class - * @param factoryImplementations the implementation classes - * @param the factory type - * @return {@code this}, to facilitate method chaining - */ - @SafeVarargs - public final MockSpringFactoriesLoader add(Class factoryType, Class... factoryImplementations) { - for (Class factoryImplementation : factoryImplementations) { - add(factoryType.getName(), factoryImplementation.getName()); - } - return this; - } - - /** - * Add factory implementations to this instance. - * @param factoryType the factory type class name - * @param factoryImplementations the implementation class names - * @return {@code this}, to facilitate method chaining - */ - public MockSpringFactoriesLoader add(String factoryType, String... factoryImplementations) { - List implementations = this.factories.computeIfAbsent(factoryType, (key) -> new ArrayList<>()); - for (String factoryImplementation : factoryImplementations) { - implementations.add(factoryImplementation); - } - return this; - } - - /** - * Add factory instances to this instance. - * @param factoryType the factory type class - * @param factoryInstances the implementation instances to add - * @param the factory type - * @return {@code this}, to facilitate method chaining - */ - @SuppressWarnings("unchecked") - public MockSpringFactoriesLoader addInstance(Class factoryType, T... factoryInstances) { - return addInstance(factoryType.getName(), factoryInstances); - } - - /** - * Add factory instances to this instance. - * @param factoryType the factory type class name - * @param factoryInstance the implementation instances to add - * @param the factory instance type - * @return {@code this}, to facilitate method chaining - */ - @SuppressWarnings("unchecked") - public MockSpringFactoriesLoader addInstance(String factoryType, T... factoryInstance) { - List implementations = this.factories.computeIfAbsent(factoryType, (key) -> new ArrayList<>()); - for (T factoryImplementation : factoryInstance) { - String reference = "!" + factoryType + ":" + factoryImplementation.getClass().getName() - + this.sequence.getAndIncrement(); - implementations.add(reference); - this.implementations.put(reference, factoryImplementation); - } - return this; - } - -} diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/mock/package-info.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/mock/package-info.java deleted file mode 100644 index 4a7f939401..0000000000 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/mock/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright 2012-2022 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. - */ - -/** - * Mock implementations of commonly used classes. - */ -package org.springframework.boot.testsupport.mock; diff --git a/spring-boot-project/spring-boot/build.gradle b/spring-boot-project/spring-boot/build.gradle index 680aa43df3..ff0de07354 100644 --- a/spring-boot-project/spring-boot/build.gradle +++ b/spring-boot-project/spring-boot/build.gradle @@ -130,6 +130,7 @@ dependencies { testImplementation("org.mockito:mockito-core") testImplementation("org.mockito:mockito-junit-jupiter") testImplementation("org.springframework:spring-context-support") + testImplementation("org.springframework:spring-core-test") testImplementation("org.springframework.data:spring-data-redis") testImplementation("org.springframework.data:spring-data-r2dbc") testImplementation("org.xerial:sqlite-jdbc") diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLoadersTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLoadersTests.java index 43b78ff21b..be05e316ec 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLoadersTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLoadersTests.java @@ -32,8 +32,8 @@ import org.springframework.boot.BootstrapRegistry.InstanceSupplier; import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.DefaultBootstrapContext; import org.springframework.boot.logging.DeferredLogFactory; -import org.springframework.boot.testsupport.mock.MockSpringFactoriesLoader; import org.springframework.core.env.PropertySource; +import org.springframework.core.mock.MockSpringFactoriesLoader; import org.springframework.mock.env.MockPropertySource; import static org.assertj.core.api.Assertions.assertThat; diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLocationResolversTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLocationResolversTests.java index 173d74cea8..bd834bd5ce 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLocationResolversTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLocationResolversTests.java @@ -34,10 +34,10 @@ import org.springframework.boot.ConfigurableBootstrapContext; import org.springframework.boot.DefaultBootstrapContext; import org.springframework.boot.context.properties.bind.Binder; import org.springframework.boot.logging.DeferredLogFactory; -import org.springframework.boot.testsupport.mock.MockSpringFactoriesLoader; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.core.io.DefaultResourceLoader; +import org.springframework.core.mock.MockSpringFactoriesLoader; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLocationRuntimeHintsRegistrarTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLocationRuntimeHintsRegistrarTests.java index 3192cf91ab..2e1303fc58 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLocationRuntimeHintsRegistrarTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/config/ConfigDataLocationRuntimeHintsRegistrarTests.java @@ -28,8 +28,8 @@ import org.springframework.aot.hint.RuntimeHints; import org.springframework.boot.env.PropertiesPropertySourceLoader; import org.springframework.boot.env.PropertySourceLoader; import org.springframework.boot.env.YamlPropertySourceLoader; -import org.springframework.boot.testsupport.mock.MockSpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader; +import org.springframework.core.mock.MockSpringFactoriesLoader; import static org.assertj.core.api.Assertions.assertThat; @@ -121,8 +121,14 @@ class ConfigDataLocationRuntimeHintsRegistrarTests { } TestConfigDataLocationRuntimeHintsRegistrar() { - this(new MockSpringFactoriesLoader().add(PropertySourceLoader.class, PropertiesPropertySourceLoader.class, - YamlPropertySourceLoader.class)); + this(springFactoriesLoader()); + } + + private static MockSpringFactoriesLoader springFactoriesLoader() { + MockSpringFactoriesLoader springFactoriesLoader = new MockSpringFactoriesLoader(); + springFactoriesLoader.add(PropertySourceLoader.class, PropertiesPropertySourceLoader.class, + YamlPropertySourceLoader.class); + return springFactoriesLoader; } @Override diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java index 2870a73190..299df28ef1 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/FailureAnalyzersTests.java @@ -22,12 +22,12 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; -import org.springframework.boot.testsupport.mock.MockSpringFactoriesLoader; import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.OutputCaptureExtension; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.core.env.Environment; +import org.springframework.core.mock.MockSpringFactoriesLoader; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.same;