pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / index / TestParallelReaderEmptyIndex.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 java.io.IOException;
21
22 import org.apache.lucene.store.Directory;
23 import org.apache.lucene.util.LuceneTestCase;
24
25 import org.apache.lucene.analysis.MockAnalyzer;
26 import org.apache.lucene.document.Document;
27 import org.apache.lucene.document.Field.Index;
28 import org.apache.lucene.document.Field.Store;
29 import org.apache.lucene.document.Field.TermVector;
30 import org.apache.lucene.index.IndexWriterConfig.OpenMode;
31
32 /**
33  * Some tests for {@link ParallelReader}s with empty indexes
34  * 
35  * @author Christian Kohlschuetter
36  */
37 public class TestParallelReaderEmptyIndex extends LuceneTestCase {
38
39   /**
40    * Creates two empty indexes and wraps a ParallelReader around. Adding this
41    * reader to a new index should not throw any exception.
42    * 
43    * @throws IOException
44    */
45   public void testEmptyIndex() throws IOException {
46     Directory rd1 = newDirectory();
47     IndexWriter iw = new IndexWriter(rd1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
48     iw.close();
49
50     Directory rd2 = newDirectory(rd1);
51
52     Directory rdOut = newDirectory();
53
54     IndexWriter iwOut = new IndexWriter(rdOut, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
55     ParallelReader pr = new ParallelReader();
56     pr.add(IndexReader.open(rd1,true));
57     pr.add(IndexReader.open(rd2,true));
58                 
59     // When unpatched, Lucene crashes here with a NoSuchElementException (caused by ParallelTermEnum)
60     iwOut.addIndexes(new IndexReader[] { pr });
61                 
62     iwOut.forceMerge(1);
63     iwOut.close();
64     rdOut.close();
65     rd1.close();
66     rd2.close();
67   }
68
69   /**
70    * This method creates an empty index (numFields=0, numDocs=0) but is marked
71    * to have TermVectors. Adding this index to another index should not throw
72    * any exception.
73    */
74   public void testEmptyIndexWithVectors() throws IOException {
75     Directory rd1 = newDirectory();
76     {
77       IndexWriter iw = new IndexWriter(rd1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
78       Document doc = new Document();
79       doc.add(newField("test", "", Store.NO, Index.ANALYZED,
80                         TermVector.YES));
81       iw.addDocument(doc);
82       doc.add(newField("test", "", Store.NO, Index.ANALYZED,
83                         TermVector.NO));
84       iw.addDocument(doc);
85       iw.close();
86
87       IndexReader ir = IndexReader.open(rd1,false);
88       ir.deleteDocument(0);
89       ir.close();
90
91       iw = new IndexWriter(rd1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND));
92       iw.forceMerge(1);
93       iw.close();
94     }
95
96     Directory rd2 = newDirectory();
97     {
98       IndexWriter iw = new IndexWriter(rd2, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
99       Document doc = new Document();
100       iw.addDocument(doc);
101       iw.close();
102     }
103
104     Directory rdOut = newDirectory();
105
106     IndexWriter iwOut = new IndexWriter(rdOut, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
107     ParallelReader pr = new ParallelReader();
108     pr.add(IndexReader.open(rd1,true));
109     pr.add(IndexReader.open(rd2,true));
110
111     // When unpatched, Lucene crashes here with an ArrayIndexOutOfBoundsException (caused by TermVectorsWriter)
112     iwOut.addIndexes(new IndexReader[] { pr });
113
114     // ParallelReader closes any IndexReader you added to it:
115     pr.close();
116
117     rd1.close();
118     rd2.close();
119                 
120     iwOut.forceMerge(1);
121     iwOut.close();
122     
123     rdOut.close();
124   }
125 }