Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
68   209   11   6.8
2   139   0.16   5
10     1.1  
2    
 
  TestTermScorer       Line # 17 65 9 100% 1.0
  TestTermScorer.TestHit       Line # 189 3 2 60% 0.6
 
  (4)
 
1    package org.apache.lucene.search;
2   
3   
4    import java.io.IOException;
5    import java.util.ArrayList;
6    import java.util.List;
7   
8    import junit.framework.TestCase;
9    import org.apache.lucene.analysis.WhitespaceAnalyzer;
10    import org.apache.lucene.document.Document;
11    import org.apache.lucene.document.Field;
12    import org.apache.lucene.index.IndexReader;
13    import org.apache.lucene.index.IndexWriter;
14    import org.apache.lucene.index.Term;
15    import org.apache.lucene.store.RAMDirectory;
16   
 
17    public class TestTermScorer extends TestCase
18    {
19    protected RAMDirectory directory;
20    private static final String FIELD = "field";
21   
22    protected String[] values = new String[]{"all", "dogs dogs", "like", "playing", "fetch", "all"};
23    protected IndexSearcher indexSearcher;
24    protected IndexReader indexReader;
25   
26   
 
27  4 toggle public TestTermScorer(String s)
28    {
29  4 super(s);
30    }
31   
 
32  4 toggle protected void setUp() throws IOException
33    {
34  4 directory = new RAMDirectory();
35   
36   
37  4 IndexWriter writer = new IndexWriter(directory, new WhitespaceAnalyzer(), true);
38  28 for (int i = 0; i < values.length; i++)
39    {
40  24 Document doc = new Document();
41  24 doc.add(new Field(FIELD, values[i], Field.Store.YES, Field.Index.TOKENIZED));
42  24 writer.addDocument(doc);
43    }
44  4 writer.close();
45  4 indexSearcher = new IndexSearcher(directory);
46  4 indexReader = indexSearcher.getIndexReader();
47   
48   
49    }
50   
 
51  4 toggle protected void tearDown()
52    {
53   
54    }
55   
 
56  1 toggle public void test() throws IOException
57    {
58   
59  1 Term allTerm = new Term(FIELD, "all");
60  1 TermQuery termQuery = new TermQuery(allTerm);
61   
62  1 Weight weight = termQuery.weight(indexSearcher);
63   
64  1 TermScorer ts = new TermScorer(weight,
65    indexReader.termDocs(allTerm), indexSearcher.getSimilarity(),
66    indexReader.norms(FIELD));
67  1 assertTrue("ts is null and it shouldn't be", ts != null);
68    //we have 2 documents with the term all in them, one document for all the other values
69  1 final List docs = new ArrayList();
70    //must call next first
71   
72   
73  1 ts.score(new HitCollector()
74    {
 
75  2 toggle public void collect(int doc, float score)
76    {
77  2 docs.add(new TestHit(doc, score));
78  2 assertTrue("score " + score + " is not greater than 0", score > 0);
79  2 assertTrue("Doc: " + doc + " does not equal: " + 0 +
80    " or doc does not equaal: " + 5, doc == 0 || doc == 5);
81    }
82    });
83  1 assertTrue("docs Size: " + docs.size() + " is not: " + 2, docs.size() == 2);
84  1 TestHit doc0 = (TestHit) docs.get(0);
85  1 TestHit doc5 = (TestHit) docs.get(1);
86    //The scores should be the same
87  1 assertTrue(doc0.score + " does not equal: " + doc5.score, doc0.score == doc5.score);
88    /*
89    Score should be (based on Default Sim.:
90    All floats are approximate
91    tf = 1
92    numDocs = 6
93    docFreq(all) = 2
94    idf = ln(6/3) + 1 = 1.693147
95    idf ^ 2 = 2.8667
96    boost = 1
97    lengthNorm = 1 //there is 1 term in every document
98    coord = 1
99    sumOfSquaredWeights = (idf * boost) ^ 2 = 1.693147 ^ 2 = 2.8667
100    queryNorm = 1 / (sumOfSquaredWeights)^0.5 = 1 /(1.693147) = 0.590
101   
102    score = 1 * 2.8667 * 1 * 1 * 0.590 = 1.69
103   
104    */
105  1 assertTrue(doc0.score + " does not equal: " + 1.6931472f, doc0.score == 1.6931472f);
106    }
107   
 
108  1 toggle public void testNext() throws Exception
109    {
110   
111  1 Term allTerm = new Term(FIELD, "all");
112  1 TermQuery termQuery = new TermQuery(allTerm);
113   
114  1 Weight weight = termQuery.weight(indexSearcher);
115   
116  1 TermScorer ts = new TermScorer(weight,
117    indexReader.termDocs(allTerm), indexSearcher.getSimilarity(),
118    indexReader.norms(FIELD));
119  1 assertTrue("ts is null and it shouldn't be", ts != null);
120  1 assertTrue("next did not return a doc", ts.next() == true);
121  1 assertTrue("score is not correct", ts.score() == 1.6931472f);
122  1 assertTrue("next did not return a doc", ts.next() == true);
123  1 assertTrue("score is not correct", ts.score() == 1.6931472f);
124  1 assertTrue("next returned a doc and it should not have", ts.next() == false);
125    }
126   
 
127  1 toggle public void testSkipTo() throws Exception
128    {
129   
130  1 Term allTerm = new Term(FIELD, "all");
131  1 TermQuery termQuery = new TermQuery(allTerm);
132   
133  1 Weight weight = termQuery.weight(indexSearcher);
134   
135  1 TermScorer ts = new TermScorer(weight,
136    indexReader.termDocs(allTerm), indexSearcher.getSimilarity(),
137    indexReader.norms(FIELD));
138  1 assertTrue("ts is null and it shouldn't be", ts != null);
139  1 assertTrue("Didn't skip", ts.skipTo(3) == true);
140    //The next doc should be doc 5
141  1 assertTrue("doc should be number 5", ts.doc() == 5);
142    }
143   
 
144  1 toggle public void testExplain() throws Exception
145    {
146  1 Term allTerm = new Term(FIELD, "all");
147  1 TermQuery termQuery = new TermQuery(allTerm);
148   
149  1 Weight weight = termQuery.weight(indexSearcher);
150   
151  1 TermScorer ts = new TermScorer(weight,
152    indexReader.termDocs(allTerm), indexSearcher.getSimilarity(),
153    indexReader.norms(FIELD));
154  1 assertTrue("ts is null and it shouldn't be", ts != null);
155  1 Explanation explanation = ts.explain(0);
156  1 assertTrue("explanation is null and it shouldn't be", explanation != null);
157    //System.out.println("Explanation: " + explanation.toString());
158    //All this Explain does is return the term frequency
159  1 assertTrue("term frq is not 1", explanation.getValue() == 1);
160  1 explanation = ts.explain(1);
161  1 assertTrue("explanation is null and it shouldn't be", explanation != n