WrappedCloseableIterable implements CloseableIterable<T> { @Override public void close() { CloseableUtil.close(iterable); } WrappedCloseableIterable();  WrappedCloseableIterable(final Iterable<T> iterable);  WrappedCloseableIterable(final Iterator<T> iterator); @Override void close(); @Override CloseableIterator<T> iterator(); @Override String toString();  }
@Test public void shouldDelegateCloseToWrappedCloseableIterable() { final CloseableIterable<Object> closeableIterable = mock(CloseableIterable.class); final WrappedCloseableIterable<Object> wrappedIterable = new WrappedCloseableIterable<>(closeableIterable); wrappedIterable.close(); verify(closeableIterable).close(); }
@Test public void shouldDoNothingWhenCloseCalledOnNoncloseableIterable() { final Iterable<Object> iterable = mock(Iterable.class); final WrappedCloseableIterable<Object> wrappedIterable = new WrappedCloseableIterable<>(iterable); assertDoesNotThrow(() -> wrappedIterable.close()); }
@Test public void shouldDelegateCloseToWrappedIterables() { final CloseableIterable<? extends Object> iterable1 = mock(CloseableIterable.class); final CloseableIterable<Integer> iterable2 = mock(CloseableIterable.class); final Iterable<Object> chainedIterable = getChainedIterable(iterable1, iterable2); final WrappedCloseableIterable<Object> wrappedIterable = new WrappedCloseableIterable<>(chainedIterable); wrappedIterable.close(); verify(iterable1, Mockito.atLeastOnce()).close(); verify(iterable2, Mockito.atLeastOnce()).close(); }