1 package org.apache.lucene.search; 2 3 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 34 public class TestRemoteSearchable extends TestCase { 35 public TestRemoteSearchable(String name) { 36 super(name); 37 } 38 39 private static Searchable getRemote() throws Exception { 40 try { 41 return lookupRemote(); 42 } catch (Throwable e) { 43 startServer(); 44 return lookupRemote(); 45 } 46 } 47 48 private static Searchable lookupRemote() throws Exception { 49 return (Searchable)Naming.lookup("//localhost/Searchable"); 50 } 51 52 private static void startServer() throws Exception { 53 RAMDirectory indexStore = new RAMDirectory(); 55 IndexWriter writer = new IndexWriter(indexStore,new SimpleAnalyzer(),true); 56 Document doc = new Document(); 57 doc.add(Field.Text("test", "test text")); 58 writer.addDocument(doc); 59 writer.optimize(); 60 writer.close(); 61 62 LocateRegistry.createRegistry(1099); 64 Searchable local = new IndexSearcher(indexStore); 65 RemoteSearchable impl = new RemoteSearchable(local); 66 Naming.rebind("//localhost/Searchable", impl); 67 } 68 69 private static void search(Query query) throws Exception { 70 Searchable[] searchables = { getRemote() }; 72 Searcher searcher = new MultiSearcher(searchables); 73 Hits result = searcher.search(query); 74 75 assertEquals(1, result.length()); 76 assertEquals("test text", result.doc(0).get("test")); 77 } 78 79 public void testTermQuery() throws Exception { 80 search(new TermQuery(new Term("test", "test"))); 81 } 82 83 public void testBooleanQuery() throws Exception { 84 BooleanQuery query = new BooleanQuery(); 85 query.add(new TermQuery(new Term("test", "test")), true, false); 86 search(query); 87 } 88 89 public void testPhraseQuery() throws Exception { 90 PhraseQuery query = new PhraseQuery(); 91 query.add(new Term("test", "test")); 92 query.add(new Term("test", "text")); 93 search(query); 94 } 95 96 public void testQueryFilter() throws Exception { 98 Searchable[] searchables = { getRemote() }; 100 Searcher searcher = new MultiSearcher(searchables); 101 Hits hits = searcher.search( 102 new TermQuery(new Term("test", "text")), 103 new QueryFilter(new TermQuery(new Term("test", "test")))); 104 Hits nohits = searcher.search( 105 new TermQuery(new Term("test", "text")), 106 new QueryFilter(new TermQuery(new Term("test", "non-existent-term")))); 107 assertEquals(0, nohits.length()); 108 } 109 } 110 | Popular Tags |