Skip to content

Commit 7903a3c

Browse files
authored
Test discover of application configuration (#1192)
1 parent d61a0d7 commit 7903a3c

File tree

10 files changed

+29
-18
lines changed

10 files changed

+29
-18
lines changed

testsuite/extra/.env

Whitespace-only changes.

testsuite/extra/config/application.properties

Whitespace-only changes.

testsuite/extra/config/application.yaml

Whitespace-only changes.

testsuite/extra/config/application.yml

Whitespace-only changes.

testsuite/extra/src/test/java/io/smallrye/config/test/location/PropertiesLocationTest.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
import org.junit.jupiter.api.Test;
2525
import org.junit.jupiter.api.io.TempDir;
2626

27-
import io.smallrye.config.PropertiesConfigSource;
2827
import io.smallrye.config.SmallRyeConfig;
2928
import io.smallrye.config.SmallRyeConfigBuilder;
30-
import io.smallrye.config.source.yaml.YamlConfigSource;
3129

3230
public class PropertiesLocationTest {
3331
@Test
@@ -56,7 +54,7 @@ void multipleResourcesInClassPath(@TempDir Path tempDir) throws Exception {
5654

5755
assertEquals("1234", config.getRawValue("my.prop.one"));
5856
assertEquals("5678", config.getRawValue("my.prop.two"));
59-
assertEquals(2, countSources(config, PropertiesConfigSource.class));
57+
assertEquals(2, countSources(config, "resources.properties"));
6058
} finally {
6159
Thread.currentThread().setContextClassLoader(contextClassLoader);
6260
}
@@ -93,7 +91,7 @@ void multipleResourcesInClassPathYaml(@TempDir Path tempDir) throws Exception {
9391

9492
assertEquals("1234", config.getRawValue("my.prop.one"));
9593
assertEquals("5678", config.getRawValue("my.prop.two"));
96-
assertEquals(2, countSources(config, YamlConfigSource.class));
94+
assertEquals(2, countSources(config, "resources.yml"));
9795
} finally {
9896
Thread.currentThread().setContextClassLoader(contextClassLoader);
9997
}
@@ -115,7 +113,7 @@ void jar(@TempDir Path tempDir) throws Exception {
115113
SmallRyeConfig config = buildConfig("jar:" + filePathOne.toUri() + "!/resources.properties");
116114

117115
assertEquals("1234", config.getRawValue("my.prop.one"));
118-
assertEquals(1, countSources(config, PropertiesConfigSource.class));
116+
assertEquals(1, countSources(config, "resources.properties"));
119117
} finally {
120118
Thread.currentThread().setContextClassLoader(contextClassLoader);
121119
}
@@ -141,7 +139,7 @@ void jarYaml(@TempDir Path tempDir) throws Exception {
141139
SmallRyeConfig config = buildConfig("jar:" + filePathOne.toUri() + "!/resources.yml");
142140

143141
assertEquals("1234", config.getRawValue("my.prop.one"));
144-
assertEquals(1, countSources(config, YamlConfigSource.class));
142+
assertEquals(1, countSources(config, "resources.yml"));
145143
} finally {
146144
Thread.currentThread().setContextClassLoader(contextClassLoader);
147145
}
@@ -206,9 +204,13 @@ void priorityLoadOrder(@TempDir Path tempDir) throws Exception {
206204
assertEquals("main", config.getRawValue("my.prop.common"));
207205
// This should be loaded by the first discovered source in the classpath
208206
assertEquals("1", config.getRawValue("my.prop.jar.common"));
209-
assertEquals(4, countSources(config, PropertiesConfigSource.class));
207+
assertEquals(3, countSources(config, "microprofile-config.properties"));
208+
assertEquals(1, countSources(config, "fallback.properties"));
210209
assertTrue(stream(config.getConfigSources().spliterator(), false)
211-
.filter(PropertiesConfigSource.class::isInstance)
210+
.filter(configSource -> configSource.getName().contains("microprofile-config.properties"))
211+
.allMatch(configSource -> configSource.getOrdinal() == 100));
212+
assertTrue(stream(config.getConfigSources().spliterator(), false)
213+
.filter(configSource -> configSource.getName().contains("fallback.properties"))
212214
.allMatch(configSource -> configSource.getOrdinal() == 100));
213215
} finally {
214216
Thread.currentThread().setContextClassLoader(contextClassLoader);
@@ -384,8 +386,10 @@ void mixedProfiles(@TempDir Path tempDir) throws Exception {
384386
assertEquals("common-file", config.getRawValue("my.prop.common"));
385387
assertEquals("dev-file", config.getRawValue("my.prop.profile"));
386388

387-
final List<ConfigSource> sources = stream(config.getConfigSources().spliterator(), false)
388-
.filter(PropertiesConfigSource.class::isInstance).collect(toList());
389+
List<ConfigSource> sources = stream(config.getConfigSources().spliterator(), false)
390+
.filter(configSource -> configSource.getName().contains("config.properties")
391+
|| configSource.getName().contains("config-"))
392+
.collect(toList());
389393
assertEquals(6, sources.size());
390394
assertEquals("1", sources.get(0).getValue("order"));
391395
assertEquals("2", sources.get(1).getValue("order"));
@@ -457,7 +461,7 @@ void mixedExtensions(@TempDir Path tempDir) throws Exception {
457461
.build();
458462

459463
assertEquals("5678", config.getRawValue("my.prop.one"));
460-
assertEquals(2, countSources(config, YamlConfigSource.class));
464+
assertEquals(2, countSources(config, "resources"));
461465
} finally {
462466
Thread.currentThread().setContextClassLoader(contextClassLoader);
463467
}
@@ -481,8 +485,9 @@ private static SmallRyeConfig buildConfig(String... locations) {
481485
.build();
482486
}
483487

484-
private static int countSources(SmallRyeConfig config, Class<?> configSource) {
485-
return (int) stream(config.getConfigSources().spliterator(), false).filter(configSource::isInstance)
488+
private static int countSources(SmallRyeConfig config, String name) {
489+
return (int) stream(config.getConfigSources().spliterator(), false)
490+
.filter(configSource -> configSource.getName().contains(name))
486491
.count();
487492
}
488493
}

testsuite/extra/src/test/java/io/smallrye/config/test/source/OrdinalSourceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ static WebArchive deploy() {
3030
@Test
3131
void ordinal() {
3232
for (ConfigSource configSource : config.getConfigSources()) {
33-
if (configSource.getName().startsWith("PropertiesConfigSource")) {
33+
if (configSource.getName().contains("microprofile-config.properties")) {
3434
assertEquals(1234, configSource.getOrdinal());
3535
}
3636
}

testsuite/extra/src/test/java/io/smallrye/config/test/source/SmallRyeConfigTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import io.smallrye.config.DefaultValuesConfigSource;
2323
import io.smallrye.config.EnvConfigSource;
24-
import io.smallrye.config.PropertiesConfigSource;
2524
import io.smallrye.config.SysPropConfigSource;
2625

2726
@ExtendWith(ArquillianExtension.class)
@@ -45,10 +44,17 @@ void config() {
4544
void sources() {
4645
List<ConfigSource> sources = StreamSupport.stream(config.getConfigSources().spliterator(), false).collect(toList());
4746

48-
assertEquals(4, sources.size());
47+
assertEquals(11, sources.size());
4948
assertTrue(sources.get(0) instanceof SysPropConfigSource);
5049
assertTrue(sources.get(1) instanceof EnvConfigSource);
51-
assertTrue(sources.get(2) instanceof PropertiesConfigSource);
52-
assertTrue(sources.get(3) instanceof DefaultValuesConfigSource);
50+
assertTrue(sources.get(2).getName().contains(".env"));
51+
assertTrue(sources.get(3).getName().contains("config/application.yaml"));
52+
assertTrue(sources.get(4).getName().contains("config/application.yml"));
53+
assertTrue(sources.get(5).getName().contains("config/application.properties"));
54+
assertTrue(sources.get(6).getName().contains("application.yaml"));
55+
assertTrue(sources.get(7).getName().contains("application.yml"));
56+
assertTrue(sources.get(8).getName().contains("application.properties"));
57+
assertTrue(sources.get(9).getName().contains("microprofile-config.properties"));
58+
assertTrue(sources.get(10) instanceof DefaultValuesConfigSource);
5359
}
5460
}

testsuite/extra/src/test/resources/application.properties

Whitespace-only changes.

testsuite/extra/src/test/resources/application.yaml

Whitespace-only changes.

testsuite/extra/src/test/resources/application.yml

Whitespace-only changes.

0 commit comments

Comments
 (0)