001: package org.apache.lucene.index;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.util.LuceneTestCase;
021: import junit.framework.TestSuite;
022: import junit.textui.TestRunner;
023:
024: import org.apache.lucene.store.RAMDirectory;
025: import org.apache.lucene.store.MockRAMDirectory;
026: import org.apache.lucene.analysis.WhitespaceAnalyzer;
027: import org.apache.lucene.document.Document;
028: import org.apache.lucene.document.Field;
029:
030: import java.io.IOException;
031:
032: public class TestFilterIndexReader extends LuceneTestCase {
033:
034: private static class TestReader extends FilterIndexReader {
035:
036: /** Filter that only permits terms containing 'e'.*/
037: private static class TestTermEnum extends FilterTermEnum {
038: public TestTermEnum(TermEnum termEnum) {
039: super (termEnum);
040: }
041:
042: /** Scan for terms containing the letter 'e'.*/
043: public boolean next() throws IOException {
044: while (in.next()) {
045: if (in.term().text().indexOf('e') != -1)
046: return true;
047: }
048: return false;
049: }
050: }
051:
052: /** Filter that only returns odd numbered documents. */
053: private static class TestTermPositions extends
054: FilterTermPositions {
055: public TestTermPositions(TermPositions in) {
056: super (in);
057: }
058:
059: /** Scan for odd numbered documents. */
060: public boolean next() throws IOException {
061: while (in.next()) {
062: if ((in.doc() % 2) == 1)
063: return true;
064: }
065: return false;
066: }
067: }
068:
069: public TestReader(IndexReader reader) {
070: super (reader);
071: }
072:
073: /** Filter terms with TestTermEnum. */
074: public TermEnum terms() throws IOException {
075: return new TestTermEnum(in.terms());
076: }
077:
078: /** Filter positions with TestTermPositions. */
079: public TermPositions termPositions() throws IOException {
080: return new TestTermPositions(in.termPositions());
081: }
082: }
083:
084: /** Main for running test case by itself. */
085: public static void main(String args[]) {
086: TestRunner.run(new TestSuite(TestIndexReader.class));
087: }
088:
089: /**
090: * Tests the IndexReader.getFieldNames implementation
091: * @throws Exception on error
092: */
093: public void testFilterIndexReader() throws Exception {
094: RAMDirectory directory = new MockRAMDirectory();
095: IndexWriter writer = new IndexWriter(directory,
096: new WhitespaceAnalyzer(), true);
097:
098: Document d1 = new Document();
099: d1.add(new Field("default", "one two", Field.Store.YES,
100: Field.Index.TOKENIZED));
101: writer.addDocument(d1);
102:
103: Document d2 = new Document();
104: d2.add(new Field("default", "one three", Field.Store.YES,
105: Field.Index.TOKENIZED));
106: writer.addDocument(d2);
107:
108: Document d3 = new Document();
109: d3.add(new Field("default", "two four", Field.Store.YES,
110: Field.Index.TOKENIZED));
111: writer.addDocument(d3);
112:
113: writer.close();
114:
115: IndexReader reader = new TestReader(IndexReader.open(directory));
116:
117: assertTrue(reader.isOptimized());
118:
119: TermEnum terms = reader.terms();
120: while (terms.next()) {
121: assertTrue(terms.term().text().indexOf('e') != -1);
122: }
123: terms.close();
124:
125: TermPositions positions = reader.termPositions(new Term(
126: "default", "one"));
127: while (positions.next()) {
128: assertTrue((positions.doc() % 2) == 1);
129: }
130:
131: reader.close();
132: directory.close();
133: }
134: }
|