Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
53   152   14   10.6
10   88   0.26   5
5     2.8  
1    
 
  TestSearchForDuplicates       Line # 39 53 14 97.1% 0.9705882
 
  (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.io.IOException;
20    import java.io.PrintWriter;
21    import java.io.StringWriter;
22   
23    import org.apache.lucene.store.*;
24    import org.apache.lucene.document.*;
25    import org.apache.lucene.analysis.*;
26    import org.apache.lucene.index.*;
27    import org.apache.lucene.search.*;
28    import org.apache.lucene.queryParser.*;
29   
30    import junit.framework.TestCase;
31    import junit.framework.TestSuite;
32    import junit.textui.TestRunner;
33   
34   
35    /** JUnit adaptation of an older test case DocTest.
36    * @author dmitrys@earthlink.net
37    * @version $Id: TestSearchForDuplicates.java 150494 2004-09-06 22:29:22Z dnaber $
38    */
 
39    public class TestSearchForDuplicates extends TestCase {
40   
41    /** Main for running test case by itself. */
 
42  0 toggle public static void main(String args[]) {
43  0 TestRunner.run (new TestSuite(TestSearchForDuplicates.class));
44    }
45   
46   
47   
48    static final String PRIORITY_FIELD ="priority";
49    static final String ID_FIELD ="id";
50    static final String HIGH_PRIORITY ="high";
51    static final String MED_PRIORITY ="medium";
52    static final String LOW_PRIORITY ="low";
53   
54   
55    /** This test compares search results when using and not using compound
56    * files.
57    *
58    * TODO: There is rudimentary search result validation as well, but it is
59    * simply based on asserting the output observed in the old test case,
60    * without really knowing if the output is correct. Someone needs to
61    * validate this output and make any changes to the checkHits method.
62    */
 
63  1 toggle public void testRun() throws Exception {
64  1 StringWriter sw = new StringWriter();
65  1 PrintWriter pw = new PrintWriter(sw, true);
66  1 doTest(pw, false);
67  1 pw.close();
68  1 sw.close();
69  1 String multiFileOutput = sw.getBuffer().toString();
70    //System.out.println(multiFileOutput);
71   
72  1 sw = new StringWriter();
73  1 pw = new PrintWriter(sw, true);
74  1 doTest(pw, true);
75  1 pw.close();
76  1 sw.close();
77  1 String singleFileOutput = sw.getBuffer().toString();
78   
79  1 assertEquals(multiFileOutput, singleFileOutput);
80    }
81   
82   
 
83  2 toggle private void doTest(PrintWriter out, boolean useCompoundFiles) throws Exception {
84  2 Directory directory = new RAMDirectory();
85  2 Analyzer analyzer = new SimpleAnalyzer();
86  2 IndexWriter writer = new IndexWriter(directory, analyzer, true);
87   
88  2 writer.setUseCompoundFile(useCompoundFiles);
89   
90  2 final int MAX_DOCS = 225;
91   
92  452 for (int j = 0; j < MAX_DOCS; j++) {
93  450 Document d = new Document();
94  450 d.add(new Field(PRIORITY_FIELD, HIGH_PRIORITY, Field.Store.YES, Field.Index.TOKENIZED));
95  450 d.add(new Field(ID_FIELD, Integer.toString(j), Field.Store.YES, Field.Index.TOKENIZED));
96  450 writer.addDocument(d);
97    }
98  2 writer.close();
99   
100    // try a search without OR
101  2 Searcher searcher = new IndexSearcher(directory);
102  2 Hits hits = null;
103   
104  2 QueryParser parser = new QueryParser(PRIORITY_FIELD, analyzer);
105   
106  2 Query query = parser.parse(HIGH_PRIORITY);
107  2 out.println("Query: " + query.toString(PRIORITY_FIELD));
108   
109  2 hits = searcher.search(query);
110  2 printHits(out, hits);
111  2 checkHits(hits, MAX_DOCS);
112   
113  2 searcher.close();
114   
115    // try a new search with OR
116  2 searcher = new IndexSearcher(directory);
117  2 hits = null;
118   
119  2 parser = new QueryParser(PRIORITY_FIELD, analyzer);
120   
121  2 query = parser.parse(HIGH_PRIORITY + " OR " + MED_PRIORITY);
122  2 out.println("Query: " + query.toString(PRIORITY_FIELD));
123   
124  2 hits = searcher.search(query);
125  2 printHits(out, hits);
126  2 checkHits(hits, MAX_DOCS);
127   
128  2 searcher.close();
129    }
130   
131   
 
132  4 toggle private void printHits(PrintWriter out, Hits hits ) throws IOException {
133  4 out.println(hits.length() + " total results\n");
134  904 for (int i = 0 ; i < hits.length(); i++) {
135  900 if ( i < 10 || (i > 94 && i < 105) ) {
136  80 Document d = hits.doc(i);
137  80 out.println(i + " " + d.get(ID_FIELD));
138    }
139    }
140    }
141   
 
142  4 toggle private void checkHits(Hits hits, int expectedCount) throws IOException {
143  4 assertEquals("total results", expectedCount, hits.length());
144  904 for (int i = 0 ; i < hits.length(); i++) {
145  900 if ( i < 10 || (i > 94 && i < 105) ) {
146  80 Document d = hits.doc(i);
147  80 assertEquals("check " + i, String.valueOf(i), d.get(ID_FIELD));
148    }
149    }
150    }
151   
152    }