001: package org.apache.lucene;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import java.io.IOException;
021: import java.io.PrintWriter;
022: import java.io.StringWriter;
023:
024: import org.apache.lucene.store.*;
025: import org.apache.lucene.document.*;
026: import org.apache.lucene.analysis.*;
027: import org.apache.lucene.index.*;
028: import org.apache.lucene.search.*;
029: import org.apache.lucene.queryParser.*;
030:
031: import org.apache.lucene.util.LuceneTestCase;
032: import junit.framework.TestSuite;
033: import junit.textui.TestRunner;
034:
035: /** JUnit adaptation of an older test case DocTest.
036: *
037: * @version $Id: TestSearchForDuplicates.java 583534 2007-10-10 16:46:35Z mikemccand $
038: */
039: public class TestSearchForDuplicates extends LuceneTestCase {
040:
041: /** Main for running test case by itself. */
042: public static void main(String args[]) {
043: TestRunner.run(new TestSuite(TestSearchForDuplicates.class));
044: }
045:
046: static final String PRIORITY_FIELD = "priority";
047: static final String ID_FIELD = "id";
048: static final String HIGH_PRIORITY = "high";
049: static final String MED_PRIORITY = "medium";
050: static final String LOW_PRIORITY = "low";
051:
052: /** This test compares search results when using and not using compound
053: * files.
054: *
055: * TODO: There is rudimentary search result validation as well, but it is
056: * simply based on asserting the output observed in the old test case,
057: * without really knowing if the output is correct. Someone needs to
058: * validate this output and make any changes to the checkHits method.
059: */
060: public void testRun() throws Exception {
061: StringWriter sw = new StringWriter();
062: PrintWriter pw = new PrintWriter(sw, true);
063: doTest(pw, false);
064: pw.close();
065: sw.close();
066: String multiFileOutput = sw.getBuffer().toString();
067: //System.out.println(multiFileOutput);
068:
069: sw = new StringWriter();
070: pw = new PrintWriter(sw, true);
071: doTest(pw, true);
072: pw.close();
073: sw.close();
074: String singleFileOutput = sw.getBuffer().toString();
075:
076: assertEquals(multiFileOutput, singleFileOutput);
077: }
078:
079: private void doTest(PrintWriter out, boolean useCompoundFiles)
080: throws Exception {
081: Directory directory = new RAMDirectory();
082: Analyzer analyzer = new SimpleAnalyzer();
083: IndexWriter writer = new IndexWriter(directory, analyzer, true);
084:
085: writer.setUseCompoundFile(useCompoundFiles);
086:
087: final int MAX_DOCS = 225;
088:
089: for (int j = 0; j < MAX_DOCS; j++) {
090: Document d = new Document();
091: d.add(new Field(PRIORITY_FIELD, HIGH_PRIORITY,
092: Field.Store.YES, Field.Index.TOKENIZED));
093: d.add(new Field(ID_FIELD, Integer.toString(j),
094: Field.Store.YES, Field.Index.TOKENIZED));
095: writer.addDocument(d);
096: }
097: writer.close();
098:
099: // try a search without OR
100: Searcher searcher = new IndexSearcher(directory);
101: Hits hits = null;
102:
103: QueryParser parser = new QueryParser(PRIORITY_FIELD, analyzer);
104:
105: Query query = parser.parse(HIGH_PRIORITY);
106: out.println("Query: " + query.toString(PRIORITY_FIELD));
107:
108: hits = searcher.search(query);
109: printHits(out, hits);
110: checkHits(hits, MAX_DOCS);
111:
112: searcher.close();
113:
114: // try a new search with OR
115: searcher = new IndexSearcher(directory);
116: hits = null;
117:
118: parser = new QueryParser(PRIORITY_FIELD, analyzer);
119:
120: query = parser.parse(HIGH_PRIORITY + " OR " + MED_PRIORITY);
121: out.println("Query: " + query.toString(PRIORITY_FIELD));
122:
123: hits = searcher.search(query);
124: printHits(out, hits);
125: checkHits(hits, MAX_DOCS);
126:
127: searcher.close();
128: }
129:
130: private void printHits(PrintWriter out, Hits hits)
131: throws IOException {
132: out.println(hits.length() + " total results\n");
133: for (int i = 0; i < hits.length(); i++) {
134: if (i < 10 || (i > 94 && i < 105)) {
135: Document d = hits.doc(i);
136: out.println(i + " " + d.get(ID_FIELD));
137: }
138: }
139: }
140:
141: private void checkHits(Hits hits, int expectedCount)
142: throws IOException {
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
148: .get(ID_FIELD));
149: }
150: }
151: }
152:
153: }
|