Use same default ClassLoader in SpringFactoriesLoader

Prior to this commit, the loadFactoryNames() and loadFactories() methods
in SpringFactoriesLoader effectively used a different default
ClassLoader.

This commit ensures that the ClassLoader for SpringFactoriesLoader.class
is now consistently used as the default ClassLoader.

Closes gh-24992
This commit is contained in:
Sam Brannen
2020-04-29 14:40:40 +02:00
committed by Sam Brannen
parent 5abca033d0
commit 859953fe81
2 changed files with 27 additions and 6 deletions

View File

@@ -19,6 +19,8 @@ package org.springframework.core.io.support;
import java.lang.reflect.Modifier;
import java.util.List;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@@ -33,6 +35,23 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
*/
class SpringFactoriesLoaderTests {
@BeforeAll
static void clearCache() {
SpringFactoriesLoader.cache.clear();
assertThat(SpringFactoriesLoader.cache).isEmpty();
}
@AfterAll
static void checkCache() {
assertThat(SpringFactoriesLoader.cache).hasSize(1);
}
@Test
void loadFactoryNames() {
List<String> factoryNames = SpringFactoriesLoader.loadFactoryNames(DummyFactory.class, null);
assertThat(factoryNames).containsExactlyInAnyOrder(MyDummyFactory1.class.getName(), MyDummyFactory2.class.getName());
}
@Test
void loadFactoriesWithNoRegisteredImplementations() {
List<Integer> factories = SpringFactoriesLoader.loadFactories(Integer.class, null);