|
16 | 16 |
|
17 | 17 | package org.springframework.boot.context.config;
|
18 | 18 |
|
| 19 | +import java.io.File; |
| 20 | +import java.io.IOException; |
| 21 | +import java.net.URL; |
19 | 22 | import java.util.Collection;
|
20 | 23 | import java.util.Collections;
|
| 24 | +import java.util.Enumeration; |
21 | 25 | import java.util.LinkedHashMap;
|
22 | 26 | import java.util.List;
|
23 | 27 | import java.util.Map;
|
|
42 | 46 | import org.springframework.core.env.PropertySource;
|
43 | 47 | import org.springframework.core.io.DefaultResourceLoader;
|
44 | 48 | import org.springframework.core.io.ResourceLoader;
|
| 49 | +import org.springframework.core.io.support.SpringFactoriesLoader; |
45 | 50 | import org.springframework.mock.env.MockPropertySource;
|
46 | 51 |
|
47 | 52 | import static org.assertj.core.api.Assertions.assertThat;
|
48 | 53 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 54 | +import static org.mockito.BDDMockito.given; |
49 | 55 | import static org.mockito.Mockito.mock;
|
50 | 56 |
|
51 | 57 | /**
|
@@ -326,6 +332,33 @@ void processAndApplyWhenHasListenerCallsOnSetProfiles(TestInfo info) {
|
326 | 332 | assertThat(listener.getProfiles().getActive()).containsExactly("one", "two", "three");
|
327 | 333 | }
|
328 | 334 |
|
| 335 | + @Test |
| 336 | + @SuppressWarnings("rawtypes") |
| 337 | + void configDataLoadersAreLoadedUsingClassLoaderFromResourceLoader() { |
| 338 | + ResourceLoader resourceLoader = mock(ResourceLoader.class); |
| 339 | + ClassLoader classLoader = new ClassLoader() { |
| 340 | + |
| 341 | + @Override |
| 342 | + public Enumeration<URL> getResources(String name) throws IOException { |
| 343 | + if (SpringFactoriesLoader.FACTORIES_RESOURCE_LOCATION.equals(name)) { |
| 344 | + return Collections.enumeration(List.of(new File( |
| 345 | + "src/test/resources/org/springframework/boot/context/config/separate-class-loader-spring.factories") |
| 346 | + .toURI() |
| 347 | + .toURL())); |
| 348 | + } |
| 349 | + return super.getResources(name); |
| 350 | + } |
| 351 | + |
| 352 | + }; |
| 353 | + given(resourceLoader.getClassLoader()).willReturn(classLoader); |
| 354 | + TestConfigDataEnvironment configDataEnvironment = new TestConfigDataEnvironment(this.logFactory, |
| 355 | + this.bootstrapContext, this.environment, resourceLoader, this.additionalProfiles, null); |
| 356 | + assertThat(configDataEnvironment).extracting("loaders.loaders") |
| 357 | + .asList() |
| 358 | + .extracting((item) -> (Class) item.getClass()) |
| 359 | + .containsOnly(SeparateClassLoaderConfigDataLoader.class); |
| 360 | + } |
| 361 | + |
329 | 362 | private String getConfigLocation(TestInfo info) {
|
330 | 363 | return "optional:classpath:" + info.getTestClass().get().getName().replace('.', '/') + "-"
|
331 | 364 | + info.getTestMethod().get().getName() + ".properties";
|
@@ -355,4 +388,14 @@ Binder getConfigDataLocationResolversBinder() {
|
355 | 388 |
|
356 | 389 | }
|
357 | 390 |
|
| 391 | + static class SeparateClassLoaderConfigDataLoader implements ConfigDataLoader<ConfigDataResource> { |
| 392 | + |
| 393 | + @Override |
| 394 | + public ConfigData load(ConfigDataLoaderContext context, ConfigDataResource resource) |
| 395 | + throws IOException, ConfigDataResourceNotFoundException { |
| 396 | + return null; |
| 397 | + } |
| 398 | + |
| 399 | + } |
| 400 | + |
358 | 401 | }
|
0 commit comments