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());
}
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());
}
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);
}
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);
}
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));
}
Aggregations