Search in sources :

Example 81 with Something

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

the class FieldMapperTest method testNestedPrefix.

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

Example 82 with Something

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

the class FieldMapperTest method shouldSetValuesOnAllFieldAccessTypes.

@Test
public void shouldSetValuesOnAllFieldAccessTypes() throws Exception {
    mockColumns("longField", "protectedStringField", "packagePrivateIntField", "privateBigDecimalField");
    Long aLongVal = 100L;
    String aStringVal = "something";
    int aIntVal = 1;
    BigDecimal aBigDecimal = BigDecimal.TEN;
    when(resultSet.getLong(1)).thenReturn(aLongVal);
    when(resultSet.getString(2)).thenReturn(aStringVal);
    when(resultSet.getInt(3)).thenReturn(aIntVal);
    when(resultSet.getBigDecimal(4)).thenReturn(aBigDecimal);
    when(resultSet.wasNull()).thenReturn(false);
    SampleBean sampleBean = mapper.map(resultSet, ctx);
    assertThat(sampleBean.getLongField()).isEqualTo(aLongVal);
    assertThat(sampleBean.getPrivateBigDecimalField()).isEqualTo(aBigDecimal);
    assertThat(sampleBean.getPackagePrivateIntField()).isEqualTo(aIntVal);
    assertThat(sampleBean.getProtectedStringField()).isEqualTo(aStringVal);
}
Also used : SampleBean(org.jdbi.v3.core.SampleBean) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 83 with Something

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

the class FieldMapperTest method testColumnNameAnnotation.

@Test
public void testColumnNameAnnotation() {
    Handle handle = dbRule.getSharedHandle();
    handle.execute("insert into something (id, name) values (1, 'foo')");
    ColumnNameThing thing = handle.createQuery("select * from something").map(FieldMapper.of(ColumnNameThing.class)).findOnly();
    assertThat(thing.i).isEqualTo(1);
    assertThat(thing.s).isEqualTo("foo");
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 84 with Something

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

the class FieldMapperTest method testNested.

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

Example 85 with Something

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

the class TestReducing method setUp.

@Before
public void setUp() {
    Handle h = dbRule.getSharedHandle();
    h.execute("CREATE TABLE something_location (id int, location varchar)");
    h.execute("INSERT INTO something (id, name) VALUES (1, 'tree')");
    h.execute("INSERT INTO something (id, name) VALUES (2, 'apple')");
    h.execute("INSERT INTO something_location (id, location) VALUES (1, 'outside')");
    h.execute("INSERT INTO something_location (id, location) VALUES (2, 'tree')");
    h.execute("INSERT INTO something_location (id, location) VALUES (2, 'pie')");
    h.registerRowMapper(new SomethingMapper());
}
Also used : SomethingMapper(org.jdbi.v3.core.mapper.SomethingMapper) Handle(org.jdbi.v3.core.Handle) Before(org.junit.Before)

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