| 1 |
|
package org.apache.lucene.search; |
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
import junit.framework.TestCase; |
| 20 |
|
import org.apache.lucene.analysis.SimpleAnalyzer; |
| 21 |
|
import org.apache.lucene.document.Document; |
| 22 |
|
import org.apache.lucene.document.Field; |
| 23 |
|
import org.apache.lucene.index.IndexWriter; |
| 24 |
|
import org.apache.lucene.index.Term; |
| 25 |
|
import org.apache.lucene.store.RAMDirectory; |
| 26 |
|
|
| 27 |
|
import java.io.IOException; |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
@version |
| 33 |
|
@author |
| 34 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (67) |
Complexity: 6 |
Complexity Density: 0.1 |
|
| 35 |
|
public class TestWildcard |
| 36 |
|
extends TestCase { |
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1
PASS
|
|
| 37 |
1
|
public void testEquals() {... |
| 38 |
1
|
WildcardQuery wq1 = new WildcardQuery(new Term("field", "b*a")); |
| 39 |
1
|
WildcardQuery wq2 = new WildcardQuery(new Term("field", "b*a")); |
| 40 |
1
|
WildcardQuery wq3 = new WildcardQuery(new Term("field", "b*a")); |
| 41 |
|
|
| 42 |
|
|
| 43 |
1
|
assertEquals(wq1, wq2); |
| 44 |
1
|
assertEquals(wq2, wq1); |
| 45 |
|
|
| 46 |
|
|
| 47 |
1
|
assertEquals(wq2, wq3); |
| 48 |
1
|
assertEquals(wq1, wq3); |
| 49 |
|
|
| 50 |
1
|
assertFalse(wq1.equals(null)); |
| 51 |
|
|
| 52 |
1
|
FuzzyQuery fq = new FuzzyQuery(new Term("field", "b*a")); |
| 53 |
1
|
assertFalse(wq1.equals(fq)); |
| 54 |
1
|
assertFalse(fq.equals(wq1)); |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 1 |
Complexity Density: 0.04 |
1
PASS
|
|
| 60 |
1
|
public void testAsterisk()... |
| 61 |
|
throws IOException { |
| 62 |
1
|
RAMDirectory indexStore = getIndexStore("body", new String[] |
| 63 |
|
{"metal", "metals"}); |
| 64 |
1
|
IndexSearcher searcher = new IndexSearcher(indexStore); |
| 65 |
1
|
Query query1 = new TermQuery(new Term("body", "metal")); |
| 66 |
1
|
Query query2 = new WildcardQuery(new Term("body", "metal*")); |
| 67 |
1
|
Query query3 = new WildcardQuery(new Term("body", "m*tal")); |
| 68 |
1
|
Query query4 = new WildcardQuery(new Term("body", "m*tal*")); |
| 69 |
1
|
Query query5 = new WildcardQuery(new Term("body", "m*tals")); |
| 70 |
|
|
| 71 |
1
|
BooleanQuery query6 = new BooleanQuery(); |
| 72 |
1
|
query6.add(query5, BooleanClause.Occur.SHOULD); |
| 73 |
|
|
| 74 |
1
|
BooleanQuery query7 = new BooleanQuery(); |
| 75 |
1
|
query7.add(query3, BooleanClause.Occur.SHOULD); |
| 76 |
1
|
query7.add(query5, BooleanClause.Occur.SHOULD); |
| 77 |
|
|
| 78 |
|
|
| 79 |
1
|
Query query8 = new WildcardQuery(new Term("body", "M*tal*")); |
| 80 |
|
|
| 81 |
1
|
assertMatches(searcher, query1, 1); |
| 82 |
1
|
assertMatches(searcher, query2, 2); |
| 83 |
1
|
assertMatches(searcher, query3, 1); |
| 84 |
1
|
assertMatches(searcher, query4, 2); |
| 85 |
1
|
assertMatches(searcher, query5, 1); |
| 86 |
1
|
assertMatches(searcher, query6, 1); |
| 87 |
1
|
assertMatches(searcher, query7, 2); |
| 88 |
1
|
assertMatches(searcher, query8, 0); |
| 89 |
1
|
assertMatches(searcher, new WildcardQuery(new Term("body", "*tall")), 0); |
| 90 |
1
|
assertMatches(searcher, new WildcardQuery(new Term("body", "*tal")), 1); |
| 91 |
1
|
assertMatches(searcher, new WildcardQuery(new Term("body", "*tal*")), 2); |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
|
|
| 96 |
|
|
| 97 |
|
@throws |
| 98 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
1
PASS
|
|
| 99 |
1
|
public void testQuestionmark()... |
| 100 |
|
throws IOException { |
| 101 |
1
|
RAMDirectory indexStore = getIndexStore("body", new String[] |
| 102 |
|
{"metal", "metals", "mXtals", "mXtXls"}); |
| 103 |
1
|
IndexSearcher searcher = new IndexSearcher(indexStore); |
| 104 |
1
|
Query query1 = new WildcardQuery(new Term("body", "m?tal")); |
| 105 |
1
|
Query query2 = new WildcardQuery(new Term("body", "metal?")); |
| 106 |
1
|
Query query3 = new WildcardQuery(new Term("body", "metals?")); |
| 107 |
1
|
Query query4 = new WildcardQuery(new Term("body", "m?t?ls")); |
| 108 |
1
|
Query query5 = new WildcardQuery(new Term("body", "M?t?ls")); |
| 109 |
1
|
Query query6 = new WildcardQuery(new Term("body", "meta??")); |
| 110 |
|
|
| 111 |
1
|
assertMatches(searcher, query1, 1); |
| 112 |
1
|
assertMatches(searcher, query2, 1); |
| 113 |
1
|
assertMatches(searcher, query3, 0); |
| 114 |
1
|
assertMatches(searcher, query4, 3); |
| 115 |
1
|
assertMatches(searcher, query5, 0); |
| 116 |
1
|
assertMatches(searcher, query6, 1); |
| 117 |
|
} |
| 118 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 119 |
2
|
private RAMDirectory getIndexStore(String field, String[] contents)... |
| 120 |
|
throws IOException { |
| 121 |
2
|
RAMDirectory indexStore = new RAMDirectory(); |
| 122 |
2
|
IndexWriter writer = new IndexWriter(indexStore, new SimpleAnalyzer(), true); |
| 123 |
8
|
for (int i = 0; i < contents.length; ++i) { |
| 124 |
6
|
Document doc = new Document(); |
| 125 |
6
|
doc.add(new Field(field, contents[i], Field.Store.YES, Field.Index.TOKENIZED)); |
| 126 |
6
|
writer.addDocument(doc); |
| 127 |
|
} |
| 128 |
2
|
writer.optimize(); |
| 129 |
2
|
writer.close(); |
| 130 |
|
|
| 131 |
2
|
return indexStore; |
| 132 |
|
} |
| 133 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 134 |
17
|
private void assertMatches(IndexSearcher searcher, Query q, int expectedMatches)... |
| 135 |
|
throws IOException { |
| 136 |
17
|
Hits result = searcher.search(q); |
| 137 |
17
|
assertEquals(expectedMatches, result.length()); |
| 138 |
|
} |
| 139 |
|
} |