Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
84   284   28   4.94
20   187   0.33   5.67
17     1.65  
3    
 
  TestCustomSearcherSort       Line # 48 69 22 75% 0.75
  TestCustomSearcherSort.CustomSearcher       Line # 229 14 5 15.8% 0.15789473
  TestCustomSearcherSort.RandomGen       Line # 275 1 1 100% 1.0
 
  (3)
 
1    package org.apache.lucene.search;
2   
3    /**
4    * Copyright 2005 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 java.io.IOException;
20    import java.io.Serializable;
21    import java.util.Calendar;
22    import java.util.GregorianCalendar;
23    import java.util.Map;
24    import java.util.Random;
25    import java.util.TreeMap;
26   
27    import junit.framework.Test;
28    import junit.framework.TestCase;
29    import junit.framework.TestSuite;
30    import junit.textui.TestRunner;
31   
32    import org.apache.lucene.analysis.standard.StandardAnalyzer;
33    import org.apache.lucene.document.DateTools;
34    import org.apache.lucene.document.Document;
35    import org.apache.lucene.document.Field;
36    import org.apache.lucene.index.IndexReader;
37    import org.apache.lucene.index.IndexWriter;
38    import org.apache.lucene.index.Term;
39    import org.apache.lucene.store.Directory;
40    import org.apache.lucene.store.RAMDirectory;
41   
42    /**
43    * Unit test for sorting code.
44    *
45    * @author Martin Seitz (T-Systems)
46    */
47   
 
48    public class TestCustomSearcherSort
49    extends TestCase
50    implements Serializable {
51   
52    private Directory index = null;
53    private Query query = null;
54    // reduced from 20000 to 2000 to speed up test...
55    private final static int INDEX_SIZE = 2000;
56   
 
57  3 toggle public TestCustomSearcherSort (String name) {
58  3 super (name);
59    }
60   
 
61  0 toggle public static void main (String[] argv) {
62  0 TestRunner.run (suite());
63    }
64   
 
65  1 toggle public static Test suite() {
66  1 return new TestSuite (TestCustomSearcherSort.class);
67    }
68   
69   
70    // create an index for testing
 
71  3 toggle private Directory getIndex()
72    throws IOException {
73  3 RAMDirectory indexStore = new RAMDirectory ();
74  3 IndexWriter writer = new IndexWriter (indexStore, new StandardAnalyzer(), true);
75  3 RandomGen random = new RandomGen();
76  6003 for (int i=0; i<INDEX_SIZE; ++i) { // don't decrease; if to low the problem doesn't show up
77  6000 Document doc = new Document();
78  6000 if((i%5)!=0) { // some documents must not have an entry in the first sort field
79  4800 doc.add (new Field("publicationDate_", random.getLuceneDate(), Field.Store.YES, Field.Index.UN_TOKENIZED));
80    }
81  6000 if((i%7)==0) { // some documents to match the query (see below)
82  858 doc.add (new Field("content", "test", Field.Store.YES, Field.Index.TOKENIZED));
83    }
84    // every document has a defined 'mandant' field
85  6000 doc.add(new Field("mandant", Integer.toString(i%3), Field.Store.YES, Field.Index.UN_TOKENIZED));
86  6000 writer.addDocument (doc);
87    }
88  3 writer.optimize ();
89  3 writer.close ();
90  3 return indexStore;
91    }
92   
93    /**
94    * Create index and query for test cases.
95    */
 
96  3 toggle public void setUp() throws Exception {
97  3 index = getIndex();
98  3 query = new TermQuery( new Term("content", "test"));
99    }
100   
101    /**
102    * Run the test using two CustomSearcher instances.
103    */
 
104  1 toggle public void testFieldSortCustomSearcher() throws Exception {
105    // log("Run testFieldSortCustomSearcher");
106    // define the sort criteria
107  1 Sort custSort = new Sort(new SortField[] {
108    new SortField("publicationDate_"),
109    SortField.FIELD_SCORE
110    });
111  1 Searcher searcher = new CustomSearcher (index, 2);
112    // search and check hits
113  1 matchHits(searcher, custSort);
114    }
115    /**
116    * Run the test using one CustomSearcher wrapped by a MultiSearcher.
117    */
 
118  1 toggle public void testFieldSortSingleSearcher() throws Exception {
119    // log("Run testFieldSortSingleSearcher");
120    // define the sort criteria
121  1 Sort custSort = new Sort(new SortField[] {
122    new SortField("publicationDate_"),
123    SortField.FIELD_SCORE
124    });
125  1 Searcher searcher =
126    new MultiSearcher(new Searchable[] {
127    new CustomSearcher (index, 2)});
128    // search and check hits
129  1 matchHits(searcher, custSort);
130    }
131    /**
132    * Run the test using two CustomSearcher instances.
133    */
 
134  1 toggle public void testFieldSortMultiCustomSearcher() throws Exception {
135    // log("Run testFieldSortMultiCustomSearcher");
136    // define the sort criteria
137  1 Sort custSort = new Sort(new SortField[] {
138    new SortField("publicationDate_"),
139    SortField.FIELD_SCORE
140    });
141  1 Searcher searcher =
142    new MultiSearcher(new Searchable[] {
143    new CustomSearcher (index, 0),
144    new CustomSearcher (index, 2)});