1 package org.apache.lucene.index; 2 3 18 19 import junit.framework.TestCase; 20 import org.apache.lucene.store.RAMDirectory; 21 22 import java.io.IOException ; 23 24 public class TestTermVectorsWriter extends TestCase { 25 26 private String [] testTerms = {"this", "is", "a", "test"}; 27 private String [] testFields = {"f1", "f2", "f3"}; 28 private int[][] positions = new int[testTerms.length][]; 29 private RAMDirectory dir = new RAMDirectory(); 30 private String seg = "testSegment"; 31 private FieldInfos fieldInfos = new FieldInfos(); 32 33 public TestTermVectorsWriter(String s) { 34 super(s); 35 } 36 37 protected void setUp() { 38 39 for (int i = 0; i < testFields.length; i++) { 40 fieldInfos.add(testFields[i], true, true); 41 } 42 43 44 for (int i = 0; i < testTerms.length; i++) { 45 positions[i] = new int[5]; 46 for (int j = 0; j < positions[i].length; j++) { 47 positions[i][j] = i * 100; 48 } 49 } 50 } 51 52 protected void tearDown() { 53 } 54 55 public void test() { 56 assertTrue(dir != null); 57 assertTrue(positions != null); 58 } 59 60 87 88 public void testWriter() { 89 try { 90 TermVectorsWriter writer = new TermVectorsWriter(dir, seg, fieldInfos); 91 writer.openDocument(); 92 assertTrue(writer.isDocumentOpen() == true); 93 writeField(writer, testFields[0]); 94 writer.closeDocument(); 95 writer.close(); 96 assertTrue(writer.isDocumentOpen() == false); 97 assertTrue(dir.fileExists(seg + TermVectorsWriter.TVD_EXTENSION)); 99 assertTrue(dir.fileExists(seg + TermVectorsWriter.TVX_EXTENSION)); 100 TermVectorsReader reader = new TermVectorsReader(dir, seg, fieldInfos); 102 assertTrue(reader != null); 103 checkTermVector(reader, 0, testFields[0]); 104 } catch (IOException e) { 105 e.printStackTrace(); 106 assertTrue(false); 107 } 108 } 109 private void checkTermVector(TermVectorsReader reader, int docNum, String field) throws IOException { 110 TermFreqVector vector = reader.get(docNum, field); 111 assertTrue(vector != null); 112 String [] terms = vector.getTerms(); 113 assertTrue(terms != null); 114 assertTrue(terms.length == testTerms.length); 115 for (int i = 0; i < terms.length; i++) { 116 String term = terms[i]; 117 assertTrue(term.equals(testTerms[i])); 118 } 119 } 120 121 124 public void testMultipleFields() { 125 try { 126 TermVectorsWriter writer = new TermVectorsWriter(dir, seg, fieldInfos); 127 writeDocument(writer, testFields.length); 128 129 writer.close(); 130 131 assertTrue(writer.isDocumentOpen() == false); 132 assertTrue(dir.fileExists(seg + TermVectorsWriter.TVD_EXTENSION)); 134 assertTrue(dir.fileExists(seg + TermVectorsWriter.TVX_EXTENSION)); 135 TermVectorsReader reader = new TermVectorsReader(dir, seg, fieldInfos); 137 assertTrue(reader != null); 138 139 for (int j = 0; j < testFields.length; j++) { 140 checkTermVector(reader, 0, testFields[j]); 141 } 142 } catch (IOException e) { 143 e.printStackTrace(); 144 assertTrue(false); 145 } 146 } 147 148 private void writeDocument(TermVectorsWriter writer, int numFields) throws IOException { 149 writer.openDocument(); 150 assertTrue(writer.isDocumentOpen() == true); 151 152 for (int j = 0; j < numFields; j++) { 153 writeField(writer, testFields[j]); 154 } 155 writer.closeDocument(); 156 assertTrue(writer.isDocumentOpen() == false); 157 } 158 159 165 private void writeField(TermVectorsWriter writer, String f) throws IOException { 166 writer.openField(f); 167 assertTrue(writer.isFieldOpen() == true); 168 for (int i = 0; i < testTerms.length; i++) { 169 writer.addTerm(testTerms[i], i); 170 } 171 writer.closeField(); 172 } 173 174 175 public void testMultipleDocuments() { 176 177 try { 178 TermVectorsWriter writer = new TermVectorsWriter(dir, seg, fieldInfos); 179 assertTrue(writer != null); 180 for (int i = 0; i < 10; i++) { 181 writeDocument(writer, testFields.length); 182 } 183 writer.close(); 184 } catch (IOException e) { 185 e.printStackTrace(); 186 assertTrue(false); 187 } 188 try { 190 TermVectorsReader reader = new TermVectorsReader(dir, seg, fieldInfos); 191 for (int i = 0; i < 10; i++) { 192 assertTrue(reader != null); 193 checkTermVector(reader, 5, testFields[0]); 194 checkTermVector(reader, 2, testFields[2]); 195 } 196 } catch (IOException e) { 197 e.printStackTrace(); 198 assertTrue(false); 199 } 200 } 201 202 } 203 | Popular Tags |