QueryResultSpliterator extends Spliterators.AbstractSpliterator<X> { @Override public void forEachRemaining(Consumer<? super X> action) { try { resultSetSpliterator.forEachRemaining(row -> mapAndApply(row, action)); } finally { onClose.execute(); } } QueryResultSpliterator(Spliterator<ResultRow> resultSetSpliterator, Function<ResultRow, Optional<X>> mapper,
                           Procedure onClose); @Override boolean tryAdvance(Consumer<? super X> action); @Override void forEachRemaining(Consumer<? super X> action);  }
@Test void forEachRemainingInvokesMapperAndConsumerForAllRemainingResultSetItems() { doAnswer(invocation -> { ((Consumer<ResultRow>) invocation.getArgument(0)).accept(resultRow); return null; }).when(resultSetSpliterator).forEachRemaining(any()); when(mapper.apply(any())).thenReturn(Optional.of("test")); sut.forEachRemaining(consumer); verify(mapper).apply(resultRow); verify(consumer).accept("test"); }
@Test void forEachRemainingInvokesClosingProcedureAfterAllItemsHaveBeenProcessed() { doAnswer(invocation -> { ((Consumer<ResultRow>) invocation.getArgument(0)).accept(resultRow); return null; }).when(resultSetSpliterator).forEachRemaining(any()); when(mapper.apply(any())).thenReturn(Optional.of("test")); sut.forEachRemaining(consumer); verify(closer).execute(); }
@Test void forEachRemainingInvokesClosingProcedureWhenProcessingThrowsException() { doAnswer(invocation -> { ((Consumer<ResultRow>) invocation.getArgument(0)).accept(resultRow); return null; }).when(resultSetSpliterator).forEachRemaining(any()); when(mapper.apply(any())).thenReturn(Optional.of("test")); doThrow(OWLPersistenceException.class).when(consumer).accept(anyString()); assertThrows(OWLPersistenceException.class, () -> sut.forEachRemaining(consumer)); verify(closer).execute(); }