KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > TestRemoteSearchable


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import junit.framework.TestCase;
20
21 import java.rmi.Naming JavaDoc;
22 import java.rmi.registry.LocateRegistry JavaDoc;
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 $Id: TestRemoteSearchable.java,v 1.7 2004/03/29 22:48:06 cutting Exp $
33  */

34 public class TestRemoteSearchable extends TestCase {
35   public TestRemoteSearchable(String JavaDoc name) {
36     super(name);
37   }
38
39   private static Searchable getRemote() throws Exception JavaDoc {
40     try {
41       return lookupRemote();
42     } catch (Throwable JavaDoc e) {
43       startServer();
44       return lookupRemote();
45     }
46   }
47
48   private static Searchable lookupRemote() throws Exception JavaDoc {
49     return (Searchable)Naming.lookup("//localhost/Searchable");
50   }
51
52   private static void startServer() throws Exception JavaDoc {
53     // construct an index
54
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     // publish it
63
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 JavaDoc {
70     // try to search the published index
71
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 JavaDoc {
80     search(new TermQuery(new Term("test", "test")));
81   }
82
83   public void testBooleanQuery() throws Exception JavaDoc {
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 JavaDoc {
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   // Tests bug fix at http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20290
97
public void testQueryFilter() throws Exception JavaDoc {
98     // try to search the published index
99
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