1 package org.apache.lucene.index; 2 3 18 19 import junit.framework.TestCase; 20 import org.apache.lucene.store.RAMDirectory; 21 import org.apache.lucene.store.Directory; 22 import org.apache.lucene.analysis.Analyzer; 23 import org.apache.lucene.analysis.WhitespaceAnalyzer; 24 import org.apache.lucene.search.Similarity; 25 import org.apache.lucene.document.Document; 26 import org.apache.lucene.document.Field; 27 28 import java.io.IOException ; 29 30 public class TestSegmentTermDocs extends TestCase { 31 private Document testDoc = new Document(); 32 private Directory dir = new RAMDirectory(); 33 34 public TestSegmentTermDocs(String s) { 35 super(s); 36 } 37 38 protected void setUp() { 39 DocHelper.setupDoc(testDoc); 40 DocHelper.writeDoc(dir, testDoc); 41 } 42 43 44 protected void tearDown() { 45 46 } 47 48 public void test() { 49 assertTrue(dir != null); 50 } 51 52 public void testTermDocs() { 53 try { 54 SegmentReader reader = new SegmentReader(new SegmentInfo("test", 1, dir)); 56 assertTrue(reader != null); 57 SegmentTermDocs segTermDocs = new SegmentTermDocs(reader); 58 assertTrue(segTermDocs != null); 59 segTermDocs.seek(new Term(DocHelper.TEXT_FIELD_2_KEY, "field")); 60 if (segTermDocs.next() == true) 61 { 62 int docId = segTermDocs.doc(); 63 assertTrue(docId == 0); 64 int freq = segTermDocs.freq(); 65 assertTrue(freq == 3); 66 } 67 reader.close(); 68 } catch (IOException e) { 69 assertTrue(false); 70 } 71 } 72 73 public void testBadSeek() { 74 try { 75 SegmentReader reader = new SegmentReader(new SegmentInfo("test", 3, dir)); 77 assertTrue(reader != null); 78 SegmentTermDocs segTermDocs = new SegmentTermDocs(reader); 79 assertTrue(segTermDocs != null); 80 segTermDocs.seek(new Term("textField2", "bad")); 81 assertTrue(segTermDocs.next() == false); 82 reader.close(); 83 } catch (IOException e) { 84 assertTrue(false); 85 } 86 try { 87 SegmentReader reader = new SegmentReader(new SegmentInfo("test", 3, dir)); 89 assertTrue(reader != null); 90 SegmentTermDocs segTermDocs = new SegmentTermDocs(reader); 91 assertTrue(segTermDocs != null); 92 segTermDocs.seek(new Term("junk", "bad")); 93 assertTrue(segTermDocs.next() == false); 94 reader.close(); 95 } catch (IOException e) { 96 assertTrue(false); 97 } 98 } 99 100 public void testSkipTo() { 101 try { 102 Directory dir = new RAMDirectory(); 103 IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true); 104 105 Term ta = new Term("content","aaa"); 106 for(int i = 0; i < 10; i++) 107 addDoc(writer, "aaa aaa aaa aaa"); 108 109 Term tb = new Term("content","bbb"); 110 for(int i = 0; i < 16; i++) 111 addDoc(writer, "bbb bbb bbb bbb"); 112 113 Term tc = new Term("content","ccc"); 114 for(int i = 0; i < 50; i++) 115 addDoc(writer, "ccc ccc ccc ccc"); 116 117 writer.optimize(); 119 writer.close(); 120 121 IndexReader reader = IndexReader.open(dir); 122 TermDocs tdocs = reader.termDocs(); 123 124 126 tdocs.seek(ta); 128 assertTrue(tdocs.next()); 129 assertEquals(0, tdocs.doc()); 130 assertEquals(4, tdocs.freq()); 131 assertTrue(tdocs.next()); 132 assertEquals(1, tdocs.doc()); 133 assertEquals(4, tdocs.freq()); 134 assertTrue(tdocs.skipTo(0)); 135 assertEquals(2, tdocs.doc()); 136 assertTrue(tdocs.skipTo(4)); 137 assertEquals(4, tdocs.doc()); 138 assertTrue(tdocs.skipTo(9)); 139 assertEquals(9, tdocs.doc()); 140 assertFalse(tdocs.skipTo(10)); 141 142 tdocs.seek(ta); 144 assertTrue(tdocs.skipTo(0)); 145 assertEquals(0, tdocs.doc()); 146 assertTrue(tdocs.skipTo(4)); 147 assertEquals(4, tdocs.doc()); 148 assertTrue(tdocs.skipTo(9)); 149 assertEquals(9, tdocs.doc()); 150 assertFalse(tdocs.skipTo(10)); 151 152 154 tdocs.seek(tb); 156 assertTrue(tdocs.next()); 157 assertEquals(10, tdocs.doc()); 158 assertEquals(4, tdocs.freq()); 159 assertTrue(tdocs.next()); 160 assertEquals(11, tdocs.doc()); 161 assertEquals(4, tdocs.freq()); 162 assertTrue(tdocs.skipTo(5)); 163 assertEquals(12, tdocs.doc()); 164 assertTrue(tdocs.skipTo(15)); 165 assertEquals(15, tdocs.doc()); 166 assertTrue(tdocs.skipTo(24)); 167 assertEquals(24, tdocs.doc()); 168 assertTrue(tdocs.skipTo(25)); 169 assertEquals(25, tdocs.doc()); 170 assertFalse(tdocs.skipTo(26)); 171 172 tdocs.seek(tb); 174 assertTrue(tdocs.skipTo(5)); 175 assertEquals(10, tdocs.doc()); 176 assertTrue(tdocs.skipTo(15)); 177 assertEquals(15, tdocs.doc()); 178 assertTrue(tdocs.skipTo(24)); 179 assertEquals(24, tdocs.doc()); 180 assertTrue(tdocs.skipTo(25)); 181 assertEquals(25, tdocs.doc()); 182 assertFalse(tdocs.skipTo(26)); 183 184 186 tdocs.seek(tc); 188 assertTrue(tdocs.next()); 189 assertEquals(26, tdocs.doc()); 190 assertEquals(4, tdocs.freq()); 191 assertTrue(tdocs.next()); 192 assertEquals(27, tdocs.doc()); 193 assertEquals(4, tdocs.freq()); 194 assertTrue(tdocs.skipTo(5)); 195 assertEquals(28, tdocs.doc()); 196 assertTrue(tdocs.skipTo(40)); 197 assertEquals(40, tdocs.doc()); 198 assertTrue(tdocs.skipTo(57)); 199 assertEquals(57, tdocs.doc()); 200 assertTrue(tdocs.skipTo(74)); 201 assertEquals(74, tdocs.doc()); 202 assertTrue(tdocs.skipTo(75)); 203 assertEquals(75, tdocs.doc()); 204 assertFalse(tdocs.skipTo(76)); 205 206 tdocs.seek(tc); 208 assertTrue(tdocs.skipTo(5)); 209 assertEquals(26, tdocs.doc()); 210 assertTrue(tdocs.skipTo(40)); 211 assertEquals(40, tdocs.doc()); 212 assertTrue(tdocs.skipTo(57)); 213 assertEquals(57, tdocs.doc()); 214 assertTrue(tdocs.skipTo(74)); 215 assertEquals(74, tdocs.doc()); 216 assertTrue(tdocs.skipTo(75)); 217 assertEquals(75, tdocs.doc()); 218 assertFalse(tdocs.skipTo(76)); 219 220 tdocs.close(); 221 reader.close(); 222 dir.close(); 223 } catch (IOException e) { 224 assertTrue(false); 225 } 226 } 227 228 private void addDoc(IndexWriter writer, String value) throws IOException 229 { 230 Document doc = new Document(); 231 doc.add(Field.UnStored("content", value)); 232 writer.addDocument(doc); 233 } 234 } 235 | Popular Tags |