Integer b = new Integer(2);
Integer c = new Integer(3);
String poop = new String("poop");
Map m = new DoubleWeakHashMap();
m.put(a, poop);
m.put(b, new Object());
m.put(c, new Object());
//race condition... b & c might already have been removed... but i doubt it
assertEquals("1) Weak values should not yet have been removed (but not guaranteed! sometimes fails without a defect!)", m.size(), 3);
// we are relying that a full, synchronous GC occurs,
// which is not guaranteed in all VMs
System.gc();
// let's see if we can force a deeper gc via a big array creation
byte[] bArray = new byte[1024 * 1024];
assertEquals("2) Weak values should have been automatically removed (but not guaranteed! sometimes fails without a defect!)", m.size(), 1);
m.put( new Object(), b);
//race condition... b & c might already have been removed... but i doubt it
assertEquals("3) Weak key should not yet have been removed (but not guaranteed! sometimes fails without a defect!)", m.size(), 2);
System.gc();
// let's see if we can force a deeper gc via a big array creation
bArray = new byte[1024 * 1024];
assertEquals("4) Weak key should have been automatically removed (but not guaranteed! sometimes fails without a defect!)", m.size(), 1);
}