68 lines
2.1 KiB
Java
68 lines
2.1 KiB
Java
package de.nclazz.commons.util;
|
|
|
|
import de.nclazz.commons.io.FileUtils;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import java.io.FileInputStream;
|
|
import java.io.InputStream;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
public class PropertyProviderTests {
|
|
|
|
@Test
|
|
void testPropertyProviderBasic() {
|
|
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 new RuntimeException(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));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|