You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sc...@apache.org on 2003/11/08 19:46:57 UTC

cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections/iterators AbstractTestIterator.java AbstractTestMapIterator.java TestUnmodifiableMapIterator.java

scolebourne    2003/11/08 10:46:57

  Modified:    collections/src/test/org/apache/commons/collections/decorators
                        TestOrderedMap.java
               collections/src/test/org/apache/commons/collections
                        TestFlat3Map.java AbstractTestBidiMap.java
               collections/src/test/org/apache/commons/collections/iterators
                        AbstractTestIterator.java
                        AbstractTestMapIterator.java
                        TestUnmodifiableMapIterator.java
  Log:
  Update tests
  
  Revision  Changes    Path
  1.4       +12 -2     jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestOrderedMap.java
  
  Index: TestOrderedMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/decorators/TestOrderedMap.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestOrderedMap.java	4 Nov 2003 23:36:23 -0000	1.3
  +++ TestOrderedMap.java	8 Nov 2003 18:46:57 -0000	1.4
  @@ -136,6 +136,16 @@
               // assumes makeFullMapIterator() called first
               return TestOrderedMap.this.map;
           }
  +        
  +        protected Map getConfirmedMap() {
  +            // assumes makeFullMapIterator() called first
  +            return TestOrderedMap.this.confirmed;
  +        }
  +        
  +        protected void verify() {
  +            super.verify();
  +            TestOrderedMap.this.verify();
  +        }
       }
       
       //-----------------------------------------------------------------------
  
  
  
  1.2       +10 -84    jakarta-commons/collections/src/test/org/apache/commons/collections/TestFlat3Map.java
  
  Index: TestFlat3Map.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestFlat3Map.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestFlat3Map.java	2 Nov 2003 23:41:46 -0000	1.1
  +++ TestFlat3Map.java	8 Nov 2003 18:46:57 -0000	1.2
  @@ -100,8 +100,8 @@
               super("TestFlatMapIterator");
           }
           
  -        protected Object addSetValue() {
  -            return TestFlat3Map.this.getNewSampleValues()[0];
  +        protected Object[] addSetValues() {
  +            return TestFlat3Map.this.getNewSampleValues();
           }
           
           protected boolean supportsRemove() {
  @@ -127,88 +127,14 @@
               return TestFlat3Map.this.map;
           }
           
  -        protected void verify() {
  -            super.verify();
  -            // cannot cross verify as we don't know what superclass did
  -        }
  -    }
  -    
  -    //-----------------------------------------------------------------------
  -    public void testMapIteratorRemove() {
  -        resetFull();
  -        Flat3Map testMap = (Flat3Map) map;
  -        MapIterator it = testMap.mapIterator();
  -        assertEquals(true, it.hasNext());
  -        Object key = it.next();
  -        
  -        if (isRemoveSupported() == false) {
  -            try {
  -                it.remove();
  -                fail();
  -            } catch (UnsupportedOperationException ex) {
  -            }
  -            return;
  -        }
  -        
  -        it.remove();
  -        confirmed.remove(key);
  -        assertEquals(false, testMap.containsKey(key));
  -        verify();
  -        
  -        try {
  -            it.remove();  // second remove fails
  -        } catch (IllegalStateException ex) {
  +        protected Map getConfirmedMap() {
  +            // assumes makeFullMapIterator() called first
  +            return TestFlat3Map.this.confirmed;
           }
  -        verify();
  -    }
  -
  -    //-----------------------------------------------------------------------
  -    public void testMapIteratorSet() {
  -        Object newValue1 = getOtherValues()[0];
  -        Object newValue2 = getOtherValues()[1];
  -        
  -        resetFull();
  -        Flat3Map testMap = (Flat3Map) map;
  -        MapIterator it = testMap.mapIterator();
  -        assertEquals(true, it.hasNext());
  -        Object key1 = it.next();
           
  -        if (isSetValueSupported() == false) {
  -            try {
  -                it.setValue(newValue1);
  -                fail();
  -            } catch (UnsupportedOperationException ex) {
  -            }
  -            return;
  +        protected void verify() {
  +            super.verify();
  +            TestFlat3Map.this.verify();
           }
  -        
  -        it.setValue(newValue1);
  -        confirmed.put(key1, newValue1);
  -        assertSame(key1, it.getKey());
  -        assertSame(newValue1, it.getValue());
  -        assertEquals(true, testMap.containsKey(key1));
  -        assertEquals(true, testMap.containsValue(newValue1));
  -        assertEquals(newValue1, testMap.get(key1));
  -        verify();
  -        
  -        it.setValue(newValue1);  // same value - should be OK
  -        confirmed.put(key1, newValue1);
  -        assertSame(key1, it.getKey());
  -        assertSame(newValue1, it.getValue());
  -        assertEquals(true, testMap.containsKey(key1));
  -        assertEquals(true, testMap.containsValue(newValue1));
  -        assertEquals(newValue1, testMap.get(key1));
  -        verify();
  -        
  -        Object key2 = it.next();
  -        it.setValue(newValue2);
  -        confirmed.put(key2, newValue2);
  -        assertSame(key2, it.getKey());
  -        assertSame(newValue2, it.getValue());
  -        assertEquals(true, testMap.containsKey(key2));
  -        assertEquals(true, testMap.containsValue(newValue2));
  -        assertEquals(newValue2, testMap.get(key2));
  -        verify();
       }
  -
   }
  
  
  
  1.8       +16 -38    jakarta-commons/collections/src/test/org/apache/commons/collections/AbstractTestBidiMap.java
  
  Index: AbstractTestBidiMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/AbstractTestBidiMap.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- AbstractTestBidiMap.java	2 Nov 2003 19:48:39 -0000	1.7
  +++ AbstractTestBidiMap.java	8 Nov 2003 18:46:57 -0000	1.8
  @@ -242,6 +242,8 @@
   
       //-----------------------------------------------------------------------
       public void testBidiModifyEntrySet() {
  +        if (isSetValueSupported() == false) return;
  +        
           modifyEntrySet(makeFullBidiMap());
           modifyEntrySet(makeFullBidiMap().inverseBidiMap());
       }
  @@ -288,8 +290,8 @@
           remove(makeFullBidiMap(), entries[0][0]);
           remove(makeFullBidiMap().inverseBidiMap(), entries[0][1]);
   
  -        removeKey(makeFullBidiMap(), entries[0][1]);
  -        removeKey(makeFullBidiMap().inverseBidiMap(), entries[0][0]);
  +        removeValue(makeFullBidiMap(), entries[0][1]);
  +        removeValue(makeFullBidiMap().inverseBidiMap(), entries[0][0]);
       }
   
       private final void remove(BidiMap map, Object key) {
  @@ -298,8 +300,8 @@
           assertNull("Value was not removed.", map.getKey(value));
       }
   
  -    private final void removeKey(BidiMap map, Object value) {
  -        final Object key = map.removeKey(value);
  +    private final void removeValue(BidiMap map, Object value) {
  +        final Object key = map.removeValue(value);
           assertTrue("Key was not removed.", !map.containsKey(key));
           assertNull("Value was not removed.", map.getKey(value));
       }
  @@ -484,8 +486,8 @@
               super("TestBidiMapIterator");
           }
           
  -        protected Object addSetValue() {
  -            return AbstractTestBidiMap.this.getNewSampleValues()[0];
  +        protected Object[] addSetValues() {
  +            return AbstractTestBidiMap.this.getNewSampleValues();
           }
           
           protected boolean supportsRemove() {
  @@ -511,41 +513,17 @@
               return AbstractTestBidiMap.this.map;
           }
           
  +        protected Map getConfirmedMap() {
  +            // assumes makeFullMapIterator() called first
  +            return AbstractTestBidiMap.this.confirmed;
  +        }
  +        
           protected void verify() {
               super.verify();
  -            AbstractTestBidiMap.this.verifyInverse();
  +            AbstractTestBidiMap.this.verify();
           }
       }
       
  -    //-----------------------------------------------------------------------
  -    public void testBidiMapIteratorRemove() {
  -        resetFull();
  -        BidiMap bidi = (BidiMap) map;
  -        MapIterator it = bidi.mapIterator();
  -        assertEquals(true, it.hasNext());
  -        Object key = it.next();
  -        
  -        if (isRemoveSupported() == false) {
  -            try {
  -                it.remove();
  -                fail();
  -            } catch (UnsupportedOperationException ex) {
  -            }
  -            return;
  -        }
  -        
  -        it.remove();
  -        confirmed.remove(key);
  -        assertEquals(false, bidi.containsKey(key));
  -        verify();
  -        
  -        try {
  -            it.remove();  // second remove fails
  -        } catch (IllegalStateException ex) {
  -        }
  -        verify();
  -    }
  -
       //-----------------------------------------------------------------------
       public void testBidiMapIteratorSet() {
           Object newValue1 = getOtherValues()[0];
  
  
  
  1.4       +2 -4      jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/AbstractTestIterator.java
  
  Index: AbstractTestIterator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/AbstractTestIterator.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- AbstractTestIterator.java	2 Nov 2003 19:47:10 -0000	1.3
  +++ AbstractTestIterator.java	8 Nov 2003 18:46:57 -0000	1.4
  @@ -229,14 +229,12 @@
           // remove after next should be fine
           it.next();
           it.remove();
  -        verify();
           
           // should throw IllegalStateException for second remove()
           try {
               it.remove();
               fail();
           } catch (IllegalStateException ex) {}
  -        verify();
       }
       
   }
  
  
  
  1.3       +42 -93    jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/AbstractTestMapIterator.java
  
  Index: AbstractTestMapIterator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/AbstractTestMapIterator.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractTestMapIterator.java	2 Nov 2003 19:47:10 -0000	1.2
  +++ AbstractTestMapIterator.java	8 Nov 2003 18:46:57 -0000	1.3
  @@ -62,7 +62,6 @@
   import java.util.Map;
   import java.util.NoSuchElementException;
   import java.util.Set;
  -import java.util.Map.Entry;
   
   /**
    * Abstract class for testing the MapIterator interface.
  @@ -112,14 +111,13 @@
       protected abstract Map getMap();
       
       /**
  -     * Override if the map returned by getMap() is NOT the one tied to the iterator.
  +     * Implement this method to return the confirmed map which contains the same
  +     * data as the iterator.
        * 
  -     * @return true if the getMap() map is the one tied to the iterator
  +     * @return a full map which can be updated
        */
  -    protected boolean supportsTiedMap() {
  -        return true;
  -    }
  -
  +    protected abstract Map getConfirmedMap();
  +    
       /**
        * Implements the abstract superclass method to return the list iterator.
        * 
  @@ -149,11 +147,11 @@
       }
   
       /**
  -     * The value to be used in the add and set tests.
  -     * Default is null.
  +     * The values to be used in the add and set tests.
  +     * Default is two strings.
        */
  -    protected Object addSetValue() {
  -        return null;
  +    protected Object[] addSetValues() {
  +        return new Object[] {"A", "B"};
       }
   
       //-----------------------------------------------------------------------
  @@ -187,23 +185,17 @@
               fail();
           } catch (IllegalStateException ex) {}
           
  -        // asMapEntry() should throw an IllegalStateException
  -        try {
  -            it.asMapEntry();
  -            fail();
  -        } catch (IllegalStateException ex) {}
  -        
           if (supportsSetValue() == false) {
               // setValue() should throw an UnsupportedOperationException/IllegalStateException
               try {
  -                it.setValue(addSetValue());
  +                it.setValue(addSetValues()[0]);
                   fail();
               } catch (UnsupportedOperationException ex) {
               } catch (IllegalStateException ex) {}
           } else {
               // setValue() should throw an IllegalStateException
               try {
  -                it.setValue(addSetValue());
  +                it.setValue(addSetValues()[0]);
                   fail();
               } catch (IllegalStateException ex) {}
           }
  @@ -223,9 +215,6 @@
           assertEquals(true, it.hasNext());
           
           assertEquals(true, it.hasNext());
  -        Map.Entry lastEntry = null;
  -        Object lastKey = null;
  -        Object lastValue = null;
           Set set = new HashSet();
           while (it.hasNext()) {
               // getKey
  @@ -239,21 +228,7 @@
               assertSame("Value must be mapped to key", map.get(key), value);
               assertTrue("Value must be in map",  map.containsValue(value));
               assertSame("Value must be mapped to key", map.get(key), value);
  -            
  -            // asMapEntry
  -            Map.Entry entry = it.asMapEntry();
  -            assertSame("MapEntry key must match", key, entry.getKey());
  -            assertSame("MapEntry value must match", value, entry.getValue());
  -            
  -            assertTrue("MapEntry must be independent", entry != lastEntry);
  -            if (lastKey != null && lastValue != null) {
  -                assertSame("MapEntry must not change after next()", lastKey, lastEntry.getKey());
  -                assertSame("MapEntry must not change after next()", lastValue, lastEntry.getValue());
  -            }
  -            
  -            lastEntry = entry;
  -            lastKey = key;
  -            lastValue = value;
  +
               verify();
           }
       }
  @@ -264,13 +239,14 @@
               return;
           }
   
  -        Object newValue = addSetValue();
  +        Object newValue = addSetValues()[0];
  +        Object newValue2 = addSetValues()[1];
           MapIterator it = makeFullMapIterator();
           Map map = getMap();
  +        Map confirmed = getConfirmedMap();
           assertEquals(true, it.hasNext());
           Object key = it.next();
           Object value = it.getValue();
  -        Entry entry = it.asMapEntry();
           
           if (supportsSetValue() == false) {
               try {
  @@ -281,97 +257,70 @@
           }
           
           Object old = it.setValue(newValue);
  +        confirmed.put(key, newValue);
           assertSame("Key must not change after setValue", key, it.getKey());
  -        assertSame("Key must not change after setValue", key, entry.getKey());
           assertSame("Value must be changed after setValue", newValue, it.getValue());
  -        assertSame("Value must be changed after setValue", newValue, entry.getValue());
           assertSame("setValue must return old value", value, old);
  -        if (supportsTiedMap()) {
  -            assertTrue("Key must be in map",  map.containsKey(key));
  -            assertTrue("Old value must not be in map",  map.containsValue(value) == false);
  -            assertTrue("Value must be in map",  map.containsValue(newValue));
  -            assertSame("Value must be mapped to key", map.get(key), newValue);
  -        }
           verify();
           
           it.setValue(newValue);  // same value - should be OK
  +        confirmed.put(key, newValue);
           assertSame("Key must not change after setValue", key, it.getKey());
  -        assertSame("Key must not change after setValue", key, entry.getKey());
           assertSame("Value must be changed after setValue", newValue, it.getValue());
  -        assertSame("Value must be changed after setValue", newValue, entry.getValue());
  -        if (supportsTiedMap()) {
  -            assertTrue("Key must be in map",  map.containsKey(key));
  -            assertTrue("Old value must not be in map",  map.containsValue(value) == false);
  -            assertTrue("Value must be in map",  map.containsValue(newValue));
  -            assertSame("Value must be mapped to key", map.get(key), newValue);
  -        }
  +        verify();
  +        
  +        it.setValue(newValue2);  // new value
  +        confirmed.put(key, newValue2);
  +        assertSame("Key must not change after setValue", key, it.getKey());
  +        assertSame("Value must be changed after setValue", newValue2, it.getValue());
           verify();
       }
   
       //-----------------------------------------------------------------------
  -    public void testMapIteratorMapEntrySet() {
  -        if (supportsFullIterator() == false) {
  -            return;
  -        }
  -
  -        Object newValue = addSetValue();
  +    public void testRemove() { // override
           MapIterator it = makeFullMapIterator();
           Map map = getMap();
  +        Map confirmed = getConfirmedMap();
           assertEquals(true, it.hasNext());
           Object key = it.next();
  -        Object value = it.getValue();
  -        Entry entry = it.asMapEntry();
           
  -        if (supportsSetValue() == false) {
  +        if (supportsRemove() == false) {
               try {
  -                entry.setValue(newValue);
  +                it.remove();
                   fail();
  -            } catch (UnsupportedOperationException ex) {}
  +            } catch (UnsupportedOperationException ex) {
  +            }
               return;
           }
           
  -        Object old = entry.setValue(newValue);
  -        assertSame("Key must not change after setValue", key, it.getKey());
  -        assertSame("Key must not change after setValue", key, entry.getKey());
  -        assertSame("Value must be changed after setValue", newValue, it.getValue());
  -        assertSame("Value must be changed after setValue", newValue, entry.getValue());
  -        assertSame("setValue must return old value", value, old);
  -        if (supportsTiedMap()) {
  -            assertTrue("Key must be in map",  map.containsKey(key));
  -            assertTrue("Old value must not be in map",  map.containsValue(value) == false);
  -            assertTrue("Value must be in map",  map.containsValue(newValue));
  -            assertSame("Value must be mapped to key", map.get(key), newValue);
  -        }
  +        it.remove();
  +        confirmed.remove(key);
  +        assertEquals(false, map.containsKey(key));
           verify();
           
  -        entry.setValue(newValue);  // same value - should be OK
  -        assertSame("Key must not change after setValue", key, it.getKey());
  -        assertSame("Key must not change after setValue", key, entry.getKey());
  -        assertSame("Value must be changed after setValue", newValue, it.getValue());
  -        assertSame("Value must be changed after setValue", newValue, entry.getValue());
  -        if (supportsTiedMap()) {
  -            assertTrue("Key must be in map",  map.containsKey(key));
  -            assertTrue("Old value must not be in map",  map.containsValue(value) == false);
  -            assertTrue("Value must be in map",  map.containsValue(newValue));
  -            assertSame("Value must be mapped to key", map.get(key), newValue);
  +        try {
  +            it.remove();  // second remove fails
  +        } catch (IllegalStateException ex) {
           }
           verify();
       }
   
       //-----------------------------------------------------------------------
  -    public void testBidiMapIteratorSetRemoveSet() {
  +    public void testMapIteratorSetRemoveSet() {
           if (supportsSetValue() == false || supportsRemove() == false) {
               return;
           }
  -        Object newValue = addSetValue();
  -        
  +        Object newValue = addSetValues()[0];
           MapIterator it = makeFullMapIterator();
           Map map = getMap();
  +        Map confirmed = getConfirmedMap();
  +        
           assertEquals(true, it.hasNext());
           Object key = it.next();
           
           it.setValue(newValue);
           it.remove();
  +        confirmed.remove(key);
           verify();
           
           try {
  
  
  
  1.2       +10 -2     jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestUnmodifiableMapIterator.java
  
  Index: TestUnmodifiableMapIterator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/iterators/TestUnmodifiableMapIterator.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestUnmodifiableMapIterator.java	2 Nov 2003 18:29:59 -0000	1.1
  +++ TestUnmodifiableMapIterator.java	8 Nov 2003 18:46:57 -0000	1.2
  @@ -57,6 +57,7 @@
    */
   package org.apache.commons.collections.iterators;
   
  +import java.util.HashMap;
   import java.util.Map;
   
   import junit.framework.Test;
  @@ -99,6 +100,13 @@
           return testMap;
       }
   
  +    protected Map getConfirmedMap() {
  +        Map testMap = new HashMap();
  +        testMap.put("A", "a");
  +        testMap.put("B", "b");
  +        testMap.put("C", "c");
  +        return testMap;
  +    }
   
       public boolean supportsRemove() {
           return false;
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org