package org.apache.lucene.index;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.IOException;

import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;

import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;

/**
 * Some tests for {@link ParallelReader}s with empty indexes
 * 
 * @author Christian Kohlschuetter
 */
public class TestParallelReaderEmptyIndex extends LuceneTestCase {

  /**
   * Creates two empty indexes and wraps a ParallelReader around. Adding this
   * reader to a new index should not throw any exception.
   * 
   * @throws IOException
   */
  public void testEmptyIndex() throws IOException {
    Directory rd1 = newDirectory();
    IndexWriter iw = new IndexWriter(rd1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
    iw.close();

    Directory rd2 = newDirectory(rd1);

    Directory rdOut = newDirectory();

    IndexWriter iwOut = new IndexWriter(rdOut, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
    ParallelReader pr = new ParallelReader();
    pr.add(IndexReader.open(rd1));
    pr.add(IndexReader.open(rd2));
		
    // When unpatched, Lucene crashes here with a NoSuchElementException (caused by ParallelTermEnum)
    iwOut.addIndexes(pr);
		
    iwOut.forceMerge(1);
    iwOut.close();
    rdOut.close();
    rd1.close();
    rd2.close();
  }

  /**
   * This method creates an empty index (numFields=0, numDocs=0) but is marked
   * to have TermVectors. Adding this index to another index should not throw
   * any exception.
   */
  /* nocommit: Fix tests to use an id and delete by term
  public void testEmptyIndexWithVectors() throws IOException {
    Directory rd1 = newDirectory();
    {
      IndexWriter iw = new IndexWriter(rd1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
      Document doc = new Document();
      FieldType customType = new FieldType(TextField.TYPE_UNSTORED);
      customType.setStoreTermVectors(true);
      doc.add(newField("test", "", customType));
      iw.addDocument(doc);
      doc.add(newField("test", "", TextField.TYPE_UNSTORED));
      iw.addDocument(doc);
      iw.close();

      IndexReader ir = IndexReader.open(rd1);
      ir.deleteDocument(0);
      ir.close();

      iw = new IndexWriter(rd1, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)).setOpenMode(OpenMode.APPEND));
      iw.forceMerge(1);
      iw.close();
    }

    Directory rd2 = newDirectory();
    {
      IndexWriter iw = new IndexWriter(rd2, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
      Document doc = new Document();
      iw.addDocument(doc);
      iw.close();
    }

    Directory rdOut = newDirectory();

    IndexWriter iwOut = new IndexWriter(rdOut, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random)));
    ParallelReader pr = new ParallelReader();
    pr.add(IndexReader.open(rd1));
    pr.add(IndexReader.open(rd2));

    // When unpatched, Lucene crashes here with an ArrayIndexOutOfBoundsException (caused by TermVectorsWriter)
    iwOut.addIndexes(pr);

    // ParallelReader closes any IndexReader you added to it:
    pr.close();

    rd1.close();
    rd2.close();
		
    iwOut.forceMerge(1);
    iwOut.close();
    
    rdOut.close();
  }
  */
}

@@ -1041,16 +1041,57 @@
 cument;%0A
+import org.apache.lucene.document.Field;%0A
 import o
@@ -2550,65 +2550,8 @@
  */%0A
-  /* nocommit: Fix tests to use an id and delete by term%0A
   pu
@@ -2800,24 +2800,115 @@
 Document();%0A
+      Field idField = newField(%22id%22, %22%22, TextField.TYPE_UNSTORED);%0A      doc.add(idField);%0A
       FieldT
@@ -3053,24 +3053,53 @@
 stomType));%0A
+      idField.setValue(%221%22);%0A
       iw.add
@@ -3171,24 +3171,53 @@
 UNSTORED));%0A
+      idField.setValue(%222%22);%0A
       iw.add
@@ -3265,68 +3265,426 @@
 ndex
-Reader ir = IndexReader.open(rd1);%0A      ir.deleteDocument(0
+WriterConfig dontMergeConfig = new IndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random))%0A        .setMergePolicy(NoMergePolicy.COMPOUND_FILES);%0A      IndexWriter writer = new IndexWriter(rd1, dontMergeConfig);%0A      %0A      writer.deleteDocuments(new Term(%22id%22, %221%22));%0A      writer.close();%0A      IndexReader ir = IndexReader.open(rd1);%0A      assertEquals(2, ir.maxDoc());%0A      assertEquals(1, ir.numDocs()
 );%0A 
@@ -4732,15 +4732,10 @@
 ();%0A  %7D%0A
-  */%0A
 %7D%0A

fix test