1 16 package org.apache.commons.collections.map; 17 18 import java.lang.ref.WeakReference ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import junit.framework.Test; 23 24 import org.apache.commons.collections.BulkTest; 25 import org.apache.commons.collections.IterableMap; 26 27 35 public class TestReferenceIdentityMap extends AbstractTestIterableMap { 36 37 private static final Integer I1A = new Integer (1); 38 private static final Integer I1B = new Integer (1); 39 private static final Integer I2A = new Integer (2); 40 private static final Integer I2B = new Integer (2); 41 42 public TestReferenceIdentityMap(String testName) { 43 super(testName); 44 } 45 46 public static Test suite() { 47 return BulkTest.makeSuite(TestReferenceIdentityMap.class); 48 } 49 50 public static void main(String args[]) { 51 String [] testCaseName = { TestReferenceIdentityMap.class.getName() }; 52 junit.textui.TestRunner.main(testCaseName); 53 } 54 55 public Map makeEmptyMap() { 56 ReferenceIdentityMap map = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.WEAK); 57 return map; 58 } 59 60 public Map makeConfirmedMap() { 61 return new IdentityMap(); 64 } 65 66 public boolean isAllowNullKey() { 67 return false; 68 } 69 70 public boolean isAllowNullValue() { 71 return false; 72 } 73 74 public String getCompatibilityVersion() { 75 return "3.1"; 76 } 77 78 public void testBasics() { 80 IterableMap map = new ReferenceIdentityMap(ReferenceIdentityMap.HARD, ReferenceIdentityMap.HARD); 81 assertEquals(0, map.size()); 82 83 map.put(I1A, I2A); 84 assertEquals(1, map.size()); 85 assertSame(I2A, map.get(I1A)); 86 assertSame(null, map.get(I1B)); 87 assertEquals(true, map.containsKey(I1A)); 88 assertEquals(false, map.containsKey(I1B)); 89 assertEquals(true, map.containsValue(I2A)); 90 assertEquals(false, map.containsValue(I2B)); 91 92 map.put(I1A, I2B); 93 assertEquals(1, map.size()); 94 assertSame(I2B, map.get(I1A)); 95 assertSame(null, map.get(I1B)); 96 assertEquals(true, map.containsKey(I1A)); 97 assertEquals(false, map.containsKey(I1B)); 98 assertEquals(false, map.containsValue(I2A)); 99 assertEquals(true, map.containsValue(I2B)); 100 101 map.put(I1B, I2B); 102 assertEquals(2, map.size()); 103 assertSame(I2B, map.get(I1A)); 104 assertSame(I2B, map.get(I1B)); 105 assertEquals(true, map.containsKey(I1A)); 106 assertEquals(true, map.containsKey(I1B)); 107 assertEquals(false, map.containsValue(I2A)); 108 assertEquals(true, map.containsValue(I2B)); 109 } 110 111 public void testHashEntry() { 113 IterableMap map = new ReferenceIdentityMap(ReferenceIdentityMap.HARD, ReferenceIdentityMap.HARD); 114 115 map.put(I1A, I2A); 116 map.put(I1B, I2A); 117 118 Map.Entry entry1 = (Map.Entry ) map.entrySet().iterator().next(); 119 Iterator it = map.entrySet().iterator(); 120 Map.Entry entry2 = (Map.Entry ) it.next(); 121 Map.Entry entry3 = (Map.Entry ) it.next(); 122 123 assertEquals(true, entry1.equals(entry2)); 124 assertEquals(true, entry2.equals(entry1)); 125 assertEquals(false, entry1.equals(entry3)); 126 } 127 128 129 public void testNullHandling() { 131 resetFull(); 132 assertEquals(null, map.get(null)); 133 assertEquals(false, map.containsKey(null)); 134 assertEquals(false, map.containsValue(null)); 135 assertEquals(null, map.remove(null)); 136 assertEquals(false, map.entrySet().contains(null)); 137 assertEquals(false, map.keySet().contains(null)); 138 assertEquals(false, map.values().contains(null)); 139 try { 140 map.put(null, null); 141 fail(); 142 } catch (NullPointerException ex) {} 143 try { 144 map.put(new Object (), null); 145 fail(); 146 } catch (NullPointerException ex) {} 147 try { 148 map.put(null, new Object ()); 149 fail(); 150 } catch (NullPointerException ex) {} 151 } 152 153 263 264 265 public void testPurgeValues() throws Exception { 266 Object key = new Object (); 268 Object value = new Object (); 269 270 WeakReference keyReference = new WeakReference (key); 271 WeakReference valueReference = new WeakReference (value); 272 273 Map testMap = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.HARD, true); 274 testMap.put(key, value); 275 276 assertEquals("In map", value, testMap.get(key)); 277 assertNotNull("Weak reference released early (1)", keyReference.get()); 278 assertNotNull("Weak reference released early (2)", valueReference.get()); 279 280 key = null; 282 value = null; 283 284 int iterations = 0; 285 int bytz = 2; 286 while(true) { 287 System.gc(); 288 if(iterations++ > 50){ 289 fail("Max iterations reached before resource released."); 290 } 291 testMap.isEmpty(); 292 if( 293 keyReference.get() == null && 294 valueReference.get() == null) { 295 break; 296 297 } else { 298 byte[] b = new byte[bytz]; 300 bytz = bytz * 2; 301 } 302 } 303 } 304 305 private static void gc() { 306 try { 307 byte[][] tooLarge = new byte[1000000000][1000000000]; 309 fail("you have too much RAM"); 310 } catch (OutOfMemoryError ex) { 311 System.gc(); } 313 } 314 315 } 316 | Popular Tags |