01: package org.apache.lucene.search;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * 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 java.io.IOException;
21: import java.util.BitSet;
22: import java.util.WeakHashMap;
23:
24: import junit.framework.TestCase;
25:
26: import org.apache.lucene.index.IndexReader;
27:
28: /**
29: * A unit test helper class to test when the filter is getting cached and when it is not.
30: */
31: public class CachingWrapperFilterHelper extends CachingWrapperFilter {
32:
33: private boolean shouldHaveCache = false;
34:
35: /**
36: * @param filter Filter to cache results of
37: */
38: public CachingWrapperFilterHelper(Filter filter) {
39: super (filter);
40: }
41:
42: public void setShouldHaveCache(boolean shouldHaveCache) {
43: this .shouldHaveCache = shouldHaveCache;
44: }
45:
46: public BitSet bits(IndexReader reader) throws IOException {
47: if (cache == null) {
48: cache = new WeakHashMap();
49: }
50:
51: synchronized (cache) { // check cache
52: BitSet cached = (BitSet) cache.get(reader);
53: if (shouldHaveCache) {
54: TestCase.assertNotNull("Cache should have data ",
55: cached);
56: } else {
57: TestCase.assertNull("Cache should be null " + cached,
58: cached);
59: }
60: if (cached != null) {
61: return cached;
62: }
63: }
64:
65: final BitSet bits = filter.bits(reader);
66:
67: synchronized (cache) { // update cache
68: cache.put(reader, bits);
69: }
70:
71: return bits;
72: }
73:
74: public String toString() {
75: return "CachingWrapperFilterHelper(" + filter + ")";
76: }
77:
78: public boolean equals(Object o) {
79: if (!(o instanceof CachingWrapperFilterHelper))
80: return false;
81: return this .filter.equals((CachingWrapperFilterHelper) o);
82: }
83: }
|