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("*");
}
use of com.tvd12.properties.file.mapping.PropertiesMapper in project properties-file by tvd12.
the class PropertiesMapperTest method testMapPropertiesToBean.
@Test
public void testMapPropertiesToBean() {
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(properties).clazz(ClassA.class).map();
assertEquals(object.getName(), "hello");
assertEquals(object.getAge(), 24);
assertEquals(object.getMoney(), 10);
assertEquals(object.getClazz(), ClassA.class);
assertNotNull(object.getDate());
}
use of com.tvd12.properties.file.mapping.PropertiesMapper 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);
}
use of com.tvd12.properties.file.mapping.PropertiesMapper in project properties-file by tvd12.
the class PropertiesMapperTest method mapToMapValueWithNoGenericType.
@Test
public void mapToMapValueWithNoGenericType() {
// given
Properties properties = new Properties();
properties.put("host", "0.0.0.0");
// when
Object actual = new PropertiesMapper().data(properties).map(Map.class, null);
// then
assertEquals(actual, properties);
}
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;
}
Aggregations