KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > index > TestTermVectorsReader


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 JavaDoc;
8 import java.util.Arrays JavaDoc;
9
10 public class TestTermVectorsReader extends TestCase {
11   private TermVectorsWriter writer = null;
12   //Must be lexicographically sorted, will do in setup, versus trying to maintain here
13
private String JavaDoc [] testFields = {"f1", "f2", "f3"};
14   private String JavaDoc [] testTerms = {"this", "is", "a", "test"};
15   private RAMDirectory dir = new RAMDirectory();
16   private String JavaDoc seg = "testSegment";
17   private FieldInfos fieldInfos = new FieldInfos();
18
19   public TestTermVectorsReader(String JavaDoc 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 JavaDoc e) {
46       e.printStackTrace();
47       assertTrue(false);
48     }
49   }
50
51   protected void tearDown() {
52
53   }
54
55   public void test() {
56       //Check to see the files were created properly in setup
57
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 JavaDoc [] terms = vector.getTerms();
69       assertTrue(terms != null);
70       assertTrue(terms.length == testTerms.length);
71       for (int i = 0; i < terms.length; i++) {
72         String JavaDoc term = terms[i];
73         //System.out.println("Term: " + term);
74
assertTrue(term.equals(testTerms[i]));
75       }
76       
77     } catch (IOException JavaDoc e) {
78       e.printStackTrace();
79       assertTrue(false);
80     }
81   }
82
83   /**
84    * Make sure exceptions and bad params are handled appropriately
85    */

86   public void testBadParams() {
87     try {
88       TermVectorsReader reader = new TermVectorsReader(dir, seg, fieldInfos);
89       assertTrue(reader != null);
90       //Bad document number, good field number
91
TermFreqVector vector = reader.get(50, testFields[0]);
92       assertTrue(vector == null);
93     } catch (Exception JavaDoc e) {
94       assertTrue(false);
95     }
96     try {
97       TermVectorsReader reader = new TermVectorsReader(dir, seg, fieldInfos);
98       assertTrue(reader != null);
99       //good document number, bad field number
100
TermFreqVector vector = reader.get(0, "f50");
101       assertTrue(vector == null);
102     } catch (Exception JavaDoc e) {
103       assertTrue(false);
104     }
105   }
106 }
107
Popular Tags