KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.lucene.document.Document;
22 import org.apache.lucene.document.Field;
23
24 import java.io.IOException JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Enumeration JavaDoc;
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 JavaDoc s) {
35     super(s);
36   }
37   
38   //TODO: Setup the reader w/ multiple documents
39
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 JavaDoc 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       //There are 2 unstored fields on the document that are not preserved across writing
68
assertTrue(DocHelper.numFields(result) == DocHelper.numFields(testDoc) - 2);
69       
70       Enumeration JavaDoc 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 JavaDoc 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 JavaDoc e) {
98         assertTrue(true);
99       }
100     } catch (IOException JavaDoc e) {
101       e.printStackTrace();
102       assertTrue(false);
103     }
104   }
105   
106   public void testGetFieldNameVariations() {
107     try {
108       Collection JavaDoc result = reader.getFieldNames();
109       assertTrue(result != null);
110       assertTrue(result.size() == 7);
111       for (Iterator JavaDoc iter = result.iterator(); iter.hasNext();) {
112         String JavaDoc s = (String JavaDoc) iter.next();
113         //System.out.println("Name: " + s);
114
assertTrue(DocHelper.nameValues.containsKey(s) == true || s.equals(""));
115       }
116       result = reader.getFieldNames(true);
117       assertTrue(result != null);
118 // System.out.println("Size: " + result.size());
119
assertTrue(result.size() == 5);
120       for (Iterator JavaDoc iter = result.iterator(); iter.hasNext();) {
121         String JavaDoc s = (String JavaDoc) 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       //Get all indexed fields that are storing term vectors
129
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 JavaDoc 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         //System.out.println("Term: " + term);
152
String JavaDoc fieldValue = (String JavaDoc)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 JavaDoc e) {
168       e.printStackTrace();
169       assertTrue(false);
170     }
171   }
172   
173   public void testNorms() {
174     //TODO: Not sure how these work/should be tested
175
/*
176     try {
177       byte [] norms = reader.norms(DocHelper.TEXT_FIELD_1_KEY);
178       System.out.println("Norms: " + norms);
179       assertTrue(norms != null);
180     } catch (IOException e) {
181       e.printStackTrace();
182       assertTrue(false);
183     }
184 */

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 JavaDoc [] 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 JavaDoc 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 JavaDoc e) {
207       e.printStackTrace();
208       assertTrue(false);
209     }
210   }
211   
212 }
213
Popular Tags