1 18 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 32 public class LRUCache2Test extends TestCase 33 { 34 37 protected void setUp() throws Exception 38 { 39 super.setUp(); 41 } 42 43 public void testTimeout() 44 { 45 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 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 ignored) {} 78 79 cache.get("key1"); 81 cache.get("key2"); 82 83 cache.put("key4", "string4"); 85 assertNull(cache.get("key3")); 86 } 87 88 public void testPurge() 89 { 90 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 [] {"key1", "key2"}); 103 assertEquals(1, cache.size()); 104 105 cache.purge(); 106 assertEquals(0, cache.size()); 107 } 108 109 112 protected void tearDown() throws Exception 113 { 114 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 |