pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestFilterIndexReader.java
1 package org.apache.lucene.index;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20
21 import org.apache.lucene.util.LuceneTestCase;
22 import junit.framework.TestSuite;
23 import junit.textui.TestRunner;
24
25 import org.apache.lucene.analysis.MockAnalyzer;
26 import org.apache.lucene.store.Directory;
27 import org.apache.lucene.document.Document;
28 import org.apache.lucene.document.Field;
29
30 import java.io.IOException;
31
32 public class TestFilterIndexReader extends LuceneTestCase {
33
34   private static class TestReader extends FilterIndexReader {
35
36      /** Filter that only permits terms containing 'e'.*/
37     private static class TestTermEnum extends FilterTermEnum {
38       public TestTermEnum(TermEnum termEnum) {
39         super(termEnum);
40       }
41
42       /** Scan for terms containing the letter 'e'.*/
43       @Override
44       public boolean next() throws IOException {
45         while (in.next()) {
46           if (in.term().text().indexOf('e') != -1)
47             return true;
48         }
49         return false;
50       }
51     }
52     
53     /** Filter that only returns odd numbered documents. */
54     private static class TestTermPositions extends FilterTermPositions {
55       public TestTermPositions(TermPositions in) {
56         super(in);
57       }
58
59       /** Scan for odd numbered documents. */
60       @Override
61       public boolean next() throws IOException {
62         while (in.next()) {
63           if ((in.doc() % 2) == 1)
64             return true;
65         }
66         return false;
67       }
68     }
69     
70     public TestReader(IndexReader reader) {
71       super(reader);
72     }
73
74     /** Filter terms with TestTermEnum. */
75     @Override
76     public TermEnum terms() throws IOException {
77       return new TestTermEnum(in.terms());
78     }
79
80     /** Filter positions with TestTermPositions. */
81     @Override
82     public TermPositions termPositions() throws IOException {
83       return new TestTermPositions(in.termPositions());
84     }
85   }
86
87
88   /** Main for running test case by itself. */
89   public static void main(String args[]) {
90     TestRunner.run (new TestSuite(TestIndexReader.class));
91   }
92     
93   /**
94    * Tests the IndexReader.getFieldNames implementation
95    * @throws Exception on error
96    */
97   public void testFilterIndexReader() throws Exception {
98     Directory directory = newDirectory();
99     IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
100
101     Document d1 = new Document();
102     d1.add(newField("default","one two", Field.Store.YES, Field.Index.ANALYZED));
103     writer.addDocument(d1);
104
105     Document d2 = new Document();
106     d2.add(newField("default","one three", Field.Store.YES, Field.Index.ANALYZED));
107     writer.addDocument(d2);
108
109     Document d3 = new Document();
110     d3.add(newField("default","two four", Field.Store.YES, Field.Index.ANALYZED));
111     writer.addDocument(d3);
112
113     writer.close();
114
115     IndexReader reader = new TestReader(IndexReader.open(directory, true));
116     TermEnum terms = reader.terms();
117     while (terms.next()) {
118       assertTrue(terms.term().text().indexOf('e') != -1);
119     }
120     terms.close();
121     
122     TermPositions positions = reader.termPositions(new Term("default", "one"));
123     while (positions.next()) {
124       assertTrue((positions.doc() % 2) == 1);
125     }
126
127     int NUM_DOCS = 3;
128
129     TermDocs td = reader.termDocs(null);
130     for(int i=0;i<NUM_DOCS;i++) {
131       assertTrue(td.next());
132       assertEquals(i, td.doc());
133       assertEquals(1, td.freq());
134     }
135     td.close();
136     reader.close();
137     directory.close();
138   }
139 }