Search in sources :

Example 1 with Db1DeepObject

use of org.simpleflatmapper.test.beans.Db1DeepObject in project SimpleFlatMapper by arnaudroger.

the class CsvMapperBuilderSubObjectTest method testMapDbObject.

@Test
public void testMapDbObject() throws Exception {
    CsvMapperBuilder<Db1DeepObject> builder = new CsvMapperBuilder<Db1DeepObject>(Db1DeepObject.class, ReflectionService.newInstance(false));
    addDbObjectFields(builder);
    CsvMapper<Db1DeepObject> mapper = builder.mapper();
    List<Db1DeepObject> list = mapper.forEach(db1deepObjectCsvReader(), new ListCollector<Db1DeepObject>()).getList();
    assertEquals(1, list.size());
    Db1DeepObject o = list.get(0);
    assertEquals(1234, o.getId());
    assertEquals("val!", o.getValue());
    DbHelper.assertDbObjectMapping(o.getDbObject());
}
Also used : Db1DeepObject(org.simpleflatmapper.test.beans.Db1DeepObject) ListCollector(org.simpleflatmapper.util.ListCollector) CsvMapperBuilder(org.simpleflatmapper.csv.CsvMapperBuilder) Test(org.junit.Test)

Example 2 with Db1DeepObject

use of org.simpleflatmapper.test.beans.Db1DeepObject in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperSubObjectTest method testMapInnerObjectWithColumnDefinition.

@Test
public void testMapInnerObjectWithColumnDefinition() throws Exception {
    JdbcMapperBuilder<Db1DeepObject> builder = JdbcMapperFactoryHelper.asm().newBuilder(Db1DeepObject.class);
    FieldMapperColumnDefinition<JdbcColumnKey> columnDefinition = FieldMapperColumnDefinition.customGetter(new Getter<ResultSet, String>() {

        @Override
        public String get(ResultSet target) throws Exception {
            return "ov1";
        }
    });
    builder.addMapping("db_object_name", columnDefinition);
    final JdbcMapper<Db1DeepObject> mapper = builder.mapper();
    ResultSet rs = mock(ResultSet.class);
    when(rs.getString(1)).thenReturn("name1");
    when(rs.next()).thenReturn(true, false);
    Db1DeepObject next = mapper.iterator(rs).next();
    assertEquals("ov1", next.getDbObject().getName());
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) Db1DeepObject(org.simpleflatmapper.test.beans.Db1DeepObject) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 3 with Db1DeepObject

use of org.simpleflatmapper.test.beans.Db1DeepObject in project SimpleFlatMapper by arnaudroger.

the class CsvMapperBuilderSubObjectTest method testMapDbObjectWithCustomReader.

@Test
public void testMapDbObjectWithCustomReader() throws Exception {
    CsvMapperBuilder<Db1DeepObject> builder = new CsvMapperBuilder<Db1DeepObject>(Db1DeepObject.class, ReflectionService.newInstance(false));
    CsvColumnDefinition columnDefinition = CsvColumnDefinition.customReaderDefinition(new CellValueReader<String>() {

        @Override
        public String read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return "cv1";
        }
    });
    builder.addMapping("db_Object_name", columnDefinition);
    CsvMapper<Db1DeepObject> mapper = builder.mapper();
    Db1DeepObject v1 = mapper.iterator(new StringReader("v1")).next();
    assertEquals("cv1", v1.getDbObject().getName());
}
Also used : Db1DeepObject(org.simpleflatmapper.test.beans.Db1DeepObject) ParsingContext(org.simpleflatmapper.csv.ParsingContext) CsvMapperBuilder(org.simpleflatmapper.csv.CsvMapperBuilder) StringReader(java.io.StringReader) CsvColumnDefinition(org.simpleflatmapper.csv.CsvColumnDefinition) Test(org.junit.Test)

Example 4 with Db1DeepObject

use of org.simpleflatmapper.test.beans.Db1DeepObject in project SimpleFlatMapper by arnaudroger.

the class SubPropertyMetaTest method testSubProperty.

@Test
@SuppressWarnings("unchecked")
public void testSubProperty() throws Exception {
    ClassMeta<Db1DeepObject> classMeta = ReflectionService.newInstance().getClassMeta(Db1DeepObject.class);
    PropertyMeta<Db1DeepObject, String> property = classMeta.newPropertyFinder(new Predicate<PropertyMeta<?, ?>>() {

        @Override
        public boolean test(PropertyMeta<?, ?> propertyMeta) {
            return true;
        }
    }).findProperty(new DefaultPropertyNameMatcher("dbObject_name", 0, false, false), new Object[0]);
    assertTrue(property instanceof SubPropertyMeta);
    assertTrue(property.isSubProperty());
    assertTrue(property.toString().startsWith("SubPropertyMeta{" + "ownerProperty=ObjectPropertyMeta{"));
    SubPropertyMeta<Db1DeepObject, DbObject, String> subPropertyMeta = (SubPropertyMeta<Db1DeepObject, DbObject, String>) property;
    assertEquals(String.class, subPropertyMeta.getPropertyType());
    assertEquals("dbObject.name", subPropertyMeta.getPath());
    Db1DeepObject object = new Db1DeepObject();
    object.setDbObject(new DbObject());
    subPropertyMeta.getSetter().set(object, "n1");
    Getter<Db1DeepObject, String> getter = subPropertyMeta.getGetter();
    assertEquals("n1", getter.get(object));
    Db1DeepObject objectNull = new Db1DeepObject();
    assertEquals(null, getter.get(objectNull));
    assertTrue(getter.toString().startsWith("GetterOnGetter{g1="));
    ClassMeta<?> meta = subPropertyMeta.getPropertyClassMeta();
    assertEquals(String.class, meta.getType());
}
Also used : Db1DeepObject(org.simpleflatmapper.test.beans.Db1DeepObject) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) DbObject(org.simpleflatmapper.test.beans.DbObject) DefaultPropertyNameMatcher(org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher) SubPropertyMeta(org.simpleflatmapper.reflect.meta.SubPropertyMeta) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) Predicate(org.simpleflatmapper.util.Predicate) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 Db1DeepObject (org.simpleflatmapper.test.beans.Db1DeepObject)4 CsvMapperBuilder (org.simpleflatmapper.csv.CsvMapperBuilder)2 StringReader (java.io.StringReader)1 ResultSet (java.sql.ResultSet)1 CsvColumnDefinition (org.simpleflatmapper.csv.CsvColumnDefinition)1 ParsingContext (org.simpleflatmapper.csv.ParsingContext)1 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)1 DefaultPropertyNameMatcher (org.simpleflatmapper.reflect.meta.DefaultPropertyNameMatcher)1 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)1 SubPropertyMeta (org.simpleflatmapper.reflect.meta.SubPropertyMeta)1 DbObject (org.simpleflatmapper.test.beans.DbObject)1 ListCollector (org.simpleflatmapper.util.ListCollector)1 Predicate (org.simpleflatmapper.util.Predicate)1