commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections AbstractTestBidiMap.java
Date Sun, 02 Nov 2003 19:48:40 GMT
scolebourne    2003/11/02 11:48:40

  Modified:    collections/src/java/org/apache/commons/collections
                        AbstractDualBidiMap.java
               collections/src/test/org/apache/commons/collections
                        AbstractTestBidiMap.java
  Log:
  Integrate new MapIterator tests into DualBidiMap tests
  
  Revision  Changes    Path
  1.8       +23 -5     jakarta-commons/collections/src/java/org/apache/commons/collections/AbstractDualBidiMap.java
  
  Index: AbstractDualBidiMap.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/AbstractDualBidiMap.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- AbstractDualBidiMap.java	2 Nov 2003 15:27:53 -0000	1.7
  +++ AbstractDualBidiMap.java	2 Nov 2003 19:48:39 -0000	1.8
  @@ -67,7 +67,7 @@
   import org.apache.commons.collections.decorators.AbstractMapEntryDecorator;
   import org.apache.commons.collections.iterators.MapIterator;
   import org.apache.commons.collections.iterators.ResetableMapIterator;
  -import org.apache.commons.collections.pairs.AbstractMapEntry;
  +import org.apache.commons.collections.pairs.TiedMapEntry;
   
   /**
    * Abstract <code>BidiMap</code> implemented using two maps.
  @@ -225,6 +225,10 @@
        * Obtains a <code>MapIterator</code> over the map.
        * The iterator implements <code>ResetableMapIterator</code>.
        * This implementation relies on the entrySet iterator.
  +     * <p>
  +     * The setValue() methods only allow a new value to be set.
  +     * If the value being set is already in the map, an IllegalArgumentException
  +     * is thrown (as setValue cannot change the size of the map).
        * 
        * @return a map iterator
        */
  @@ -268,6 +272,17 @@
           return values;
       }
   
  +    /**
  +     * Gets an entrySet view of the map.
  +     * Changes made on the set are reflected in the map.
  +     * The set supports remove and clear but not add.
  +     * <p>
  +     * The Map Entry setValue() method only allow a new value to be set.
  +     * If the value being set is already in the map, an IllegalArgumentException
  +     * is thrown (as setValue cannot change the size of the map).
  +     * 
  +     * @return the entrySet view
  +     */
       public Set entrySet() {
           if (entrySet == null) {
               entrySet = new EntrySet(this);
  @@ -601,9 +616,12 @@
           }
           
           public Map.Entry asMapEntry() {
  -            return new AbstractMapEntry(getKey(), getValue()) {
  +            return new TiedMapEntry(map, getKey()) {
                   public Object setValue(Object value) {
  -                    BidiMapIterator.this.setValue(value);
  +                    if (map.maps[1].containsKey(value) &&
  +                        map.maps[1].get(value) != last.getKey()) {
  +                        throw new IllegalArgumentException("Cannot use setValue() when
the object being set is already in the map");
  +                    }
                       return super.setValue(value);
                   }
               };
  
  
  
  1.7       +36 -93    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- AbstractTestBidiMap.java	2 Nov 2003 18:29:33 -0000	1.6
  +++ AbstractTestBidiMap.java	2 Nov 2003 19:48:39 -0000	1.7
  @@ -61,9 +61,9 @@
   import java.util.HashMap;
   import java.util.Iterator;
   import java.util.Map;
  -import java.util.NoSuchElementException;
   import java.util.Set;
   
  +import org.apache.commons.collections.iterators.AbstractTestMapIterator;
   import org.apache.commons.collections.iterators.MapIterator;
   
   /**
  @@ -430,7 +430,7 @@
           return new TestInverseBidiMap(this);
       }
   
  -    class TestInverseBidiMap extends AbstractTestBidiMap {
  +    public class TestInverseBidiMap extends AbstractTestBidiMap {
           final AbstractTestBidiMap main;
           
           public TestInverseBidiMap(AbstractTestBidiMap main) {
  @@ -475,76 +475,48 @@
       }
       
       //-----------------------------------------------------------------------
  -    public void testBidiMapIteratorEmpty() {
  -        resetEmpty();
  -        BidiMap bidi = (BidiMap) map;
  -        MapIterator it = bidi.mapIterator();
  -        assertEquals(false, it.hasNext());
  -        try {
  -            it.next();
  -            fail();
  -        } catch (NoSuchElementException ex) {}
  -        try {
  -            it.getKey();
  -            fail();
  -        } catch (IllegalStateException ex) {
  +    public BulkTest bulkTestBidiMapIterator() {
  +        return new TestBidiMapIterator();
  +    }
  +    
  +    public class TestBidiMapIterator extends AbstractTestMapIterator {
  +        public TestBidiMapIterator() {
  +            super("TestBidiMapIterator");
           }
  -        try {
  -            it.getValue();
  -            fail();
  -        } catch (IllegalStateException ex) {
  +        
  +        protected Object addSetValue() {
  +            return AbstractTestBidiMap.this.getNewSampleValues()[0];
           }
  -        try {
  -            it.remove();
  -            fail();
  -        } catch (IllegalStateException ex) {
  +        
  +        protected boolean supportsRemove() {
  +            return AbstractTestBidiMap.this.isRemoveSupported();
           }
  -        try {
  -            it.setValue(null);
  -            fail();
  -        } catch (IllegalStateException ex) {
  +
  +        protected boolean supportsSetValue() {
  +            return AbstractTestBidiMap.this.isSetValueSupported();
           }
  -        try {
  -            it.asMapEntry();
  -            fail();
  -        } catch (IllegalStateException ex) {
  +
  +        protected MapIterator makeEmptyMapIterator() {
  +            resetEmpty();
  +            return ((BidiMap) AbstractTestBidiMap.this.map).mapIterator();
           }
  -        verify();
  -    }
   
  -    //-----------------------------------------------------------------------
  -    public void testBidiMapIteratorFull() {
  -        resetFull();
  -        BidiMap bidi = (BidiMap) map;
  -        MapIterator it = bidi.mapIterator();
  +        protected MapIterator makeFullMapIterator() {
  +            resetFull();
  +            return ((BidiMap) AbstractTestBidiMap.this.map).mapIterator();
  +        }
           
  -        assertEquals(true, it.hasNext());
  -        Map.Entry lastEntry = null;
  -        Object lastKey = null;
  -        Object lastValue = null;
  -        while (it.hasNext()) {
  -            Object key = it.next();
  -            assertSame(key, it.getKey());
  +        protected Map getMap() {
  +            // assumes makeFullMapIterator() called first
  +            return AbstractTestBidiMap.this.map;
  +        }
           
  -            Object value = it.getValue();
  -            assertSame(bidi.get(key), value);
  -            
  -            Map.Entry entry = it.asMapEntry();
  -            assertTrue(entry != lastEntry);
  -            if (lastKey != null && lastValue != null) {
  -                assertSame(lastKey, lastEntry.getKey());
  -                assertSame(lastValue, lastEntry.getValue());
  -            }
  -            assertSame(key, entry.getKey());
  -            assertSame(value, entry.getValue());
  -            
  -            lastEntry = entry;
  -            lastKey = key;
  -            lastValue = value;
  +        protected void verify() {
  +            super.verify();
  +            AbstractTestBidiMap.this.verifyInverse();
           }
  -        verify();
       }
  -
  +    
       //-----------------------------------------------------------------------
       public void testBidiMapIteratorRemove() {
           resetFull();
  @@ -645,35 +617,6 @@
           if (isRemoveSupported()) {
               it.remove();
           }
  -    }
  -
  -    //-----------------------------------------------------------------------
  -    public void testBidiMapIteratorSetRemoveSet() {
  -        if (isSetValueSupported() == false || isRemoveSupported() == false) {
  -            return;
  -        }
  -        Object newValue1 = getOtherValues()[0];
  -        
  -        resetFull();
  -        BidiMap bidi = (BidiMap) map;
  -        MapIterator it = bidi.mapIterator();
  -        assertEquals(true, it.hasNext());
  -        Object key = it.next();
  -        
  -        it.setValue(newValue1);
  -        confirmed.put(key, newValue1);
  -        verify();
  -        
  -        it.remove();
  -        confirmed.remove(key);
  -        verify();
  -        
  -        try {
  -            it.setValue(newValue1);
  -            fail();
  -        } catch (IllegalStateException ex) {
  -        }
  -        verify();
       }
   
   }
  
  
  

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


Mime
View raw message