FEAT simple Optional based PropertyProvider class

pull/1/head
Niclas Thobaben 2020-12-01 12:02:52 +01:00
parent f18958ad8f
commit 09d9b085cf
3 changed files with 139 additions and 0 deletions

View File

@ -0,0 +1,67 @@
package de.nclazz.commons.env;
import lombok.SneakyThrows;
import java.io.InputStream;
import java.io.Writer;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
public class PropertyProvider {
private final Properties properties;
public PropertyProvider(Properties defaults) {
this.properties = new Properties(defaults);
}
public PropertyProvider() {
this.properties = new Properties();
}
public Optional<String> getProperty(String key) {
return Optional.ofNullable(this.properties.getProperty(key));
}
public Optional<String> putProperty(String key, Object value) {
String last = this.properties.getProperty(key);
if(value == null) {
this.properties.remove(key);
}else {
this.properties.setProperty(key, value.toString());
}
return Optional.ofNullable(last);
}
public void clear() {
this.properties.clear();
}
@SneakyThrows
public void write(Writer writer) {
for(Map.Entry<Object, Object> entry : this.properties.entrySet()) {
String key = entry.getKey().toString();
Object value = entry.getValue();
String valueStr = value != null ? value.toString() : "";
writer.append(key)
.append(" = ")
.append(valueStr)
.append(System.lineSeparator());
}
}
/**
* Loads the properties from given {@code stream}.
* The stream remains open after loading.
* @param stream
*/
@SneakyThrows
public void load(InputStream stream) {
this.properties.load(stream);
}
}

View File

@ -0,0 +1,69 @@
package de.nclazz.commons.env;
import de.nclazz.commons.io.FileUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class PropertyProviderTests {
@Test
void testPropertyProviderBasic() throws Exception {
PropertyProvider provider = new PropertyProvider();
try(InputStream stream = PropertyProviderTests.class.getResourceAsStream("/test-files/env.properties")) {
provider.load(stream);
assertEquals("Hello World", provider.getProperty("property.string").orElse(""));
assertEquals(12, (int)provider.getProperty("property.int")
.map(Integer::parseInt)
.orElse(0));
assertEquals(12.2f, (float)provider.getProperty("property.float")
.map(Float::parseFloat)
.orElse(0f));
}catch(Exception e) {
throw e;
}
}
@Test
void testIORoundTrip() throws Exception {
PropertyProvider provider = new PropertyProvider();
InputStream stream = PropertyProviderTests.class.getResourceAsStream("/test-files/env.properties");
provider.load(stream);
stream.close();
FileUtils.withTempFile("test", ".properties", file -> {
FileUtils.writeFile(file, provider::write);
provider.clear();
try(InputStream fstream = new FileInputStream(file)) {
provider.load(fstream);
}catch(Exception e) {
throw new RuntimeException(e);
}
});
assertEquals("Hello World", provider.getProperty("property.string").orElse(""));
assertEquals(12, (int)provider.getProperty("property.int")
.map(Integer::parseInt)
.orElse(0));
assertEquals(12.2f, (float)provider.getProperty("property.float")
.map(Float::parseFloat)
.orElse(0f));
}
}

View File

@ -0,0 +1,3 @@
property.string = Hello World
property.int = 12
property.float = 12.2