Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
41   140   8   10.25
6   85   0.2   4
4     2  
1    
 
  TestSearch       Line # 38 41 8 88.2% 0.88235295
 
  (1)
 
1    package org.apache.lucene;
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 java.util.GregorianCalendar;
20    import java.io.PrintWriter;
21    import java.io.StringWriter;
22   
23    import junit.framework.TestCase;
24    import junit.framework.TestSuite;
25    import junit.textui.TestRunner;
26   
27    import org.apache.lucene.store.*;
28    import org.apache.lucene.document.*;
29    import org.apache.lucene.analysis.*;
30    import org.apache.lucene.index.*;
31    import org.apache.lucene.search.*;
32    import org.apache.lucene.queryParser.*;
33   
34    /** JUnit adaptation of an older test case SearchTest.
35    * @author dmitrys@earthlink.net
36    * @version $Id: TestSearch.java 150494 2004-09-06 22:29:22Z dnaber $
37    */
 
38    public class TestSearch extends TestCase {
39   
40    /** Main for running test case by itself. */
 
41  0 toggle public static void main(String args[]) {
42  0 TestRunner.run (new TestSuite(TestSearch.class));
43    }
44   
45    /** This test performs a number of searches. It also compares output
46    * of searches using multi-file index segments with single-file
47    * index segments.
48    *
49    * TODO: someone should check that the results of the searches are
50    * still correct by adding assert statements. Right now, the test
51    * passes if the results are the same between multi-file and
52    * single-file formats, even if the results are wrong.
53    */
 
54  1 toggle public void testSearch() throws Exception {
55  1 StringWriter sw = new StringWriter();
56  1 PrintWriter pw = new PrintWriter(sw, true);
57  1 doTestSearch(pw, false);
58  1 pw.close();
59  1 sw.close();
60  1 String multiFileOutput = sw.getBuffer().toString();
61    //System.out.println(multiFileOutput);
62   
63  1 sw = new StringWriter();
64  1 pw = new PrintWriter(sw, true);
65  1 doTestSearch(pw, true);
66  1 pw.close();
67  1 sw.close();
68  1 String singleFileOutput = sw.getBuffer().toString();
69   
70  1 assertEquals(multiFileOutput, singleFileOutput);
71    }
72   
73   
 
74  2 toggle private void doTestSearch(PrintWriter out, boolean useCompoundFile)
75    throws Exception
76    {
77  2 Directory directory = new RAMDirectory();
78  2 Analyzer analyzer = new SimpleAnalyzer();
79  2 IndexWriter writer = new IndexWriter(directory, analyzer, true);
80   
81  2 writer.setUseCompoundFile(useCompoundFile);
82   
83  2 String[] docs = {
84    "a b c d e",
85    "a b c d e a b c d e",
86    "a b c d e f g h i j",
87    "a c e",
88    "e c a",
89    "a c e a c e",
90    "a c e a b c"
91    };
92  16 for (int j = 0; j < docs.length; j++) {
93  14 Document d = new Document();
94  14 d.add(new Field("contents", docs[j], Field.Store.YES, Field.Index.TOKENIZED));
95  14 writer.addDocument(d);
96    }
97  2 writer.close();
98   
99  2 Searcher searcher = new IndexSearcher(directory);
100   
101  2 String[] queries = {
102    "a b",
103    "\"a b\"",
104    "\"a b c\"",
105    "a c",
106    "\"a c\"",
107    "\"a c e\"",
108    };
109  2 Hits hits = null;
110   
111  2 QueryParser parser = new QueryParser("contents", analyzer);
112  2 parser.setPhraseSlop(4);
113  14 for (int j = 0; j < queries.length; j++) {
114  12 Query query = parser.parse(queries[j]);
115  12 out.println("Query: " + query.toString("contents"));
116   
117    //DateFilter filter =
118    // new DateFilter("modified", Time(1997,0,1), Time(1998,0,1));
119    //DateFilter filter = DateFilter.Before("modified", Time(1997,00,01));
120    //System.out.println(filter);
121   
122  12 hits = searcher.search(query);
123   
124  12 out.println(hits.length() + " total results");
125  84 for (int i = 0 ; i < hits.length() && i < 10; i++) {
126  72 Document d = hits.doc(i);
127  72 out.println(i + " " + hits.score(i)
128    // + " " + DateField.stringToDate(d.get("modified"))
129    + " " + d.get("contents"));
130    }
131    }
132  2 searcher.close();
133    }
134   
 
135  0 toggle static long Time(int year, int month, int day) {
136  0 GregorianCalendar calendar = new GregorianCalendar();
137  0 calendar.set(year, month, day);
138  0 return calendar.getTime().getTime();
139    }
140    }