scolebourne    2004/04/30 15:53:16

  Modified:    collections/src/test/org/apache/commons/collections/map
                        TestReferenceIdentityMap.java TestReferenceMap.java
               collections/src/test/org/apache/commons/collections
                        TestReferenceMap.java
  Log:
  Improve reference map testing
  
  Revision  Changes    Path
  1.2       +48 -28    
jakarta-commons/collections/src/test/org/apache/commons/collections/map/TestReferenceIdentityMap.java
  
  Index: TestReferenceIdentityMap.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/map/TestReferenceIdentityMap.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestReferenceIdentityMap.java     27 Apr 2004 21:37:32 -0000      1.1
  +++ TestReferenceIdentityMap.java     30 Apr 2004 22:53:16 -0000      1.2
  @@ -23,6 +23,7 @@
   
   import org.apache.commons.collections.BulkTest;
   import org.apache.commons.collections.IterableMap;
  +import org.apache.commons.collections.MapIterator;
   
   /**
    * Tests for ReferenceIdentityMap. 
  @@ -71,6 +72,10 @@
           return false;
       }
   
  +    public String getCompatibilityVersion() {
  +        return "3.1";
  +    }
  +
       //-----------------------------------------------------------------------
       public void testBasics() {
           IterableMap map = new ReferenceIdentityMap(ReferenceIdentityMap.HARD, 
ReferenceIdentityMap.HARD);
  @@ -123,11 +128,30 @@
       
       
       //-----------------------------------------------------------------------
  -   // 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 @@
               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 @@
               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 @@
               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 @@
               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 @@
               map.put(hard[i], hard[i]);
           }
   
  -        System.gc();
  +        gc();
           MapIterator iterator = map.mapIterator();
           while (iterator.hasNext()) {
               Object key1 = iterator.next();
  @@ -224,7 +248,7 @@
           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 @@
           }
   
       }
  -*/
  -/*
  -    // 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 @@
               }
           }
       }
  +
  +    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
  +        }
  +    }
  +
   }
  
  
  
  1.7       +48 -30    
jakarta-commons/collections/src/test/org/apache/commons/collections/map/TestReferenceMap.java
  
  Index: TestReferenceMap.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/map/TestReferenceMap.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- TestReferenceMap.java     27 Apr 2004 21:35:23 -0000      1.6
  +++ TestReferenceMap.java     30 Apr 2004 22:53:16 -0000      1.7
  @@ -16,11 +16,13 @@
   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. 
  @@ -57,13 +59,35 @@
           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 @@
               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 @@
               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 @@
               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 @@
               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 @@
               map.put(hard[i], hard[i]);
           }
   
  -        System.gc();
  +        gc();
           MapIterator iterator = map.mapIterator();
           while (iterator.hasNext()) {
               Object key1 = iterator.next();
  @@ -160,7 +184,7 @@
           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 @@
   
       }
   
  -*/
  -
  -/*
  -    // 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 @@
               }
           }
       }
  +
  +    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
  +        }
  +    }
  +
   }
  
  
  
  1.18      +44 -27    
jakarta-commons/collections/src/test/org/apache/commons/collections/TestReferenceMap.java
  
  Index: TestReferenceMap.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestReferenceMap.java,v
  retrieving revision 1.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- TestReferenceMap.java     18 Feb 2004 01:20:35 -0000      1.17
  +++ TestReferenceMap.java     30 Apr 2004 22:53:16 -0000      1.18
  @@ -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;
  @@ -57,13 +58,35 @@
           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 @@
               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 @@
               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 @@
               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 @@
               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 @@
           }
   
       }
  -*/
  -
  -
  -/*
  -    // 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
  @@ -181,6 +188,16 @@
                   byte[] b =  new byte[bytz];
                   bytz = bytz * 2;
               }
  +        }
  +    }
  +    
  +    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
           }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to