@ParameterizedTest @ArgumentsSource(TestCaseArgumentsProvider.class) @TestCase("true") @TestCase("false") void stream(boolean close) throws SQLException { int count = RandomUtils.nextInt(5, 10); String[] mappedRows = Stream.generate(() -> RandomStringUtils.randomAlphabetic(30)).limit(count).toArray(String[]::new); MockUtil.when(rowMapper.mapRow(resultSet)).thenReturn(mappedRows); MockUtil.when(resultSet.next()).thenReturn(count, true).thenReturn(false); Stream<String> result = ResultSetUtil.stream(resultSet, rowMapper); List<String> items = result.collect(toList()); if (close) { result.close(); } assertThat(items, contains(mappedRows)); verify(resultSet, times(count + 1)).next(); verify(rowMapper, times(count)).mapRow(resultSet); verify(resultSet, times(1)).close(); verifyNoMoreInteractions(resultSet, rowMapper); }
public static <T> Stream<T> stream(ResultSet rs, RowMapper<T> rowMapper) { CompositeAutoCloseable closer = new CompositeAutoCloseable(); closer.add(rs); return StreamSupport.stream(new ResultSetSpliterator<>(rs, rowMapper, closer::close), false).onClose(closer::close); }
ResultSetUtil extends UtilityClass { public static <T> Stream<T> stream(ResultSet rs, RowMapper<T> rowMapper) { CompositeAutoCloseable closer = new CompositeAutoCloseable(); closer.add(rs); return StreamSupport.stream(new ResultSetSpliterator<>(rs, rowMapper, closer::close), false).onClose(closer::close); } }
ResultSetUtil extends UtilityClass { public static <T> Stream<T> stream(ResultSet rs, RowMapper<T> rowMapper) { CompositeAutoCloseable closer = new CompositeAutoCloseable(); closer.add(rs); return StreamSupport.stream(new ResultSetSpliterator<>(rs, rowMapper, closer::close), false).onClose(closer::close); }  }
ResultSetUtil extends UtilityClass { public static <T> Stream<T> stream(ResultSet rs, RowMapper<T> rowMapper) { CompositeAutoCloseable closer = new CompositeAutoCloseable(); closer.add(rs); return StreamSupport.stream(new ResultSetSpliterator<>(rs, rowMapper, closer::close), false).onClose(closer::close); }  static String getString(ResultSet rs, String columnLabel); static void close(ResultSet rs); static Stream<T> stream(ResultSet rs, RowMapper<T> rowMapper); static Stream<T> stream(ThrowingSupplier<ResultSet, ? extends SQLException> rs, RowMapper<T> rowMapper); }
ResultSetUtil extends UtilityClass { public static <T> Stream<T> stream(ResultSet rs, RowMapper<T> rowMapper) { CompositeAutoCloseable closer = new CompositeAutoCloseable(); closer.add(rs); return StreamSupport.stream(new ResultSetSpliterator<>(rs, rowMapper, closer::close), false).onClose(closer::close); }  static String getString(ResultSet rs, String columnLabel); static void close(ResultSet rs); static Stream<T> stream(ResultSet rs, RowMapper<T> rowMapper); static Stream<T> stream(ThrowingSupplier<ResultSet, ? extends SQLException> rs, RowMapper<T> rowMapper);  }