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 java.util.Random;
import java.util.concurrent.ExecutorService;
import java.io.IOException;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexReader.AtomicReaderContext;
import org.apache.lucene.index.IndexReader.ReaderContext;
import org.apache.lucene.util.Bits;

/** 
 * Helper class that adds some extra checks to ensure correct
 * usage of {@code IndexSearcher} and {@code Weight}.
 * TODO: Extend this by more checks, that's just a start.
 */
public class AssertingIndexSearcher extends IndexSearcher {
  final Random random;
  public  AssertingIndexSearcher(Random random, IndexReader r) {
    super(r);
    this.random = new Random(random.nextLong());
  }
  
  public  AssertingIndexSearcher(Random random, ReaderContext context) {
    super(context);
    this.random = new Random(random.nextLong());
  }
  
  public  AssertingIndexSearcher(Random random, IndexReader r, ExecutorService ex) {
    super(r, ex);
    this.random = new Random(random.nextLong());
  }
  
  public  AssertingIndexSearcher(Random random, ReaderContext context, ExecutorService ex) {
    super(context, ex);
    this.random = new Random(random.nextLong());
  }
  
  /** Ensures, that the returned {@code Weight} is not normalized again, which may produce wrong scores. */
  @Override
  public Weight createNormalizedWeight(Query query) throws IOException {
    final Weight w = super.createNormalizedWeight(query);
    return new Weight() {
      @Override
      public Explanation explain(AtomicReaderContext context, int doc) throws IOException {
        return w.explain(context, doc);
      }

      @Override
      public Query getQuery() {
        return w.getQuery();
      }

      @Override
      public void normalize(float norm, float topLevelBoost) {
        throw new IllegalStateException("Weight already normalized.");
      }

      @Override
      public Scorer scorer(AtomicReaderContext context, boolean scoreDocsInOrder,
          boolean topScorer, Bits acceptDocs) throws IOException {
        return w.scorer(context, scoreDocsInOrder, topScorer, acceptDocs);
      }

      @Override
      public float getValueForNormalization() throws IOException {
        throw new IllegalStateException("Weight already normalized.");
      }

      @Override
      public boolean scoresDocsOutOfOrder() {
        return w.scoresDocsOutOfOrder();
      }
    };
  }

  @Override
  protected Query wrapFilter(Query query, Filter filter) {
    if (random.nextBoolean())
      return super.wrapFilter(query, filter);
    return (filter == null) ? query : new FilteredQuery(query, filter) {
      @Override
      protected boolean useRandomAccess(Bits bits, int firstFilterDoc) {
        return random.nextBoolean();
      }
    };
  }
}

@@ -2860,38 +2860,47 @@
 ption %7B%0A        
-return
+Scorer scorer =
  w.scorer(contex
@@ -2944,16 +2944,432 @@
 tDocs);%0A
+        if (scorer != null) %7B%0A          // check that scorer obeys disi contract for docID() before next()/advance%0A          try %7B%0A            int docid = scorer.docID();%0A            assert docid == -1 %7C%7C docid == DocIdSetIterator.NO_MORE_DOCS;%0A          %7D catch (UnsupportedOperationException ignored) %7B%0A            // from a top-level BS1%0A            assert topScorer;%0A          %7D%0A        %7D%0A        return scorer;%0A
       %7D%0A

add check to AssertingIndexSearcher that scorers obey DISI contract before next/advance