1 package org.apache.lucene.queryParser.surround.query;
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.index.IndexReader;
23 import org.apache.lucene.search.IndexSearcher;
24 import org.apache.lucene.search.Searcher;
25 import org.apache.lucene.search.Collector;
26 import org.apache.lucene.search.Scorer;
27 import org.apache.lucene.search.Query;
29 import org.apache.lucene.queryParser.surround.parser.QueryParser;
31 import org.junit.Assert;
33 public class BooleanQueryTst {
35 final int[] expectedDocNrs;
36 SingleFieldTestDb dBase;
40 boolean verbose = true;
42 public BooleanQueryTst(
45 SingleFieldTestDb dBase,
48 BasicQueryFactory qf) {
49 this.queryText = queryText;
50 this.expectedDocNrs = expectedDocNrs;
52 this.fieldName = fieldName;
53 this.testCase = testCase;
57 public void setVerbose(boolean verbose) {this.verbose = verbose;}
59 class TestCollector extends Collector { // FIXME: use check hits from Lucene tests
61 boolean[] encountered;
62 private Scorer scorer = null;
63 private int docBase = 0;
67 encountered = new boolean[expectedDocNrs.length];
71 public void setScorer(Scorer scorer) throws IOException {
76 public boolean acceptsDocsOutOfOrder() {
81 public void setNextReader(IndexReader reader, int docBase) throws IOException {
82 this.docBase = docBase;
86 public void collect(int docNr) throws IOException {
87 float score = scorer.score();
89 /* System.out.println(docNr + " '" + dBase.getDocs()[docNr] + "': " + score); */
90 Assert.assertTrue(queryText + ": positive score", score > 0.0);
91 Assert.assertTrue(queryText + ": too many hits", totalMatched < expectedDocNrs.length);
93 for (i = 0; i < expectedDocNrs.length; i++) {
94 if ((! encountered[i]) && (expectedDocNrs[i] == docNr)) {
95 encountered[i] = true;
99 if (i == expectedDocNrs.length) {
100 Assert.assertTrue(queryText + ": doc nr for hit not expected: " + docNr, false);
106 Assert.assertEquals(queryText + ": nr of hits", expectedDocNrs.length, totalMatched);
110 public void doTest() throws Exception {
113 System.out.println("");
114 System.out.println("Query: " + queryText);
117 SrndQuery lq = QueryParser.parse(queryText);
119 /* if (verbose) System.out.println("Srnd: " + lq.toString()); */
121 Query query = lq.makeLuceneQueryField(fieldName, qf);
122 /* if (verbose) System.out.println("Lucene: " + query.toString()); */
124 TestCollector tc = new TestCollector();
125 Searcher searcher = new IndexSearcher(dBase.getDb(), true);
127 searcher.search(query, tc);