001: package junit.api;
002:
003: import junit.support.EventRecordingCacheListener;
004: import junit.support.EventRecordingMemoryCacheLoader;
005: import junit.support.MethodInvocation;
006: import static org.junit.Assert.assertEquals;
007: import static org.junit.Assert.assertNull;
008: import org.junit.Before;
009: import org.junit.Test;
010:
011: import javax.cache.CacheException;
012: import javax.cache.spi.CacheLoaderException;
013: import java.util.HashMap;
014: import java.util.HashSet;
015: import java.util.LinkedList;
016: import java.util.List;
017: import java.util.Map;
018: import java.util.Set;
019:
020: /**
021: * Tests cache loading and eviction functionality of the cache
022: *
023: * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
024: */
025: public class CacheLoadingTest extends CacheAPITestBase {
026: private EventRecordingCacheListener listener;
027: private EventRecordingMemoryCacheLoader loader;
028:
029: @Before
030: public void addCacheLoaderAndListener() throws CacheException {
031: listener = new EventRecordingCacheListener();
032: loader = new EventRecordingMemoryCacheLoader();
033: setCacheLoader(loader);
034: cache.addListener(listener);
035: }
036:
037: @Test
038: public void loaderStoreAndRemove() throws CacheLoaderException {
039: cache.put(key, value);
040: assertEquals(value, loader.load(key));
041: assertEquals(value, cache.remove(key));
042: assertNull(cache.get(key));
043: assertNull(
044: "Should have been removed from the cache loader too!",
045: loader.load(key));
046: }
047:
048: @Test
049: public void forcedLoad() throws CacheException {
050: cache.load("key1");
051:
052: Set keys = new HashSet();
053: keys.add("key2");
054: keys.add("key3");
055: cache.loadAll(keys);
056:
057: List<MethodInvocation> expected = new LinkedList<MethodInvocation>();
058: for (int i = 1; i < 4; i++)
059: expected.add(new MethodInvocation(
060: CacheAPITestBase.listenerLoadMethod, key + i));
061:
062: assertEquals(expected, listener.getEvents());
063:
064: expected.clear();
065: expected.add(new MethodInvocation(
066: CacheAPITestBase.loaderLoadMethod, "key1"));
067: expected.add(new MethodInvocation(
068: CacheAPITestBase.loaderLoadAllMethod, keys));
069:
070: assertEquals(expected, loader.getEvents());
071: }
072:
073: @Test
074: public void loadOnMiss() throws CacheException {
075: cache.get("key1");
076:
077: Set keys = new HashSet();
078: keys.add("key2");
079: keys.add("key3");
080: cache.getAll(keys);
081:
082: cache.put("key4", "value4");
083:
084: Map data = new HashMap();
085: data.put("key5", "value5");
086: data.put("key6", "value6");
087:
088: cache.putAll(data);
089:
090: List<MethodInvocation> expected = new LinkedList<MethodInvocation>();
091: for (int i = 1; i < 7; i++)
092: expected.add(new MethodInvocation(
093: CacheAPITestBase.listenerLoadMethod, key + i));
094:
095: assertEquals(expected, scrubNonLoadEvents(listener.getEvents()));
096:
097: expected.clear();
098: expected.add(new MethodInvocation(
099: CacheAPITestBase.loaderLoadMethod, "key1"));
100: expected.add(new MethodInvocation(
101: CacheAPITestBase.loaderLoadAllMethod, keys));
102: expected.add(new MethodInvocation(
103: CacheAPITestBase.loaderLoadMethod, "key4"));
104: expected.add(new MethodInvocation(
105: CacheAPITestBase.loaderPutMethod, "key4", "value4"));
106: expected.add(new MethodInvocation(
107: CacheAPITestBase.loaderPutMethod, "key5", "value5"));
108: expected.add(new MethodInvocation(
109: CacheAPITestBase.loaderPutMethod, "key6", "value6"));
110:
111: assertEquals(expected, loader.getEvents());
112: }
113:
114: private List<MethodInvocation> scrubNonLoadEvents(
115: List<MethodInvocation> l) {
116: List<MethodInvocation> toReturn = new LinkedList<MethodInvocation>();
117: for (MethodInvocation m : l) {
118: if (m.getMethod().equals(
119: CacheAPITestBase.listenerLoadMethod))
120: toReturn.add(m);
121: }
122: return toReturn;
123: }
124: }
|