| 1 |
|
package org.apache.lucene; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 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 |
|
|
| 36 |
|
@author |
| 37 |
|
@version |
| 38 |
|
|
|
|
|
| 97.1% |
Uncovered Elements: 2 (68) |
Complexity: 14 |
Complexity Density: 0.26 |
|
| 39 |
|
public class TestSearchForDuplicates extends TestCase { |
| 40 |
|
|
| 41 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 42 |
0
|
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 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1
PASS
|
|
| 63 |
1
|
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 |
|
|
| 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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 2 |
Complexity Density: 0.07 |
|
| 83 |
2
|
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 |
|
|
| 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 |
|
|
| 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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 5 |
Complexity Density: 1 |
|
| 132 |
4
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 5 |
Complexity Density: 1 |
|
| 142 |
4
|
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 |
|
} |