KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > cache > LFUCacheTest


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.cache;
4
5 import junit.framework.TestCase;
6 import jodd.util.ThreadUtil;
7
8 public class LFUCacheTest extends TestCase {
9
10     public void testCache() {
11         Cache cache = new LFUCache(3);
12         cache.put("1", "1");
13         cache.put("2", "2");
14         assertFalse(cache.isFull());
15         cache.put("3", "3");
16         assertTrue(cache.isFull());
17
18         assertNotNull(cache.get("1"));
19         assertNotNull(cache.get("2"));
20         cache.put("4", "4"); // new element, cache is full, prune is invoked
21
assertNull(cache.get("3"));
22         assertNotNull(cache.get("1"));
23         assertNotNull(cache.get("2"));
24         cache.put("3", "3");
25         assertNull(cache.get("4"));
26         assertNotNull(cache.get("3"));
27     }
28
29     public void testCacheTime() {
30         Cache cache = new LFUCache(3);
31         cache.put("1", "1", 50);
32         assertNotNull(cache.get("1"));
33         assertNotNull(cache.get("1")); // boost usage
34
cache.put("2", "2");
35         cache.get("2");
36         assertFalse(cache.isFull());
37         cache.put("3", "3");
38         assertTrue(cache.isFull());
39
40         ThreadUtil.sleep(100);
41         assertNull(cache.get("1")); // expired
42
assertFalse(cache.isFull());
43
44         cache.put("1", "1", 50);
45         assertNotNull(cache.get("1"));
46         assertNotNull(cache.get("1"));
47
48         ThreadUtil.sleep(100);
49         assertTrue(cache.isFull());
50         cache.put("4", "4");
51         assertNotNull(cache.get("3"));
52         assertNotNull(cache.get("2"));
53         assertNotNull(cache.get("4"));
54         assertNull(cache.get("1"));
55     }
56
57 }
Popular Tags