Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 913 invoked from network); 2 Nov 2003 19:48:44 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 2 Nov 2003 19:48:44 -0000 Received: (qmail 22038 invoked by uid 500); 2 Nov 2003 19:48:32 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 21971 invoked by uid 500); 2 Nov 2003 19:48:31 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 21957 invoked by uid 500); 2 Nov 2003 19:48:30 -0000 Received: (qmail 21954 invoked from network); 2 Nov 2003 19:48:30 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 2 Nov 2003 19:48:30 -0000 Received: (qmail 877 invoked by uid 1529); 2 Nov 2003 19:48:40 -0000 Date: 2 Nov 2003 19:48:40 -0000 Message-ID: <20031102194840.876.qmail@minotaur.apache.org> From: scolebourne@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/collections/src/test/org/apache/commons/collections AbstractTestBidiMap.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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 BidiMap implemented using two maps. @@ -225,6 +225,10 @@ * 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 @@ 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 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