KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.lucene.index;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import junit.framework.TestCase;
20 import org.apache.lucene.store.RAMDirectory;
21
22 import java.io.IOException JavaDoc;
23
24 public class TestTermVectorsWriter extends TestCase {
25
26   private String JavaDoc[] testTerms = {"this", "is", "a", "test"};
27   private String JavaDoc [] testFields = {"f1", "f2", "f3"};
28   private int[][] positions = new int[testTerms.length][];
29   private RAMDirectory dir = new RAMDirectory();
30   private String JavaDoc seg = "testSegment";
31   private FieldInfos fieldInfos = new FieldInfos();
32
33   public TestTermVectorsWriter(String JavaDoc 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   /*public void testWriteNoPositions() {
61     try {
62       TermVectorsWriter writer = new TermVectorsWriter(dir, seg, 50);
63       writer.openDocument();
64       assertTrue(writer.isDocumentOpen() == true);
65       writer.openField(0);
66       assertTrue(writer.isFieldOpen() == true);
67       for (int i = 0; i < testTerms.length; i++) {
68         writer.addTerm(testTerms[i], i);
69       }
70       writer.closeField();
71       
72       writer.closeDocument();
73       writer.close();
74       assertTrue(writer.isDocumentOpen() == false);
75       //Check to see the files were created
76       assertTrue(dir.fileExists(seg + TermVectorsWriter.TVD_EXTENSION));
77       assertTrue(dir.fileExists(seg + TermVectorsWriter.TVX_EXTENSION));
78       //Now read it back in
79       TermVectorsReader reader = new TermVectorsReader(dir, seg);
80       assertTrue(reader != null);
81       checkTermVector(reader, 0, 0);
82     } catch (IOException e) {
83       e.printStackTrace();
84       assertTrue(false);
85     }
86   } */

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       //Check to see the files were created
98
assertTrue(dir.fileExists(seg + TermVectorsWriter.TVD_EXTENSION));
99       assertTrue(dir.fileExists(seg + TermVectorsWriter.TVX_EXTENSION));
100       //Now read it back in
101
TermVectorsReader reader = new TermVectorsReader(dir, seg, fieldInfos);
102       assertTrue(reader != null);
103       checkTermVector(reader, 0, testFields[0]);
104     } catch (IOException JavaDoc e) {
105       e.printStackTrace();
106       assertTrue(false);
107     }
108   }
109   private void checkTermVector(TermVectorsReader reader, int docNum, String JavaDoc field) throws IOException JavaDoc {
110     TermFreqVector vector = reader.get(docNum, field);
111     assertTrue(vector != null);
112     String JavaDoc[] terms = vector.getTerms();
113     assertTrue(terms != null);
114     assertTrue(terms.length == testTerms.length);
115     for (int i = 0; i < terms.length; i++) {
116       String JavaDoc term = terms[i];
117       assertTrue(term.equals(testTerms[i]));
118     }
119   }
120
121   /**
122    * Test one document, multiple fields
123    */

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       //Check to see the files were created
133
assertTrue(dir.fileExists(seg + TermVectorsWriter.TVD_EXTENSION));
134       assertTrue(dir.fileExists(seg + TermVectorsWriter.TVX_EXTENSION));
135       //Now read it back in
136
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 JavaDoc e) {
143       e.printStackTrace();
144       assertTrue(false);
145     }
146   }
147
148   private void writeDocument(TermVectorsWriter writer, int numFields) throws IOException JavaDoc {
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   /**
160    *
161    * @param writer The writer to write to
162    * @param j The field number
163    * @throws IOException
164    */

165   private void writeField(TermVectorsWriter writer, String JavaDoc f) throws IOException JavaDoc {
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 JavaDoc e) {
185       e.printStackTrace();
186       assertTrue(false);
187     }
188     //Do some arbitrary tests
189
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 JavaDoc e) {
197       e.printStackTrace();
198       assertTrue(false);
199     }
200   }
201
202 }
203
Popular Tags