Better testing of MapIterators

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131356 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-11-20 00:31:42 +00:00
parent 367f8710b0
commit 257ef4a6d4
4 changed files with 108 additions and 36 deletions

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bidimap/DualTreeBidiMap.java,v 1.2 2003/11/19 23:58:11 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bidimap/DualTreeBidiMap.java,v 1.3 2003/11/20 00:31:42 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -61,6 +61,7 @@ import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.ListIterator;
@ -68,7 +69,6 @@ import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.commons.collections.iterators.ListIteratorWrapper;
import org.apache.commons.collections.iterators.OrderedMapIterator;
import org.apache.commons.collections.iterators.ResettableIterator;
import org.apache.commons.collections.map.AbstractSortedMapDecorator;
@ -85,7 +85,7 @@ import org.apache.commons.collections.map.OrderedMap;
* not store each object twice, which can save on memory use.
*
* @since Commons Collections 3.0
* @version $Id: DualTreeBidiMap.java,v 1.2 2003/11/19 23:58:11 scolebourne Exp $
* @version $Id: DualTreeBidiMap.java,v 1.3 2003/11/20 00:31:42 scolebourne Exp $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
@ -290,7 +290,7 @@ public class DualTreeBidiMap extends AbstractDualBidiMap implements SortedBidiMa
protected BidiOrderedMapIterator(AbstractDualBidiMap map) {
super();
this.map = map;
iterator = new ListIteratorWrapper(map.entrySet().iterator());
iterator = new ArrayList(map.entrySet()).listIterator();
}
public boolean hasNext() {
@ -314,6 +314,7 @@ public class DualTreeBidiMap extends AbstractDualBidiMap implements SortedBidiMa
public void remove() {
iterator.remove();
map.remove(last.getKey());
last = null;
}
public Object getKey() {
@ -342,7 +343,7 @@ public class DualTreeBidiMap extends AbstractDualBidiMap implements SortedBidiMa
}
public void reset() {
iterator = new ListIteratorWrapper(map.entrySet().iterator());
iterator = new ArrayList(map.entrySet()).listIterator();
last = null;
}

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bidimap/TreeBidiMap.java,v 1.1 2003/11/16 20:35:46 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/bidimap/TreeBidiMap.java,v 1.2 2003/11/20 00:31:42 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -105,7 +105,7 @@ import org.apache.commons.collections.pairs.UnmodifiableMapEntry;
* UnsupportedOperationException on attempts to call that method.
*
* @since Commons Collections 3.0 (previously DoubleOrderedMap v2.0)
* @version $Revision: 1.1 $ $Date: 2003/11/16 20:35:46 $
* @version $Revision: 1.2 $ $Date: 2003/11/20 00:31:42 $
*
* @author Marc Johnson
* @author Stephen Colebourne
@ -1644,10 +1644,16 @@ public class TreeBidiMap implements OrderedBidiMap {
}
public Object getKey() {
if (lastReturnedNode == null) {
throw new IllegalStateException("Iterator getKey() can only be called after next() and before remove()");
}
return lastReturnedNode.getData(dataType);
}
public Object getValue() {
if (lastReturnedNode == null) {
throw new IllegalStateException("Iterator getValue() can only be called after next() and before remove()");
}
return lastReturnedNode.getData(oppositeType);
}

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bidimap/AbstractTestOrderedBidiMap.java,v 1.1 2003/11/16 20:35:46 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/bidimap/AbstractTestOrderedBidiMap.java,v 1.2 2003/11/20 00:31:42 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -61,15 +61,17 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import org.apache.commons.collections.BulkTest;
import org.apache.commons.collections.iterators.AbstractTestMapIterator;
import org.apache.commons.collections.iterators.MapIterator;
import org.apache.commons.collections.iterators.OrderedMapIterator;
/**
* Abstract test class for {@link OrderedBidiMap} methods and contracts.
*
* @version $Revision: 1.1 $ $Date: 2003/11/16 20:35:46 $
* @version $Revision: 1.2 $ $Date: 2003/11/20 00:31:42 $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
@ -186,33 +188,50 @@ public abstract class AbstractTestOrderedBidiMap extends AbstractTestBidiMap {
}
//-----------------------------------------------------------------------
public void testMapIteratorOrder() {
resetFull();
OrderedBidiMap bidi = (OrderedBidiMap) map;
List ordered = new ArrayList(map.keySet());
List ordered2 = new ArrayList(map.keySet());
assertEquals("KeySet iterator is not consistent", ordered, ordered2);
public BulkTest bulkTestOrderedMapIterator() {
return new TestBidiOrderedMapIterator();
}
int i = 0;
for (MapIterator it = bidi.mapIterator(); it.hasNext(); i++) {
Object key = (Object) it.next();
assertEquals("Inconsistent order", ordered.get(i), key);
assertEquals("Incorrect value for key", bidi.get(key), it.getValue());
public class TestBidiOrderedMapIterator extends AbstractTestMapIterator {
public TestBidiOrderedMapIterator() {
super("TestBidiOrderedMapIterator");
}
i = 0;
OrderedMapIterator it = bidi.orderedMapIterator();
for (; it.hasNext(); i++) {
Object key = (Object) it.next();
assertEquals("Inconsistent order", ordered.get(i), key);
assertEquals("Incorrect value for key", bidi.get(key), it.getValue());
assertEquals(true, it.hasPrevious());
public Object[] addSetValues() {
return AbstractTestOrderedBidiMap.this.getNewSampleValues();
}
i--;
for (; it.hasPrevious(); i--) {
Object key = (Object) it.previous();
assertEquals("Inconsistent order", ordered.get(i), key);
assertEquals("Incorrect value for key", bidi.get(key), it.getValue());
assertEquals(true, it.hasNext());
public boolean supportsRemove() {
return AbstractTestOrderedBidiMap.this.isRemoveSupported();
}
public boolean supportsSetValue() {
return AbstractTestOrderedBidiMap.this.isSetValueSupported();
}
public MapIterator makeEmptyMapIterator() {
resetEmpty();
return ((OrderedBidiMap) AbstractTestOrderedBidiMap.this.map).orderedMapIterator();
}
public MapIterator makeFullMapIterator() {
resetFull();
return ((OrderedBidiMap) AbstractTestOrderedBidiMap.this.map).orderedMapIterator();
}
public Map getMap() {
// assumes makeFullMapIterator() called first
return AbstractTestOrderedBidiMap.this.map;
}
public Map getConfirmedMap() {
// assumes makeFullMapIterator() called first
return AbstractTestOrderedBidiMap.this.confirmed;
}
public void verify() {
super.verify();
AbstractTestOrderedBidiMap.this.verify();
}
}

View File

@ -1,5 +1,5 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/AbstractTestMapIterator.java,v 1.5 2003/11/20 00:03:05 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/iterators/AbstractTestMapIterator.java,v 1.6 2003/11/20 00:31:42 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -72,7 +72,7 @@ import java.util.Set;
* overriding the supportsXxx() methods if necessary.
*
* @since Commons Collections 3.0
* @version $Revision: 1.5 $ $Date: 2003/11/20 00:03:05 $
* @version $Revision: 1.6 $ $Date: 2003/11/20 00:31:42 $
*
* @author Stephen Colebourne
*/
@ -333,4 +333,50 @@ public abstract class AbstractTestMapIterator extends AbstractTestIterator {
verify();
}
//-----------------------------------------------------------------------
public void testMapIteratorRemoveGetKey() {
if (supportsRemove() == false) {
return;
}
MapIterator it = makeFullMapIterator();
Map map = getMap();
Map confirmed = getConfirmedMap();
assertEquals(true, it.hasNext());
Object key = it.next();
it.remove();
confirmed.remove(key);
verify();
try {
it.getKey();
fail();
} catch (IllegalStateException ex) {}
verify();
}
//-----------------------------------------------------------------------
public void testMapIteratorRemoveGetValue() {
if (supportsRemove() == false) {
return;
}
MapIterator it = makeFullMapIterator();
Map map = getMap();
Map confirmed = getConfirmedMap();
assertEquals(true, it.hasNext());
Object key = it.next();
it.remove();
confirmed.remove(key);
verify();
try {
it.getValue();
fail();
} catch (IllegalStateException ex) {}
verify();
}
}