pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / test / org / apache / lucene / queryParser / surround / query / BooleanQueryTst.java
diff --git a/lucene-java-3.5.0/lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/surround/query/BooleanQueryTst.java b/lucene-java-3.5.0/lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/surround/query/BooleanQueryTst.java
new file mode 100644 (file)
index 0000000..631b7d8
--- /dev/null
@@ -0,0 +1,135 @@
+package org.apache.lucene.queryParser.surround.query;
+
+/**
+ * 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.index.IndexReader;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Searcher;
+import org.apache.lucene.search.Collector;
+import org.apache.lucene.search.Scorer;
+import org.apache.lucene.search.Query;
+
+import org.apache.lucene.queryParser.surround.parser.QueryParser;
+
+import org.junit.Assert;
+
+public class BooleanQueryTst {
+  String queryText;
+  final int[] expectedDocNrs;
+  SingleFieldTestDb dBase;
+  String fieldName;
+  Assert testCase;
+  BasicQueryFactory qf;
+  boolean verbose = true;
+
+  public BooleanQueryTst(
+      String queryText,
+      int[] expectedDocNrs,
+      SingleFieldTestDb dBase,
+      String fieldName,
+      Assert testCase,
+      BasicQueryFactory qf) {
+    this.queryText = queryText;
+    this.expectedDocNrs = expectedDocNrs;
+    this.dBase = dBase;
+    this.fieldName = fieldName;
+    this.testCase = testCase;
+    this.qf = qf;
+  }
+  
+  public void setVerbose(boolean verbose) {this.verbose = verbose;}
+
+  class TestCollector extends Collector { // FIXME: use check hits from Lucene tests
+    int totalMatched;
+    boolean[] encountered;
+    private Scorer scorer = null;
+    private int docBase = 0;
+
+    TestCollector() {
+      totalMatched = 0;
+      encountered = new boolean[expectedDocNrs.length];
+    }
+
+    @Override
+    public void setScorer(Scorer scorer) throws IOException {
+      this.scorer = scorer;
+    }
+
+    @Override
+    public boolean acceptsDocsOutOfOrder() {
+      return true;
+    }
+
+    @Override
+    public void setNextReader(IndexReader reader, int docBase) throws IOException {
+      this.docBase = docBase;
+    }
+    
+    @Override
+    public void collect(int docNr) throws IOException {
+      float score = scorer.score();
+      docNr += docBase;
+      /* System.out.println(docNr + " '" + dBase.getDocs()[docNr] + "': " + score); */
+      Assert.assertTrue(queryText + ": positive score", score > 0.0);
+      Assert.assertTrue(queryText + ": too many hits", totalMatched < expectedDocNrs.length);
+      int i;
+      for (i = 0; i < expectedDocNrs.length; i++) {
+        if ((! encountered[i]) && (expectedDocNrs[i] == docNr)) {
+          encountered[i] = true;
+          break;
+        }
+      }
+      if (i == expectedDocNrs.length) {
+        Assert.assertTrue(queryText + ": doc nr for hit not expected: " + docNr, false);
+      }
+      totalMatched++;
+    }
+
+    void checkNrHits() {
+      Assert.assertEquals(queryText + ": nr of hits", expectedDocNrs.length, totalMatched);
+    }
+  }
+
+  public void doTest() throws Exception {
+
+    if (verbose) {    
+        System.out.println("");
+        System.out.println("Query: " + queryText);
+    }
+    
+    SrndQuery lq = QueryParser.parse(queryText);
+    
+    /* if (verbose) System.out.println("Srnd: " + lq.toString()); */
+    
+    Query query = lq.makeLuceneQueryField(fieldName, qf);
+    /* if (verbose) System.out.println("Lucene: " + query.toString()); */
+
+    TestCollector tc = new TestCollector();
+    Searcher searcher = new IndexSearcher(dBase.getDb(), true);
+    try {
+      searcher.search(query, tc);
+    } finally {
+      searcher.close();
+    }
+    tc.checkNrHits();
+  }
+}
+
+