From 559db22fc253f3ec4690531fddbed457773d053d Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sun, 2 Nov 2003 19:48:39 +0000 Subject: [PATCH] Integrate new MapIterator tests into DualBidiMap tests git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131322 13f79535-47bb-0310-9956-ffa450edef68 --- .../collections/AbstractDualBidiMap.java | 28 +++- .../collections/AbstractTestBidiMap.java | 143 ++++++------------ 2 files changed, 66 insertions(+), 105 deletions(-) diff --git a/src/java/org/apache/commons/collections/AbstractDualBidiMap.java b/src/java/org/apache/commons/collections/AbstractDualBidiMap.java index 767d9679a..2cc22cd01 100644 --- a/src/java/org/apache/commons/collections/AbstractDualBidiMap.java +++ b/src/java/org/apache/commons/collections/AbstractDualBidiMap.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/AbstractDualBidiMap.java,v 1.7 2003/11/02 15:27:53 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/AbstractDualBidiMap.java,v 1.8 2003/11/02 19:48:39 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -67,7 +67,7 @@ import org.apache.commons.collections.decorators.AbstractIteratorDecorator; 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 BidiMap implemented using two maps. @@ -76,7 +76,7 @@ import org.apache.commons.collections.pairs.AbstractMapEntry; * createMap method. * * @since Commons Collections 3.0 - * @version $Id: AbstractDualBidiMap.java,v 1.7 2003/11/02 15:27:53 scolebourne Exp $ + * @version $Id: AbstractDualBidiMap.java,v 1.8 2003/11/02 19:48:39 scolebourne Exp $ * * @author Matthew Hawthorne * @author Stephen Colebourne @@ -225,6 +225,10 @@ public abstract class AbstractDualBidiMap implements BidiMap { * Obtains a MapIterator over the map. * The iterator implements ResetableMapIterator. * This implementation relies on the entrySet iterator. + *

+ * 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 @@ public abstract class AbstractDualBidiMap implements BidiMap { 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. + *

+ * 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 abstract class AbstractDualBidiMap implements BidiMap { } 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); } }; diff --git a/src/test/org/apache/commons/collections/AbstractTestBidiMap.java b/src/test/org/apache/commons/collections/AbstractTestBidiMap.java index 4b51e345a..d6bfd46db 100644 --- a/src/test/org/apache/commons/collections/AbstractTestBidiMap.java +++ b/src/test/org/apache/commons/collections/AbstractTestBidiMap.java @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/AbstractTestBidiMap.java,v 1.6 2003/11/02 18:29:33 scolebourne Exp $ + * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/AbstractTestBidiMap.java,v 1.7 2003/11/02 19:48:39 scolebourne Exp $ * ==================================================================== * * The Apache Software License, Version 1.1 @@ -61,15 +61,15 @@ import java.util.Collection; 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; /** * Abstract test class for {@link BidiMap} methods and contracts. * - * @version $Revision: 1.6 $ $Date: 2003/11/02 18:29:33 $ + * @version $Revision: 1.7 $ $Date: 2003/11/02 19:48:39 $ * * @author Matthew Hawthorne * @author Stephen Colebourne @@ -430,7 +430,7 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap { 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 abstract class AbstractTestBidiMap extends AbstractTestMap { } //----------------------------------------------------------------------- - 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) { - } - try { - it.getValue(); - fail(); - } catch (IllegalStateException ex) { - } - try { - it.remove(); - fail(); - } catch (IllegalStateException ex) { - } - try { - it.setValue(null); - fail(); - } catch (IllegalStateException ex) { - } - try { - it.asMapEntry(); - fail(); - } catch (IllegalStateException ex) { - } - verify(); + public BulkTest bulkTestBidiMapIterator() { + return new TestBidiMapIterator(); } - - //----------------------------------------------------------------------- - public void testBidiMapIteratorFull() { - resetFull(); - BidiMap bidi = (BidiMap) map; - MapIterator it = bidi.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()); - - 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; + + public class TestBidiMapIterator extends AbstractTestMapIterator { + public TestBidiMapIterator() { + super("TestBidiMapIterator"); + } + + protected Object addSetValue() { + return AbstractTestBidiMap.this.getNewSampleValues()[0]; + } + + protected boolean supportsRemove() { + return AbstractTestBidiMap.this.isRemoveSupported(); } - verify(); - } + protected boolean supportsSetValue() { + return AbstractTestBidiMap.this.isSetValueSupported(); + } + + protected MapIterator makeEmptyMapIterator() { + resetEmpty(); + return ((BidiMap) AbstractTestBidiMap.this.map).mapIterator(); + } + + protected MapIterator makeFullMapIterator() { + resetFull(); + return ((BidiMap) AbstractTestBidiMap.this.map).mapIterator(); + } + + protected Map getMap() { + // assumes makeFullMapIterator() called first + return AbstractTestBidiMap.this.map; + } + + protected void verify() { + super.verify(); + AbstractTestBidiMap.this.verifyInverse(); + } + } + //----------------------------------------------------------------------- public void testBidiMapIteratorRemove() { resetFull(); @@ -647,33 +619,4 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap { } } - //----------------------------------------------------------------------- - 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(); - } - }