001: package org.apache.lucene.queryParser.surround.query;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.search.IndexSearcher;
021: import org.apache.lucene.search.Searcher;
022: import org.apache.lucene.search.Query;
023: import org.apache.lucene.search.HitCollector;
024:
025: import org.apache.lucene.queryParser.surround.parser.QueryParser;
026:
027: import junit.framework.TestCase;
028:
029: public class BooleanQueryTst {
030: String queryText;
031: final int[] expectedDocNrs;
032: SingleFieldTestDb dBase;
033: String fieldName;
034: TestCase testCase;
035: BasicQueryFactory qf;
036: boolean verbose = true;
037:
038: public BooleanQueryTst(String queryText, int[] expectedDocNrs,
039: SingleFieldTestDb dBase, String fieldName,
040: TestCase testCase, BasicQueryFactory qf) {
041: this .queryText = queryText;
042: this .expectedDocNrs = expectedDocNrs;
043: this .dBase = dBase;
044: this .fieldName = fieldName;
045: this .testCase = testCase;
046: this .qf = qf;
047: }
048:
049: public void setVerbose(boolean verbose) {
050: this .verbose = verbose;
051: }
052:
053: class TestCollector extends HitCollector { // FIXME: use check hits from Lucene tests
054: int totalMatched;
055: boolean[] encountered;
056:
057: TestCollector() {
058: totalMatched = 0;
059: encountered = new boolean[expectedDocNrs.length];
060: }
061:
062: public void collect(int docNr, float score) {
063: /* System.out.println(docNr + " '" + dBase.getDocs()[docNr] + "': " + score); */
064: TestCase.assertTrue(queryText + ": positive score",
065: score > 0.0);
066: TestCase.assertTrue(queryText + ": too many hits",
067: totalMatched < expectedDocNrs.length);
068: int i;
069: for (i = 0; i < expectedDocNrs.length; i++) {
070: if ((!encountered[i]) && (expectedDocNrs[i] == docNr)) {
071: encountered[i] = true;
072: break;
073: }
074: }
075: if (i == expectedDocNrs.length) {
076: TestCase.assertTrue(queryText
077: + ": doc nr for hit not expected: " + docNr,
078: false);
079: }
080: totalMatched++;
081: }
082:
083: void checkNrHits() {
084: TestCase.assertEquals(queryText + ": nr of hits",
085: expectedDocNrs.length, totalMatched);
086: }
087: }
088:
089: public void doTest() throws Exception {
090:
091: if (verbose) {
092: System.out.println("");
093: System.out.println("Query: " + queryText);
094: }
095:
096: SrndQuery lq = QueryParser.parse(queryText);
097:
098: /* if (verbose) System.out.println("Srnd: " + lq.toString()); */
099:
100: Query query = lq.makeLuceneQueryField(fieldName, qf);
101: /* if (verbose) System.out.println("Lucene: " + query.toString()); */
102:
103: TestCollector tc = new TestCollector();
104: Searcher searcher = new IndexSearcher(dBase.getDb());
105: try {
106: searcher.search(query, tc);
107: } finally {
108: searcher.close();
109: }
110: tc.checkNrHits();
111: }
112: }
|