FileDocCategorySizeDatePackage
TestHitIterator.javaAPI DocApache Lucene 2.2.02835Sat Jun 16 22:20:30 BST 2007org.apache.lucene

TestHitIterator

public class TestHitIterator extends TestCase
This test intentionally not put in the search package in order to test HitIterator and Hit package protection.

Fields Summary
Constructors Summary
Methods Summary
public voidtestIterator()

    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);