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;
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;
33 * Some tests for {@link ParallelReader}s with empty indexes
35 * @author Christian Kohlschuetter
37 public class TestParallelReaderEmptyIndex extends LuceneTestCase {
40 * Creates two empty indexes and wraps a ParallelReader around. Adding this
41 * reader to a new index should not throw any exception.
45 public void testEmptyIndex() throws IOException {
46 Directory rd1 = newDirectory();
47 IndexWriter iw = new IndexWriter(rd1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
50 Directory rd2 = newDirectory(rd1);
52 Directory rdOut = newDirectory();
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));
59 // When unpatched, Lucene crashes here with a NoSuchElementException (caused by ParallelTermEnum)
60 iwOut.addIndexes(new IndexReader[] { pr });
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
74 public void testEmptyIndexWithVectors() throws IOException {
75 Directory rd1 = newDirectory();
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,
82 doc.add(newField("test", "", Store.NO, Index.ANALYZED,
87 IndexReader ir = IndexReader.open(rd1,false);
91 iw = new IndexWriter(rd1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND));
96 Directory rd2 = newDirectory();
98 IndexWriter iw = new IndexWriter(rd2, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
99 Document doc = new Document();
104 Directory rdOut = newDirectory();
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));
111 // When unpatched, Lucene crashes here with an ArrayIndexOutOfBoundsException (caused by TermVectorsWriter)
112 iwOut.addIndexes(new IndexReader[] { pr });
114 // ParallelReader closes any IndexReader you added to it: