1
package org.apache.lucene.index;
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
11
* http://www.apache.org/licenses/LICENSE-2.0
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.
21
import org.apache.lucene.util.LuceneTestCase;
22
import junit.framework.TestSuite;
23
import junit.textui.TestRunner;
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;
30
import java.io.IOException;
32
public class TestFilterIndexReader extends LuceneTestCase {
34
private static class TestReader extends FilterIndexReader {
36
/** Filter that only permits terms containing 'e'.*/
37
private static class TestTermEnum extends FilterTermEnum {
38
public TestTermEnum(TermEnum termEnum) {
42
/** Scan for terms containing the letter 'e'.*/
44
public boolean next() throws IOException {
46
if (in.term().text().indexOf('e') != -1)
53
/** Filter that only returns odd numbered documents. */
54
private static class TestTermPositions extends FilterTermPositions {
55
public TestTermPositions(TermPositions in) {
59
/** Scan for odd numbered documents. */
61
public boolean next() throws IOException {
63
if ((in.doc() % 2) == 1)
70
public TestReader(IndexReader reader) {
74
/** Filter terms with TestTermEnum. */
76
public TermEnum terms() throws IOException {
77
return new TestTermEnum(in.terms());
80
/** Filter positions with TestTermPositions. */
82
public TermPositions termPositions() throws IOException {
83
return new TestTermPositions(in.termPositions());
88
/** Main for running test case by itself. */
89
public static void main(String args[]) {
90
TestRunner.run (new TestSuite(TestIndexReader.class));
94
* Tests the IndexReader.getFieldNames implementation
95
* @throws Exception on error
97
public void testFilterIndexReader() throws Exception {
98
Directory directory = newDirectory();
99
IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)));
101
Document d1 = new Document();
102
d1.add(newField("default","one two", Field.Store.YES, Field.Index.ANALYZED));
103
writer.addDocument(d1);
105
Document d2 = new Document();
106
d2.add(newField("default","one three", Field.Store.YES, Field.Index.ANALYZED));
107
writer.addDocument(d2);
109
Document d3 = new Document();
110
d3.add(newField("default","two four", Field.Store.YES, Field.Index.ANALYZED));
111
writer.addDocument(d3);
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);
122
TermPositions positions = reader.termPositions(new Term("default", "one"));
123
while (positions.next()) {
124
assertTrue((positions.doc() % 2) == 1);
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());