use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.
the class JdbcMapperTupleTest method testIssue450.
@Test
public void testIssue450() throws Exception {
JdbcMapper<Tuple2<Integer, List<Privilege>>> mapper = JdbcMapperFactory.newInstance().reflectionService(ReflectionService.newInstance().withSelfScoreFullName(true)).addKeys("resource_id", "id").newBuilder(new TypeReference<Tuple2<Integer, List<Privilege>>>() {
}).addMapping("id", 1, 4).addMapping("name", 2, 12).addMapping("resource_id", 3, 4).mapper();
Connection dbConnection = DbHelper.getDbConnection(DbHelper.TargetDB.POSTGRESQL);
if (dbConnection == null) {
return;
}
Statement statement = dbConnection.createStatement();
ResultSet rs = statement.executeQuery("with \n" + "\tresource_privileges(resource_id, privilege_id) AS (VALUES(1, 10), (2, 10), (3, 11)),\n" + "\tprivilege (id, name) as (values(10, 'write'), ('11', 'read'))\n" + "select privilege.id, privilege.name, resource_privileges.resource_id\n" + "from privilege join resource_privileges on resource_privileges.privilege_id = privilege.id\n" + "order by resource_privileges.resource_id");
List<Tuple2<Integer, List<Privilege>>> list = mapper.forEach(rs, new ListCollector<Tuple2<Integer, List<Privilege>>>()).getList();
assertEquals(2, list.size());
assertEquals((Integer) 10, list.get(0).first());
assertEquals((Integer) 11, list.get(1).first());
assertEquals(2, list.get(0).second().size());
assertEquals(1, list.get(1).second().size());
dbConnection.close();
}
use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.
the class JoinSample method stackOverFlowJoin.
/**
* Test for https://arnaudroger.github.io/blog/2017/02/24/jooq-one-to-many.html
*
* To avoid having to mock the result set metadata I used a static mapper here. In production code
* you can just call
* newMapper(Location.class)
* instead of newBuilder()...mapper()
*
* @throws SQLException
*/
@Test
public void stackOverFlowJoin() throws SQLException {
JdbcMapper<Location> mapper = JdbcMapperFactory.newInstance().addKeys("player").newBuilder(Location.class).addMapping(new JdbcColumnKey("name", 1, Types.VARCHAR)).addMapping(new JdbcColumnKey("player", 2, Types.VARCHAR)).addMapping(new JdbcColumnKey("invited_players_player", 3, Types.VARCHAR)).mapper();
UUID[] players = new UUID[] { UUID.randomUUID(), UUID.randomUUID() };
UUID[] invitedPlayers = new UUID[] { UUID.randomUUID(), UUID.randomUUID(), UUID.randomUUID() };
String[] name = new String[] { "location1", "location2" };
ResultSet rs = mock(ResultSet.class);
when(rs.next()).thenReturn(true, true, true, false);
when(rs.getString(1)).thenReturn(name[0], name[1]);
when(rs.getString(2)).thenReturn(players[0].toString(), players[1].toString());
when(rs.getObject(2)).thenReturn(players[0].toString(), players[0].toString(), players[0].toString(), players[0].toString(), players[1].toString(), players[1].toString());
when(rs.getString(3)).thenReturn(invitedPlayers[0].toString(), invitedPlayers[1].toString(), invitedPlayers[2].toString());
List<Location> list = mapper.forEach(rs, new ListCollector<Location>()).getList();
assertEquals(2, list.size());
assertEquals("location1", list.get(0).getName());
assertEquals(players[0], list.get(0).getPlayer());
assertEquals(Arrays.asList(invitedPlayers[0], invitedPlayers[1]), list.get(0).getInvitedPlayers());
assertEquals("location2", list.get(1).getName());
assertEquals(players[1], list.get(1).getPlayer());
assertEquals(Arrays.asList(invitedPlayers[2]), list.get(1).getInvitedPlayers());
}
use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.
the class CsvParserTest method testNoUnescaping.
@Test
public void testNoUnescaping() throws IOException {
StringReader stringReader = new StringReader("test,\" \"\"hello\"\" \"\n# this a comment, not data");
List<String[]> data = CsvParser.dsl().disableUnescaping().forEach(stringReader, new ListCollector<String[]>()).getList();
assertEquals(2, data.size());
assertArrayEquals(new String[] { "test", "\" \"\"hello\"\" \"" }, data.get(0));
assertArrayEquals(new String[] { "# this a comment", " not data" }, data.get(1));
}
use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.
the class CsvMapperBuilderTest method testMapDbObjectWithColumnIndex.
@Test
public void testMapDbObjectWithColumnIndex() throws Exception {
CsvMapperBuilder<DbObject> builder = csvMapperFactory.newBuilder(DbObject.class);
builder.addMapping("email", 2);
CsvMapper<DbObject> mapper = builder.mapper();
List<DbObject> list = mapper.forEach(CsvMapperImplTest.dbObjectCsvReader(), new ListCollector<DbObject>()).getList();
assertEquals(1, list.size());
DbObject o = list.get(0);
assertEquals(0, o.getId());
assertNull(o.getName());
assertEquals("name1@mail.com", o.getEmail());
assertNull(o.getCreationTime());
assertNull(o.getTypeName());
assertNull(o.getTypeOrdinal());
}
use of org.simpleflatmapper.util.ListCollector in project SimpleFlatMapper by arnaudroger.
the class CsvMapperBuilderTest method testMapDbObjectWithWrongColumnStillMapGoodOne.
@Test
public void testMapDbObjectWithWrongColumnStillMapGoodOne() throws Exception {
CsvMapperBuilder<DbObject> builder = csvMapperFactory.mapperBuilderErrorHandler(MapperBuilderErrorHandler.NULL).newBuilder(DbObject.class);
builder.addMapping("no_id");
builder.addMapping("no_name");
builder.addMapping("email");
CsvMapper<DbObject> mapper = builder.mapper();
List<DbObject> list = mapper.forEach(CsvMapperImplTest.dbObjectCsvReader(), new ListCollector<DbObject>()).getList();
assertEquals(1, list.size());
DbObject o = list.get(0);
assertEquals(0, o.getId());
assertNull(o.getName());
assertEquals("name1@mail.com", o.getEmail());
assertNull(o.getCreationTime());
assertNull(o.getTypeName());
assertNull(o.getTypeOrdinal());
}
Aggregations