JdbcResultSetStreamer { public <T> Stream<T> streamOf(final PreparedStatementWrapper psWrapper, final Function<ResultSet, T> resultSetToEntityMapper) throws SQLException { final ResultSet resultSet = psWrapper.executeQuery(); resultSet.setFetchSize(FETCH_SIZE); return internalStreamOf(psWrapper, resultSet, resultSetToEntityMapper); }  Stream<T> streamOf(final PreparedStatementWrapper psWrapper, final Function<ResultSet, T> resultSetToEntityMapper);  }
@Test public void shouldConvertJdbcResultSetToAStreamOfObjects() throws Exception { final PreparedStatementWrapper preparedStatementWrapper = mock(PreparedStatementWrapper.class); final ResultSet resultSet = mock(ResultSet.class); when(preparedStatementWrapper.executeQuery()).thenReturn(resultSet); when(resultSet.next()).thenReturn(true, true, false); when(resultSet.getString("name")).thenReturn("Fred", "Bert"); when(resultSet.getInt("age")).thenReturn(63, 59); final Function<ResultSet, Student> resultSetFunction = rs -> { try { return new Student( rs.getString("name"), rs.getInt("age")); } catch (final SQLException e) { throw new RuntimeException(e); } }; try (final Stream<Student> studentStream = jdbcResultSetStreamer.streamOf(preparedStatementWrapper, resultSetFunction)) { final List<Student> students = studentStream.collect(toList()); assertThat(students.size(), is(2)); assertThat(students.get(0).getName(), is("Fred")); assertThat(students.get(0).getAge(), is(63)); assertThat(students.get(1).getName(), is("Bert")); assertThat(students.get(1).getAge(), is(59)); } verify(preparedStatementWrapper).close(); }
@SuppressWarnings("ResultOfMethodCallIgnored") @Test public void shouldAlwaysCloseThePreparedStatmentOnException() throws Exception { final NullPointerException nullPointerException = new NullPointerException("Ooops"); final PreparedStatementWrapper preparedStatementWrapper = mock(PreparedStatementWrapper.class); final ResultSet resultSet = mock(ResultSet.class); when(preparedStatementWrapper.executeQuery()).thenReturn(resultSet); when(resultSet.next()).thenReturn(true); final Stream<Student> studentStream = jdbcResultSetStreamer.streamOf(preparedStatementWrapper, rs -> { throw nullPointerException; }); try { studentStream.collect(toList()); fail(); } catch (final JdbcRepositoryException expected) { assertThat(expected.getCause(), is(nullPointerException)); } verify(preparedStatementWrapper).close(); }