FilteredObjectStore implements ObjectStore<O> { @Override public CloseableIterator<O> iterator(QueryOptions queryOptions) { final CloseableIterator<O> backingIterator = backingObjectStore.iterator(queryOptions); final FilteringIterator<O> filteringIterator = new FilteringIterator<O>(backingIterator, queryOptions) { @Override public boolean isValid(O object, QueryOptions queryOptions) { return filterQuery.matches(object, queryOptions); } }; return new CloseableIterator<O>() { @Override public void close() { backingIterator.close(); } @Override public boolean hasNext() { return filteringIterator.hasNext(); } @Override public O next() { return filteringIterator.next(); } @Override public void remove() { throw new UnsupportedOperationException("Modification not supported"); } }; } FilteredObjectStore(ObjectStore<O> backingObjectStore, Query<O> filterQuery); @Override CloseableIterator<O> iterator(QueryOptions queryOptions); @Override int size(QueryOptions queryOptions); @Override boolean contains(Object o, QueryOptions queryOptions); @Override boolean isEmpty(QueryOptions queryOptions); @Override boolean add(O object, QueryOptions queryOptions); @Override boolean remove(Object o, QueryOptions queryOptions); @Override boolean containsAll(Collection<?> c, QueryOptions queryOptions); @Override boolean addAll(Collection<? extends O> c, QueryOptions queryOptions); @Override boolean retainAll(Collection<?> c, QueryOptions queryOptions); @Override boolean removeAll(Collection<?> c, QueryOptions queryOptions); @Override void clear(QueryOptions queryOptions);  }
@Test @SuppressWarnings("unchecked") public void testIterator_Close() { CloseableIterator<Car> mockIterator = mock(CloseableIterator.class); ObjectStore<Car> mockObjectStore = mock(ObjectStore.class); Mockito.when(mockObjectStore.iterator(Mockito.<QueryOptions>any())).thenReturn(mockIterator); FilteredObjectStore<Car> filteredObjectStore = new FilteredObjectStore<Car>( mockObjectStore, between(Car.CAR_ID, 2, 4) ); filteredObjectStore.iterator(noQueryOptions()).close(); verify(mockIterator, times(1)).close(); }