Search in sources :

Example 6 with GenericType

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"));
}
Also used : GenericType(org.jdbi.v3.core.generic.GenericType) JdbiCollectors(org.jdbi.v3.core.collector.JdbiCollectors) Collector(java.util.stream.Collector) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 7 with GenericType

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"));
}
Also used : Optional(com.google.common.base.Optional) Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 8 with GenericType

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");
}
Also used : GenericType(org.jdbi.v3.core.generic.GenericType) GenericTypes.resolveMapEntryType(org.jdbi.v3.core.generic.GenericTypes.resolveMapEntryType) GenericTypes.getErasedType(org.jdbi.v3.core.generic.GenericTypes.getErasedType) Type(java.lang.reflect.Type) Collector(java.util.stream.Collector)

Example 9 with GenericType

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");
}
Also used : GenericType(org.jdbi.v3.core.generic.GenericType) GenericTypes.resolveMapEntryType(org.jdbi.v3.core.generic.GenericTypes.resolveMapEntryType) GenericTypes.getErasedType(org.jdbi.v3.core.generic.GenericTypes.getErasedType) Type(java.lang.reflect.Type) Collector(java.util.stream.Collector) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap)

Example 10 with GenericType

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");
}
Also used : GenericType(org.jdbi.v3.core.generic.GenericType) GenericTypes.resolveMapEntryType(org.jdbi.v3.core.generic.GenericTypes.resolveMapEntryType) GenericTypes.getErasedType(org.jdbi.v3.core.generic.GenericTypes.getErasedType) Type(java.lang.reflect.Type) Optional(java.util.Optional) Collector(java.util.stream.Collector) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)14 GenericType (org.jdbi.v3.core.generic.GenericType)11 Handle (org.jdbi.v3.core.Handle)10 Something (org.jdbi.v3.core.Something)6 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)6 Collector (java.util.stream.Collector)5 Type (java.lang.reflect.Type)3 Map (java.util.Map)3 GenericTypes.getErasedType (org.jdbi.v3.core.generic.GenericTypes.getErasedType)3 GenericTypes.resolveMapEntryType (org.jdbi.v3.core.generic.GenericTypes.resolveMapEntryType)3 NoSuchMapperException (org.jdbi.v3.core.mapper.NoSuchMapperException)3 Optional (com.google.common.base.Optional)2 BiMap (com.google.common.collect.BiMap)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Tuple2 (io.vavr.Tuple2)2 HashMap (io.vavr.collection.HashMap)2 JdbiCollectors (org.jdbi.v3.core.collector.JdbiCollectors)2 Tuple3 (io.vavr.Tuple3)1 Map (io.vavr.collection.Map)1 Multimap (io.vavr.collection.Multimap)1