KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > cache > LRUCacheTest


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 LRUCacheTest extends TestCase {
9
10     public void testCache() {
11         Cache cache = new LRUCache(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");
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     }
27
28     public void testCache2() {
29         Cache cache = new LRUCache(3);
30         cache.put("1", "1");
31         cache.put("2", "2");
32         assertFalse(cache.isFull());
33         cache.put("3", "3");
34         assertTrue(cache.isFull());
35
36         assertNotNull(cache.get("3"));
37         assertNotNull(cache.get("3"));
38         assertNotNull(cache.get("3")); // boost usage of a 3
39
assertNotNull(cache.get("1"));
40         assertNotNull(cache.get("2"));
41         cache.put("4", "4");
42         assertNull(cache.get("3"));
43         assertNotNull(cache.get("1"));
44         assertNotNull(cache.get("2"));
45         assertNotNull(cache.get("4"));
46         cache.put("3", "3");
47         assertNull(cache.get("1"));
48     }
49
50
51     public void testCacheTime() {
52         Cache cache = new LRUCache(3);
53         cache.put("3", "3");
54         cache.put("2", "2");
55         assertNotNull(cache.get("2"));
56         cache.put("1", "1", 50);
57         assertNotNull(cache.get("1"));
58         assertTrue(cache.isFull());
59
60         ThreadUtil.sleep(100);
61         assertNull(cache.get("1")); // expired
62
assertFalse(cache.isFull());
63     }
64
65 }
66
Popular Tags