package org.apache.lucene.search;

/**
 * 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 org.apache.lucene.document.Document;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.English;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util._TestUtil;

/**
 * Tests IndexSearcher's searchAfter() method
 */
public class TestSearchAfter extends LuceneTestCase {
  private Directory dir;
  private IndexReader reader;
  private IndexSearcher searcher;
   
  @Override
  public void setUp() throws Exception {
    super.setUp();
    dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random, dir);
    int numDocs = atLeast(200);
    for (int i = 0; i < numDocs; i++) {
      Document document = new Document();
      document.add(newField("english", English.intToEnglish(i), TextField.TYPE_UNSTORED));
      document.add(newField("oddeven", (i % 2 == 0) ? "even" : "odd", TextField.TYPE_UNSTORED));
      iw.addDocument(document);
    }
    reader = iw.getReader();
    iw.close();
    searcher = newSearcher(reader);
  }

  @Override
  public void tearDown() throws Exception {
    reader.close();
    dir.close();
    super.tearDown();
  }
  
  public void testQueries() throws Exception {
    Filter odd = new QueryWrapperFilter(new TermQuery(new Term("oddeven", "odd")));
    assertQuery(new MatchAllDocsQuery(), null);
    assertQuery(new TermQuery(new Term("english", "one")), null);
    assertQuery(new MatchAllDocsQuery(), odd);
    assertQuery(new TermQuery(new Term("english", "four")), odd);
    BooleanQuery bq = new BooleanQuery();
    bq.add(new TermQuery(new Term("english", "one")), BooleanClause.Occur.SHOULD);
    bq.add(new TermQuery(new Term("oddeven", "even")), BooleanClause.Occur.SHOULD);
    assertQuery(bq, null);
  }
  
  void assertQuery(Query query, Filter filter) throws Exception {
    int maxDoc = searcher.getIndexReader().maxDoc();
    TopDocs all = searcher.search(query, filter, maxDoc);
    int pageSize = _TestUtil.nextInt(random, 1, maxDoc*2);
    int pageStart = 0;
    ScoreDoc lastBottom = null;
    while (pageStart < all.totalHits) {
      TopDocs paged = searcher.searchAfter(lastBottom, query, filter, pageSize);
      if (paged.scoreDocs.length == 0) {
        break;
      }
      assertPage(pageStart, all, paged);
      pageStart += paged.scoreDocs.length;
      lastBottom = paged.scoreDocs[paged.scoreDocs.length - 1];
    }
    assertEquals(all.scoreDocs.length, pageStart);
  }

  static void assertPage(int pageStart, TopDocs all, TopDocs paged) {
    assertEquals(all.totalHits, paged.totalHits);
    for (int i = 0; i < paged.scoreDocs.length; i++) {
      assertEquals(all.scoreDocs[pageStart + i].doc, paged.scoreDocs[i].doc);
      assertEquals(all.scoreDocs[pageStart + i].score, paged.scoreDocs[i].score, 0f);
    }
  }
}

@@ -2174,24 +2174,257 @@
 Exception %7B%0A
+    // because the first page has a null 'after', we get a normal collector.%0A    // so we need to run the test a few times to ensure we will collect multiple%0A    // pages.%0A    int n = atLeast(10);%0A    for (int i = 0; i %3C n; i++) %7B%0A  
     Filter o
@@ -2495,24 +2495,26 @@
 dd%22)));%0A    
+  
 assertQuery(
@@ -2541,32 +2541,34 @@
 y(), null);%0A    
+ 
+ 
 assertQuery(new 
@@ -2613,24 +2613,26 @@
  null);%0A    
+  
 assertQuery(
@@ -2662,24 +2662,26 @@
 , odd);%0A    
+ 
+ 
 assertQuery(
@@ -2734,16 +2734,18 @@
 d);%0A    
+  
 BooleanQ
@@ -2774,24 +2774,26 @@
 uery();%0A    
+ 
+ 
 bq.add(new T
@@ -2855,24 +2855,26 @@
 ur.SHOULD);%0A
+  
     bq.add(n
@@ -2945,24 +2945,26 @@
 HOULD);%0A    
+ 
+ 
 assertQuery(
@@ -2974,16 +2974,22 @@
  null);%0A
+    %7D%0A
   %7D%0A  %0A 

increase test coverage