KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > TestSearchForDuplicates


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 JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
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,v 1.3 2004/03/29 22:48:05 cutting Exp $
38  */

39 public class TestSearchForDuplicates extends TestCase {
40
41     /** Main for running test case by itself. */
42     public static void main(String JavaDoc args[]) {
43         TestRunner.run (new TestSuite(TestSearchForDuplicates.class));
44     }
45
46
47
48   static final String JavaDoc PRIORITY_FIELD ="priority";
49   static final String JavaDoc ID_FIELD ="id";
50   static final String JavaDoc HIGH_PRIORITY ="high";
51   static final String JavaDoc MED_PRIORITY ="medium";
52   static final String JavaDoc 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   public void testRun() throws Exception JavaDoc {
64       StringWriter JavaDoc sw = new StringWriter JavaDoc();
65       PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw, true);
66       doTest(pw, false);
67       pw.close();
68       sw.close();
69       String JavaDoc multiFileOutput = sw.getBuffer().toString();
70       //System.out.println(multiFileOutput);
71

72       sw = new StringWriter JavaDoc();
73       pw = new PrintWriter JavaDoc(sw, true);
74       doTest(pw, true);
75       pw.close();
76       sw.close();
77       String JavaDoc singleFileOutput = sw.getBuffer().toString();
78
79       assertEquals(multiFileOutput, singleFileOutput);
80   }
81
82
83   private void doTest(PrintWriter JavaDoc out, boolean useCompoundFiles) throws Exception JavaDoc {
84       Directory directory = new RAMDirectory();
85       Analyzer analyzer = new SimpleAnalyzer();
86       IndexWriter writer = new IndexWriter(directory, analyzer, true);
87
88       writer.setUseCompoundFile(useCompoundFiles);
89
90       final int MAX_DOCS = 225;
91
92       for (int j = 0; j < MAX_DOCS; j++) {
93         Document d = new Document();
94         d.add(Field.Text(PRIORITY_FIELD, HIGH_PRIORITY));
95         d.add(Field.Text(ID_FIELD, Integer.toString(j)));
96         writer.addDocument(d);
97       }
98       writer.close();
99
100       // try a search without OR
101
Searcher searcher = new IndexSearcher(directory);
102       Hits hits = null;
103
104       QueryParser parser = new QueryParser(PRIORITY_FIELD, analyzer);
105
106       Query query = parser.parse(HIGH_PRIORITY);
107       out.println("Query: " + query.toString(PRIORITY_FIELD));
108
109       hits = searcher.search(query);
110       printHits(out, hits);
111       checkHits(hits, MAX_DOCS);
112
113       searcher.close();
114
115       // try a new search with OR
116
searcher = new IndexSearcher(directory);
117       hits = null;
118
119       parser = new QueryParser(PRIORITY_FIELD, analyzer);
120
121       query = parser.parse(HIGH_PRIORITY + " OR " + MED_PRIORITY);
122       out.println("Query: " + query.toString(PRIORITY_FIELD));
123
124       hits = searcher.search(query);
125       printHits(out, hits);
126       checkHits(hits, MAX_DOCS);
127
128       searcher.close();
129   }
130
131
132   private void printHits(PrintWriter JavaDoc out, Hits hits ) throws IOException JavaDoc {
133     out.println(hits.length() + " total results\n");
134     for (int i = 0 ; i < hits.length(); i++) {
135       if ( i < 10 || (i > 94 && i < 105) ) {
136         Document d = hits.doc(i);
137         out.println(i + " " + d.get(ID_FIELD));
138       }
139     }
140   }
141
142   private void checkHits(Hits hits, int expectedCount) throws IOException JavaDoc {
143     assertEquals("total results", expectedCount, hits.length());
144     for (int i = 0 ; i < hits.length(); i++) {
145       if ( i < 10 || (i > 94 && i < 105) ) {
146         Document d = hits.doc(i);
147         assertEquals("check " + i, String.valueOf(i), d.get(ID_FIELD));
148       }
149     }
150   }
151
152 }
153
Popular Tags