Search in sources :

Example 21 with ListCollector

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

the class CsvMapperBuilderSubObjectTest method testMapDbFinalObject.

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

Example 22 with ListCollector

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

the class JdbcMapperFactoryTest method testFieldErrorHandling.

@Test
public void testFieldErrorHandling() throws SQLException, Exception, ParseException {
    @SuppressWarnings("unchecked") FieldMapperErrorHandler<JdbcColumnKey> fieldMapperErrorHandler = mock(FieldMapperErrorHandler.class);
    final Exception exception = new Exception("Error!");
    JdbcMapper<DbObject> mapper = JdbcMapperFactoryHelper.asm().fieldMapperErrorHandler(fieldMapperErrorHandler).addCustomFieldMapper("id", new FieldMapper<ResultSet, DbObject>() {

        @Override
        public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> mappingContext) throws Exception {
            throw exception;
        }
    }).newBuilder(DbObject.class).addMapping("id").mapper();
    List<DbObject> list = mapper.forEach(new MockDbObjectResultSet(1), 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) MappingContext(org.simpleflatmapper.map.MappingContext) ListCollector(org.simpleflatmapper.util.ListCollector) DbObject(org.simpleflatmapper.test.beans.DbObject) FieldMapper(org.simpleflatmapper.map.FieldMapper) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 23 with ListCollector

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

the class JdbcMapperListTest method testIssue458.

private void testIssue458(boolean useAsm) throws SQLException {
    JdbcMapper<Col> mapper = JdbcMapperFactory.newInstance().useAsm(useAsm).addKeys("id", "photos_name", "videos_name").newBuilder(Col.class).addMapping("id", 1, 4).addMapping("name", 2, 12).addMapping("photos_name", 3, 12).addMapping("videos_name", 4, 12).mapper();
    Connection dbConnection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
    if (dbConnection == null) {
        return;
    }
    Statement statement = dbConnection.createStatement();
    ResultSet rs = statement.executeQuery("with \n" + "\tcols(id, name, photos, videos) " + "AS (VALUES(2, 'trees', 'some_photo.jpg', 'videos'), " + "(2, 'trees', 'some_photo2.jpg', 'videos'), " + "(4, 'flowers', null, 'some_videos.mpg'))\n" + "select id, name, photos, videos\n" + "from cols\n" + "order by id");
    List<Col> list = mapper.forEach(rs, new ListCollector<Col>()).getList();
    assertEquals(2, list.size());
    assertEquals(2, list.get(0).id);
    assertEquals("trees", list.get(0).name);
    assertEquals(Arrays.asList("some_photo.jpg", "some_photo2.jpg"), list.get(0).photos);
    assertEquals(Arrays.asList("videos"), list.get(0).videos);
    assertEquals(4, list.get(1).id);
    assertEquals("flowers", list.get(1).name);
    assertEquals(Arrays.asList(), list.get(1).photos);
    assertEquals(Arrays.asList("some_videos.mpg"), list.get(1).videos);
    dbConnection.close();
}
Also used : ListCollector(org.simpleflatmapper.util.ListCollector) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet)

Example 24 with ListCollector

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

the class DiscriminatorMapperTest method testMapStudentProfessor.

@Test
public void testMapStudentProfessor() {
    List<Person> list = mapper.forEach(new Object[][] { { "student", "sname", 1 }, { "professor", "pname", 2 } }, new ListCollector<Person>()).getList();
    assertEquals(2, list.size());
    StudentGS studentGS = (StudentGS) list.get(0);
    ProfessorGS professorGS = (ProfessorGS) list.get(1);
    assertEquals("sname", studentGS.getName());
    assertEquals("pname", professorGS.getName());
}
Also used : ProfessorGS(org.simpleflatmapper.test.beans.ProfessorGS) ListCollector(org.simpleflatmapper.util.ListCollector) Person(org.simpleflatmapper.test.beans.Person) StudentGS(org.simpleflatmapper.test.beans.StudentGS) Test(org.junit.Test)

Example 25 with ListCollector

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

the class JoinJdbcMapperTest method validateMapper.

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

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