diff --git a/spring-core-test/src/main/java/org/springframework/core/mock/MockSpringFactoriesLoader.java b/spring-core-test/src/main/java/org/springframework/core/mock/MockSpringFactoriesLoader.java new file mode 100644 index 00000000000..c4b1a6f42fa --- /dev/null +++ b/spring-core-test/src/main/java/org/springframework/core/mock/MockSpringFactoriesLoader.java @@ -0,0 +1,131 @@ +/* + * Copyright 2002-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.core.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; +import org.springframework.lang.Nullable; + +/** + * Simple mock {@link SpringFactoriesLoader} implementation that can be used for testing + * purposes. + * + * @author Phillip Webb + * @since 6.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(@Nullable 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 + */ + @SafeVarargs + @SuppressWarnings("unchecked") + public final void add(Class factoryType, Class... factoryImplementations) { + for (Class factoryImplementation : factoryImplementations) { + add(factoryType.getName(), factoryImplementation.getName()); + } + } + + /** + * Add factory implementations to this instance. + * @param factoryType the factory type class name + * @param factoryImplementations the implementation class names + */ + public void add(String factoryType, String... factoryImplementations) { + List implementations = this.factories.computeIfAbsent(factoryType, key -> new ArrayList<>()); + for (String factoryImplementation : factoryImplementations) { + implementations.add(factoryImplementation); + } + } + + /** + * Add factory instances to this instance. + * @param factoryType the factory type class + * @param factoryInstances the implementation instances to add + */ + @SuppressWarnings("unchecked") + public void addInstance(Class factoryType, T... factoryInstances) { + addInstance(factoryType.getName(), factoryInstances); + } + + /** + * Add factory instances to this instance. + * @param factoryType the factory type class name + * @param factoryInstance the implementation instances to add + */ + @SuppressWarnings("unchecked") + public void 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); + } + } + +} diff --git a/spring-core-test/src/test/java/org/springframework/core/mock/MockSpringFactoriesLoaderTests.java b/spring-core-test/src/test/java/org/springframework/core/mock/MockSpringFactoriesLoaderTests.java new file mode 100644 index 00000000000..db46e5e2242 --- /dev/null +++ b/spring-core-test/src/test/java/org/springframework/core/mock/MockSpringFactoriesLoaderTests.java @@ -0,0 +1,83 @@ +/* + * Copyright 2002-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.core.mock; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +import org.springframework.core.annotation.Order; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link MockSpringFactoriesLoader}. + * + * @author Phillip Webb + */ +class MockSpringFactoriesLoaderTests { + + @Test + void addWithClassesAddsFactories() { + MockSpringFactoriesLoader loader = new MockSpringFactoriesLoader(); + loader.add(TestFactoryType.class, TestFactoryOne.class, TestFactoryTwo.class); + assertThatLoaderHasTestFactories(loader); + } + + @Test + void addWithClassNamesAddsFactories() { + MockSpringFactoriesLoader loader = new MockSpringFactoriesLoader(); + loader.add(TestFactoryType.class.getName(), TestFactoryOne.class.getName(), TestFactoryTwo.class.getName()); + assertThatLoaderHasTestFactories(loader); + } + + @Test + void addWithClassAndInstancesAddsFactories() { + MockSpringFactoriesLoader loader = new MockSpringFactoriesLoader(); + loader.addInstance(TestFactoryType.class, new TestFactoryOne(), new TestFactoryTwo()); + assertThatLoaderHasTestFactories(loader); + } + + @Test + void addWithClassNameAndInstancesAddsFactories() { + MockSpringFactoriesLoader loader = new MockSpringFactoriesLoader(); + loader.addInstance(TestFactoryType.class.getName(), new TestFactoryOne(), new TestFactoryTwo()); + assertThatLoaderHasTestFactories(loader); + } + + private void assertThatLoaderHasTestFactories(MockSpringFactoriesLoader loader) { + List factories = loader.load(TestFactoryType.class); + assertThat(factories).hasSize(2); + assertThat(factories.get(0)).isInstanceOf(TestFactoryOne.class); + assertThat(factories.get(1)).isInstanceOf(TestFactoryTwo.class); + } + + static interface TestFactoryType { + + } + + @Order(1) + static class TestFactoryOne implements TestFactoryType { + + } + + @Order(2) + static class TestFactoryTwo implements TestFactoryType { + + } + +}