1 16 package org.apache.commons.collections; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import junit.framework.Test; 25 26 35 public class TestLRUMap extends TestSequencedHashMap { 36 37 public TestLRUMap(String testName) { 38 super(testName); 39 } 40 41 public static Test suite() { 42 return BulkTest.makeSuite(TestLRUMap.class); 43 } 44 45 public static void main(String args[]) { 46 String [] testCaseName = { TestLRUMap.class.getName() }; 47 junit.textui.TestRunner.main(testCaseName); 48 } 49 50 public Map makeEmptyMap() { 52 LRUMap map = new LRUMap(); 53 return map; 54 } 55 56 59 public String [] ignoredTests() { 60 return new String [] {"TestLRUMap.bulkTestMapEntrySet.testMapEntrySetIteratorEntry"}; 61 } 62 63 public void testRemoveLRU() { 65 LRUMap map2 = new LRUMap(3); 66 map2.put(new Integer (1),"foo"); 67 map2.put(new Integer (2),"foo"); 68 map2.put(new Integer (3),"foo"); 69 map2.put(new Integer (4),"foo"); map2.removeLRU(); 72 assertTrue("Second to last value should exist",map2.get(new Integer (3)).equals("foo")); 73 assertTrue("First value inserted should not exist", map2.get(new Integer (1)) == null); 74 } 75 76 public void testMultiplePuts() { 77 LRUMap map2 = new LRUMap(2); 78 map2.put(new Integer (1),"foo"); 79 map2.put(new Integer (2),"bar"); 80 map2.put(new Integer (3),"foo"); 81 map2.put(new Integer (4),"bar"); 82 83 assertTrue("last value should exist",map2.get(new Integer (4)).equals("bar")); 84 assertTrue("LRU should not exist", map2.get(new Integer (1)) == null); 85 } 86 87 91 public void testPutAll() { 92 LRUMap map2 = new LRUMap(3); 93 map2.put(new Integer (1),"foo"); 94 map2.put(new Integer (2),"foo"); 95 map2.put(new Integer (3),"foo"); 96 97 HashMap hashMap = new HashMap (); 98 hashMap.put(new Integer (4),"foo"); 99 100 map2.putAll(hashMap); 101 102 assertTrue("max size is 3, but actual size is " + map2.size(), 103 map2.size() == 3); 104 assertTrue("map should contain the Integer(4) object", 105 map2.containsKey(new Integer (4))); 106 } 107 108 112 public void testSetMaximumSize() { 113 LRUMap map = new LRUMap(6); 114 map.put("1","1"); 115 map.put("2","2"); 116 map.put("3","3"); 117 map.put("4","4"); 118 map.put("5","5"); 119 map.put("6","6"); 120 map.setMaximumSize(3); 121 122 assertTrue("map should have size = 3, but actually = " + map.size(), 123 map.size() == 3); 124 } 125 126 public void testGetPromotion() { 127 LRUMap map = new LRUMap(3); 128 map.put("1","1"); 129 map.put("2","2"); 130 map.put("3","3"); 131 133 map.get("1"); 136 137 map.put("4","4"); 140 141 Iterator keyIterator = map.keySet().iterator(); 142 Object [] keys = new Object [3]; 143 for (int i = 0; keyIterator.hasNext() ; ++i) { 144 keys[i] = keyIterator.next(); 145 } 146 147 assertTrue("first evicted should be 3, was " + keys[0], keys[0].equals("3")); 148 assertTrue("second evicted should be 1, was " + keys[1], keys[1].equals("1")); 149 assertTrue("third evicted should be 4, was " + keys[2], keys[2].equals("4")); 150 151 } 152 153 158 public void testLRUSubclass() { 159 LRUCounter counter = new LRUCounter(3); 160 counter.put("1","foo"); 163 counter.put("2","foo"); 165 counter.put("3","foo"); 167 counter.put("1","foo"); 169 counter.put("4","foo"); 171 counter.put("5","foo"); 173 counter.put("2","foo"); 175 counter.remove("5"); 177 178 assertTrue("size should be 2, but was " + counter.size(), counter.size() == 2); 179 assertTrue("removedCount should be 3 but was " + counter.removedCount, 180 counter.removedCount == 3); 181 182 assertTrue("first removed was '2'",counter.list.get(0).equals("2")); 183 assertTrue("second removed was '3'",counter.list.get(1).equals("3")); 184 assertTrue("third removed was '1'",counter.list.get(2).equals("1")); 185 186 assertTrue("oldest key is '4'",counter.get(0).equals("4")); 187 assertTrue("newest key is '2'",counter.get(1).equals("2")); 188 } 189 190 191 protected void entrySetEqualsMap(Set set, Map m) { 192 } 196 197 private class LRUCounter extends LRUMap { 198 int removedCount = 0; 199 ArrayList list = new ArrayList (3); 200 201 LRUCounter(int i) { 202 super(i); 203 } 204 205 protected void processRemovedLRU(Object key, Object value) { 206 ++removedCount; 207 list.add(key); 208 } 209 } 210 211 } 212 | Popular Tags |