| 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 |
|
|
| 21 |
|
import java.rmi.Naming; |
| 22 |
|
import java.rmi.registry.LocateRegistry; |
| 23 |
|
|
| 24 |
|
import org.apache.lucene.index.Term; |
| 25 |
|
import org.apache.lucene.index.IndexWriter; |
| 26 |
|
import org.apache.lucene.store.RAMDirectory; |
| 27 |
|
import org.apache.lucene.analysis.SimpleAnalyzer; |
| 28 |
|
import org.apache.lucene.document.Document; |
| 29 |
|
import org.apache.lucene.document.Field; |
| 30 |
|
|
| 31 |
|
|
| 32 |
|
@version |
| 33 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (50) |
Complexity: 11 |
Complexity Density: 0.28 |
|
| 34 |
|
public class TestRemoteSearchable extends TestCase { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 35 |
5
|
public TestRemoteSearchable(String name) {... |
| 36 |
5
|
super(name); |
| 37 |
|
} |
| 38 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
| 39 |
5
|
private static Searchable getRemote() throws Exception {... |
| 40 |
5
|
try { |
| 41 |
5
|
return lookupRemote(); |
| 42 |
|
} catch (Throwable e) { |
| 43 |
1
|
startServer(); |
| 44 |
1
|
return lookupRemote(); |
| 45 |
|
} |
| 46 |
|
} |
| 47 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 48 |
6
|
private static Searchable lookupRemote() throws Exception {... |
| 49 |
6
|
return (Searchable)Naming.lookup("//localhost/Searchable"); |
| 50 |
|
} |
| 51 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
| 52 |
1
|
private static void startServer() throws Exception {... |
| 53 |
|
|
| 54 |
1
|
RAMDirectory indexStore = new RAMDirectory(); |
| 55 |
1
|
IndexWriter writer = new IndexWriter(indexStore,new SimpleAnalyzer(),true); |
| 56 |
1
|
Document doc = new Document(); |
| 57 |
1
|
doc.add(new Field("test", "test text", Field.Store.YES, Field.Index.TOKENIZED)); |
| 58 |
1
|
writer.addDocument(doc); |
| 59 |
1
|
writer.optimize(); |
| 60 |
1
|
writer.close(); |
| 61 |
|
|
| 62 |
|
|
| 63 |
1
|
LocateRegistry.createRegistry(1099); |
| 64 |
1
|
Searchable local = new IndexSearcher(indexStore); |
| 65 |
1
|
RemoteSearchable impl = new RemoteSearchable(local); |
| 66 |
1
|
Naming.rebind("//localhost/Searchable", impl); |
| 67 |
|
} |
| 68 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
| 69 |
3
|
private static void search(Query query) throws Exception {... |
| 70 |
|
|
| 71 |
3
|
Searchable[] searchables = { getRemote() }; |
| 72 |
3
|
Searcher searcher = new MultiSearcher(searchables); |
| 73 |
3
|
Hits result = searcher.search(query); |
| 74 |
|
|
| 75 |
3
|
assertEquals(1, result.length()); |
| 76 |
3
|
assertEquals("test text", result.doc(0).get("test")); |
| 77 |
|
} |
| 78 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1
PASS
|
|
| 79 |
1
|
public void testTermQuery() throws Exception {... |
| 80 |
1
|
search(new TermQuery(new Term("test", "test"))); |
| 81 |
|
} |
| 82 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1
PASS
|
|
| 83 |
1
|
public void testBooleanQuery() throws Exception {... |
| 84 |
1
|
BooleanQuery query = new BooleanQuery(); |
| 85 |
1
|
query.add(new TermQuery(new Term("test", "test")), BooleanClause.Occur.MUST); |
| 86 |
1
|
search(query); |
| 87 |
|
} |
| 88 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
| 89 |
1
|
public void testPhraseQuery() throws Exception {... |
| 90 |
1
|
PhraseQuery query = new PhraseQuery(); |
| 91 |
1
|
query.add(new Term("test", "test")); |
| 92 |
1
|
query.add(new Term("test", "text")); |
| 93 |
1
|
search(query); |
| 94 |
|
} |
| 95 |
|
|
| 96 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1
PASS
|
|
| 97 |
1
|
public void testQueryFilter() throws Exception {... |
| 98 |
|
|
| 99 |
1
|
Searchable[] searchables = { getRemote() }; |
| 100 |
1
|
Searcher searcher = new MultiSearcher(searchables); |
| 101 |
1
|
Hits hits = searcher.search( |
| 102 |
|
new TermQuery(new Term("test", "text")), |
| 103 |
|
new QueryFilter(new TermQuery(new Term("test", "test")))); |
| 104 |
1
|
assertEquals(1, hits.length()); |
| 105 |
1
|
Hits nohits = searcher.search( |
| 106 |
|
new TermQuery(new Term("test", "text")), |
| 107 |
|
new QueryFilter(new TermQuery(new Term("test", "non-existent-term")))); |
| 108 |
1
|
assertEquals(0, nohits.length()); |
| 109 |
|
} |
| 110 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1
PASS
|
|
| 111 |
1
|
public void testConstantScoreQuery() throws Exception {... |
| 112 |
|
|
| 113 |
1
|
Searchable[] searchables = { getRemote() }; |
| 114 |
1
|
Searcher searcher = new MultiSearcher(searchables); |
| 115 |
1
|
Hits hits = searcher.search( |
| 116 |
|
new ConstantScoreQuery(new QueryFilter( |
| 117 |
|
new TermQuery(new Term("test", "test"))))); |
| 118 |
1
|
assertEquals(1, hits.length()); |
| 119 |
|
} |
| 120 |
|
} |