### Question:
GlusterDirectoryStream implements DirectoryStream<Path> { @Override public Iterator<Path> iterator() { if (null != iterator || closed) { throw new IllegalStateException("Already iterating!"); } GlusterDirectoryIterator iterator = new GlusterDirectoryIterator(); iterator.setStream(this); iterator.setFilter(filter); this.iterator = iterator; return iterator; }  @Override Iterator<Path> iterator(); @Override void close(); void open(GlusterPath path);  }### Answer:
@Test(expected = IllegalStateException.class) public void testIterator_whenAlreadyIterating() { stream.setIterator(mockIterator); stream.iterator(); }
@Test(expected = IllegalStateException.class) public void testIterator_whenClosed() { stream.setClosed(true); stream.iterator(); }
@Test public void testIterator() throws Exception { stream.setClosed(false); stream.setFilter(mockFilter); whenNew(GlusterDirectoryIterator.class). withNoArguments().thenReturn(mockIterator); stream.setDirHandle(dirHandle); Iterator<Path> iterator = stream.iterator(); verifyNew(GlusterDirectoryIterator.class).withNoArguments(); assertEquals(mockIterator, iterator); assertEquals(mockIterator, stream.getIterator()); verify(mockIterator).setStream(stream); verify(mockIterator).setFilter(mockFilter); }