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 java.io.IOException;
22 import org.apache.lucene.store.Directory;
23 import org.apache.lucene.util.LuceneTestCase;
24 import org.apache.lucene.util._TestUtil;
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;
34 * Some tests for {@link ParallelReader}s with empty indexes
36 * @author Christian Kohlschuetter
38 public class TestParallelReaderEmptyIndex extends LuceneTestCase {
41 * Creates two empty indexes and wraps a ParallelReader around. Adding this
42 * reader to a new index should not throw any exception.
46 public void testEmptyIndex() throws IOException {
47 Directory rd1 = newDirectory();
48 IndexWriter iw = new IndexWriter(rd1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
51 Directory rd2 = newDirectory(rd1);
53 Directory rdOut = newDirectory();
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));
60 // When unpatched, Lucene crashes here with a NoSuchElementException (caused by ParallelTermEnum)
61 iwOut.addIndexes(new IndexReader[] { pr });
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
75 public void testEmptyIndexWithVectors() throws IOException {
76 Directory rd1 = newDirectory();
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,
83 doc.add(newField("test", "", Store.NO, Index.ANALYZED,
88 IndexReader ir = IndexReader.open(rd1,false);
92 iw = new IndexWriter(rd1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND));
97 Directory rd2 = newDirectory();
99 IndexWriter iw = new IndexWriter(rd2, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
100 Document doc = new Document();
105 Directory rdOut = newDirectory();
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));
112 // When unpatched, Lucene crashes here with an ArrayIndexOutOfBoundsException (caused by TermVectorsWriter)
113 iwOut.addIndexes(new IndexReader[] { pr });
115 // ParallelReader closes any IndexReader you added to it: