Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
153   350   17   11.77
8   214   0.11   13
13     1.31  
1    
 
  TestConstantScoreRangeQuery       Line # 32 153 17 98.9% 0.9885057
 
  (6)
 
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    import org.apache.lucene.analysis.WhitespaceAnalyzer;
20    import org.apache.lucene.document.Document;
21    import org.apache.lucene.document.Field;
22    import org.apache.lucene.index.IndexReader;
23    import org.apache.lucene.index.IndexWriter;
24    import org.apache.lucene.index.Term;
25    import org.apache.lucene.store.Directory;
26    import org.apache.lucene.store.RAMDirectory;
27   
28    import java.io.IOException;
29   
30    import junit.framework.Assert;
31   
 
32    public class TestConstantScoreRangeQuery extends BaseTestRangeFilter {
33   
34    /** threshold for comparing floats */
35    public static final float SCORE_COMP_THRESH = 1e-6f;
36   
 
37  7 toggle public TestConstantScoreRangeQuery(String name) {
38  7 super(name);
39    }
 
40  0 toggle public TestConstantScoreRangeQuery() {
41  0 super();
42    }
43   
44    Directory small;
45   
 
46  11 toggle void assertEquals(String m, float e, float a) {
47  11 assertEquals(m, e, a, SCORE_COMP_THRESH);
48    }
49   
 
50  44 toggle static public void assertEquals(String m, int e, int a) {
51  44 Assert.assertEquals(m, e, a);
52    }
53   
 
54  7 toggle public void setUp() throws Exception {
55  7 super.setUp();
56   
57  7 String[] data = new String [] {
58    "A 1 2 3 4 5 6",
59    "Z 4 5 6",
60    null,
61    "B 2 4 5 6",
62    "Y 3 5 6",
63    null,
64    "C 3 6",
65    "X 4 5 6"
66    };
67   
68  7 small = new RAMDirectory();
69  7 IndexWriter writer = new IndexWriter(small,
70    new WhitespaceAnalyzer(),
71    true);
72   
73  63 for (int i = 0; i < data.length; i++) {
74  56 Document doc = new Document();
75  56 doc.add(new Field("id", String.valueOf(i), Field.Store.YES, Field.Index.UN_TOKENIZED));//Field.Keyword("id",String.valueOf(i)));
76  56 doc.add(new Field("all", "all", Field.Store.YES, Field.Index.UN_TOKENIZED));//Field.Keyword("all","all"));
77  56 if (null != data[i]) {
78  42 doc.add(new Field("data", data[i], Field.Store.YES, Field.Index.TOKENIZED));//Field.Text("data",data[i]));
79    }
80  56 writer.addDocument(doc);
81    }
82   
83  7 writer.optimize();
84  7 writer.close();
85    }
86   
87   
88   
89    /** macro for readability */
 
90  45 toggle public static Query csrq(String f, String l, String h,
91    boolean il, boolean ih) {
92  45 return new ConstantScoreRangeQuery(f,l,h,il,ih);
93    }
94   
 
95  1 toggle public void testBasics() throws IOException {
96  1 QueryUtils.check(csrq("data","1","6",T,T));
97  1 QueryUtils.check(csrq("data","A","Z",T,T));
98  1 QueryUtils.checkUnequal(csrq("data","1","6",T,T), csrq("data","A","Z",T,T));
99    }
100   
 
101  1 toggle public void testEqualScores() throws IOException {
102    // NOTE: uses index build in *this* setUp
103   
104  1 IndexReader reader = IndexReader.open(small);
105  1 IndexSearcher search = new IndexSearcher(reader);
106   
107  1 Hits result;
108   
109    // some hits match more terms then others, score should be the same
110   
111  1 result = search.search(csrq("data","1","6",T,T));
112  1 int numHits = result.length();
113  1 assertEquals("wrong number of results", 6, numHits);
114  1 float score = result.score(0);
115  6 for (int i = 1; i < numHits; i++) {
116  5 assertEquals("score for " + i +" was not the same",
117    score, result.score(i));
118    }
119   
120    }
121   
 
122  1 toggle public void testBoost() throws IOException {
123    // NOTE: uses index build in *this* setUp
124   
125  1 IndexReader reader = IndexReader.open(small);
126  1 IndexSearcher search = new IndexSearcher(reader);
127   
128    // test for correct application of query normalization
129    // must use a non score normalizing method for this.
130  1 Query q = csrq("data","1","6",T,T);
131  1 q.setBoost(100);
132  1 search.search(q,null, new HitCollector() {
 
133  6 toggle public void collect(int doc, float score) {
134  6 assertEquals("score for doc " + doc +" was not correct",
135    1.0f, score);
136    }
137    });
138   
139   
140    //
141    // Ensure that boosting works to score one clause of a query higher
142    // than another.
143    //
144  1 Query q1 = csrq("data","A","A",T,T); // matches document #0
145  1 q1.setBoost(.1f);