Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
311   564   35   14.81
22   408   0.11   21
21     1.67  
1    
 
  TestIndexReader       Line # 36 311 35 98.3% 0.9830508
 
  (11)
 
1    package org.apache.lucene.index;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18   
19   
20    import junit.framework.TestCase;
21    import junit.framework.TestSuite;
22    import junit.textui.TestRunner;
23   
24    import org.apache.lucene.store.Directory;
25    import org.apache.lucene.store.RAMDirectory;
26    import org.apache.lucene.store.FSDirectory;
27    import org.apache.lucene.analysis.standard.StandardAnalyzer;
28    import org.apache.lucene.analysis.WhitespaceAnalyzer;
29    import org.apache.lucene.document.Document;
30    import org.apache.lucene.document.Field;
31   
32    import java.util.Collection;
33    import java.io.IOException;
34    import java.io.File;
35   
 
36    public class TestIndexReader extends TestCase
37    {
38    /** Main for running test case by itself. */
 
39  0 toggle public static void main(String args[]) {
40  0 TestRunner.run (new TestSuite(TestIndexReader.class));
41    // TestRunner.run (new TestIndexReader("testBasicDelete"));
42    // TestRunner.run (new TestIndexReader("testDeleteReaderWriterConflict"));
43    // TestRunner.run (new TestIndexReader("testDeleteReaderReaderConflict"));
44    // TestRunner.run (new TestIndexReader("testFilesOpenClose"));
45    }
46   
 
47  11 toggle public TestIndexReader(String name) {
48  11 super(name);
49    }
50   
 
51  1 toggle public void testIsCurrent() throws Exception
52    {
53  1 RAMDirectory d = new RAMDirectory();
54  1 IndexWriter writer = new IndexWriter(d, new StandardAnalyzer(), true);
55  1 addDocumentWithFields(writer);
56  1 writer.close();
57    // set up reader:
58  1 IndexReader reader = IndexReader.open(d);
59  1 assertTrue(reader.isCurrent());
60    // modify index by adding another document:
61  1 writer = new IndexWriter(d, new StandardAnalyzer(), false);
62  1 addDocumentWithFields(writer);
63  1 writer.close();
64  1 assertFalse(reader.isCurrent());
65    // re-create index:
66  1 writer = new IndexWriter(d, new StandardAnalyzer(), true);
67  1 addDocumentWithFields(writer);
68  1 writer.close();
69  1 assertFalse(reader.isCurrent());
70  1 reader.close();
71    }
72   
73    /**
74    * Tests the IndexReader.getFieldNames implementation
75    * @throws Exception on error
76    */
 
77  1 toggle public void testGetFieldNames() throws Exception
78    {
79  1 RAMDirectory d = new RAMDirectory();
80    // set up writer
81  1 IndexWriter writer = new IndexWriter(d, new StandardAnalyzer(), true);
82  1 addDocumentWithFields(writer);
83  1 writer.close();
84    // set up reader
85  1 IndexReader reader = IndexReader.open(d);
86  1 Collection fieldNames = reader.getFieldNames(IndexReader.FieldOption.ALL);
87  1 assertTrue(fieldNames.contains("keyword"));
88  1 assertTrue(fieldNames.contains("text"));
89  1 assertTrue(fieldNames.contains("unindexed"));
90  1 assertTrue(fieldNames.contains("unstored"));
91    // add more documents
92  1 writer = new IndexWriter(d, new StandardAnalyzer(), false);
93    // want to get some more segments here
94  51 for (int i = 0; i < 5*writer.getMergeFactor(); i++)
95    {
96  50 addDocumentWithFields(writer);
97    }
98    // new fields are in some different segments (we hope)
99  51 for (int i = 0; i < 5*writer.getMergeFactor(); i++)
100    {
101  50 addDocumentWithDifferentFields(writer);
102    }
103    // new termvector fields
104  51 for (int i = 0; i < 5*writer.getMergeFactor(); i++)
105    {
106  50 addDocumentWithTermVectorFields(writer);
107    }
108   
109  1 writer.close();
110    // verify fields again
111  1 reader = IndexReader.open(d);
112  1 fieldNames = reader.getFieldNames(IndexReader.FieldOption.ALL);
113  1 assertEquals(13, fieldNames.size()); // the following fields
114  1 assertTrue(fieldNames.contains("keyword"));
115  1 assertTrue(fieldNames.contains("text"));
116  1 assertTrue(fieldNames.contains("unindexed"));
117  1 assertTrue(fieldNames.contains("unstored"));
118  1 assertTrue(fieldNames.contains("keyword2"));
119  1 assertTrue(fieldNames.contains("text2"));
120