Search in sources :

Example 1 with PropertyAnnotations

use of com.tvd12.properties.file.annotation.PropertyAnnotations in project properties-file by tvd12.

the class PropertiesBeanTest method test.

@Test
public void test() {
    new PropertiesBean(ClassA.class);
    ClassA classA = new ClassA();
    PropertiesBean mapping2 = new PropertiesBean(classA, new PropertyAnnotations());
    mapping2.put("charValue", "c");
    mapping2.put("byteValue", "1");
    mapping2.put("doubleValue", "2.0");
    mapping2.put("floatValue", "3.0");
    mapping2.put("longValue", "4");
    mapping2.put("shortValue", "5");
    mapping2.put("not found", "not found");
    assertEquals(classA.getCharValue(), 'c');
    assertEquals(classA.getByteValue(), 1);
    assertEquals(classA.getDoubleValue(), 2.0D);
    assertEquals(classA.getFloatValue(), 3.0F);
    assertEquals(classA.getLongValue(), 4);
    assertEquals(classA.getShortValue(), 5);
    Properties dataSourceProperties = new Properties();
    dataSourceProperties.put("datasource.username", "hello");
    dataSourceProperties.put("datasource.password", "world");
    PropertiesBean mapping3 = new PropertiesBean(classA, new PropertyAnnotations());
    mapping3.put("dataSourceConfig", null, dataSourceProperties);
    assert classA.dataSourceConfig.username.equals("hello");
    assert classA.dataSourceConfig.password.equals("world");
}
Also used : PropertyAnnotations(com.tvd12.properties.file.annotation.PropertyAnnotations) PropertiesBean(com.tvd12.properties.file.struct.PropertiesBean) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Example 2 with PropertyAnnotations

use of com.tvd12.properties.file.annotation.PropertyAnnotations in project properties-file by tvd12.

the class PropertiesMapper method map.

@SuppressWarnings({ "unchecked" })
public <T> T map(Class<T> clazz, Type genericType) {
    if (clazz == null && bean == null) {
        throw new IllegalArgumentException("there is nothing to map, plase provice clazz, or bean");
    }
    this.clazz(clazz);
    this.readProperties();
    if (propertyAnnotations == null) {
        propertyAnnotations = new PropertyAnnotations();
    }
    if (clazz == Map.class) {
        return (T) doMapToMapValue(genericType);
    } else if (clazz == Properties.class) {
        return (T) properties;
    } else {
        return (T) doMapValue();
    }
}
Also used : PropertyAnnotations(com.tvd12.properties.file.annotation.PropertyAnnotations) Properties(java.util.Properties) PropertiesUtil.toProperties(com.tvd12.properties.file.util.PropertiesUtil.toProperties)

Example 3 with PropertyAnnotations

use of com.tvd12.properties.file.annotation.PropertyAnnotations in project properties-file by tvd12.

the class PropertiesBeanTest method test.

@Test
public void test() {
    new PropertiesBean(ClassA.class);
    ClassA classA = new ClassA();
    PropertiesBean mapping2 = new PropertiesBean(classA, new PropertyAnnotations());
    mapping2.put("charValue", "c");
    mapping2.put("byteValue", "1");
    mapping2.put("doubleValue", "2.0");
    mapping2.put("floatValue", "3.0");
    mapping2.put("longValue", "4");
    mapping2.put("shortValue", "5");
    mapping2.put("not found", "not found");
    assertEquals(classA.getCharValue(), 'c');
    assertEquals(classA.getByteValue(), 1);
    assertEquals(classA.getDoubleValue(), 2.0D);
    assertEquals(classA.getFloatValue(), 3.0F);
    assertEquals(classA.getLongValue(), 4);
    assertEquals(classA.getShortValue(), 5);
    Properties dataSourceProperties = new Properties();
    dataSourceProperties.put("datasource.username", "hello");
    dataSourceProperties.put("datasource.password", "world");
    PropertiesBean mapping3 = new PropertiesBean(classA, new PropertyAnnotations());
    mapping3.put("dataSourceConfig", null, dataSourceProperties);
    assert classA.dataSourceConfig.username.equals("hello");
    assert classA.dataSourceConfig.password.equals("world");
}
Also used : PropertyAnnotations(com.tvd12.properties.file.annotation.PropertyAnnotations) PropertiesBean(com.tvd12.properties.file.struct.PropertiesBean) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Example 4 with PropertyAnnotations

use of com.tvd12.properties.file.annotation.PropertyAnnotations in project properties-file by tvd12.

the class SetterMethodTest method testInvalidCase.

@Test(expectedExceptions = { IllegalStateException.class })
public void testInvalidCase() {
    Method method = MethodBuilder.create().clazz(ClassA.class).method("setValues").argument(String.class).argument(String.class).build();
    SetterMethod setterMethod = new SetterMethod(new PropertyAnnotations());
    setterMethod.initWithMethod(method);
    setterMethod.invoke(new ClassA(), "123");
}
Also used : PropertyAnnotations(com.tvd12.properties.file.annotation.PropertyAnnotations) SetterMethod(com.tvd12.properties.file.struct.SetterMethod) SetterMethod(com.tvd12.properties.file.struct.SetterMethod) Method(java.lang.reflect.Method) Test(org.testng.annotations.Test)

Example 5 with PropertyAnnotations

use of com.tvd12.properties.file.annotation.PropertyAnnotations in project properties-file by tvd12.

the class SetterMethodTest method test.

@Test
public void test() {
    Method method = MethodBuilder.create().clazz(ClassA.class).method("setName").argument(String.class).build();
    SetterMethod setterMethod = new SetterMethod(new PropertyAnnotations());
    setterMethod.initWithMethod(method);
    setterMethod.invoke(new ClassA(), "123");
}
Also used : PropertyAnnotations(com.tvd12.properties.file.annotation.PropertyAnnotations) SetterMethod(com.tvd12.properties.file.struct.SetterMethod) SetterMethod(com.tvd12.properties.file.struct.SetterMethod) Method(java.lang.reflect.Method) Test(org.testng.annotations.Test)

Aggregations

PropertyAnnotations (com.tvd12.properties.file.annotation.PropertyAnnotations)9 Test (org.testng.annotations.Test)8 SetterMethod (com.tvd12.properties.file.struct.SetterMethod)6 Method (java.lang.reflect.Method)4 Properties (java.util.Properties)4 PropertiesBean (com.tvd12.properties.file.struct.PropertiesBean)2 PropertiesUtil.toProperties (com.tvd12.properties.file.util.PropertiesUtil.toProperties)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1