FileDocCategorySizeDatePackage
TestHitIterator.javaAPI DocApache Lucene 1.92031Mon Feb 20 09:19:40 GMT 2006org.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);