Search in sources :

Example 1 with MappingLevel

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

the class PropertiesMapperTest method testMapPropertiesToBeanWithAnntations.

@Test
public void testMapPropertiesToBeanWithAnntations() {
    Properties properties = new Properties();
    properties.setProperty("name", "hello");
    properties.put("value", "world");
    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("datasource.driver", "dahlia");
    ClassD object = new PropertiesMapper().data(PropertiesUtil.toMap(properties)).mappingLevel(MappingLevel.ANNOTATION).addPropertyAnnotation(new PropertyAnnotation(PropertyForTest.class, a -> ((PropertyForTest) a).value(), a -> ((PropertyForTest) a).prefix())).addPropertyAnnotation(new PropertyAnnotation(PropertyForTest.class, a -> ((PropertyForTest) a).value(), a -> ((PropertyForTest) a).prefix())).map(ClassD.class);
    assertEquals(object.name, "hello");
    assertEquals(object.value, "value");
    assertEquals(object.age, 24);
    assertEquals(object.money, 10);
    assertEquals(object.clazz, ClassC.class);
    assertEquals(object.datasourceDriver, "dahlia");
    assertNotNull(object.date);
    assertEquals(object.dataSourceConfig.username, "hello");
    assertEquals(object.dataSourceConfig.password, "world");
    assertEquals(object.dataSourceConfig.driverClass, "dahlia");
    Properties dataSourceProperties = PropertiesUtil.getPropertiesByPrefix(properties, "datasource");
    assertEquals(object.dataSourceProperties, dataSourceProperties);
    System.out.println("dataSourceProperties: " + dataSourceProperties);
}
Also used : Setter(lombok.Setter) Properties(java.util.Properties) Getter(lombok.Getter) Date(java.util.Date) BaseFileReader(com.tvd12.properties.file.reader.BaseFileReader) Assert.assertEquals(org.testng.Assert.assertEquals) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.testng.annotations.Test) Assert.assertNotNull(org.testng.Assert.assertNotNull) Property(com.tvd12.properties.file.annotation.Property) PropertiesMapper(com.tvd12.properties.file.mapping.PropertiesMapper) Dates(com.tvd12.properties.file.io.Dates) MappingLevel(com.tvd12.properties.file.mapping.MappingLevel) Map(java.util.Map) Data(lombok.Data) PropertyAnnotation(com.tvd12.properties.file.annotation.PropertyAnnotation) PropertiesUtil(com.tvd12.properties.file.util.PropertiesUtil) PropertiesMapper(com.tvd12.properties.file.mapping.PropertiesMapper) Properties(java.util.Properties) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) PropertyAnnotation(com.tvd12.properties.file.annotation.PropertyAnnotation) Test(org.testng.annotations.Test)

Example 2 with MappingLevel

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

the class PropertiesMapperTest method testMapPropertiesToBeanWithAnntations.

@Test
public void testMapPropertiesToBeanWithAnntations() {
    Properties properties = new Properties();
    properties.setProperty("name", "hello");
    properties.put("value", "world");
    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("datasource.driver", "dahlia");
    ClassD object = new PropertiesMapper().data(PropertiesUtil.toMap(properties)).mappingLevel(MappingLevel.ANNOTATION).addPropertyAnnotation(new PropertyAnnotation(PropertyForTest.class, a -> ((PropertyForTest) a).value(), a -> ((PropertyForTest) a).prefix())).addPropertyAnnotation(new PropertyAnnotation(PropertyForTest.class, a -> ((PropertyForTest) a).value(), a -> ((PropertyForTest) a).prefix())).map(ClassD.class);
    assertEquals(object.name, "hello");
    assertEquals(object.value, "value");
    assertEquals(object.age, 24);
    assertEquals(object.money, 10);
    assertEquals(object.clazz, ClassC.class);
    assertEquals(object.datasourceDriver, "dahlia");
    assertNotNull(object.date);
    assertEquals(object.dataSourceConfig.username, "hello");
    assertEquals(object.dataSourceConfig.password, "world");
    assertEquals(object.dataSourceConfig.driverClass, "dahlia");
    Properties dataSourceProperties = PropertiesUtil.getPropertiesByPrefix(properties, "datasource");
    assertEquals(object.dataSourceProperties, dataSourceProperties);
    System.out.println("dataSourceProperties: " + dataSourceProperties);
}
Also used : Setter(lombok.Setter) Properties(java.util.Properties) Getter(lombok.Getter) Date(java.util.Date) BaseFileReader(com.tvd12.properties.file.reader.BaseFileReader) Assert.assertEquals(org.testng.Assert.assertEquals) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.testng.annotations.Test) Assert.assertNotNull(org.testng.Assert.assertNotNull) Property(com.tvd12.properties.file.annotation.Property) PropertiesMapper(com.tvd12.properties.file.mapping.PropertiesMapper) Dates(com.tvd12.properties.file.io.Dates) MappingLevel(com.tvd12.properties.file.mapping.MappingLevel) Map(java.util.Map) Data(lombok.Data) PropertyAnnotation(com.tvd12.properties.file.annotation.PropertyAnnotation) PropertiesUtil(com.tvd12.properties.file.util.PropertiesUtil) PropertiesMapper(com.tvd12.properties.file.mapping.PropertiesMapper) Properties(java.util.Properties) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) PropertyAnnotation(com.tvd12.properties.file.annotation.PropertyAnnotation) Test(org.testng.annotations.Test)

Aggregations

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 PropertiesMapper (com.tvd12.properties.file.mapping.PropertiesMapper)2 BaseFileReader (com.tvd12.properties.file.reader.BaseFileReader)2 PropertiesUtil (com.tvd12.properties.file.util.PropertiesUtil)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Map (java.util.Map)2 Properties (java.util.Properties)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 Test (org.testng.annotations.Test)2