1 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 15 public class LRUCache2Test extends TestCase 16 { 17 20 protected void setUp() throws Exception 21 { 22 super.setUp(); 24 } 25 26 public void testTimeout() 27 { 28 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 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 ignored) {} 61 62 cache.get("key1"); 64 cache.get("key2"); 65 66 cache.put("key4", "string4"); 68 assertNull(cache.get("key3")); 69 } 70 71 public void testPurge() 72 { 73 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 [] {"key1", "key2"}); 86 assertEquals(1, cache.size()); 87 88 cache.purge(); 89 assertEquals(0, cache.size()); 90 } 91 92 95 protected void tearDown() throws Exception 96 { 97 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 |