add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / remote / src / test / org / apache / lucene / search / TestRemoteCachingWrapperFilter.java
1 package org.apache.lucene.search;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.analysis.SimpleAnalyzer;
21 import org.apache.lucene.document.Document;
22 import org.apache.lucene.document.Field;
23 import org.apache.lucene.index.IndexWriter;
24 import org.apache.lucene.index.Term;
25 import org.apache.lucene.store.Directory;
26 import org.junit.AfterClass;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29
30 /**
31  * Tests that the index is cached on the searcher side of things.
32  */
33 public class TestRemoteCachingWrapperFilter extends RemoteTestCase {
34   private static Directory indexStore;
35   private static Searchable local;
36   
37   @BeforeClass
38   public static void beforeClass() throws Exception {
39     // construct an index
40     indexStore = newDirectory();
41     IndexWriter writer = new IndexWriter(indexStore, newIndexWriterConfig(
42         TEST_VERSION_CURRENT, new SimpleAnalyzer(
43         TEST_VERSION_CURRENT)));
44     Document doc = new Document();
45     doc.add(newField("test", "test text", Field.Store.YES, Field.Index.ANALYZED));
46     doc.add(newField("type", "A", Field.Store.YES, Field.Index.ANALYZED));
47     doc.add(newField("other", "other test text", Field.Store.YES, Field.Index.ANALYZED));
48     writer.addDocument(doc);
49     //Need a second document to search for
50     doc = new Document();
51     doc.add(newField("test", "test text", Field.Store.YES, Field.Index.ANALYZED));
52     doc.add(newField("type", "B", Field.Store.YES, Field.Index.ANALYZED));
53     doc.add(newField("other", "other test text", Field.Store.YES, Field.Index.ANALYZED));
54     writer.addDocument(doc);
55     writer.optimize();
56     writer.close();
57     local = new IndexSearcher(indexStore, true);
58     startServer(local);
59   }
60
61   @AfterClass
62   public static void afterClass() throws Exception {
63     local.close();
64     indexStore.close();
65     indexStore = null;
66   }
67   
68   private static void search(Query query, Filter filter, int hitNumber, String typeValue) throws Exception {
69     Searchable[] searchables = { lookupRemote() };
70     Searcher searcher = new MultiSearcher(searchables);
71     ScoreDoc[] result = searcher.search(query,filter, 1000).scoreDocs;
72     assertEquals(1, result.length);
73     Document document = searcher.doc(result[hitNumber].doc);
74     assertTrue("document is null and it shouldn't be", document != null);
75     assertEquals(typeValue, document.get("type"));
76     assertTrue("document.getFields() Size: " + document.getFields().size() + " is not: " + 3, document.getFields().size() == 3);
77   }
78
79   @Test
80   public void testTermRemoteFilter() throws Exception {
81     CachingWrapperFilterHelper cwfh = new CachingWrapperFilterHelper(new QueryWrapperFilter(new TermQuery(new Term("type", "a"))));
82     
83     // This is what we are fixing - if one uses a CachingWrapperFilter(Helper) it will never 
84     // cache the filter on the remote site
85     cwfh.setShouldHaveCache(false);
86     search(new TermQuery(new Term("test", "test")), cwfh, 0, "A");
87     cwfh.setShouldHaveCache(false);
88     search(new TermQuery(new Term("test", "test")), cwfh, 0, "A");
89     
90     // This is how we fix caching - we wrap a Filter in the RemoteCachingWrapperFilter(Handler - for testing)
91     // to cache the Filter on the searcher (remote) side
92     RemoteCachingWrapperFilterHelper rcwfh = new RemoteCachingWrapperFilterHelper(cwfh, false);
93     search(new TermQuery(new Term("test", "test")), rcwfh, 0, "A");
94
95     // 2nd time we do the search, we should be using the cached Filter
96     rcwfh.shouldHaveCache(true);
97     search(new TermQuery(new Term("test", "test")), rcwfh, 0, "A");
98
99     // assert that we get the same cached Filter, even if we create a new instance of RemoteCachingWrapperFilter(Helper)
100     // this should pass because the Filter parameters are the same, and the cache uses Filter's hashCode() as cache keys,
101     // and Filters' hashCode() builds on Filter parameters, not the Filter instance itself
102     rcwfh = new RemoteCachingWrapperFilterHelper(new QueryWrapperFilter(new TermQuery(new Term("type", "a"))), false);
103     rcwfh.shouldHaveCache(false);
104     search(new TermQuery(new Term("test", "test")), rcwfh, 0, "A");
105
106     rcwfh = new RemoteCachingWrapperFilterHelper(new QueryWrapperFilter(new TermQuery(new Term("type", "a"))), false);
107     rcwfh.shouldHaveCache(true);
108     search(new TermQuery(new Term("test", "test")), rcwfh, 0, "A");
109
110     // assert that we get a non-cached version of the Filter because this is a new Query (type:b)
111     rcwfh = new RemoteCachingWrapperFilterHelper(new QueryWrapperFilter(new TermQuery(new Term("type", "b"))), false);
112     rcwfh.shouldHaveCache(false);
113     search(new TermQuery(new Term("type", "b")), rcwfh, 0, "B");
114   }
115 }