diff --git a/src/test/org/apache/commons/collections/TestReferenceMap.java b/src/test/org/apache/commons/collections/TestReferenceMap.java index 94b7507f7..fba08bf73 100644 --- a/src/test/org/apache/commons/collections/TestReferenceMap.java +++ b/src/test/org/apache/commons/collections/TestReferenceMap.java @@ -16,6 +16,7 @@ package org.apache.commons.collections; import java.lang.ref.WeakReference; +import java.util.Iterator; import java.util.Map; import junit.framework.Test; @@ -25,7 +26,7 @@ import org.apache.commons.collections.map.AbstractTestMap; /** * Tests for ReferenceMap. * - * @version $Revision: 1.17 $ $Date: 2004/02/18 01:20:35 $ + * @version $Revision: 1.18 $ $Date: 2004/04/30 22:53:16 $ * * @author Paul Jack */ @@ -57,13 +58,35 @@ public class TestReferenceMap extends AbstractTestMap { return false; } + public String getCompatibilityVersion() { + return "2.1"; + } -/* - // Unfortunately, these tests all rely on System.gc(), which is - // not reliable across platforms. Not sure how to code the tests - // without using System.gc() though... - // They all passed on my platform though. :) + //----------------------------------------------------------------------- + public void testNullHandling() { + resetFull(); + assertEquals(null, map.get(null)); + assertEquals(false, map.containsKey(null)); + assertEquals(false, map.containsValue(null)); + assertEquals(null, map.remove(null)); + assertEquals(false, map.entrySet().contains(null)); + assertEquals(false, map.keySet().contains(null)); + assertEquals(false, map.values().contains(null)); + try { + map.put(null, null); + fail(); + } catch (NullPointerException ex) {} + try { + map.put(new Object(), null); + fail(); + } catch (NullPointerException ex) {} + try { + map.put(null, new Object()); + fail(); + } catch (NullPointerException ex) {} + } + //----------------------------------------------------------------------- public void testPurge() { ReferenceMap map = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.WEAK); Object[] hard = new Object[10]; @@ -71,13 +94,13 @@ public class TestReferenceMap extends AbstractTestMap { hard[i] = new Object(); map.put(hard[i], new Object()); } - System.gc(); + gc(); assertTrue("map should be empty after purge of weak values", map.isEmpty()); for (int i = 0; i < hard.length; i++) { map.put(new Object(), hard[i]); } - System.gc(); + gc(); assertTrue("map should be empty after purge of weak keys", map.isEmpty()); for (int i = 0; i < hard.length; i++) { @@ -85,7 +108,7 @@ public class TestReferenceMap extends AbstractTestMap { map.put(hard[i], new Object()); } - System.gc(); + gc(); assertTrue("map should be empty after purge of weak keys and values", map.isEmpty()); } @@ -96,7 +119,7 @@ public class TestReferenceMap extends AbstractTestMap { map.put(new Integer(i), new Integer(i)); } - System.gc(); + gc(); for (int i = 0; i < 10; i++) { Integer I = new Integer(i); assertTrue("map.containsKey should return false for GC'd element", !map.containsKey(I)); @@ -114,7 +137,7 @@ public class TestReferenceMap extends AbstractTestMap { map.put(hard[i], hard[i]); } - System.gc(); + gc(); Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry)iterator.next(); @@ -125,24 +148,8 @@ public class TestReferenceMap extends AbstractTestMap { } } -*/ -/* - // Uncomment to create test files in /data/test - public void testCreateTestFiles() throws Exception { - ReferenceMap m = (ReferenceMap)makeEmptyMap(); - writeExternalFormToDisk(m, getCanonicalEmptyCollectionName(m)); - m = (ReferenceMap)makeFullMap(); - writeExternalFormToDisk(m, getCanonicalFullCollectionName(m)); - } -*/ - - - public String getCompatibilityVersion() { - return "2.1"; - } - /** Tests whether purge values setting works */ public void testPurgeValues() throws Exception { // many thanks to Juozas Baliuka for suggesting this method @@ -183,4 +190,14 @@ public class TestReferenceMap extends AbstractTestMap { } } } + + private static void gc() { + try { + // trigger GC + byte[][] tooLarge = new byte[1000000000][1000000000]; + fail("you have too much RAM"); + } catch (OutOfMemoryError ex) { + System.gc(); // ignore + } + } } diff --git a/src/test/org/apache/commons/collections/map/TestReferenceIdentityMap.java b/src/test/org/apache/commons/collections/map/TestReferenceIdentityMap.java index d9234937f..b0357cb2f 100644 --- a/src/test/org/apache/commons/collections/map/TestReferenceIdentityMap.java +++ b/src/test/org/apache/commons/collections/map/TestReferenceIdentityMap.java @@ -23,11 +23,12 @@ import junit.framework.Test; import org.apache.commons.collections.BulkTest; import org.apache.commons.collections.IterableMap; +import org.apache.commons.collections.MapIterator; /** * Tests for ReferenceIdentityMap. * - * @version $Revision: 1.1 $ + * @version $Revision: 1.2 $ * * @author Paul Jack * @author Stephen Colebourne @@ -71,6 +72,10 @@ public class TestReferenceIdentityMap extends AbstractTestIterableMap { return false; } + public String getCompatibilityVersion() { + return "3.1"; + } + //----------------------------------------------------------------------- public void testBasics() { IterableMap map = new ReferenceIdentityMap(ReferenceIdentityMap.HARD, ReferenceIdentityMap.HARD); @@ -123,11 +128,30 @@ public class TestReferenceIdentityMap extends AbstractTestIterableMap { //----------------------------------------------------------------------- - // Unfortunately, these tests all rely on System.gc(), which is - // not reliable across platforms. Not sure how to code the tests - // without using System.gc() though... - // They all passed on my platform though. :) -/* + public void testNullHandling() { + resetFull(); + assertEquals(null, map.get(null)); + assertEquals(false, map.containsKey(null)); + assertEquals(false, map.containsValue(null)); + assertEquals(null, map.remove(null)); + assertEquals(false, map.entrySet().contains(null)); + assertEquals(false, map.keySet().contains(null)); + assertEquals(false, map.values().contains(null)); + try { + map.put(null, null); + fail(); + } catch (NullPointerException ex) {} + try { + map.put(new Object(), null); + fail(); + } catch (NullPointerException ex) {} + try { + map.put(null, new Object()); + fail(); + } catch (NullPointerException ex) {} + } + + //----------------------------------------------------------------------- public void testPurge() { ReferenceIdentityMap map = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.WEAK); Object[] hard = new Object[10]; @@ -135,13 +159,13 @@ public class TestReferenceIdentityMap extends AbstractTestIterableMap { hard[i] = new Object(); map.put(hard[i], new Object()); } - System.gc(); + gc(); assertTrue("map should be empty after purge of weak values", map.isEmpty()); for (int i = 0; i < hard.length; i++) { map.put(new Object(), hard[i]); } - System.gc(); + gc(); assertTrue("map should be empty after purge of weak keys", map.isEmpty()); for (int i = 0; i < hard.length; i++) { @@ -149,7 +173,7 @@ public class TestReferenceIdentityMap extends AbstractTestIterableMap { map.put(hard[i], new Object()); } - System.gc(); + gc(); assertTrue("map should be empty after purge of weak keys and values", map.isEmpty()); } @@ -160,7 +184,7 @@ public class TestReferenceIdentityMap extends AbstractTestIterableMap { map.put(new Integer(i), new Integer(i)); } - System.gc(); + gc(); for (int i = 0; i < 10; i++) { Integer I = new Integer(i); assertTrue("map.containsKey should return false for GC'd element", !map.containsKey(I)); @@ -178,7 +202,7 @@ public class TestReferenceIdentityMap extends AbstractTestIterableMap { map.put(hard[i], hard[i]); } - System.gc(); + gc(); Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry)iterator.next(); @@ -199,7 +223,7 @@ public class TestReferenceIdentityMap extends AbstractTestIterableMap { map.put(hard[i], hard[i]); } - System.gc(); + gc(); MapIterator iterator = map.mapIterator(); while (iterator.hasNext()) { Object key1 = iterator.next(); @@ -224,7 +248,7 @@ public class TestReferenceIdentityMap extends AbstractTestIterableMap { MapIterator iterator = map.mapIterator(); while (iterator.hasNext()) { Object key1 = iterator.next(); - System.gc(); + gc(); Integer key = (Integer) iterator.getKey(); Integer value = (Integer) iterator.getValue(); assertTrue("iterator keys should match", key == key1); @@ -233,21 +257,6 @@ public class TestReferenceIdentityMap extends AbstractTestIterableMap { } } -*/ -/* - // Uncomment to create test files in /data/test - public void testCreateTestFiles() throws Exception { - ReferenceIdentityMap m = (ReferenceIdentityMap) makeEmptyMap(); - writeExternalFormToDisk(m, getCanonicalEmptyCollectionName(m)); - m = (ReferenceIdentityMap) makeFullMap(); - writeExternalFormToDisk(m, getCanonicalFullCollectionName(m)); - } -*/ - - - public String getCompatibilityVersion() { - return "3.1"; - } /** Tests whether purge values setting works */ public void testPurgeValues() throws Exception { @@ -289,4 +298,15 @@ public class TestReferenceIdentityMap extends AbstractTestIterableMap { } } } + + private static void gc() { + try { + // trigger GC + byte[][] tooLarge = new byte[1000000000][1000000000]; + fail("you have too much RAM"); + } catch (OutOfMemoryError ex) { + System.gc(); // ignore + } + } + } diff --git a/src/test/org/apache/commons/collections/map/TestReferenceMap.java b/src/test/org/apache/commons/collections/map/TestReferenceMap.java index 73c7d37bd..7ff3bbf38 100644 --- a/src/test/org/apache/commons/collections/map/TestReferenceMap.java +++ b/src/test/org/apache/commons/collections/map/TestReferenceMap.java @@ -16,16 +16,18 @@ package org.apache.commons.collections.map; import java.lang.ref.WeakReference; +import java.util.Iterator; import java.util.Map; import junit.framework.Test; import org.apache.commons.collections.BulkTest; +import org.apache.commons.collections.MapIterator; /** * Tests for ReferenceMap. * - * @version $Revision: 1.6 $ $Date: 2004/04/27 21:35:23 $ + * @version $Revision: 1.7 $ $Date: 2004/04/30 22:53:16 $ * * @author Paul Jack */ @@ -57,13 +59,35 @@ public class TestReferenceMap extends AbstractTestIterableMap { return false; } + public String getCompatibilityVersion() { + return "3.1"; + } -/* - // Unfortunately, these tests all rely on System.gc(), which is - // not reliable across platforms. Not sure how to code the tests - // without using System.gc() though... - // They all passed on my platform though. :) + //----------------------------------------------------------------------- + public void testNullHandling() { + resetFull(); + assertEquals(null, map.get(null)); + assertEquals(false, map.containsKey(null)); + assertEquals(false, map.containsValue(null)); + assertEquals(null, map.remove(null)); + assertEquals(false, map.entrySet().contains(null)); + assertEquals(false, map.keySet().contains(null)); + assertEquals(false, map.values().contains(null)); + try { + map.put(null, null); + fail(); + } catch (NullPointerException ex) {} + try { + map.put(new Object(), null); + fail(); + } catch (NullPointerException ex) {} + try { + map.put(null, new Object()); + fail(); + } catch (NullPointerException ex) {} + } + //----------------------------------------------------------------------- public void testPurge() { ReferenceMap map = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.WEAK); Object[] hard = new Object[10]; @@ -71,13 +95,13 @@ public class TestReferenceMap extends AbstractTestIterableMap { hard[i] = new Object(); map.put(hard[i], new Object()); } - System.gc(); + gc(); assertTrue("map should be empty after purge of weak values", map.isEmpty()); for (int i = 0; i < hard.length; i++) { map.put(new Object(), hard[i]); } - System.gc(); + gc(); assertTrue("map should be empty after purge of weak keys", map.isEmpty()); for (int i = 0; i < hard.length; i++) { @@ -85,7 +109,7 @@ public class TestReferenceMap extends AbstractTestIterableMap { map.put(hard[i], new Object()); } - System.gc(); + gc(); assertTrue("map should be empty after purge of weak keys and values", map.isEmpty()); } @@ -96,7 +120,7 @@ public class TestReferenceMap extends AbstractTestIterableMap { map.put(new Integer(i), new Integer(i)); } - System.gc(); + gc(); for (int i = 0; i < 10; i++) { Integer I = new Integer(i); assertTrue("map.containsKey should return false for GC'd element", !map.containsKey(I)); @@ -114,7 +138,7 @@ public class TestReferenceMap extends AbstractTestIterableMap { map.put(hard[i], hard[i]); } - System.gc(); + gc(); Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry)iterator.next(); @@ -135,7 +159,7 @@ public class TestReferenceMap extends AbstractTestIterableMap { map.put(hard[i], hard[i]); } - System.gc(); + gc(); MapIterator iterator = map.mapIterator(); while (iterator.hasNext()) { Object key1 = iterator.next(); @@ -160,7 +184,7 @@ public class TestReferenceMap extends AbstractTestIterableMap { MapIterator iterator = map.mapIterator(); while (iterator.hasNext()) { Object key1 = iterator.next(); - System.gc(); + gc(); Integer key = (Integer) iterator.getKey(); Integer value = (Integer) iterator.getValue(); assertTrue("iterator keys should match", key == key1); @@ -170,23 +194,6 @@ public class TestReferenceMap extends AbstractTestIterableMap { } -*/ - -/* - // Uncomment to create test files in /data/test - public void testCreateTestFiles() throws Exception { - ReferenceMap m = (ReferenceMap)makeEmptyMap(); - writeExternalFormToDisk(m, getCanonicalEmptyCollectionName(m)); - m = (ReferenceMap)makeFullMap(); - writeExternalFormToDisk(m, getCanonicalFullCollectionName(m)); - } -*/ - - - public String getCompatibilityVersion() { - return "3.1"; - } - /** Tests whether purge values setting works */ public void testPurgeValues() throws Exception { // many thanks to Juozas Baliuka for suggesting this method @@ -227,4 +234,15 @@ public class TestReferenceMap extends AbstractTestIterableMap { } } } + + private static void gc() { + try { + // trigger GC + byte[][] tooLarge = new byte[1000000000][1000000000]; + fail("you have too much RAM"); + } catch (OutOfMemoryError ex) { + System.gc(); // ignore + } + } + }