add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / queryparser / src / test / org / apache / lucene / queryParser / surround / query / BooleanQueryTst.java
1 package org.apache.lucene.queryParser.surround.query;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21
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;
28
29 import org.apache.lucene.queryParser.surround.parser.QueryParser;
30
31 import org.junit.Assert;
32
33 public class BooleanQueryTst {
34   String queryText;
35   final int[] expectedDocNrs;
36   SingleFieldTestDb dBase;
37   String fieldName;
38   Assert testCase;
39   BasicQueryFactory qf;
40   boolean verbose = true;
41
42   public BooleanQueryTst(
43       String queryText,
44       int[] expectedDocNrs,
45       SingleFieldTestDb dBase,
46       String fieldName,
47       Assert testCase,
48       BasicQueryFactory qf) {
49     this.queryText = queryText;
50     this.expectedDocNrs = expectedDocNrs;
51     this.dBase = dBase;
52     this.fieldName = fieldName;
53     this.testCase = testCase;
54     this.qf = qf;
55   }
56   
57   public void setVerbose(boolean verbose) {this.verbose = verbose;}
58
59   class TestCollector extends Collector { // FIXME: use check hits from Lucene tests
60     int totalMatched;
61     boolean[] encountered;
62     private Scorer scorer = null;
63     private int docBase = 0;
64
65     TestCollector() {
66       totalMatched = 0;
67       encountered = new boolean[expectedDocNrs.length];
68     }
69
70     @Override
71     public void setScorer(Scorer scorer) throws IOException {
72       this.scorer = scorer;
73     }
74
75     @Override
76     public boolean acceptsDocsOutOfOrder() {
77       return true;
78     }
79
80     @Override
81     public void setNextReader(IndexReader reader, int docBase) throws IOException {
82       this.docBase = docBase;
83     }
84     
85     @Override
86     public void collect(int docNr) throws IOException {
87       float score = scorer.score();
88       docNr += docBase;
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);
92       int i;
93       for (i = 0; i < expectedDocNrs.length; i++) {
94         if ((! encountered[i]) && (expectedDocNrs[i] == docNr)) {
95           encountered[i] = true;
96           break;
97         }
98       }
99       if (i == expectedDocNrs.length) {
100         Assert.assertTrue(queryText + ": doc nr for hit not expected: " + docNr, false);
101       }
102       totalMatched++;
103     }
104
105     void checkNrHits() {
106       Assert.assertEquals(queryText + ": nr of hits", expectedDocNrs.length, totalMatched);
107     }
108   }
109
110   public void doTest() throws Exception {
111
112     if (verbose) {    
113         System.out.println("");
114         System.out.println("Query: " + queryText);
115     }
116     
117     SrndQuery lq = QueryParser.parse(queryText);
118     
119     /* if (verbose) System.out.println("Srnd: " + lq.toString()); */
120     
121     Query query = lq.makeLuceneQueryField(fieldName, qf);
122     /* if (verbose) System.out.println("Lucene: " + query.toString()); */
123
124     TestCollector tc = new TestCollector();
125     Searcher searcher = new IndexSearcher(dBase.getDb(), true);
126     try {
127       searcher.search(query, tc);
128     } finally {
129       searcher.close();
130     }
131     tc.checkNrHits();
132   }
133 }
134
135