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 java.rmi.Naming;
021: import java.rmi.registry.LocateRegistry;
022:
023: import org.apache.lucene.util.LuceneTestCase;
024:
025: import org.apache.lucene.analysis.SimpleAnalyzer;
026: import org.apache.lucene.document.Document;
027: import org.apache.lucene.document.Field;
028: import org.apache.lucene.index.IndexWriter;
029: import org.apache.lucene.index.Term;
030: import org.apache.lucene.store.RAMDirectory;
031:
032: /**
033: * Tests that the index is cached on the searcher side of things.
034: * NOTE: This is copied from TestRemoteSearchable since it already had a remote index set up.
035: * @author Matt Ericson
036: */
037: public class TestRemoteCachingWrapperFilter extends LuceneTestCase {
038: public TestRemoteCachingWrapperFilter(String name) {
039: super (name);
040: }
041:
042: private static Searchable getRemote() throws Exception {
043: try {
044: return lookupRemote();
045: } catch (Throwable e) {
046: startServer();
047: return lookupRemote();
048: }
049: }
050:
051: private static Searchable lookupRemote() throws Exception {
052: return (Searchable) Naming.lookup("//localhost/Searchable");
053: }
054:
055: private static void startServer() throws Exception {
056: // construct an index
057: RAMDirectory indexStore = new RAMDirectory();
058: IndexWriter writer = new IndexWriter(indexStore,
059: new SimpleAnalyzer(), true);
060: Document doc = new Document();
061: doc.add(new Field("test", "test text", Field.Store.YES,
062: Field.Index.TOKENIZED));
063: doc.add(new Field("type", "A", Field.Store.YES,
064: Field.Index.TOKENIZED));
065: doc.add(new Field("other", "other test text", Field.Store.YES,
066: Field.Index.TOKENIZED));
067: writer.addDocument(doc);
068: //Need a second document to search for
069: doc = new Document();
070: doc.add(new Field("test", "test text", Field.Store.YES,
071: Field.Index.TOKENIZED));
072: doc.add(new Field("type", "B", Field.Store.YES,
073: Field.Index.TOKENIZED));
074: doc.add(new Field("other", "other test text", Field.Store.YES,
075: Field.Index.TOKENIZED));
076: writer.addDocument(doc);
077: writer.optimize();
078: writer.close();
079:
080: // publish it
081: LocateRegistry.createRegistry(1099);
082: Searchable local = new IndexSearcher(indexStore);
083: RemoteSearchable impl = new RemoteSearchable(local);
084: Naming.rebind("//localhost/Searchable", impl);
085: }
086:
087: private static void search(Query query, Filter filter,
088: int hitNumber, String typeValue) throws Exception {
089: Searchable[] searchables = { getRemote() };
090: Searcher searcher = new MultiSearcher(searchables);
091: Hits result = searcher.search(query, filter);
092: assertEquals(1, result.length());
093: Document document = result.doc(hitNumber);
094: assertTrue("document is null and it shouldn't be",
095: document != null);
096: assertEquals(typeValue, document.get("type"));
097: assertTrue("document.getFields() Size: "
098: + document.getFields().size() + " is not: " + 3,
099: document.getFields().size() == 3);
100: }
101:
102: public void testTermRemoteFilter() throws Exception {
103: CachingWrapperFilterHelper cwfh = new CachingWrapperFilterHelper(
104: new QueryFilter(new TermQuery(new Term("type", "a"))));
105:
106: // This is what we are fixing - if one uses a CachingWrapperFilter(Helper) it will never
107: // cache the filter on the remote site
108: cwfh.setShouldHaveCache(false);
109: search(new TermQuery(new Term("test", "test")), cwfh, 0, "A");
110: cwfh.setShouldHaveCache(false);
111: search(new TermQuery(new Term("test", "test")), cwfh, 0, "A");
112:
113: // This is how we fix caching - we wrap a Filter in the RemoteCachingWrapperFilter(Handler - for testing)
114: // to cache the Filter on the searcher (remote) side
115: RemoteCachingWrapperFilterHelper rcwfh = new RemoteCachingWrapperFilterHelper(
116: cwfh, false);
117: search(new TermQuery(new Term("test", "test")), rcwfh, 0, "A");
118:
119: // 2nd time we do the search, we should be using the cached Filter
120: rcwfh.shouldHaveCache(true);
121: search(new TermQuery(new Term("test", "test")), rcwfh, 0, "A");
122:
123: // assert that we get the same cached Filter, even if we create a new instance of RemoteCachingWrapperFilter(Helper)
124: // this should pass because the Filter parameters are the same, and the cache uses Filter's hashCode() as cache keys,
125: // and Filters' hashCode() builds on Filter parameters, not the Filter instance itself
126: rcwfh = new RemoteCachingWrapperFilterHelper(new QueryFilter(
127: new TermQuery(new Term("type", "a"))), false);
128: rcwfh.shouldHaveCache(false);
129: search(new TermQuery(new Term("test", "test")), rcwfh, 0, "A");
130:
131: rcwfh = new RemoteCachingWrapperFilterHelper(new QueryFilter(
132: new TermQuery(new Term("type", "a"))), false);
133: rcwfh.shouldHaveCache(true);
134: search(new TermQuery(new Term("test", "test")), rcwfh, 0, "A");
135:
136: // assert that we get a non-cached version of the Filter because this is a new Query (type:b)
137: rcwfh = new RemoteCachingWrapperFilterHelper(new QueryFilter(
138: new TermQuery(new Term("type", "b"))), false);
139: rcwfh.shouldHaveCache(false);
140: search(new TermQuery(new Term("type", "b")), rcwfh, 0, "B");
141: }
142: }
|