pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestDirectoryReader.java
1 package org.apache.lucene.index;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import org.apache.lucene.util.LuceneTestCase;
21
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;
27
28 import java.io.IOException;
29 import java.util.Random;
30
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;
37
38   @Override
39   public void setUp() throws Exception {
40     super.setUp();
41     dir = newDirectory();
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();
49     sis.read(dir);
50   }
51   
52   @Override
53   public void tearDown() throws Exception {
54     if (readers[0] != null) readers[0].close();
55     if (readers[1] != null) readers[1].close();
56     dir.close();
57     super.tearDown();
58   }
59
60   protected IndexReader openReader() throws IOException {
61     IndexReader reader;
62     reader = IndexReader.open(dir, false);
63     assertTrue(reader instanceof DirectoryReader);
64
65     assertTrue(dir != null);
66     assertTrue(sis != null);
67     assertTrue(reader != null);
68     
69     return reader;
70   }
71
72   public void test() throws Exception {
73     doTestDocument();
74     doTestUndeleteAll();
75   }    
76
77   public void doTestDocument() throws IOException {
78     sis.read(dir);
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);
90     reader.close();
91   }
92
93   public void doTestUndeleteAll() throws IOException {
94     sis.read(dir);
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() );
102
103     // Ensure undeleteAll survives commit/close/reopen:
104     reader.commit();
105     reader.close();
106
107     if (reader instanceof MultiReader)
108       // MultiReader does not "own" the directory so it does
109       // not write the changes to sis on commit:
110       sis.commit(dir);
111
112     sis.read(dir);
113     reader = openReader();
114     assertEquals( 2, reader.numDocs() );
115
116     reader.deleteDocument(0);
117     assertEquals( 1, reader.numDocs() );
118     reader.commit();
119     reader.close();
120     if (reader instanceof MultiReader)
121       // MultiReader does not "own" the directory so it does
122       // not write the changes to sis on commit:
123       sis.commit(dir);
124     sis.read(dir);
125     reader = openReader();
126     assertEquals( 1, reader.numDocs() );
127     reader.close();
128   }
129         
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
142     try {
143       mr.getVersion();
144       fail();
145     } catch (UnsupportedOperationException e) {
146       // expected exception
147     }
148     mr.close();
149     ramDir1.close();
150     ramDir2.close();
151   }
152
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);
160
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);
165
166     // test mixing up TermDocs and TermEnums from different readers.
167     TermDocs td2 = mr2.termDocs();
168     TermEnum te3 = mr3.terms(new Term("body","wow"));
169     td2.seek(te3);
170     int ret = 0;
171
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();
175     td2.close();
176     te3.close();
177
178     // really a dummy assert to ensure that we got some docs and to ensure that
179     // nothing is eliminated by hotspot
180     assertTrue(ret > 0);
181     readers1[0].close();
182     readers1[1].close();
183     readers2[0].close();
184     readers2[1].close();
185     readers2[2].close();
186     ramDir1.close();
187     ramDir2.close();
188     ramDir3.close();
189   }
190
191   public void testAllTermDocs() throws IOException {
192     IndexReader reader = openReader();
193     int NUM_DOCS = 2;
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());
199     }
200     td.close();
201     reader.close();
202   }
203
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));
211     iw.addDocument(doc);
212     iw.close();
213   }
214 }