KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > CheckHits


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 /* 20 May 2004: Factored out of spans tests. Please leave this comment
20                   until this class is evt. also used by tests in search package.
21  */

22
23 import org.apache.lucene.search.Searcher;
24 import org.apache.lucene.search.Query;
25 import org.apache.lucene.search.Hits;
26 import junit.framework.TestCase;
27
28 import java.io.IOException JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.TreeSet JavaDoc;
31
32 public class CheckHits {
33   public static void checkHits(
34         Query query,
35         String JavaDoc defaultFieldName,
36         Searcher searcher,
37         int[] results,
38         TestCase testCase)
39           throws IOException JavaDoc {
40     Hits hits = searcher.search(query);
41
42     Set JavaDoc correct = new TreeSet JavaDoc();
43     for (int i = 0; i < results.length; i++) {
44       correct.add(new Integer JavaDoc(results[i]));
45     }
46
47     Set JavaDoc actual = new TreeSet JavaDoc();
48     for (int i = 0; i < hits.length(); i++) {
49       actual.add(new Integer JavaDoc(hits.id(i)));
50     }
51
52     testCase.assertEquals(query.toString(defaultFieldName), correct, actual);
53   }
54
55   public static void printDocNrs(Hits hits) throws IOException JavaDoc {
56     System.out.print("new int[] {");
57     for (int i = 0; i < hits.length(); i++) {
58       System.out.print(hits.id(i));
59       if (i != hits.length()-1)
60         System.out.print(", ");
61     }
62     System.out.println("}");
63   }
64 }
65
66
Popular Tags