1 package org.apache.lucene.index; 2 3 4 import junit.framework.TestCase; 5 import org.apache.lucene.store.RAMDirectory; 6 7 import java.io.IOException ; 8 import java.util.Arrays ; 9 10 public class TestTermVectorsReader extends TestCase { 11 private TermVectorsWriter writer = null; 12 private String [] testFields = {"f1", "f2", "f3"}; 14 private String [] testTerms = {"this", "is", "a", "test"}; 15 private RAMDirectory dir = new RAMDirectory(); 16 private String seg = "testSegment"; 17 private FieldInfos fieldInfos = new FieldInfos(); 18 19 public TestTermVectorsReader(String s) { 20 super(s); 21 } 22 23 protected void setUp() { 24 for (int i = 0; i < testFields.length; i++) { 25 fieldInfos.add(testFields[i], true, true); 26 } 27 28 try { 29 Arrays.sort(testTerms); 30 for (int j = 0; j < 5; j++) { 31 writer = new TermVectorsWriter(dir, seg, fieldInfos); 32 writer.openDocument(); 33 34 for (int k = 0; k < testFields.length; k++) { 35 writer.openField(testFields[k]); 36 for (int i = 0; i < testTerms.length; i++) { 37 writer.addTerm(testTerms[i], i); 38 } 39 writer.closeField(); 40 } 41 writer.closeDocument(); 42 writer.close(); 43 } 44 45 } catch (IOException e) { 46 e.printStackTrace(); 47 assertTrue(false); 48 } 49 } 50 51 protected void tearDown() { 52 53 } 54 55 public void test() { 56 assertTrue(writer.isDocumentOpen() == false); 58 assertTrue(dir.fileExists(seg + TermVectorsWriter.TVD_EXTENSION)); 59 assertTrue(dir.fileExists(seg + TermVectorsWriter.TVX_EXTENSION)); 60 } 61 62 public void testReader() { 63 try { 64 TermVectorsReader reader = new TermVectorsReader(dir, seg, fieldInfos); 65 assertTrue(reader != null); 66 TermFreqVector vector = reader.get(0, testFields[0]); 67 assertTrue(vector != null); 68 String [] terms = vector.getTerms(); 69 assertTrue(terms != null); 70 assertTrue(terms.length == testTerms.length); 71 for (int i = 0; i < terms.length; i++) { 72 String term = terms[i]; 73 assertTrue(term.equals(testTerms[i])); 75 } 76 77 } catch (IOException e) { 78 e.printStackTrace(); 79 assertTrue(false); 80 } 81 } 82 83 86 public void testBadParams() { 87 try { 88 TermVectorsReader reader = new TermVectorsReader(dir, seg, fieldInfos); 89 assertTrue(reader != null); 90 TermFreqVector vector = reader.get(50, testFields[0]); 92 assertTrue(vector == null); 93 } catch (Exception e) { 94 assertTrue(false); 95 } 96 try { 97 TermVectorsReader reader = new TermVectorsReader(dir, seg, fieldInfos); 98 assertTrue(reader != null); 99 TermFreqVector vector = reader.get(0, "f50"); 101 assertTrue(vector == null); 102 } catch (Exception e) { 103 assertTrue(false); 104 } 105 } 106 } 107 | Popular Tags |