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