01: package junit;
02:
03: import static org.junit.Assert.*;
04: import org.junit.Before;
05: import org.junit.After;
06:
07: import javax.cache.Cache;
08: import javax.cache.CacheException;
09: import javax.cache.CacheStatistics;
10:
11: import ri.cache.transport.TransportException;
12:
13: /**
14: * ParentChildCacheTestCase
15: *
16: * @author Brian Goetz
17: */
18: public abstract class ParentChildCacheTestCase extends CacheTestCase {
19:
20: protected Cache childCache, parentCache;
21: protected CacheStatistics childStats, parentStats;
22: protected TestListener childListener, parentListener;
23:
24: @Before
25: public void setUp() throws Exception {
26: parentCache = createParentCache();
27: childCache = createChildCache();
28: childStats = childCache.getCacheStatistics();
29: parentStats = parentCache.getCacheStatistics();
30: childListener = new TestListener();
31: childCache.addListener(childListener);
32: parentListener = new TestListener();
33: parentCache.addListener(parentListener);
34: }
35:
36: @After
37: public void tearDown() throws Exception {
38: childCache = parentCache = null;
39: childStats = parentStats = null;
40: }
41:
42: protected abstract Cache createParentCache()
43: throws TransportException;
44:
45: protected abstract Cache createChildCache()
46: throws TransportException;
47:
48: protected void assertStats(int childHits, int childMisses,
49: int parentHits, int parentMisses) {
50: assertEquals(childHits, childStats.getCacheHits());
51: assertEquals(childMisses, childStats.getCacheMisses());
52: assertEquals(parentHits, parentStats.getCacheHits());
53: assertEquals(parentMisses, parentStats.getCacheMisses());
54: }
55:
56: protected void assertEvents(int childLoads, int childPuts,
57: int parentLoads, int parentPuts) {
58: assertEquals(childLoads, childListener.loads);
59: assertEquals(childPuts, childListener.puts);
60: assertEquals(parentLoads, parentListener.loads);
61: assertEquals(parentPuts, parentListener.puts);
62: }
63:
64: protected void doTest1() throws CacheException {
65: assertStats(0, 0, 0, 0);
66: assertEvents(0, 0, 0, 0);
67:
68: Object value = childCache.get("a");
69: assertNotNull(value);
70: assertEquals("a", value);
71: assertStats(0, 1, 0, 1);
72: assertEvents(1, 0, 1, 0);
73:
74: value = childCache.get("a");
75: assertNotNull(value);
76: assertEquals("a", value);
77: assertStats(1, 1, 0, 1);
78: assertEvents(1, 0, 1, 0);
79:
80: childCache.remove("a");
81:
82: value = childCache.get("a");
83: assertNotNull(value);
84: assertEquals("a", value);
85: assertStats(1, 2, 1, 1);
86: assertEvents(2, 0, 1, 0);
87:
88: parentCache.remove("a");
89: value = childCache.get("a");
90: assertNotNull(value);
91: assertEquals("a", value);
92: assertStats(2, 2, 1, 1);
93: assertEvents(2, 0, 1, 0);
94: }
95:
96: }
|