use of com.helger.settings.ISettings in project ph-commons by phax.
the class SettingsPersistenceProperties method readSettings.
@Nonnull
public ISettings readSettings(@Nonnull @WillClose final InputStream aIS) {
ValueEnforcer.notNull(aIS, "InputStream");
// Create the settings object
final ISettings aSettings = m_aSettingsFactory.apply(getReadSettingsName());
// Read the properties file from the input stream
final NonBlockingProperties aProps = PropertiesHelper.loadProperties(aIS);
if (aProps != null)
for (final Map.Entry<String, String> aEntry : aProps.entrySet()) aSettings.putIn(aEntry.getKey(), aEntry.getValue());
return aSettings;
}
use of com.helger.settings.ISettings in project ph-commons by phax.
the class SettingsPersistencePropertiesTest method testViceVersaConversion.
@Test
public void testViceVersaConversion() throws UnsupportedEncodingException {
// Name is important!
final ISettings aSrc = new Settings("anonymous");
aSrc.putIn("field1a", BigInteger.valueOf(1234));
aSrc.putIn("field1b", BigInteger.valueOf(-23423424));
aSrc.putIn("field2a", BigDecimal.valueOf(12.34));
aSrc.putIn("field2b", BigDecimal.valueOf(-2342.334599424));
aSrc.putIn("field3a", "My wonderbra string\n(incl newline)");
aSrc.putIn("field3b", "");
aSrc.putIn("field9a", Boolean.TRUE);
aSrc.putIn("field9b", StringParser.parseByteObj("5"));
aSrc.putIn("field9c", Character.valueOf('ä'));
aSrc.putIn("fieldxa", PDTFactory.getCurrentLocalDate());
aSrc.putIn("fieldxb", PDTFactory.getCurrentLocalTime());
aSrc.putIn("fieldxc", PDTFactory.getCurrentLocalDateTime());
aSrc.putIn("fieldxd", PDTFactory.getCurrentZonedDateTime());
aSrc.putIn("fieldxe", Duration.ofHours(5));
aSrc.putIn("fieldxf", Period.ofDays(3));
aSrc.putIn("fieldxg", "Any byte ärräy".getBytes(StandardCharsets.UTF_8.name()));
// null value
aSrc.putIn("fieldnull", null);
final SettingsPersistenceProperties aSPP = new SettingsPersistenceProperties();
final String sSrc = aSPP.writeSettings(aSrc);
assertNotNull(sSrc);
// The created object is different, because now all values are String typed!
final ISettings aDst1 = aSPP.readSettings(sSrc);
assertNotNull(aDst1);
// Reading the String typed version again should result in the same object
final ISettings aDst2 = aSPP.readSettings(aSPP.writeSettings(aDst1));
assertNotNull(aDst2);
assertEquals(aDst1, aDst2);
assertNotNull(aDst2.getAsLocalDate("fieldxa"));
assertNotNull(aDst2.getAsLocalTime("fieldxb"));
assertNotNull(aDst2.getAsLocalDateTime("fieldxc"));
assertNotNull(aDst2.getConvertedValue("fieldxd", ZonedDateTime.class));
assertNotNull(aDst2.getConvertedValue("fieldxe", Duration.class));
assertNotNull(aDst2.getConvertedValue("fieldxf", Period.class));
}
use of com.helger.settings.ISettings in project ph-commons by phax.
the class SettingsMicroDocumentConverterTest method testConversionWithTypes.
@Test
public void testConversionWithTypes() throws UnsupportedEncodingException {
final Settings aSrc = new Settings("myName");
aSrc.putIn("field1a", BigInteger.valueOf(1234));
aSrc.putIn("field1b", BigInteger.valueOf(-23423424));
aSrc.putIn("field2a", BigDecimal.valueOf(12.34));
aSrc.putIn("field2b", BigDecimal.valueOf(-2342.334599424));
aSrc.putIn("field3a", "My wonderbra string\n(incl newline)");
aSrc.putIn("field3b", "");
aSrc.putIn("field9a", Boolean.TRUE);
aSrc.putIn("field9b", StringParser.parseByteObj("5"));
aSrc.putIn("field9c", Character.valueOf('ä'));
aSrc.putIn("fieldxa", PDTFactory.getCurrentLocalDate());
aSrc.putIn("fieldxb", PDTFactory.getCurrentLocalTime());
aSrc.putIn("fieldxc", PDTFactory.getCurrentLocalDateTime());
aSrc.putIn("fieldxd", PDTFactory.getCurrentZonedDateTime());
aSrc.putIn("fieldxe", Duration.ofHours(5));
aSrc.putIn("fieldxf", Period.ofDays(3));
aSrc.putIn("fieldxg", "Any byte ärräy".getBytes(StandardCharsets.UTF_8.name()));
final Settings aNestedSettings = new Settings("nestedSettings");
aNestedSettings.putIn("a", "b");
aNestedSettings.putIn("c", "d");
aNestedSettings.putIn("e", Clock.systemDefaultZone().millis());
aSrc.putIn("fieldxh", aNestedSettings);
// null value
aSrc.putIn("fieldnull", null);
// To XML
final IMicroElement eSrcElement = MicroTypeConverter.convertToMicroElement(aSrc, "root");
assertNotNull(eSrcElement);
if (false)
LOGGER.info(MicroWriter.getNodeAsString(eSrcElement));
// From XML
final ISettings aDst = MicroTypeConverter.convertToNative(eSrcElement, Settings.class);
assertNotNull(aDst);
// No longer true, because now all values are String
if (false) {
assertEquals(aSrc, aDst);
// Compare list
assertEquals(BigInteger.valueOf(1234), aDst.getValue("field1a"));
} else {
assertEquals("1234", aDst.getValue("field1a"));
}
final ISettings aDst2 = new Settings(aDst.getName());
aDst2.putAllIn(aDst);
assertEquals(aDst, aDst2);
assertTrue(aDst2.putIn("field3b", "doch was").isChanged());
assertFalse(aDst.equals(aDst2));
}
Aggregations