Search in sources :

Example 6 with ISettings

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;
}
Also used : NonBlockingProperties(com.helger.commons.lang.NonBlockingProperties) ISettings(com.helger.settings.ISettings) Nonnull(javax.annotation.Nonnull)

Example 7 with ISettings

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));
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Period(java.time.Period) Duration(java.time.Duration) ISettings(com.helger.settings.ISettings) ISettings(com.helger.settings.ISettings) Settings(com.helger.settings.Settings) Test(org.junit.Test)

Example 8 with ISettings

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));
}
Also used : IMicroElement(com.helger.xml.microdom.IMicroElement) ISettings(com.helger.settings.ISettings) ISettings(com.helger.settings.ISettings) Settings(com.helger.settings.Settings) Test(org.junit.Test)

Aggregations

ISettings (com.helger.settings.ISettings)8 Nonnull (javax.annotation.Nonnull)5 Settings (com.helger.settings.Settings)4 Test (org.junit.Test)3 NonBlockingProperties (com.helger.commons.lang.NonBlockingProperties)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Duration (java.time.Duration)2 Period (java.time.Period)2 ZonedDateTime (java.time.ZonedDateTime)2 Map (java.util.Map)2 ValueEnforcer (com.helger.commons.ValueEnforcer)1 Nonempty (com.helger.commons.annotation.Nonempty)1 CollectionHelper (com.helger.commons.collection.CollectionHelper)1 IReadableResource (com.helger.commons.io.resource.IReadableResource)1 StreamHelper (com.helger.commons.io.stream.StreamHelper)1 ESuccess (com.helger.commons.state.ESuccess)1 TypeConverter (com.helger.commons.typeconvert.TypeConverter)1 IJson (com.helger.json.IJson)1 IJsonObject (com.helger.json.IJsonObject)1