use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.
the class TestGuavaCollectors method testMapCollector.
@SuppressWarnings("unchecked")
private <M extends Map<Long, String>> void testMapCollector(Class<? extends Map> 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).containsExactly(entry(1L, "foo"), entry(2L, "bar"));
}
use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.
the class TestGuavaOptional method testDynamicBindOptionalPresent.
@Test
public void testDynamicBindOptionalPresent() throws Exception {
Something result = handle.createQuery(SELECT_BY_NAME).bindByType("name", Optional.of("eric"), new GenericType<Optional<String>>() {
}).mapToBean(Something.class).findOnly();
assertThat(result).isEqualTo(new Something(1, "eric"));
}
use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.
the class BuiltInCollectorFactoryTest method testCollectionType.
private <C extends Collection<String>> void testCollectionType(GenericType<C> genericType) {
Type containerType = genericType.getType();
Class<?> erasedType = getErasedType(containerType);
assertThat(factory.accepts(containerType)).isTrue();
assertThat(factory.accepts(erasedType)).isFalse();
assertThat(factory.elementType(containerType)).contains(String.class);
Collector<String, ?, C> collector = (Collector<String, ?, C>) factory.build(containerType);
assertThat(Stream.of("foo", "bar", "baz").collect(collector)).isInstanceOf(erasedType).containsOnly("foo", "bar", "baz");
}
use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.
the class BuiltInCollectorFactoryTest method testMapType.
private <M extends Map<Long, String>> void testMapType(GenericType<M> genericType) {
Type containerType = genericType.getType();
Class<?> erasedType = getErasedType(containerType);
assertThat(factory.accepts(containerType)).isTrue();
assertThat(factory.accepts(erasedType)).isFalse();
assertThat(factory.elementType(containerType)).contains(resolveMapEntryType(Long.class, String.class));
Collector<Map.Entry<Long, String>, ?, M> collector = (Collector<Map.Entry<Long, String>, ?, M>) factory.build(containerType);
assertThat(Stream.of(entry(1L, "foo"), entry(2L, "bar"), entry(3L, "baz")).collect(collector)).isInstanceOf(erasedType).containsOnly(entry(1L, "foo"), entry(2L, "bar"), entry(3L, "baz"));
assertThatThrownBy(() -> Stream.of(entry(1L, "foo"), entry(1L, "bar")).collect(collector)).isInstanceOf(IllegalStateException.class).hasMessageContaining("Multiple values");
}
use of org.jdbi.v3.core.generic.GenericType in project jdbi by jdbi.
the class BuiltInCollectorFactoryTest method optional.
@Test
public void optional() {
Type optionalString = new GenericType<Optional<String>>() {
}.getType();
assertThat(factory.accepts(optionalString)).isTrue();
assertThat(factory.accepts(Optional.class)).isFalse();
assertThat(factory.elementType(optionalString)).contains(String.class);
Collector<String, ?, Optional<String>> collector = (Collector<String, ?, Optional<String>>) factory.build(optionalString);
assertThat(Stream.<String>empty().collect(collector)).isEmpty();
assertThat(Stream.of("foo").collect(collector)).contains("foo");
assertThatThrownBy(() -> Stream.of("foo", "bar").collect(collector)).isInstanceOf(IllegalStateException.class).hasMessageContaining("Multiple values");
}
Aggregations