01: package junit.support;
02:
03: import junit.api.CacheAPITestBase;
04:
05: import javax.cache.spi.CacheLoader;
06: import javax.cache.spi.CacheLoaderException;
07: import java.util.Collection;
08: import java.util.HashMap;
09: import java.util.Map;
10:
11: /**
12: * A {@link javax.cache.CacheListener} implementation that records all invocations in a List of events, but also stores
13: * data in memory as a mock cache loader.
14: *
15: * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
16: */
17: public class EventRecordingMemoryCacheLoader extends EventRecorder
18: implements CacheLoader {
19: private Map store = new HashMap();
20:
21: public synchronized Object load(Object oKey)
22: throws CacheLoaderException {
23: events.add(new MethodInvocation(
24: CacheAPITestBase.loaderLoadMethod, oKey));
25: return store.get(oKey);
26: }
27:
28: public synchronized Map loadAll(Collection colKeys)
29: throws CacheLoaderException {
30: events.add(new MethodInvocation(
31: CacheAPITestBase.loaderLoadAllMethod, colKeys));
32:
33: Map retValue = new HashMap();
34:
35: for (Object key : colKeys)
36: retValue.put(key, store.get(key));
37:
38: return retValue;
39: }
40:
41: public synchronized void censorPut(Object key, Object value)
42: throws CacheLoaderException {
43: events.add(new MethodInvocation(
44: CacheAPITestBase.loaderPutMethod, key, value));
45: store.put(key, value);
46: }
47: }
|