RAMDirectory directory = new RAMDirectory();
IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
Document doc = new Document();
doc.add(new Field("field", "iterator test doc 1", Field.Store.YES, Field.Index.TOKENIZED));
writer.addDocument(doc);
doc = new Document();
doc.add(new Field("field", "iterator test doc 2", Field.Store.YES, Field.Index.TOKENIZED));
writer.addDocument(doc);
writer.close();
IndexSearcher searcher = new IndexSearcher(directory);
Hits hits = searcher.search(new TermQuery(new Term("field", "iterator")));
HitIterator iterator = (HitIterator) hits.iterator();
assertEquals(2, iterator.length());
assertTrue(iterator.hasNext());
Hit hit = (Hit) iterator.next();
assertEquals("iterator test doc 1", hit.get("field"));
assertTrue(iterator.hasNext());
hit = (Hit) iterator.next();
assertEquals("iterator test doc 2", hit.getDocument().get("field"));
assertFalse(iterator.hasNext());
boolean caughtException = false;
try {
iterator.next();
} catch (NoSuchElementException e) {
assertTrue(true);
caughtException = true;
}
assertTrue(caughtException);