001: package org.apache.lucene.index;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.apache.lucene.util.LuceneTestCase;
026:
027: import org.apache.lucene.document.Document;
028: import org.apache.lucene.document.Fieldable;
029: import org.apache.lucene.search.DefaultSimilarity;
030: import org.apache.lucene.store.RAMDirectory;
031: import org.apache.lucene.store.MockRAMDirectory;
032: import org.apache.lucene.analysis.WhitespaceAnalyzer;
033: import org.apache.lucene.search.Similarity;
034:
035: public class TestSegmentReader extends LuceneTestCase {
036: private RAMDirectory dir = new RAMDirectory();
037: private Document testDoc = new Document();
038: private SegmentReader reader = null;
039:
040: public TestSegmentReader(String s) {
041: super (s);
042: }
043:
044: //TODO: Setup the reader w/ multiple documents
045: protected void setUp() throws Exception {
046: super .setUp();
047: DocHelper.setupDoc(testDoc);
048: SegmentInfo info = DocHelper.writeDoc(dir, testDoc);
049: reader = SegmentReader.get(info);
050: }
051:
052: public void test() {
053: assertTrue(dir != null);
054: assertTrue(reader != null);
055: assertTrue(DocHelper.nameValues.size() > 0);
056: assertTrue(DocHelper.numFields(testDoc) == DocHelper.all.size());
057: }
058:
059: public void testDocument() throws IOException {
060: assertTrue(reader.numDocs() == 1);
061: assertTrue(reader.maxDoc() >= 1);
062: Document result = reader.document(0);
063: assertTrue(result != null);
064: //There are 2 unstored fields on the document that are not preserved across writing
065: assertTrue(DocHelper.numFields(result) == DocHelper
066: .numFields(testDoc)
067: - DocHelper.unstored.size());
068:
069: List fields = result.getFields();
070: for (Iterator iter = fields.iterator(); iter.hasNext();) {
071: Fieldable field = (Fieldable) iter.next();
072: assertTrue(field != null);
073: assertTrue(DocHelper.nameValues.containsKey(field.name()));
074: }
075: }
076:
077: public void testDelete() throws IOException {
078: Document docToDelete = new Document();
079: DocHelper.setupDoc(docToDelete);
080: SegmentInfo info = DocHelper.writeDoc(dir, docToDelete);
081: SegmentReader deleteReader = SegmentReader.get(info);
082: assertTrue(deleteReader != null);
083: assertTrue(deleteReader.numDocs() == 1);
084: deleteReader.deleteDocument(0);
085: assertTrue(deleteReader.isDeleted(0) == true);
086: assertTrue(deleteReader.hasDeletions() == true);
087: assertTrue(deleteReader.numDocs() == 0);
088: try {
089: deleteReader.document(0);
090: fail();
091: } catch (IllegalArgumentException e) {
092: // expcected exception
093: }
094: }
095:
096: public void testGetFieldNameVariations() {
097: Collection result = reader
098: .getFieldNames(IndexReader.FieldOption.ALL);
099: assertTrue(result != null);
100: assertTrue(result.size() == DocHelper.all.size());
101: for (Iterator iter = result.iterator(); iter.hasNext();) {
102: String s = (String) iter.next();
103: //System.out.println("Name: " + s);
104: assertTrue(DocHelper.nameValues.containsKey(s) == true
105: || s.equals(""));
106: }
107: result = reader.getFieldNames(IndexReader.FieldOption.INDEXED);
108: assertTrue(result != null);
109: assertTrue(result.size() == DocHelper.indexed.size());
110: for (Iterator iter = result.iterator(); iter.hasNext();) {
111: String s = (String) iter.next();
112: assertTrue(DocHelper.indexed.containsKey(s) == true
113: || s.equals(""));
114: }
115:
116: result = reader
117: .getFieldNames(IndexReader.FieldOption.UNINDEXED);
118: assertTrue(result != null);
119: assertTrue(result.size() == DocHelper.unindexed.size());
120: //Get all indexed fields that are storing term vectors
121: result = reader
122: .getFieldNames(IndexReader.FieldOption.INDEXED_WITH_TERMVECTOR);
123: assertTrue(result != null);
124: assertTrue(result.size() == DocHelper.termvector.size());
125:
126: result = reader
127: .getFieldNames(IndexReader.FieldOption.INDEXED_NO_TERMVECTOR);
128: assertTrue(result != null);
129: assertTrue(result.size() == DocHelper.notermvector.size());
130: }
131:
132: public void testTerms() throws IOException {
133: TermEnum terms = reader.terms();
134: assertTrue(terms != null);
135: while (terms.next() == true) {
136: Term term = terms.term();
137: assertTrue(term != null);
138: //System.out.println("Term: " + term);
139: String fieldValue = (String) DocHelper.nameValues.get(term
140: .field());
141: assertTrue(fieldValue.indexOf(term.text()) != -1);
142: }
143:
144: TermDocs termDocs = reader.termDocs();
145: assertTrue(termDocs != null);
146: termDocs.seek(new Term(DocHelper.TEXT_FIELD_1_KEY, "field"));
147: assertTrue(termDocs.next() == true);
148:
149: termDocs.seek(new Term(DocHelper.NO_NORMS_KEY,
150: DocHelper.NO_NORMS_TEXT));
151: assertTrue(termDocs.next() == true);
152:
153: TermPositions positions = reader.termPositions();
154: positions.seek(new Term(DocHelper.TEXT_FIELD_1_KEY, "field"));
155: assertTrue(positions != null);
156: assertTrue(positions.doc() == 0);
157: assertTrue(positions.nextPosition() >= 0);
158: }
159:
160: public void testNorms() throws IOException {
161: //TODO: Not sure how these work/should be tested
162: /*
163: try {
164: byte [] norms = reader.norms(DocHelper.TEXT_FIELD_1_KEY);
165: System.out.println("Norms: " + norms);
166: assertTrue(norms != null);
167: } catch (IOException e) {
168: e.printStackTrace();
169: assertTrue(false);
170: }
171: */
172:
173: checkNorms(reader);
174: }
175:
176: public static void checkNorms(IndexReader reader)
177: throws IOException {
178: // test omit norms
179: for (int i = 0; i < DocHelper.fields.length; i++) {
180: Fieldable f = DocHelper.fields[i];
181: if (f.isIndexed()) {
182: assertEquals(reader.hasNorms(f.name()), !f
183: .getOmitNorms());
184: assertEquals(reader.hasNorms(f.name()),
185: !DocHelper.noNorms.containsKey(f.name()));
186: if (!reader.hasNorms(f.name())) {
187: // test for fake norms of 1.0
188: byte[] norms = reader.norms(f.name());
189: assertEquals(norms.length, reader.maxDoc());
190: for (int j = 0; j < reader.maxDoc(); j++) {
191: assertEquals(norms[j], DefaultSimilarity
192: .encodeNorm(1.0f));
193: }
194: norms = new byte[reader.maxDoc()];
195: reader.norms(f.name(), norms, 0);
196: for (int j = 0; j < reader.maxDoc(); j++) {
197: assertEquals(norms[j], DefaultSimilarity
198: .encodeNorm(1.0f));
199: }
200: }
201: }
202: }
203: }
204:
205: public void testTermVectors() throws IOException {
206: TermFreqVector result = reader.getTermFreqVector(0,
207: DocHelper.TEXT_FIELD_2_KEY);
208: assertTrue(result != null);
209: String[] terms = result.getTerms();
210: int[] freqs = result.getTermFrequencies();
211: assertTrue(terms != null && terms.length == 3 && freqs != null
212: && freqs.length == 3);
213: for (int i = 0; i < terms.length; i++) {
214: String term = terms[i];
215: int freq = freqs[i];
216: assertTrue(DocHelper.FIELD_2_TEXT.indexOf(term) != -1);
217: assertTrue(freq > 0);
218: }
219:
220: TermFreqVector[] results = reader.getTermFreqVectors(0);
221: assertTrue(results != null);
222: assertTrue("We do not have 4 term freq vectors, we have: "
223: + results.length, results.length == 4);
224: }
225:
226: public void testIndexDivisor() throws IOException {
227: dir = new MockRAMDirectory();
228: testDoc = new Document();
229: DocHelper.setupDoc(testDoc);
230: SegmentInfo si = DocHelper.writeDoc(dir, testDoc);
231:
232: reader = SegmentReader.get(si);
233: reader.setTermInfosIndexDivisor(3);
234: testDocument();
235: testDelete();
236: testGetFieldNameVariations();
237: testNorms();
238: testTerms();
239: testTermVectors();
240: }
241: }
|