Last active
September 5, 2022 12:41
-
-
Save FonzTech/b206aeac3d08a2071524b1c7f4a5e6f1 to your computer and use it in GitHub Desktop.
Load multiple properties files from different paths. Useful when working with MicroProfile and Quarkus. Put your filepaths, separated by colon (like the PATH variable) in the PROPERTIES_FILES environment variable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.alfonsopauciello.config; | |
import org.eclipse.microprofile.config.spi.ConfigSource; | |
import java.io.FileReader; | |
import java.util.Properties; | |
import java.util.Set; | |
public class MultipleFilesConfigSource implements ConfigSource { | |
private static final Properties configuration = new Properties(); | |
static void loadProperties(final String files) { | |
if (files == null || files.length() <= 0) { | |
return; | |
} | |
for (final String fpath : files.split(":")) { | |
FileReader reader = null; | |
try { | |
reader = new FileReader(fpath); | |
final Properties prop = new Properties(); | |
prop.load(reader); | |
configuration.putAll(prop); | |
} catch (Exception e) { | |
configuration.clear(); | |
throw new RuntimeException(e); | |
} finally { | |
if (reader != null) { | |
try { | |
reader.close(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} | |
static void clear() { | |
configuration.clear(); | |
} | |
static { | |
loadProperties(System.getenv("PROPERTIES_FILES")); | |
} | |
@Override | |
public int getOrdinal() { | |
return 275; | |
} | |
@Override | |
public Set<String> getPropertyNames() { | |
return configuration.stringPropertyNames(); | |
} | |
@Override | |
public String getValue(final String propertyName) { | |
return configuration.getProperty(propertyName); | |
} | |
@Override | |
public String getName() { | |
return MultipleFilesConfigSource.class.getSimpleName(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.alfonsopauciello.config; | |
import io.quarkus.test.junit.QuarkusTest; | |
import org.eclipse.microprofile.config.ConfigProvider; | |
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
@QuarkusTest | |
public class MultipleFilesConfigSourceTest { | |
private static final String FILES_VALID = | |
"./src/test/resources/application_unit_test_1.properties" + ":" + | |
"./src/test/resources/application_unit_test_2.properties"; | |
private static final String FILES_INVALID = | |
"./src/test/resources/application_unit_test_1.properties" + ":" + | |
"./src/test/resources/i_am_inexistent.properties"; | |
@BeforeEach | |
public void clearProperties() { | |
MultipleFilesConfigSource.clear(); | |
} | |
@Test | |
public void checkValidFile() { | |
MultipleFilesConfigSource.loadProperties(FILES_VALID); | |
// Regular properties file | |
Assertions.assertFalse(ConfigProvider.getConfig().getOptionalValue("quarkus.log.category.\"it.infocert\".level", String.class).isEmpty()); | |
// Custom properties file 1 | |
Assertions.assertEquals("true", ConfigProvider.getConfig().getValue("check.one", String.class)); | |
Assertions.assertEquals("false", ConfigProvider.getConfig().getValue("check.two", String.class)); | |
Assertions.assertTrue(ConfigProvider.getConfig().getOptionalValue("check.three", String.class).isEmpty()); | |
} | |
@Test | |
public void checkNonExistentFile() { | |
Assertions.assertThrows(RuntimeException.class, () -> MultipleFilesConfigSource.loadProperties(FILES_INVALID)); | |
// Regular properties file | |
Assertions.assertFalse(ConfigProvider.getConfig().getOptionalValue("quarkus.log.category.\"it.infocert\".level", String.class).isEmpty()); | |
// Custom properties file 1 | |
Assertions.assertTrue(ConfigProvider.getConfig().getOptionalValue("check.one", String.class).isEmpty()); | |
Assertions.assertTrue(ConfigProvider.getConfig().getOptionalValue("check.two", String.class).isEmpty()); | |
Assertions.assertTrue(ConfigProvider.getConfig().getOptionalValue("check.three", String.class).isEmpty()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment