Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
31   94   4   10.33
2   58   0.13   3
3     1.33  
1    
 
  TestSegmentTermEnum       Line # 20 31 4 100% 1.0
 
  (1)
 
1    package org.apache.lucene.index;
2   
3    import java.io.IOException;
4   
5    import junit.framework.TestCase;
6   
7    import org.apache.lucene.analysis.WhitespaceAnalyzer;
8    import org.apache.lucene.document.Document;
9    import org.apache.lucene.document.Field;
10    import org.apache.lucene.index.IndexReader;
11    import org.apache.lucene.index.IndexWriter;
12    import org.apache.lucene.index.Term;
13    import org.apache.lucene.index.TermEnum;
14    import org.apache.lucene.store.Directory;
15    import org.apache.lucene.store.RAMDirectory;
16   
17    /**
18    * @author goller
19    */
 
20    public class TestSegmentTermEnum extends TestCase
21    {
22    Directory dir = new RAMDirectory();
23   
 
24  1 toggle public void testTermEnum() throws IOException
25    {
26  1 IndexWriter writer = null;
27   
28  1 writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
29   
30    // add 100 documents with term : aaa
31    // add 100 documents with terms: aaa bbb
32    // Therefore, term 'aaa' has document frequency of 200 and term 'bbb' 100
33  101 for (int i = 0; i < 100; i++) {
34  100 addDoc(writer, "aaa");
35  100 addDoc(writer, "aaa bbb");
36    }
37   
38  1 writer.close();
39   
40    // verify document frequency of terms in an unoptimized index
41  1 verifyDocFreq();
42   
43    // merge segments by optimizing the index
44  1 writer = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
45  1 writer.optimize();
46  1 writer.close();
47   
48    // verify document frequency of terms in an optimized index
49  1 verifyDocFreq();
50    }
51   
 
52  2 toggle private void verifyDocFreq()
53    throws IOException
54    {
55  2 IndexReader reader = IndexReader.open(dir);
56  2 TermEnum termEnum = null;
57   
58    // create enumeration of all terms
59  2 termEnum = reader.terms();
60    // go to the first term (aaa)
61  2 termEnum.next();
62    // assert that term is 'aaa'
63  2 assertEquals("aaa", termEnum.term().text());
64  2 assertEquals(200, termEnum.docFreq());
65    // go to the second term (bbb)
66  2 termEnum.next();
67    // assert that term is 'bbb'
68  2 assertEquals("bbb", termEnum.term().text());
69  2 assertEquals(100, termEnum.docFreq());
70   
71  2 termEnum.close();
72   
73   
74    // create enumeration of terms after term 'aaa', including 'aaa'
75  2 termEnum = reader.terms(new Term("content", "aaa"));
76    // assert that term is 'aaa'
77  2 assertEquals("aaa", termEnum.term().text());
78  2 assertEquals(200, termEnum.docFreq());
79    // go to term 'bbb'
80  2 termEnum.next();
81    // assert that term is 'bbb'
82  2 assertEquals("bbb", termEnum.term().text());
83  2 assertEquals(100, termEnum.docFreq());
84   
85  2 termEnum.close();
86    }
87   
 
88  200 toggle private void addDoc(IndexWriter writer, String value) throws IOException
89    {
90  200 Document doc = new Document();
91  200 doc.add(new Field("content", value, Field.Store.NO, Field.Index.TOKENIZED));
92  200 writer.addDocument(doc);
93    }
94    }