KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 /*
19  * Created on Jun 15, 2004
20  */

21 package org.apache.roller.util;
22
23 import org.apache.roller.ui.authoring.struts.actions.BookmarksActionTest;
24
25 import junit.framework.Test;
26 import junit.framework.TestCase;
27 import junit.framework.TestSuite;
28
29 /**
30  * @author dmj
31  */

32 public class LRUCache2Test extends TestCase
33 {
34     /**
35      * @see junit.framework.TestCase#setUp()
36      */

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

112     protected void tearDown() throws Exception JavaDoc
113     {
114         // TODO Auto-generated method stub
115
super.tearDown();
116     }
117     
118     public static class TestEnvironment implements LRUCache2.Environment
119     {
120         public long time = 0;
121         public long getCurrentTimeInMillis()
122         {
123             return time;
124         }
125     }
126
127     public static Test suite()
128     {
129         return new TestSuite(LRUCache2Test.class);
130     }
131 }
132
Popular Tags