1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.util.LuceneTestCase;
22 import org.apache.lucene.analysis.MockAnalyzer;
23 import org.apache.lucene.document.Document;
24 import org.apache.lucene.document.Field;
25 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
26 import org.apache.lucene.store.Directory;
28 import java.io.IOException;
29 import java.util.Random;
31 public class TestDirectoryReader extends LuceneTestCase {
32 protected Directory dir;
33 private Document doc1;
34 private Document doc2;
35 protected SegmentReader [] readers = new SegmentReader[2];
36 protected SegmentInfos sis;
39 public void setUp() throws Exception {
42 doc1 = new Document();
43 doc2 = new Document();
44 DocHelper.setupDoc(doc1);
45 DocHelper.setupDoc(doc2);
46 DocHelper.writeDoc(random, dir, doc1);
47 DocHelper.writeDoc(random, dir, doc2);
48 sis = new SegmentInfos();
53 public void tearDown() throws Exception {
54 if (readers[0] != null) readers[0].close();
55 if (readers[1] != null) readers[1].close();
60 protected IndexReader openReader() throws IOException {
62 reader = IndexReader.open(dir, false);
63 assertTrue(reader instanceof DirectoryReader);
65 assertTrue(dir != null);
66 assertTrue(sis != null);
67 assertTrue(reader != null);
72 public void test() throws Exception {
77 public void doTestDocument() throws IOException {
79 IndexReader reader = openReader();
80 assertTrue(reader != null);
81 Document newDoc1 = reader.document(0);
82 assertTrue(newDoc1 != null);
83 assertTrue(DocHelper.numFields(newDoc1) == DocHelper.numFields(doc1) - DocHelper.unstored.size());
84 Document newDoc2 = reader.document(1);
85 assertTrue(newDoc2 != null);
86 assertTrue(DocHelper.numFields(newDoc2) == DocHelper.numFields(doc2) - DocHelper.unstored.size());
87 TermFreqVector vector = reader.getTermFreqVector(0, DocHelper.TEXT_FIELD_2_KEY);
88 assertTrue(vector != null);
89 TestSegmentReader.checkNorms(reader);
93 public void doTestUndeleteAll() throws IOException {
95 IndexReader reader = openReader();
96 assertTrue(reader != null);
97 assertEquals( 2, reader.numDocs() );
98 reader.deleteDocument(0);
99 assertEquals( 1, reader.numDocs() );
100 reader.undeleteAll();
101 assertEquals( 2, reader.numDocs() );
103 // Ensure undeleteAll survives commit/close/reopen:
107 if (reader instanceof MultiReader)
108 // MultiReader does not "own" the directory so it does
109 // not write the changes to sis on commit:
113 reader = openReader();
114 assertEquals( 2, reader.numDocs() );
116 reader.deleteDocument(0);
117 assertEquals( 1, reader.numDocs() );
120 if (reader instanceof MultiReader)
121 // MultiReader does not "own" the directory so it does
122 // not write the changes to sis on commit:
125 reader = openReader();
126 assertEquals( 1, reader.numDocs() );
130 public void testIsCurrent() throws IOException {
131 Directory ramDir1=newDirectory();
132 addDoc(random, ramDir1, "test foo", true);
133 Directory ramDir2=newDirectory();
134 addDoc(random, ramDir2, "test blah", true);
135 IndexReader[] readers = new IndexReader[]{IndexReader.open(ramDir1, false), IndexReader.open(ramDir2, false)};
136 MultiReader mr = new MultiReader(readers);
137 assertTrue(mr.isCurrent()); // just opened, must be current
138 addDoc(random, ramDir1, "more text", false);
139 assertFalse(mr.isCurrent()); // has been modified, not current anymore
140 addDoc(random, ramDir2, "even more text", false);
141 assertFalse(mr.isCurrent()); // has been modified even more, not current anymore
145 } catch (UnsupportedOperationException e) {
146 // expected exception
153 public void testMultiTermDocs() throws IOException {
154 Directory ramDir1=newDirectory();
155 addDoc(random, ramDir1, "test foo", true);
156 Directory ramDir2=newDirectory();
157 addDoc(random, ramDir2, "test blah", true);
158 Directory ramDir3=newDirectory();
159 addDoc(random, ramDir3, "test wow", true);
161 IndexReader[] readers1 = new IndexReader[]{IndexReader.open(ramDir1, false), IndexReader.open(ramDir3, false)};
162 IndexReader[] readers2 = new IndexReader[]{IndexReader.open(ramDir1, false), IndexReader.open(ramDir2, false), IndexReader.open(ramDir3, false)};
163 MultiReader mr2 = new MultiReader(readers1);
164 MultiReader mr3 = new MultiReader(readers2);
166 // test mixing up TermDocs and TermEnums from different readers.
167 TermDocs td2 = mr2.termDocs();
168 TermEnum te3 = mr3.terms(new Term("body","wow"));
172 // This should blow up if we forget to check that the TermEnum is from the same
173 // reader as the TermDocs.
174 while (td2.next()) ret += td2.doc();
178 // really a dummy assert to ensure that we got some docs and to ensure that
179 // nothing is eliminated by hotspot
191 public void testAllTermDocs() throws IOException {
192 IndexReader reader = openReader();
194 TermDocs td = reader.termDocs(null);
195 for(int i=0;i<NUM_DOCS;i++) {
196 assertTrue(td.next());
197 assertEquals(i, td.doc());
198 assertEquals(1, td.freq());
204 private void addDoc(Random random, Directory ramDir1, String s, boolean create) throws IOException {
205 IndexWriter iw = new IndexWriter(ramDir1, newIndexWriterConfig(
206 TEST_VERSION_CURRENT,
207 new MockAnalyzer(random)).setOpenMode(
208 create ? OpenMode.CREATE : OpenMode.APPEND));
209 Document doc = new Document();
210 doc.add(newField("body", s, Field.Store.YES, Field.Index.ANALYZED));