01: package junit;
02:
03: import static org.junit.Assert.*;
04: import org.junit.Test;
05:
06: import ri.cache.BasicCache;
07: import ri.cache.eviction.BatchLRUEvictionStrategy;
08: import ri.cache.eviction.EvictAllEvictionStrategy;
09: import ri.cache.eviction.LRUChainEvictionStrategy;
10: import ri.cache.loader.NullCacheLoader;
11:
12: import javax.cache.Cache;
13:
14: public class EvictionStrategyTestCase extends CacheTestCase {
15:
16: @Test
17: public void testEviction() throws Exception {
18: Cache c = new BasicCache("TestCache", 10, 0L,
19: new EvictAllEvictionStrategy(), new NullCacheLoader());
20: TestListener l = new TestListener();
21: c.addListener(l);
22: for (int i = 0; i < 11; i++) {
23: c.put(new Integer(i), new Integer(i));
24: }
25: assertTrue(l.evicts >= 1);
26: assertTrue(c.get(new Integer(0)) == null);
27:
28: assertInternallyConsistent(c);
29: }
30:
31: @Test
32: public void testLRUEviction() throws Exception {
33: Cache c = new BasicCache("TestCache", 10, 0L,
34: new LRUChainEvictionStrategy(), new NullCacheLoader());
35: TestListener l = new TestListener();
36: c.addListener(l);
37: for (int i = 0; i < 11; i++) {
38: c.put(new Integer(i), new Integer(i));
39: }
40: assertTrue(l.evicts == 1);
41: assertTrue(c.get(new Integer(0)) == null);
42:
43: assertInternallyConsistent(c);
44: }
45:
46: @Test
47: public void testLFUEviction() throws Exception {
48: Cache c = new BasicCache("TestCache", 10, 0L,
49: new LRUChainEvictionStrategy(), new NullCacheLoader());
50: TestListener l = new TestListener();
51: c.addListener(l);
52: for (int i = 0; i < 11; i++) {
53: c.put(new Integer(i), new Integer(i));
54: }
55: assertTrue(l.evicts == 1);
56: assertTrue(c.get(new Integer(0)) == null);
57:
58: assertInternallyConsistent(c);
59: }
60:
61: @Test
62: public void testBatchLRUEviction() throws Exception {
63: Cache c = new BasicCache("TestCache", 10, 0L,
64: new BatchLRUEvictionStrategy(5, 10),
65: new NullCacheLoader());
66: TestListener l = new TestListener();
67: c.addListener(l);
68: for (int i = 0; i < 11; i++) {
69: c.put(new Integer(i), new Integer(i));
70: // sleep is required here so that all of the entries don't have
71: // the same creation time
72: Thread.sleep(10L);
73: }
74: assertTrue(l.evicts >= 1);
75: assertTrue(c.get(new Integer(0)) == null);
76:
77: assertInternallyConsistent(c);
78: }
79: }
|