com.github.valfirst.slf4jtest.OverridableProperties Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of slf4j-test Show documentation
Show all versions of slf4j-test Show documentation
An in-memory SLF4J implementation focused on aiding unit testing.
package com.github.valfirst.slf4jtest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
class OverridableProperties {
private static final Properties EMPTY_PROPERTIES = new Properties();
private final String propertySourceName;
private final Properties properties;
OverridableProperties(final String propertySourceName) throws IOException {
this.propertySourceName = propertySourceName;
this.properties = getProperties();
}
private Properties getProperties() throws IOException {
InputStream resourceAsStream =
Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream(propertySourceName + ".properties");
if (resourceAsStream != null) {
return loadProperties(resourceAsStream);
}
return EMPTY_PROPERTIES;
}
private static Properties loadProperties(InputStream propertyResource) throws IOException {
try (InputStream closablePropertyResource = propertyResource) {
final Properties loadedProperties = new Properties();
loadedProperties.load(closablePropertyResource);
return loadedProperties;
}
}
String getProperty(final String propertyKey, final String defaultValue) {
final String propertyFileProperty = properties.getProperty(propertyKey, defaultValue);
return System.getProperty(propertySourceName + "." + propertyKey, propertyFileProperty);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy