001: package org.apache.lucene.search;
002:
003: /**
004: * Licensed to the Apache Software Foundation (ASF) under one or more
005: * contributor license agreements. See the NOTICE file distributed with
006: * this work for additional information regarding copyright ownership.
007: * The ASF licenses this file to You under the Apache License, Version 2.0
008: * (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: import org.apache.lucene.util.LuceneTestCase;
021: import org.apache.lucene.analysis.SimpleAnalyzer;
022: import org.apache.lucene.document.*;
023: import org.apache.lucene.index.IndexWriter;
024: import org.apache.lucene.index.Term;
025: import org.apache.lucene.store.RAMDirectory;
026:
027: import java.rmi.Naming;
028: import java.rmi.registry.LocateRegistry;
029: import java.util.Collections;
030: import java.util.Set;
031: import java.util.HashSet;
032:
033: /**
034: * @version $Id: TestRemoteSearchable.java 583534 2007-10-10 16:46:35Z mikemccand $
035: */
036: public class TestRemoteSearchable extends LuceneTestCase {
037: public TestRemoteSearchable(String name) {
038: super (name);
039: }
040:
041: private static Searchable getRemote() throws Exception {
042: try {
043: return lookupRemote();
044: } catch (Throwable e) {
045: startServer();
046: return lookupRemote();
047: }
048: }
049:
050: private static Searchable lookupRemote() throws Exception {
051: return (Searchable) Naming.lookup("//localhost/Searchable");
052: }
053:
054: private static void startServer() throws Exception {
055: // construct an index
056: RAMDirectory indexStore = new RAMDirectory();
057: IndexWriter writer = new IndexWriter(indexStore,
058: new SimpleAnalyzer(), true);
059: Document doc = new Document();
060: doc.add(new Field("test", "test text", Field.Store.YES,
061: Field.Index.TOKENIZED));
062: doc.add(new Field("other", "other test text", Field.Store.YES,
063: Field.Index.TOKENIZED));
064: writer.addDocument(doc);
065: writer.optimize();
066: writer.close();
067:
068: // publish it
069: LocateRegistry.createRegistry(1099);
070: Searchable local = new IndexSearcher(indexStore);
071: RemoteSearchable impl = new RemoteSearchable(local);
072: Naming.rebind("//localhost/Searchable", impl);
073: }
074:
075: private static void search(Query query) throws Exception {
076: // try to search the published index
077: Searchable[] searchables = { getRemote() };
078: Searcher searcher = new MultiSearcher(searchables);
079: Hits result = searcher.search(query);
080:
081: assertEquals(1, result.length());
082: Document document = result.doc(0);
083: assertTrue("document is null and it shouldn't be",
084: document != null);
085: assertEquals("test text", document.get("test"));
086: assertTrue("document.getFields() Size: "
087: + document.getFields().size() + " is not: " + 2,
088: document.getFields().size() == 2);
089: Set ftl = new HashSet();
090: ftl.add("other");
091: FieldSelector fs = new SetBasedFieldSelector(ftl,
092: Collections.EMPTY_SET);
093: document = searcher.doc(0, fs);
094: assertTrue("document is null and it shouldn't be",
095: document != null);
096: assertTrue("document.getFields() Size: "
097: + document.getFields().size() + " is not: " + 1,
098: document.getFields().size() == 1);
099: fs = new MapFieldSelector(new String[] { "other" });
100: document = searcher.doc(0, fs);
101: assertTrue("document is null and it shouldn't be",
102: document != null);
103: assertTrue("document.getFields() Size: "
104: + document.getFields().size() + " is not: " + 1,
105: document.getFields().size() == 1);
106: }
107:
108: public void testTermQuery() throws Exception {
109: search(new TermQuery(new Term("test", "test")));
110: }
111:
112: public void testBooleanQuery() throws Exception {
113: BooleanQuery query = new BooleanQuery();
114: query.add(new TermQuery(new Term("test", "test")),
115: BooleanClause.Occur.MUST);
116: search(query);
117: }
118:
119: public void testPhraseQuery() throws Exception {
120: PhraseQuery query = new PhraseQuery();
121: query.add(new Term("test", "test"));
122: query.add(new Term("test", "text"));
123: search(query);
124: }
125:
126: // Tests bug fix at http://nagoya.apache.org/bugzilla/show_bug.cgi?id=20290
127: public void testQueryFilter() throws Exception {
128: // try to search the published index
129: Searchable[] searchables = { getRemote() };
130: Searcher searcher = new MultiSearcher(searchables);
131: Hits hits = searcher.search(new TermQuery(new Term("test",
132: "text")), new QueryFilter(new TermQuery(new Term(
133: "test", "test"))));
134: assertEquals(1, hits.length());
135: Hits nohits = searcher.search(new TermQuery(new Term("test",
136: "text")), new QueryFilter(new TermQuery(new Term(
137: "test", "non-existent-term"))));
138: assertEquals(0, nohits.length());
139: }
140:
141: public void testConstantScoreQuery() throws Exception {
142: // try to search the published index
143: Searchable[] searchables = { getRemote() };
144: Searcher searcher = new MultiSearcher(searchables);
145: Hits hits = searcher
146: .search(new ConstantScoreQuery(new QueryFilter(
147: new TermQuery(new Term("test", "test")))));
148: assertEquals(1, hits.length());
149: }
150: }
|