KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > TestFilterIndexReader


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 import junit.framework.TestResult;
24
25 import org.apache.lucene.search.IndexSearcher;
26 import org.apache.lucene.search.Searcher;
27 import org.apache.lucene.search.Hits;
28 import org.apache.lucene.search.TermQuery;
29 import org.apache.lucene.store.Directory;
30 import org.apache.lucene.store.RAMDirectory;
31 import org.apache.lucene.store.FSDirectory;
32 import org.apache.lucene.analysis.standard.StandardAnalyzer;
33 import org.apache.lucene.analysis.WhitespaceAnalyzer;
34 import org.apache.lucene.document.Document;
35 import org.apache.lucene.document.Field;
36
37 import java.util.Collection JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 public class TestFilterIndexReader extends TestCase {
41
42   private static class TestReader extends FilterIndexReader {
43
44      /** Filter that only permits terms containing 'e'.*/
45     private static class TestTermEnum extends FilterTermEnum {
46       public TestTermEnum(TermEnum termEnum)
47         throws IOException JavaDoc {
48         super(termEnum);
49       }
50
51       /** Scan for terms containing the letter 'e'.*/
52       public boolean next() throws IOException JavaDoc {
53         while (in.next()) {
54           if (in.term().text().indexOf('e') != -1)
55             return true;
56         }
57         return false;
58       }
59     }
60     
61     /** Filter that only returns odd numbered documents. */
62     private static class TestTermPositions extends FilterTermPositions {
63       public TestTermPositions(TermPositions in)
64         throws IOException JavaDoc {
65         super(in);
66       }
67
68       /** Scan for odd numbered documents. */
69       public boolean next() throws IOException JavaDoc {
70         while (in.next()) {
71           if ((in.doc() % 2) == 1)
72             return true;
73         }
74         return false;
75       }
76     }
77     
78     public TestReader(IndexReader reader) {
79       super(reader);
80     }
81
82     /** Filter terms with TestTermEnum. */
83     public TermEnum terms() throws IOException JavaDoc {
84       return new TestTermEnum(in.terms());
85     }
86
87     /** Filter positions with TestTermPositions. */
88     public TermPositions termPositions() throws IOException JavaDoc {
89       return new TestTermPositions(in.termPositions());
90     }
91   }
92
93
94   /** Main for running test case by itself. */
95   public static void main(String JavaDoc args[]) {
96     TestRunner.run (new TestSuite(TestIndexReader.class));
97   }
98     
99   /**
100    * Tests the IndexReader.getFieldNames implementation
101    * @throws Exception on error
102    */

103   public void testFilterIndexReader() throws Exception JavaDoc {
104     RAMDirectory directory = new RAMDirectory();
105     IndexWriter writer =
106       new IndexWriter(directory, new WhitespaceAnalyzer(), true);
107
108     Document d1 = new Document();
109     d1.add(Field.Text("default","one two"));
110     writer.addDocument(d1);
111
112     Document d2 = new Document();
113     d2.add(Field.Text("default","one three"));
114     writer.addDocument(d2);
115
116     Document d3 = new Document();
117     d3.add(Field.Text("default","two four"));
118     writer.addDocument(d3);
119
120     writer.close();
121
122     IndexReader reader = new TestReader(IndexReader.open(directory));
123
124     TermEnum terms = reader.terms();
125     while (terms.next()) {
126       assertTrue(terms.term().text().indexOf('e') != -1);
127     }
128     terms.close();
129     
130     TermPositions positions = reader.termPositions(new Term("default", "one"));
131     while (positions.next()) {
132       assertTrue((positions.doc() % 2) == 1);
133     }
134
135     reader.close();
136   }
137 }
138
Popular Tags