Search in sources :

Example 1 with Settings

use of com.helger.settings.Settings in project phoss-smp by phax.

the class SMPSettingsManagerXML method createWriteData.

@Override
@Nonnull
protected IMicroDocument createWriteData() {
    final IMicroDocument ret = new MicroDocument();
    final SettingsMicroDocumentConverter<Settings> aConverter = new SettingsMicroDocumentConverter<>(ISettingsFactory.newInstance());
    ret.appendChild(aConverter.convertToMicroElement(m_aSettings.getAsSettings(), null, "root"));
    return ret;
}
Also used : IMicroDocument(com.helger.xml.microdom.IMicroDocument) MicroDocument(com.helger.xml.microdom.MicroDocument) SettingsMicroDocumentConverter(com.helger.settings.exchange.xml.SettingsMicroDocumentConverter) IMicroDocument(com.helger.xml.microdom.IMicroDocument) ISettings(com.helger.settings.ISettings) Settings(com.helger.settings.Settings) Nonnull(javax.annotation.Nonnull)

Example 2 with Settings

use of com.helger.settings.Settings in project phoss-smp by phax.

the class SMPSettingsManagerXML method onRead.

@Override
@Nonnull
protected EChange onRead(@Nonnull final IMicroDocument aDoc) {
    final SettingsMicroDocumentConverter<Settings> aConverter = new SettingsMicroDocumentConverter<>(ISettingsFactory.newInstance());
    final ISettings aSettings = aConverter.convertToNative(aDoc.getDocumentElement());
    m_aSettings.initFromSettings(aSettings);
    return EChange.UNCHANGED;
}
Also used : SettingsMicroDocumentConverter(com.helger.settings.exchange.xml.SettingsMicroDocumentConverter) ISettings(com.helger.settings.ISettings) ISettings(com.helger.settings.ISettings) Settings(com.helger.settings.Settings) Nonnull(javax.annotation.Nonnull)

Example 3 with Settings

use of com.helger.settings.Settings in project ph-commons by phax.

the class SettingsPersistenceJsonTest 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 SettingsPersistenceJson aSPP = new SettingsPersistenceJson();
    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);
    if (false) {
        // Differs because of different types
        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 4 with Settings

use of com.helger.settings.Settings 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 5 with Settings

use of com.helger.settings.Settings 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)5 Settings (com.helger.settings.Settings)5 Test (org.junit.Test)3 SettingsMicroDocumentConverter (com.helger.settings.exchange.xml.SettingsMicroDocumentConverter)2 Duration (java.time.Duration)2 Period (java.time.Period)2 ZonedDateTime (java.time.ZonedDateTime)2 Nonnull (javax.annotation.Nonnull)2 IMicroDocument (com.helger.xml.microdom.IMicroDocument)1 IMicroElement (com.helger.xml.microdom.IMicroElement)1 MicroDocument (com.helger.xml.microdom.MicroDocument)1