Search in sources :

Example 6 with ListCollector

use of org.simpleflatmapper.util.ListCollector 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 7 with ListCollector

use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.

the class CsvMapperBuilderSubObjectTest method testMapDbPartialObjectNoAsm.

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

Example 8 with ListCollector

use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.

the class JoinJdbcMapperCustomKeyTest method test.

@Test
public void test() throws SQLException {
    JdbcMapper<Person> mapper = JdbcMapperFactory.newInstance().addColumnDefinition(new Predicate<JdbcColumnKey>() {

        @Override
        public boolean test(JdbcColumnKey jdbcColumnKey) {
            return jdbcColumnKey.getIndex() <= 2;
        }
    }, FieldMapperColumnDefinition.<JdbcColumnKey>key(new Predicate<PropertyMeta<?, ?>>() {

        @Override
        public boolean test(PropertyMeta<?, ?> propertyMeta) {
            return propertyMeta.getPath().startsWith("key.");
        }
    })).newBuilder(Person.class).addMapping("key_tag").addMapping("key_n").addMapping("name").addMapping("phones_value").mapper();
    ResultSet rs = JoinJdbcMapperOnTupleTest.setUpResultSetMock(new String[] { "key_tag", "key_n", "name", "phones_value" }, new Object[][] { { "t1", 1, "t11", "p111" }, { "t1", 1, "t11", "p112" }, { "t1", 2, "t12", "p121" } });
    final List<Person> list = mapper.forEach(rs, new ListCollector<Person>()).getList();
    assertEquals(2, list.size());
    assertEquals("t1", list.get(0).key.tag);
    assertEquals(1, list.get(0).key.n);
    assertEquals("t11", list.get(0).name);
    assertEquals(Arrays.asList("p111", "p112"), list.get(0).phones);
    assertEquals("t1", list.get(1).key.tag);
    assertEquals(2, list.get(1).key.n);
    assertEquals("t12", list.get(1).name);
    assertEquals(Arrays.asList("p121"), list.get(1).phones);
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) ListCollector(org.simpleflatmapper.util.ListCollector) ResultSet(java.sql.ResultSet) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) Predicate(org.simpleflatmapper.util.Predicate) Test(org.junit.Test)

Example 9 with ListCollector

use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.

the class DiscriminatorJdbcMapperTest method validateMapper.

private <T extends Person> void validateMapper(JdbcMapper<T> mapper) throws Exception {
    List<T> persons = mapper.forEach(setUpResultSetMock(), new ListCollector<T>()).getList();
    validatePersons(persons);
    // IFJAVA8_START
    validatePersons(mapper.stream(setUpResultSetMock()).collect(Collectors.<T>toList()));
    final Stream<T> stream = mapper.stream(setUpResultSetMock());
    validatePersons(stream.limit(10).collect(Collectors.<T>toList()));
    // IFJAVA8_END
    Iterator<T> iterator = mapper.iterator(setUpResultSetMock());
    persons = new ArrayList<T>();
    while (iterator.hasNext()) {
        persons.add(iterator.next());
    }
    validatePersons(persons);
    ResultSet rs = setUpResultSetMock();
    rs.next();
    MappingContext<? super ResultSet> mappingContext = mapper.newMappingContext(rs);
    mappingContext.broke(rs);
    final T professor = mapper.map(rs, mappingContext);
    validateProfessorMap((Professor) professor);
    rs.next();
    mappingContext.broke(rs);
    rs.next();
    mappingContext.broke(rs);
    mapper.mapTo(rs, professor, mappingContext);
    validateProfessorMapTo((Professor) professor);
}
Also used : ListCollector(org.simpleflatmapper.util.ListCollector) ResultSet(java.sql.ResultSet)

Example 10 with ListCollector

use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.

the class JdbcMapperFactoryTest method testFieldErrorHandlingOnResultSet.

@Test
public void testFieldErrorHandlingOnResultSet() throws SQLException, Exception, ParseException {
    @SuppressWarnings("unchecked") FieldMapperErrorHandler<JdbcColumnKey> fieldMapperErrorHandler = mock(FieldMapperErrorHandler.class);
    ResultSet rs = mock(ResultSet.class);
    final Exception exception = new SQLException("Error!");
    JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().fieldMapperErrorHandler(fieldMapperErrorHandler).newBuilder(DbObject.class).addMapping("id").mapper();
    when(rs.next()).thenReturn(true, false);
    when(rs.getLong(1)).thenThrow(exception);
    List<DbObject> list = mapper.forEach(rs, new ListCollector<DbObject>()).getList();
    assertNotNull(list.get(0));
    verify(fieldMapperErrorHandler).errorMappingField(eq(new JdbcColumnKey("id", 1)), any(), same(list.get(0)), same(exception));
}
Also used : JdbcColumnKey(org.simpleflatmapper.jdbc.JdbcColumnKey) ListCollector(org.simpleflatmapper.util.ListCollector) DbObject(org.simpleflatmapper.test.beans.DbObject) ParseException(java.text.ParseException) Test(org.junit.Test)

Aggregations

ListCollector (org.simpleflatmapper.util.ListCollector)26 Test (org.junit.Test)22 StringReader (java.io.StringReader)10 ResultSet (java.sql.ResultSet)6 CsvMapperFactory (org.simpleflatmapper.csv.CsvMapperFactory)5 JoinTest (org.simpleflatmapper.test.jdbc.JoinTest)5 CsvMapperBuilder (org.simpleflatmapper.csv.CsvMapperBuilder)4 JdbcColumnKey (org.simpleflatmapper.jdbc.JdbcColumnKey)4 DbObject (org.simpleflatmapper.test.beans.DbObject)4 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 ProfessorGS (org.simpleflatmapper.test.beans.ProfessorGS)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 Statement (java.sql.Statement)2 DateFormatProperty (org.simpleflatmapper.map.property.DateFormatProperty)2 Db1DeepPartialObject (org.simpleflatmapper.test.beans.Db1DeepPartialObject)2 ProfessorC (org.simpleflatmapper.test.beans.ProfessorC)2 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1