use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.
the class TestGuavaCollectors method testMultimapCollector.
@SuppressWarnings("unchecked")
private <M extends Multimap<Long, String>> void testMultimapCollector(Class<? extends Multimap> erasedType, GenericType<M> genericType) {
JdbiCollectors registry = dbRule.getJdbi().getConfig(JdbiCollectors.class);
assertThat(registry.findElementTypeFor(genericType.getType())).contains(new GenericType<Map.Entry<Long, String>>() {
}.getType());
Collector<Map.Entry<Long, String>, ?, M> collector = (Collector<Map.Entry<Long, String>, ?, M>) registry.findFor(genericType.getType()).orElseThrow(() -> new IllegalStateException("Missing collector for " + genericType));
M map = Stream.of(entry(1L, "foo"), entry(2L, "bar")).collect(collector);
assertThat(map).isInstanceOf(erasedType).containsAllEntriesOf(ImmutableMultimap.of(1L, "foo", 2L, "bar"));
}
use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.
the class TestGuavaOptional method testDynamicBindOptionalEmpty.
@Test
public void testDynamicBindOptionalEmpty() throws Exception {
List<Something> result = handle.createQuery(SELECT_BY_NAME).bindByType("name", Optional.absent(), new GenericType<Optional<String>>() {
}).mapToBean(Something.class).list();
assertThat(result).containsExactly(new Something(1, "eric"), new Something(2, "brian"));
}
use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.
the class TestVavrMapCollectorWithDB method testNonUniqueIndex_withMultimap.
@Test
public void testNonUniqueIndex_withMultimap() {
Handle h = dbRule.getSharedHandle();
h.execute("create table user (id int, name varchar)");
h.prepareBatch("insert into user (id, name) values (?, ?)").add(1, "alice").add(2, "bob").add(3, "alice").execute();
Multimap<String, User> usersByName = h.createQuery("select * from user").setMapKeyColumn("name").registerRowMapper(ConstructorMapper.factory(User.class)).collectInto(new GenericType<Multimap<String, User>>() {
});
assertThat(usersByName.apply("alice")).hasSize(2).containsExactly(new User(1, "alice"), new User(3, "alice"));
assertThat(usersByName.apply("bob")).hasSize(1).containsExactly(new User(2, "bob"));
}
use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.
the class TestVavrMapCollectorWithDB method uniqueIndex.
/**
* from {@link org.jdbi.v3.core.mapper.MapEntryMapperTest}
*/
@Test
public void uniqueIndex() {
Handle h = dbRule.getSharedHandle();
h.execute("create table user (id int, name varchar)");
h.prepareBatch("insert into user (id, name) values (?, ?)").add(1, "alice").add(2, "bob").add(3, "cathy").add(4, "dilbert").execute();
Map<Integer, User> map = h.createQuery("select * from user").setMapKeyColumn("id").registerRowMapper(ConstructorMapper.factory(User.class)).collectInto(new GenericType<Map<Integer, User>>() {
});
assertThat(map).containsOnly(Tuple.of(1, new User(1, "alice")), Tuple.of(2, new User(2, "bob")), Tuple.of(3, new User(3, "cathy")), Tuple.of(4, new User(4, "dilbert")));
}
use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.
the class TestVavrMapCollectorWithDB method testMapCollectorWithGlobalKeyValue_shouldSucceed.
@Test
public void testMapCollectorWithGlobalKeyValue_shouldSucceed() {
Jdbi jdbiWithKeyColAndValCol = dbRule.getJdbi().setMapKeyColumn("key_c").setMapValueColumn("val_c");
Boolean executed = jdbiWithKeyColAndValCol.withHandle(h -> {
HashMap<String, String> valueMap = h.createQuery("select val_c, key_c from keyval").collectInto(new GenericType<HashMap<String, String>>() {
});
assertThat(valueMap).containsOnlyElementsOf(expectedMap);
return true;
});
assertTrue(executed);
}
Aggregations