Skip to content

Commit

Permalink
Test discover of application configuration (#1192)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Jul 4, 2024
1 parent d61a0d7 commit 7903a3c
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 18 deletions.
Empty file added testsuite/extra/.env
Empty file.
Empty file.
Empty file.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import io.smallrye.config.PropertiesConfigSource;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;
import io.smallrye.config.source.yaml.YamlConfigSource;

public class PropertiesLocationTest {
@Test
Expand Down Expand Up @@ -56,7 +54,7 @@ void multipleResourcesInClassPath(@TempDir Path tempDir) throws Exception {

assertEquals("1234", config.getRawValue("my.prop.one"));
assertEquals("5678", config.getRawValue("my.prop.two"));
assertEquals(2, countSources(config, PropertiesConfigSource.class));
assertEquals(2, countSources(config, "resources.properties"));
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
Expand Down Expand Up @@ -93,7 +91,7 @@ void multipleResourcesInClassPathYaml(@TempDir Path tempDir) throws Exception {

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

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

assertEquals("1234", config.getRawValue("my.prop.one"));
assertEquals(1, countSources(config, YamlConfigSource.class));
assertEquals(1, countSources(config, "resources.yml"));
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
Expand Down Expand Up @@ -206,9 +204,13 @@ void priorityLoadOrder(@TempDir Path tempDir) throws Exception {
assertEquals("main", config.getRawValue("my.prop.common"));
// This should be loaded by the first discovered source in the classpath
assertEquals("1", config.getRawValue("my.prop.jar.common"));
assertEquals(4, countSources(config, PropertiesConfigSource.class));
assertEquals(3, countSources(config, "microprofile-config.properties"));
assertEquals(1, countSources(config, "fallback.properties"));
assertTrue(stream(config.getConfigSources().spliterator(), false)
.filter(PropertiesConfigSource.class::isInstance)
.filter(configSource -> configSource.getName().contains("microprofile-config.properties"))
.allMatch(configSource -> configSource.getOrdinal() == 100));
assertTrue(stream(config.getConfigSources().spliterator(), false)
.filter(configSource -> configSource.getName().contains("fallback.properties"))
.allMatch(configSource -> configSource.getOrdinal() == 100));
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
Expand Down Expand Up @@ -384,8 +386,10 @@ void mixedProfiles(@TempDir Path tempDir) throws Exception {
assertEquals("common-file", config.getRawValue("my.prop.common"));
assertEquals("dev-file", config.getRawValue("my.prop.profile"));

final List<ConfigSource> sources = stream(config.getConfigSources().spliterator(), false)
.filter(PropertiesConfigSource.class::isInstance).collect(toList());
List<ConfigSource> sources = stream(config.getConfigSources().spliterator(), false)
.filter(configSource -> configSource.getName().contains("config.properties")
|| configSource.getName().contains("config-"))
.collect(toList());
assertEquals(6, sources.size());
assertEquals("1", sources.get(0).getValue("order"));
assertEquals("2", sources.get(1).getValue("order"));
Expand Down Expand Up @@ -457,7 +461,7 @@ void mixedExtensions(@TempDir Path tempDir) throws Exception {
.build();

assertEquals("5678", config.getRawValue("my.prop.one"));
assertEquals(2, countSources(config, YamlConfigSource.class));
assertEquals(2, countSources(config, "resources"));
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader);
}
Expand All @@ -481,8 +485,9 @@ private static SmallRyeConfig buildConfig(String... locations) {
.build();
}

private static int countSources(SmallRyeConfig config, Class<?> configSource) {
return (int) stream(config.getConfigSources().spliterator(), false).filter(configSource::isInstance)
private static int countSources(SmallRyeConfig config, String name) {
return (int) stream(config.getConfigSources().spliterator(), false)
.filter(configSource -> configSource.getName().contains(name))
.count();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static WebArchive deploy() {
@Test
void ordinal() {
for (ConfigSource configSource : config.getConfigSources()) {
if (configSource.getName().startsWith("PropertiesConfigSource")) {
if (configSource.getName().contains("microprofile-config.properties")) {
assertEquals(1234, configSource.getOrdinal());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import io.smallrye.config.DefaultValuesConfigSource;
import io.smallrye.config.EnvConfigSource;
import io.smallrye.config.PropertiesConfigSource;
import io.smallrye.config.SysPropConfigSource;

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

assertEquals(4, sources.size());
assertEquals(11, sources.size());
assertTrue(sources.get(0) instanceof SysPropConfigSource);
assertTrue(sources.get(1) instanceof EnvConfigSource);
assertTrue(sources.get(2) instanceof PropertiesConfigSource);
assertTrue(sources.get(3) instanceof DefaultValuesConfigSource);
assertTrue(sources.get(2).getName().contains(".env"));
assertTrue(sources.get(3).getName().contains("config/application.yaml"));
assertTrue(sources.get(4).getName().contains("config/application.yml"));
assertTrue(sources.get(5).getName().contains("config/application.properties"));
assertTrue(sources.get(6).getName().contains("application.yaml"));
assertTrue(sources.get(7).getName().contains("application.yml"));
assertTrue(sources.get(8).getName().contains("application.properties"));
assertTrue(sources.get(9).getName().contains("microprofile-config.properties"));
assertTrue(sources.get(10) instanceof DefaultValuesConfigSource);
}
}
Empty file.
Empty file.
Empty file.

0 comments on commit 7903a3c

Please sign in to comment.