Search in sources :

Example 11 with PropertiesMapper

use of com.tvd12.properties.file.mapping.PropertiesMapper in project properties-file by tvd12.

the class PropertiesMapperTest method mapByConstructorTest.

@Test
public void mapByConstructorTest() {
    Properties properties = new Properties();
    properties.put("host", "0.0.0.0");
    properties.put("port", 3005);
    properties.put("admin.name", "Admin");
    properties.put("guest.name", "Guest");
    ServerConfig output = new PropertiesMapper().data(properties).map(ServerConfig.class);
    assert output.getHost().equals("0.0.0.0");
    assert output.getPort() == 3005;
    assert output.getAdminName().equals("Admin");
    assert output.getAdminPassword() == null;
}
Also used : PropertiesMapper(com.tvd12.properties.file.mapping.PropertiesMapper) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Example 12 with PropertiesMapper

use of com.tvd12.properties.file.mapping.PropertiesMapper in project properties-file by tvd12.

the class PropertiesMapperTest method testMapPropertiesToBean2.

@Test
public void testMapPropertiesToBean2() {
    Properties properties = new Properties();
    properties.setProperty("name", "hello");
    properties.put("age", 24);
    properties.put("clazz", ClassA.class.getName());
    properties.put("date", new SimpleDateFormat(Dates.getPattern()).format(new Date()));
    ClassA object = new PropertiesMapper().data(PropertiesUtil.toMap(properties)).bean(new ClassA()).map();
    assertEquals(object.getName(), "hello");
    assertEquals(object.getAge(), 24);
    assertEquals(object.getMoney(), 10);
    assertEquals(object.getClazz(), ClassA.class);
    assertNotNull(object.getDate());
}
Also used : PropertiesMapper(com.tvd12.properties.file.mapping.PropertiesMapper) Properties(java.util.Properties) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 13 with PropertiesMapper

use of com.tvd12.properties.file.mapping.PropertiesMapper in project properties-file by tvd12.

the class PropertiesMapperTest method testMapPropertiesToBeanWithMapClazz.

@Test
public void testMapPropertiesToBeanWithMapClazz() {
    Properties properties = new Properties();
    properties.setProperty("name", "hello");
    properties.put("age", 24);
    properties.put("clazz", ClassA.class.getName());
    properties.put("date", new SimpleDateFormat(Dates.getPattern()).format(new Date()));
    ClassA object = new PropertiesMapper().data(PropertiesUtil.toMap(properties)).map(ClassA.class);
    assertEquals(object.getName(), "hello");
    assertEquals(object.getAge(), 24);
    assertEquals(object.getMoney(), 10);
    assertEquals(object.getClazz(), ClassA.class);
    assertNotNull(object.getDate());
}
Also used : PropertiesMapper(com.tvd12.properties.file.mapping.PropertiesMapper) Properties(java.util.Properties) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 14 with PropertiesMapper

use of com.tvd12.properties.file.mapping.PropertiesMapper in project properties-file by tvd12.

the class PropertiesMapperTest method testMapPropertiesToBeanWithFields.

@Test
public void testMapPropertiesToBeanWithFields() {
    Properties properties = new Properties();
    properties.setProperty("name", "hello");
    properties.put("age", 24);
    properties.put("clazz", ClassC.class.getName());
    properties.put("date", new SimpleDateFormat(Dates.getPattern()).format(new Date()));
    properties.put("datasource.username", "hello");
    properties.put("datasource.password", "world");
    properties.put("cors_config-detail.enable", true);
    properties.put("cors_config-detail.host", "*");
    properties.put("corsConfigDetailEnable", true);
    ClassC object = new PropertiesMapper().data(PropertiesUtil.toMap(properties)).map(ClassC.class);
    assertEquals(object.name, "hello");
    assertEquals(object.age, 24);
    assertEquals(object.money, 10);
    assertEquals(object.clazz, ClassC.class);
    assertEquals(object.datasourceUsername, "hello");
    assertNotNull(object.date);
    assertEquals(object.dataSourceConfig.username, "hello");
    assertEquals(object.dataSourceConfig.password, "world");
    Properties dataSourceProperties = PropertiesUtil.getPropertiesByPrefix(properties, "datasource");
    assertEquals(object.dataSourceProperties, dataSourceProperties);
    System.out.println("dataSourceProperties: " + dataSourceProperties);
    assert object.corsConfigDetail.enable;
    assert object.corsConfigDetail.host.equals("*");
}
Also used : PropertiesMapper(com.tvd12.properties.file.mapping.PropertiesMapper) Properties(java.util.Properties) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 15 with PropertiesMapper

use of com.tvd12.properties.file.mapping.PropertiesMapper in project properties-file by tvd12.

the class PropertiesMapper3Test method test2.

@Test
public void test2() {
    Properties properties = new Properties();
    properties.setProperty("valueC", "c");
    properties.setProperty("valueD", "d");
    properties.setProperty("hello", "galaxy");
    ClassD obj = new PropertiesMapper().clazz(ClassD.class).context(getClass()).data(properties).map();
    assertEquals(obj.getValueC(), "c");
    assertEquals(obj.getValueD(), "d");
}
Also used : PropertiesMapper(com.tvd12.properties.file.mapping.PropertiesMapper) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Aggregations

Properties (java.util.Properties)26 PropertiesMapper (com.tvd12.properties.file.mapping.PropertiesMapper)25 Test (org.testng.annotations.Test)24 SimpleDateFormat (java.text.SimpleDateFormat)12 Date (java.util.Date)12 BaseFileReader (com.tvd12.properties.file.reader.BaseFileReader)3 Map (java.util.Map)3 Property (com.tvd12.properties.file.annotation.Property)2 PropertyAnnotation (com.tvd12.properties.file.annotation.PropertyAnnotation)2 Dates (com.tvd12.properties.file.io.Dates)2 MappingLevel (com.tvd12.properties.file.mapping.MappingLevel)2 PropertiesUtil (com.tvd12.properties.file.util.PropertiesUtil)2 Data (lombok.Data)2 Getter (lombok.Getter)2 Setter (lombok.Setter)2 Assert.assertEquals (org.testng.Assert.assertEquals)2 Assert.assertNotNull (org.testng.Assert.assertNotNull)2 MultiFileReader (com.tvd12.properties.file.reader.MultiFileReader)1 PropertiesUtil.toProperties (com.tvd12.properties.file.util.PropertiesUtil.toProperties)1 HashMap (java.util.HashMap)1