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.document.Document; 22 import org.apache.lucene.document.Field; 23 24 import java.io.IOException ; 25 import java.util.Collection ; 26 import java.util.Iterator ; 27 import java.util.Enumeration ; 28 29 public class TestSegmentReader extends TestCase { 30 private RAMDirectory dir = new RAMDirectory(); 31 private Document testDoc = new Document(); 32 private SegmentReader reader = null; 33 34 public TestSegmentReader(String s) { 35 super(s); 36 } 37 38 protected void setUp() { 40 41 try { 42 DocHelper.setupDoc(testDoc); 43 DocHelper.writeDoc(dir, testDoc); 44 reader = new SegmentReader(new SegmentInfo("test", 1, dir)); 45 } catch (IOException e) { 46 47 } 48 } 49 50 protected void tearDown() { 51 52 } 53 54 public void test() { 55 assertTrue(dir != null); 56 assertTrue(reader != null); 57 assertTrue(DocHelper.nameValues.size() > 0); 58 assertTrue(DocHelper.numFields(testDoc) == 6); 59 } 60 61 public void testDocument() { 62 try { 63 assertTrue(reader.numDocs() == 1); 64 assertTrue(reader.maxDoc() >= 1); 65 Document result = reader.document(0); 66 assertTrue(result != null); 67 assertTrue(DocHelper.numFields(result) == DocHelper.numFields(testDoc) - 2); 69 70 Enumeration fields = result.fields(); 71 while (fields.hasMoreElements()) { 72 Field field = (Field) fields.nextElement(); 73 assertTrue(field != null); 74 assertTrue(DocHelper.nameValues.containsKey(field.name())); 75 } 76 } catch (IOException e) { 77 e.printStackTrace(); 78 assertTrue(false); 79 } 80 } 81 82 public void testDelete() { 83 Document docToDelete = new Document(); 84 DocHelper.setupDoc(docToDelete); 85 DocHelper.writeDoc(dir, "seg-to-delete", docToDelete); 86 try { 87 SegmentReader deleteReader = new SegmentReader(new SegmentInfo("seg-to-delete", 1, dir)); 88 assertTrue(deleteReader != null); 89 assertTrue(deleteReader.numDocs() == 1); 90 deleteReader.delete(0); 91 assertTrue(deleteReader.isDeleted(0) == true); 92 assertTrue(deleteReader.hasDeletions() == true); 93 assertTrue(deleteReader.numDocs() == 0); 94 try { 95 Document test = deleteReader.document(0); 96 assertTrue(false); 97 } catch (IllegalArgumentException e) { 98 assertTrue(true); 99 } 100 } catch (IOException e) { 101 e.printStackTrace(); 102 assertTrue(false); 103 } 104 } 105 106 public void testGetFieldNameVariations() { 107 try { 108 Collection result = reader.getFieldNames(); 109 assertTrue(result != null); 110 assertTrue(result.size() == 7); 111 for (Iterator iter = result.iterator(); iter.hasNext();) { 112 String s = (String ) iter.next(); 113 assertTrue(DocHelper.nameValues.containsKey(s) == true || s.equals("")); 115 } 116 result = reader.getFieldNames(true); 117 assertTrue(result != null); 118 assertTrue(result.size() == 5); 120 for (Iterator iter = result.iterator(); iter.hasNext();) { 121 String s = (String ) iter.next(); 122 assertTrue(DocHelper.nameValues.containsKey(s) == true || s.equals("")); 123 } 124 125 result = reader.getFieldNames(false); 126 assertTrue(result != null); 127 assertTrue(result.size() == 2); 128 result = reader.getIndexedFieldNames(true); 130 assertTrue(result != null); 131 assertTrue(result.size() == 2); 132 133 result = reader.getIndexedFieldNames(false); 134 assertTrue(result != null); 135 assertTrue(result.size() == 3); 136 137 } catch (IOException e) { 138 e.printStackTrace(); 139 assertTrue(false); 140 } 141 } 142 143 public void testTerms() { 144 try { 145 TermEnum terms = reader.terms(); 146 assertTrue(terms != null); 147 while (terms.next() == true) 148 { 149 Term term = terms.term(); 150 assertTrue(term != null); 151 String fieldValue = (String )DocHelper.nameValues.get(term.field()); 153 assertTrue(fieldValue.indexOf(term.text()) != -1); 154 } 155 156 TermDocs termDocs = reader.termDocs(); 157 assertTrue(termDocs != null); 158 termDocs.seek(new Term(DocHelper.TEXT_FIELD_1_KEY, "field")); 159 assertTrue(termDocs.next() == true); 160 161 TermPositions positions = reader.termPositions(); 162 positions.seek(new Term(DocHelper.TEXT_FIELD_1_KEY, "field")); 163 assertTrue(positions != null); 164 assertTrue(positions.doc() == 0); 165 assertTrue(positions.nextPosition() >= 0); 166 167 } catch (IOException e) { 168 e.printStackTrace(); 169 assertTrue(false); 170 } 171 } 172 173 public void testNorms() { 174 185 186 } 187 188 public void testTermVectors() { 189 try { 190 TermFreqVector result = reader.getTermFreqVector(0, DocHelper.TEXT_FIELD_2_KEY); 191 assertTrue(result != null); 192 String [] terms = result.getTerms(); 193 int [] freqs = result.getTermFrequencies(); 194 assertTrue(terms != null && terms.length == 3 && freqs != null && freqs.length == 3); 195 for (int i = 0; i < terms.length; i++) { 196 String term = terms[i]; 197 int freq = freqs[i]; 198 assertTrue(DocHelper.FIELD_2_TEXT.indexOf(term) != -1); 199 assertTrue(freq > 0); 200 } 201 202 TermFreqVector [] results = reader.getTermFreqVectors(0); 203 assertTrue(results != null); 204 assertTrue(results.length == 2); 205 206 } catch (IOException e) { 207 e.printStackTrace(); 208 assertTrue(false); 209 } 210 } 211 212 } 213 | Popular Tags |