Search in sources :

Example 76 with Something

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

the class BeanMapperTest method testNestedStrict.

@Test
public void testNestedStrict() {
    Handle handle = dbRule.getSharedHandle();
    handle.getConfig(ReflectionMappers.class).setStrictMatching(true);
    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");
    assertThatThrownBy(() -> handle.createQuery("select id, name, 1 as other from something").mapTo(NestedBean.class).findOnly()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("could not match properties for columns: [other]");
}
Also used : Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 77 with Something

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

the class TestTransactionsAutoCommit method restoreAutoCommitInitialStateOnUnexpectedError.

@Test
public void restoreAutoCommitInitialStateOnUnexpectedError() throws Exception {
    final Connection connection = mock(Connection.class);
    final PreparedStatement statement = mock(PreparedStatement.class);
    InOrder inOrder = inOrder(connection, statement);
    Handle h = Jdbi.create(() -> connection).open();
    when(connection.getAutoCommit()).thenReturn(true);
    when(connection.prepareStatement(anyString(), anyInt(), anyInt())).thenReturn(statement);
    when(statement.execute()).thenReturn(true);
    when(statement.getUpdateCount()).thenReturn(1);
    // throw e.g some underlying database error
    doThrow(new SQLException("infrastructure error")).when(connection).commit();
    h.begin();
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
        h.execute(SAMPLE_SQL, 1L, "Tom");
        // throws exception on commit
        h.commit();
    });
    // expected behaviour chain:
    // 1. store initial auto-commit state
    inOrder.verify(connection, atLeastOnce()).getAutoCommit();
    // 2. turn off auto-commit
    inOrder.verify(connection).setAutoCommit(false);
    // 3. execute statement (without commit)
    inOrder.verify(connection).prepareStatement("insert into something (id, name) values (?, ?)", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
    inOrder.verify(statement).execute();
    inOrder.verify(statement).getUpdateCount();
    // 4. commit transaction
    inOrder.verify(connection).commit();
    // 5. set auto-commit back to initial state
    inOrder.verify(connection).setAutoCommit(true);
}
Also used : InOrder(org.mockito.InOrder) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) SQLException(java.sql.SQLException) Handle(org.jdbi.v3.core.Handle) Test(org.junit.Test)

Example 78 with Something

use of org.jdbi.v3.core.Something 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 79 with Something

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

the class TestGuavaOptional method testBindOptionalOfCustomType.

@Test
public void testBindOptionalOfCustomType() throws Exception {
    handle.registerArgument(new NameArgumentFactory());
    List<Something> result = handle.createQuery(SELECT_BY_NAME).bind("name", Optional.of(new Name("eric"))).mapToBean(Something.class).list();
    assertThat(result).containsExactly(new Something(1, "eric"));
}
Also used : Something(org.jdbi.v3.core.Something) Test(org.junit.Test)

Example 80 with Something

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

the class TestGuavaOptional method testBindOptionalPresent.

@Test
public void testBindOptionalPresent() throws Exception {
    Something result = handle.createQuery(SELECT_BY_NAME).bind("name", Optional.of("brian")).mapToBean(Something.class).findOnly();
    assertThat(result).isEqualTo(new Something(2, "brian"));
}
Also used : Something(org.jdbi.v3.core.Something) 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