Search in sources :

Example 16 with PropertiesMapper

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);
}
Also used : PropertiesMapper(com.tvd12.properties.file.mapping.PropertiesMapper) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Example 17 with PropertiesMapper

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

the class PropertiesMapperTest method mapToMapValueWithInvalidGenericType.

@Test
public void mapToMapValueWithInvalidGenericType() {
    // given
    Properties properties = new Properties();
    properties.put("host", "0.0.0.0");
    // when
    Object actual = new PropertiesMapper().data(properties).map(Map.class, String.class);
    // then
    assertEquals(actual, properties);
}
Also used : PropertiesMapper(com.tvd12.properties.file.mapping.PropertiesMapper) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Example 18 with PropertiesMapper

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());
}
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 19 with PropertiesMapper

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

the class PropertiesMapperTest method testWithNoClassCase.

@Test
public void testWithNoClassCase() {
    Properties properties = new Properties();
    Object output = new PropertiesMapper().data(properties).map();
    assert properties == output;
}
Also used : PropertiesMapper(com.tvd12.properties.file.mapping.PropertiesMapper) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Example 20 with PropertiesMapper

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

the class PropertiesMapper method doMapToMapValue.

@SuppressWarnings({ "unchecked", "rawtypes" })
private Map doMapToMapValue(Type genericType) {
    if (genericType == null) {
        return properties;
    }
    Class<?> mapValueType = ReflectionGenericUtil.getTwoGenericClassArguments(genericType)[1];
    if (mapValueType == null) {
        return properties;
    }
    Map answer = new HashMap<>();
    Map<String, Properties> propertiesMap = PropertiesUtil.getPropertiesMap(properties);
    for (String key : propertiesMap.keySet()) {
        Object value = new PropertiesMapper().data(properties).propertyPrefix(key).classLoader(classLoader).valueConverter(valueConverter).propertyAnnotations(propertyAnnotations).map(mapValueType);
        answer.put(key, value);
    }
    return answer;
}
Also used : HashMap(java.util.HashMap) Properties(java.util.Properties) PropertiesUtil.toProperties(com.tvd12.properties.file.util.PropertiesUtil.toProperties) HashMap(java.util.HashMap) Map(java.util.Map)

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