Search in sources :

Example 11 with CompositeAutoCloseable

use of com.cadenzauk.core.lang.CompositeAutoCloseable in project siesta by cadenzauk.

the class JdbcSqlExecutorTest method streamClosedIfComplete.

@Test
void streamClosedIfComplete() throws SQLException {
    CompositeAutoCloseable closeable = new CompositeAutoCloseable();
    when(preparedStatement.executeQuery()).thenReturn(resultSet);
    when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false);
    when(rowMapper.mapRow(resultSet)).thenReturn("Fred").thenReturn("Barney").thenThrow(new AssertionError("Unexpected call to row mapper"));
    JdbcSqlExecutor sut = JdbcSqlExecutor.of(dataSource, 100);
    String sql = "select name from foo where bar = ?";
    List<String> result = sut.stream(connection, sql, toArray("Bob"), rowMapper, closeable).collect(Collectors.toList());
    verify(connection).prepareStatement(sql);
    verify(preparedStatement).executeQuery();
    verify(preparedStatement).setObject(1, "Bob");
    verify(preparedStatement).setFetchSize(100);
    verify(preparedStatement).close();
    verify(resultSet, times(3)).next();
    verify(resultSet).close();
    verify(rowMapper, times(2)).mapRow(resultSet);
    verifyNoMoreInteractions(connection, preparedStatement, resultSet, rowMapper);
    assertThat(result, contains("Fred", "Barney"));
}
Also used : CompositeAutoCloseable(com.cadenzauk.core.lang.CompositeAutoCloseable) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Example 12 with CompositeAutoCloseable

use of com.cadenzauk.core.lang.CompositeAutoCloseable in project siesta by cadenzauk.

the class JdbcSqlExecutorTest method stream.

@Test
void stream() throws SQLException {
    CompositeAutoCloseable closeable = new CompositeAutoCloseable();
    when(preparedStatement.executeQuery()).thenReturn(resultSet);
    when(resultSet.next()).thenReturn(true).thenReturn(true).thenReturn(false);
    when(rowMapper.mapRow(resultSet)).thenReturn("Fred").thenReturn("Barney").thenThrow(new AssertionError("Unexpected call to row mapper"));
    JdbcSqlExecutor sut = JdbcSqlExecutor.of(dataSource, 100);
    String sql = "select name from foo where bar = ?";
    List<String> result;
    try (Stream<String> stream = sut.stream(connection, sql, toArray("Bob"), rowMapper, closeable)) {
        result = stream.collect(Collectors.toList());
    }
    verify(connection).prepareStatement(sql);
    verify(preparedStatement).executeQuery();
    verify(preparedStatement).setObject(1, "Bob");
    verify(preparedStatement).setFetchSize(100);
    verify(preparedStatement).close();
    verify(resultSet, times(3)).next();
    verify(resultSet).close();
    verify(rowMapper, times(2)).mapRow(resultSet);
    verifyNoMoreInteractions(connection, preparedStatement, resultSet, rowMapper);
    assertThat(result, contains("Fred", "Barney"));
}
Also used : CompositeAutoCloseable(com.cadenzauk.core.lang.CompositeAutoCloseable) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test) MockitoTest(com.cadenzauk.core.MockitoTest)

Aggregations

CompositeAutoCloseable (com.cadenzauk.core.lang.CompositeAutoCloseable)12 Test (org.junit.jupiter.api.Test)11 MockitoTest (com.cadenzauk.core.MockitoTest)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 TestDatabase (com.cadenzauk.siesta.model.TestDatabase)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 IncorrectSalesAreaRow (com.cadenzauk.siesta.model.IncorrectSalesAreaRow)3 SalesAreaRow (com.cadenzauk.siesta.model.SalesAreaRow)3 SQLException (java.sql.SQLException)2 Stream (java.util.stream.Stream)2 RuntimeSqlException (com.cadenzauk.core.sql.RuntimeSqlException)1 Tuple2 (com.cadenzauk.core.tuple.Tuple2)1 Database (com.cadenzauk.siesta.Database)1 IntegrationTest (com.cadenzauk.siesta.IntegrationTest)1 LockTestRow (com.cadenzauk.siesta.model.LockTestRow)1 Connection (java.sql.Connection)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeUnit (java.util.concurrent.TimeUnit)1 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)1