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]");
}
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);
}
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"));
}
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"));
}
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"));
}
Aggregations