1 package org.apache.lucene.index; 2 3 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 ; 38 import java.io.IOException ; 39 40 public class TestFilterIndexReader extends TestCase { 41 42 private static class TestReader extends FilterIndexReader { 43 44 45 private static class TestTermEnum extends FilterTermEnum { 46 public TestTermEnum(TermEnum termEnum) 47 throws IOException { 48 super(termEnum); 49 } 50 51 52 public boolean next() throws IOException { 53 while (in.next()) { 54 if (in.term().text().indexOf('e') != -1) 55 return true; 56 } 57 return false; 58 } 59 } 60 61 62 private static class TestTermPositions extends FilterTermPositions { 63 public TestTermPositions(TermPositions in) 64 throws IOException { 65 super(in); 66 } 67 68 69 public boolean next() throws IOException { 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 83 public TermEnum terms() throws IOException { 84 return new TestTermEnum(in.terms()); 85 } 86 87 88 public TermPositions termPositions() throws IOException { 89 return new TestTermPositions(in.termPositions()); 90 } 91 } 92 93 94 95 public static void main(String args[]) { 96 TestRunner.run (new TestSuite(TestIndexReader.class)); 97 } 98 99 103 public void testFilterIndexReader() throws Exception { 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 |