Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
35   126   15   3.89
12   75   0.43   2.25
9     1.67  
4    
 
  TestFilterIndexReader       Line # 31 22 4 92.9% 0.9285714
  TestFilterIndexReader.TestReader       Line # 33 3 3 100% 1.0
  TestFilterIndexReader.TestReader.TestTermEnum       Line # 36 5 4 100% 1.0
  TestFilterIndexReader.TestReader.TestTermPositions       Line # 52 5 4 100% 1.0
 
  (1)
 
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.RAMDirectory;
25    import org.apache.lucene.analysis.WhitespaceAnalyzer;
26    import org.apache.lucene.document.Document;
27    import org.apache.lucene.document.Field;
28   
29    import java.io.IOException;
30   
 
31    public class TestFilterIndexReader extends TestCase {
32   
 
33    private static class TestReader extends FilterIndexReader {
34   
35    /** Filter that only permits terms containing 'e'.*/
 
36    private static class TestTermEnum extends FilterTermEnum {
 
37  1 toggle public TestTermEnum(TermEnum termEnum) {
38  1 super(termEnum);
39    }
40   
41    /** Scan for terms containing the letter 'e'.*/
 
42  3 toggle public boolean next() throws IOException {
43  5 while (in.next()) {
44  4 if (in.term().text().indexOf('e') != -1)
45  2 return true;
46    }
47  1 return false;
48    }
49    }
50   
51    /** Filter that only returns odd numbered documents. */
 
52    private static class TestTermPositions extends FilterTermPositions {
 
53  1 toggle public TestTermPositions(TermPositions in) {
54  1 super(in);
55    }
56   
57    /** Scan for odd numbered documents. */
 
58  2 toggle public boolean next() throws IOException {
59  3 while (in.next()) {
60  2 if ((in.doc() % 2) == 1)
61  1 return true;
62    }
63  1 return false;
64    }
65    }
66   
 
67  1 toggle public TestReader(IndexReader reader) {
68  1 super(reader);
69    }
70   
71    /** Filter terms with TestTermEnum. */
 
72  1 toggle public TermEnum terms() throws IOException {
73  1 return new TestTermEnum(in.terms());
74    }
75   
76    /** Filter positions with TestTermPositions. */
 
77  1 toggle public TermPositions termPositions() throws IOException {
78  1 return new TestTermPositions(in.termPositions());
79    }
80    }
81   
82   
83    /** Main for running test case by itself. */
 
84  0 toggle public static void main(String args[]) {
85  0 TestRunner.run (new TestSuite(TestIndexReader.class));
86    }
87   
88    /**
89    * Tests the IndexReader.getFieldNames implementation
90    * @throws Exception on error
91    */
 
92  1 toggle public void testFilterIndexReader() throws Exception {
93  1 RAMDirectory directory = new RAMDirectory();
94  1 IndexWriter writer =
95    new IndexWriter(directory, new WhitespaceAnalyzer(), true);
96   
97  1 Document d1 = new Document();
98  1 d1.add(new Field("default","one two", Field.Store.YES, Field.Index.TOKENIZED));
99  1 writer.addDocument(d1);
100   
101  1 Document d2 = new Document();
102  1 d2.add(new Field("default","one three", Field.Store.YES, Field.Index.TOKENIZED));
103  1 writer.addDocument(d2);
104   
105  1 Document d3 = new Document();
106  1 d3.add(new Field("default","two four", Field.Store.YES, Field.Index.TOKENIZED));
107  1 writer.addDocument(d3);
108   
109  1 writer.close();
110   
111  1 IndexReader reader = new TestReader(IndexReader.open(directory));
112   
113  1 TermEnum terms = reader.terms();
114  3 while (terms.next()) {
115  2 assertTrue(terms.term().text().indexOf('e') != -1);
116    }
117  1 terms.close();
118   
119  1 TermPositions positions = reader.termPositions(new Term("default", "one"));
120  2 while (positions.next()) {
121  1 assertTrue((positions.doc() % 2) == 1);
122    }
123   
124  1 reader.close();
125    }
126    }