1 16 package org.apache.commons.collections.map; 17 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import junit.framework.Test; 24 import junit.textui.TestRunner; 25 26 import org.apache.commons.collections.BulkTest; 27 import org.apache.commons.collections.OrderedMap; 28 import org.apache.commons.collections.ResettableIterator; 29 30 37 public class TestLRUMap extends AbstractTestOrderedMap { 38 39 public TestLRUMap(String testName) { 40 super(testName); 41 } 42 43 public static void main(String [] args) { 44 TestRunner.run(suite()); 45 } 46 47 public static Test suite() { 48 return BulkTest.makeSuite(TestLRUMap.class); 49 } 50 51 public Map makeEmptyMap() { 52 return new LRUMap(); 53 } 54 55 public boolean isGetStructuralModify() { 56 return true; 57 } 58 59 public String getCompatibilityVersion() { 60 return "3"; 61 } 62 63 public void testLRU() { 65 if (isPutAddSupported() == false || isPutChangeSupported() == false) return; 66 Object [] keys = getSampleKeys(); 67 Object [] values = getSampleValues(); 68 Iterator it = null; 69 70 LRUMap map = new LRUMap(2); 71 assertEquals(0, map.size()); 72 assertEquals(false, map.isFull()); 73 assertEquals(2, map.maxSize()); 74 75 map.put(keys[0], values[0]); 76 assertEquals(1, map.size()); 77 assertEquals(false, map.isFull()); 78 assertEquals(2, map.maxSize()); 79 80 map.put(keys[1], values[1]); 81 assertEquals(2, map.size()); 82 assertEquals(true, map.isFull()); 83 assertEquals(2, map.maxSize()); 84 it = map.keySet().iterator(); 85 assertSame(keys[0], it.next()); 86 assertSame(keys[1], it.next()); 87 it = map.values().iterator(); 88 assertSame(values[0], it.next()); 89 assertSame(values[1], it.next()); 90 91 map.put(keys[2], values[2]); 92 assertEquals(2, map.size()); 93 assertEquals(true, map.isFull()); 94 assertEquals(2, map.maxSize()); 95 it = map.keySet().iterator(); 96 assertSame(keys[1], it.next()); 97 assertSame(keys[2], it.next()); 98 it = map.values().iterator(); 99 assertSame(values[1], it.next()); 100 assertSame(values[2], it.next()); 101 102 map.put(keys[2], values[0]); 103 assertEquals(2, map.size()); 104 assertEquals(true, map.isFull()); 105 assertEquals(2, map.maxSize()); 106 it = map.keySet().iterator(); 107 assertSame(keys[1], it.next()); 108 assertSame(keys[2], it.next()); 109 it = map.values().iterator(); 110 assertSame(values[1], it.next()); 111 assertSame(values[0], it.next()); 112 113 map.put(keys[1], values[3]); 114 assertEquals(2, map.size()); 115 assertEquals(true, map.isFull()); 116 assertEquals(2, map.maxSize()); 117 it = map.keySet().iterator(); 118 assertSame(keys[2], it.next()); 119 assertSame(keys[1], it.next()); 120 it = map.values().iterator(); 121 assertSame(values[0], it.next()); 122 assertSame(values[3], it.next()); 123 } 124 125 public void testReset() { 127 resetEmpty(); 128 OrderedMap ordered = (OrderedMap) map; 129 ((ResettableIterator) ordered.mapIterator()).reset(); 130 131 resetFull(); 132 ordered = (OrderedMap) map; 133 List list = new ArrayList (ordered.keySet()); 134 ResettableIterator it = (ResettableIterator) ordered.mapIterator(); 135 assertSame(list.get(0), it.next()); 136 assertSame(list.get(1), it.next()); 137 it.reset(); 138 assertSame(list.get(0), it.next()); 139 } 140 141 public void testAccessOrder() { 143 if (isPutAddSupported() == false || isPutChangeSupported() == false) return; 144 Object [] keys = getSampleKeys(); 145 Object [] values = getSampleValues(); 146 Iterator it = null; 147 148 resetEmpty(); 149 map.put(keys[0], values[0]); 150 map.put(keys[1], values[1]); 151 it = map.keySet().iterator(); 152 assertSame(keys[0], it.next()); 153 assertSame(keys[1], it.next()); 154 it = map.values().iterator(); 155 assertSame(values[0], it.next()); 156 assertSame(values[1], it.next()); 157 158 map.put(keys[1], values[1]); 160 it = map.keySet().iterator(); 161 assertSame(keys[0], it.next()); 162 assertSame(keys[1], it.next()); 163 it = map.values().iterator(); 164 assertSame(values[0], it.next()); 165 assertSame(values[1], it.next()); 166 167 map.put(keys[1], values[2]); 169 it = map.keySet().iterator(); 170 assertSame(keys[0], it.next()); 171 assertSame(keys[1], it.next()); 172 it = map.values().iterator(); 173 assertSame(values[0], it.next()); 174 assertSame(values[2], it.next()); 175 176 map.put(keys[0], values[3]); 178 it = map.keySet().iterator(); 179 assertSame(keys[1], it.next()); 180 assertSame(keys[0], it.next()); 181 it = map.values().iterator(); 182 assertSame(values[2], it.next()); 183 assertSame(values[3], it.next()); 184 185 map.get(keys[1]); 187 it = map.keySet().iterator(); 188 assertSame(keys[0], it.next()); 189 assertSame(keys[1], it.next()); 190 it = map.values().iterator(); 191 assertSame(values[3], it.next()); 192 assertSame(values[2], it.next()); 193 194 map.get(keys[0]); 196 it = map.keySet().iterator(); 197 assertSame(keys[1], it.next()); 198 assertSame(keys[0], it.next()); 199 it = map.values().iterator(); 200 assertSame(values[2], it.next()); 201 assertSame(values[3], it.next()); 202 203 map.get(keys[0]); 205 it = map.keySet().iterator(); 206 assertSame(keys[1], it.next()); 207 assertSame(keys[0], it.next()); 208 it = map.values().iterator(); 209 assertSame(values[2], it.next()); 210 assertSame(values[3], it.next()); 211 } 212 213 public void testClone() { 214 LRUMap map = new LRUMap(10); 215 map.put("1", "1"); 216 Map cloned = (Map ) map.clone(); 217 assertEquals(map.size(), cloned.size()); 218 assertSame(map.get("1"), cloned.get("1")); 219 } 220 221 public void testRemoveLRU() { 222 MockLRUMapSubclass map = new MockLRUMapSubclass(2); 223 assertNull(map.entry); 224 map.put("A", "a"); 225 assertNull(map.entry); 226 map.put("B", "b"); 227 assertNull(map.entry); 228 map.put("C", "c"); assertNotNull(map.entry); 230 assertEquals("A", map.key); 231 assertEquals("a", map.value); 232 assertEquals("C", map.entry.getKey()); assertEquals("c", map.entry.getValue()); assertEquals(false, map.containsKey("A")); 235 assertEquals(true, map.containsKey("B")); 236 assertEquals(true, map.containsKey("C")); 237 } 238 239 static class MockLRUMapSubclass extends LRUMap { 240 LinkEntry entry; 241 Object key; 242 Object value; 243 244 MockLRUMapSubclass(int size) { 245 super(size); 246 } 247 248 protected boolean removeLRU(LinkEntry entry) { 249 this.entry = entry; 250 this.key = entry.getKey(); 251 this.value = entry.getValue(); 252 return true; 253 } 254 } 255 256 public void testRemoveLRUBlocksRemove() { 257 MockLRUMapSubclassBlocksRemove map = new MockLRUMapSubclassBlocksRemove(2, false); 258 assertEquals(0, map.size()); 259 map.put("A", "a"); 260 assertEquals(1, map.size()); 261 map.put("B", "b"); 262 assertEquals(2, map.size()); 263 map.put("C", "c"); assertEquals(3, map.size()); 265 assertEquals(2, map.maxSize()); 266 assertEquals(true, map.containsKey("A")); 267 assertEquals(true, map.containsKey("B")); 268 assertEquals(true, map.containsKey("C")); 269 } 270 271 public void testRemoveLRUBlocksRemoveScan() { 272 MockLRUMapSubclassBlocksRemove map = new MockLRUMapSubclassBlocksRemove(2, true); 273 assertEquals(0, map.size()); 274 map.put("A", "a"); 275 assertEquals(1, map.size()); 276 map.put("B", "b"); 277 assertEquals(2, map.size()); 278 map.put("C", "c"); assertEquals(3, map.size()); 280 assertEquals(2, map.maxSize()); 281 assertEquals(true, map.containsKey("A")); 282 assertEquals(true, map.containsKey("B")); 283 assertEquals(true, map.containsKey("C")); 284 } 285 286 static class MockLRUMapSubclassBlocksRemove extends LRUMap { 287 MockLRUMapSubclassBlocksRemove(int size, boolean scanUntilRemove) { 288 super(size, scanUntilRemove); 289 } 290 291 protected boolean removeLRU(LinkEntry entry) { 292 return false; 293 } 294 } 295 296 public void testRemoveLRUFirstBlocksRemove() { 297 MockLRUMapSubclassFirstBlocksRemove map = new MockLRUMapSubclassFirstBlocksRemove(2); 298 assertEquals(0, map.size()); 299 map.put("A", "a"); 300 assertEquals(1, map.size()); 301 map.put("B", "b"); 302 assertEquals(2, map.size()); 303 map.put("C", "c"); assertEquals(2, map.size()); 305 assertEquals(2, map.maxSize()); 306 assertEquals(true, map.containsKey("A")); 307 assertEquals(false, map.containsKey("B")); 308 assertEquals(true, map.containsKey("C")); 309 } 310 311 static class MockLRUMapSubclassFirstBlocksRemove extends LRUMap { 312 MockLRUMapSubclassFirstBlocksRemove(int size) { 313 super(size, true); 314 } 315 316 protected boolean removeLRU(LinkEntry entry) { 317 if ("a".equals(entry.getValue())) { 318 return false; 319 } else { 320 return true; 321 } 322 } 323 } 324 325 } 332 | Popular Tags |