Search in sources :

Example 71 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class TestEnums method testMapToEnum.

@Test
public void testMapToEnum() throws Exception {
    Handle h = dbRule.openHandle();
    h.createUpdate("insert into something (id, name) values (1, 'eric')").execute();
    h.createUpdate("insert into something (id, name) values (2, 'brian')").execute();
    List<SomethingElse.Name> results = h.createQuery("select name from something order by id").mapTo(SomethingElse.Name.class).list();
    assertThat(results).containsExactly(SomethingElse.Name.eric, SomethingElse.Name.brian);
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 72 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class BeanMapperTest method testNestedPrefix.

@Test
public void testNestedPrefix() {
    Handle handle = dbRule.getSharedHandle();
    handle.registerRowMapper(BeanMapper.factory(NestedPrefixBean.class));
    handle.execute("insert into something (id, name) values (1, 'foo')");
    assertThat(handle.createQuery("select id nested_id, name nested_name from something").mapTo(NestedPrefixBean.class).findOnly()).extracting("nested.id", "nested.name").containsExactly(1, "foo");
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 73 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class BeanMapperTest method testNested.

@Test
public void testNested() {
    Handle handle = dbRule.getSharedHandle();
    handle.registerRowMapper(BeanMapper.factory(NestedBean.class));
    handle.execute("insert into something (id, name) values (1, 'foo')");
    assertThat(handle.createQuery("select id, name from something").mapTo(NestedBean.class).findOnly()).extracting("nested.id", "nested.name").containsExactly(1, "foo");
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 74 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class BeanMapperTest method testColumnNameAnnotation.

@Test
public void testColumnNameAnnotation() {
    Handle handle = dbRule.getSharedHandle();
    handle.registerRowMapper(BeanMapper.factory(ColumnNameBean.class));
    handle.execute("insert into something (id, name) values (1, 'foo')");
    ColumnNameBean bean = handle.createQuery("select * from something").mapTo(ColumnNameBean.class).findOnly();
    assertThat(bean.getI()).isEqualTo(1);
    assertThat(bean.getS()).isEqualTo("foo");
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 75 with Something

use of org.jdbi.v3.core.Something in project jdbi by jdbi.

the class BeanMapperTest method testNestedPrefixStrict.

@Test
public void testNestedPrefixStrict() {
    Handle handle = dbRule.getSharedHandle();
    handle.getConfig(ReflectionMappers.class).setStrictMatching(true);
    handle.registerRowMapper(BeanMapper.factory(NestedPrefixBean.class));
    // three, sir!
    handle.execute("insert into something (id, name, integerValue) values (1, 'foo', 5)");
    assertThat(handle.createQuery("select id nested_id, name nested_name, integerValue from something").mapTo(NestedPrefixBean.class).findOnly()).extracting("nested.id", "nested.name", "integerValue").containsExactly(1, "foo", 5);
    assertThatThrownBy(() -> handle.createQuery("select id nested_id, name nested_name, 1 as other from something").mapTo(NestedPrefixBean.class).findOnly()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("could not match properties for columns: [other]");
    assertThatThrownBy(() -> handle.createQuery("select id nested_id, name nested_name, 1 as nested_other from something").mapTo(NestedPrefixBean.class).findOnly()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("could not match properties for columns: [nested_other]");
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)170 Something (org.jdbi.v3.core.Something)123 Handle (org.jdbi.v3.core.Handle)80 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)17 Before (org.junit.Before)11 ArrayList (java.util.ArrayList)6 Jdbi (org.jdbi.v3.core.Jdbi)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 GenericType (org.jdbi.v3.core.generic.GenericType)5 List (java.util.List)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 H2DatabaseRule (org.jdbi.v3.core.rule.H2DatabaseRule)4 Rule (org.junit.Rule)4 Connection (java.sql.Connection)3 LinkedHashMap (java.util.LinkedHashMap)3 Collector (java.util.stream.Collector)3 Collectors.toList (java.util.stream.Collectors.toList)3 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)3 NoSuchMapperException (org.jdbi.v3.core.mapper.NoSuchMapperException)3