KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > util > LRUCache2Test


1 /*
2  * Created on Jun 15, 2004
3  */

4 package org.roller.util;
5
6 import org.roller.presentation.bookmarks.BookmarksActionTest;
7
8 import junit.framework.Test;
9 import junit.framework.TestCase;
10 import junit.framework.TestSuite;
11
12 /**
13  * @author dmj
14  */

15 public class LRUCache2Test extends TestCase
16 {
17     /**
18      * @see junit.framework.TestCase#setUp()
19      */

20     protected void setUp() throws Exception JavaDoc
21     {
22         // TODO Auto-generated method stub
23
super.setUp();
24     }
25     
26     public void testTimeout()
27     {
28         // Create cache with 100 item limit and 15 second timeout
29
TestEnvironment env = new TestEnvironment();
30         LRUCache2 cache = new LRUCache2(env, 100, 15000);
31             
32         env.time = 1000;
33         cache.put("key1", "string1");
34         cache.put("key2", "string2");
35         cache.put("key3", "string3");
36         assertNotNull(cache.get("key1"));
37         assertNotNull(cache.get("key2"));
38         assertNotNull(cache.get("key3"));
39
40         env.time = 16000;
41         assertNull(cache.get("key1"));
42         assertNull(cache.get("key2"));
43         assertNull(cache.get("key3"));
44     }
45     
46     public void testLRU()
47     {
48         // Create cache with 3 item limit and 15 second timeout
49
TestEnvironment env = new TestEnvironment();
50         LRUCache2 cache = new LRUCache2(env, 3, 15000);
51             
52         env.time = 1000;
53         cache.put("key1", "string1");
54         cache.put("key2", "string2");
55         cache.put("key3", "string3");
56         assertNotNull(cache.get("key1"));
57         assertNotNull(cache.get("key2"));
58         assertNotNull(cache.get("key3"));
59         
60         try { Thread.sleep(200); } catch (InterruptedException JavaDoc ignored) {}
61         
62         // accessing key1 and key2 will make key3 LRU
63
cache.get("key1");
64         cache.get("key2");
65         
66         // adding a forth key will push out the LRU entry
67
cache.put("key4", "string4");
68         assertNull(cache.get("key3"));
69     }
70     
71     public void testPurge()
72     {
73         // Create cache with 100 item limit and 15 second timeout
74
TestEnvironment env = new TestEnvironment();
75         LRUCache2 cache = new LRUCache2(env, 100, 15000);
76             
77         env.time = 1000;
78         cache.put("key1", "string1");
79         cache.put("key2", "string2");
80         cache.put("key3", "string3");
81         assertNotNull(cache.get("key1"));
82         assertNotNull(cache.get("key2"));
83         assertNotNull(cache.get("key3"));
84         
85         cache.purge(new String JavaDoc[] {"key1", "key2"});
86         assertEquals(1, cache.size());
87         
88         cache.purge();
89         assertEquals(0, cache.size());
90     }
91     
92     /**
93      * @see junit.framework.TestCase#tearDown()
94      */

95     protected void tearDown() throws Exception JavaDoc
96     {
97         // TODO Auto-generated method stub
98
super.tearDown();
99     }
100     
101     public static class TestEnvironment implements LRUCache2.Environment
102     {
103         public long time = 0;
104         public long getCurrentTimeInMillis()
105         {
106             return time;
107         }
108     }
109
110     public static Test suite()
111     {
112         return new TestSuite(LRUCache2Test.class);
113     }
114 }
115
Popular Tags