Add extra tests for maps

Make the tests pass


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@131309 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-11-01 18:47:18 +00:00
parent 5acf38c49a
commit be74e690e0
8 changed files with 647 additions and 103 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/Attic/AbstractDualBidiMap.java,v 1.5 2003/10/31 01:26:25 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.6 2003/11/01 18:47:18 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -73,7 +73,7 @@ import org.apache.commons.collections.decorators.AbstractMapEntryDecorator;
* <code>createMap</code> method.
*
* @since Commons Collections 3.0
* @version $Id: AbstractDualBidiMap.java,v 1.5 2003/10/31 01:26:25 scolebourne Exp $
* @version $Id: AbstractDualBidiMap.java,v 1.6 2003/11/01 18:47:18 scolebourne Exp $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
@ -510,12 +510,13 @@ public abstract class AbstractDualBidiMap implements BidiMap {
}
public Object setValue(Object value) {
Object key = MapEntry.this.getKey();
if (map.maps[1].containsKey(value) &&
map.maps[1].get(value) != key) {
throw new IllegalArgumentException("Cannot use setValue() when the object being set is already in the map");
}
map.put(key, value);
final Object oldValue = super.setValue(value);
// Gets old key and pairs with new value
final Object inverseKey = map.maps[1].remove(oldValue);
map.maps[1].put(value, inverseKey);
return oldValue;
}
}
@ -570,13 +571,11 @@ public abstract class AbstractDualBidiMap implements BidiMap {
if (last == null) {
throw new IllegalStateException("Iterator setValue() can only be called after next() and before remove()");
}
Object oldValue = last.setValue(value);
// Gets old key and pairs with new value
final Object inverseKey = map.maps[1].remove(oldValue);
map.maps[1].put(value, inverseKey);
return oldValue;
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 map.put(last.getKey(), value);
}
}

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/Attic/DualTreeBidiMap.java,v 1.1 2003/10/31 01:26:25 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/java/org/apache/commons/collections/Attic/DualTreeBidiMap.java,v 1.2 2003/11/01 18:47:18 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -58,6 +58,7 @@
package org.apache.commons.collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
@ -68,7 +69,7 @@ import org.apache.commons.collections.decorators.AbstractSortedMapDecorator;
* Implementation of <code>BidiMap</code> that uses two <code>TreeMap</code> instances.
*
* @since Commons Collections 3.0
* @version $Id: DualTreeBidiMap.java,v 1.1 2003/10/31 01:26:25 scolebourne Exp $
* @version $Id: DualTreeBidiMap.java,v 1.2 2003/11/01 18:47:18 scolebourne Exp $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
@ -165,6 +166,9 @@ public class DualTreeBidiMap extends AbstractDualBidiMap implements SortedBidiMa
final DualTreeBidiMap bidi;
protected ViewMap(DualTreeBidiMap bidi, SortedMap sm) {
// the implementation is not great here...
// use the maps[0] as the filtered map, but maps[1] as the full map
// this forces containsValue and clear to be overridden
super((SortedMap) bidi.createBidiMap(sm, bidi.maps[1], bidi.inverseBidiMap));
this.bidi = (DualTreeBidiMap) map;
}
@ -174,6 +178,14 @@ public class DualTreeBidiMap extends AbstractDualBidiMap implements SortedBidiMa
return bidi.maps[0].containsValue(value);
}
public void clear() {
// override as default implementation jumps to [1]
for (Iterator it = keySet().iterator(); it.hasNext();) {
it.next();
it.remove();
}
}
public SortedMap headMap(Object toKey) {
return new ViewMap(bidi, super.headMap(toKey));
}

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/Attic/AbstractTestBidiMap.java,v 1.3 2003/10/31 01:25:24 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.4 2003/11/01 18:47:18 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -57,15 +57,17 @@
*/
package org.apache.commons.collections;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
/**
* Abstract test class for {@link BidiMap} methods and contracts.
*
* @version $Revision: 1.3 $ $Date: 2003/10/31 01:25:24 $
* @version $Revision: 1.4 $ $Date: 2003/11/01 18:47:18 $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
@ -184,6 +186,16 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap {
protected void verifyInverse() {
assertEquals(map.size(), ((BidiMap) map).inverseBidiMap().size());
Map map1 = new HashMap(map);
Map map2 = new HashMap(((BidiMap) map).inverseBidiMap());
Set keys1 = map1.keySet();
Set keys2 = map2.keySet();
Collection values1 = map1.values();
Collection values2 = map2.values();
assertEquals(true, keys1.containsAll(values2));
assertEquals(true, values2.containsAll(keys1));
assertEquals(true, values1.containsAll(keys2));
assertEquals(true, keys2.containsAll(values1));
}
// testGetKey
@ -346,6 +358,72 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap {
!map.inverseBidiMap().containsKey(value));
}
public BulkTest bulkTestMapEntrySet() {
return new TestBidiMapEntrySet();
}
public class TestBidiMapEntrySet extends TestMapEntrySet {
public TestBidiMapEntrySet() {
super();
}
public void testMapEntrySetIteratorEntrySetValueCrossCheck() {
Object key1 = getSampleKeys()[0];
Object key2 = getSampleKeys()[1];
Object newValue1 = getNewSampleValues()[0];
Object newValue2 = getNewSampleValues()[1];
resetFull();
// explicitly get entries as sample values/keys are connected for some maps
// such as BeanMap
Iterator it = TestBidiMapEntrySet.this.collection.iterator();
Map.Entry entry1 = getEntry(it, key1);
it = TestBidiMapEntrySet.this.collection.iterator();
Map.Entry entry2 = getEntry(it, key2);
Iterator itConfirmed = TestBidiMapEntrySet.this.confirmed.iterator();
Map.Entry entryConfirmed1 = getEntry(itConfirmed, key1);
itConfirmed = TestBidiMapEntrySet.this.confirmed.iterator();
Map.Entry entryConfirmed2 = getEntry(itConfirmed, key2);
TestBidiMapEntrySet.this.verify();
if (isSetValueSupported() == false) {
try {
entry1.setValue(newValue1);
} catch (UnsupportedOperationException ex) {
}
return;
}
// these checked in superclass
entry1.setValue(newValue1);
entryConfirmed1.setValue(newValue1);
entry2.setValue(newValue2);
entryConfirmed2.setValue(newValue2);
// at this point
// key1=newValue1, key2=newValue2
try {
entry2.setValue(newValue1); // should remove key1
} catch (IllegalArgumentException ex) {
return; // simplest way of dealing with tricky situation
}
entryConfirmed2.setValue(newValue1);
AbstractTestBidiMap.this.confirmed.remove(key1);
assertEquals(newValue1, entry2.getValue());
assertEquals(true, AbstractTestBidiMap.this.map.containsKey(entry2.getKey()));
assertEquals(true, AbstractTestBidiMap.this.map.containsValue(newValue1));
assertEquals(newValue1, AbstractTestBidiMap.this.map.get(entry2.getKey()));
assertEquals(false, AbstractTestBidiMap.this.map.containsKey(key1));
assertEquals(false, AbstractTestBidiMap.this.map.containsValue(newValue2));
TestBidiMapEntrySet.this.verify();
// check for ConcurrentModification
it.next(); // if you fail here, maybe you should be throwing an IAE, see above
if (isRemoveSupported()) {
it.remove();
}
}
}
public BulkTest bulkTestInverseMap() {
return new TestInverseBidiMap(this);
}
@ -385,6 +463,9 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap {
protected boolean isPutChangeSupported() {
return main.isPutChangeSupported();
}
protected boolean isSetValueSupported() {
return main.isSetValueSupported();
}
protected boolean isRemoveSupported() {
return main.isRemoveSupported();
}
@ -468,29 +549,98 @@ public abstract class AbstractTestBidiMap extends AbstractTestMap {
//-----------------------------------------------------------------------
public void testBidiMapIteratorSet() {
Object newValue1 = getOtherValues()[0];
Object newValue2 = getOtherValues()[1];
resetFull();
BidiMap bidi = (BidiMap) map;
MapIterator it = bidi.mapIterator();
assertEquals(true, it.hasNext());
Object key1 = it.next();
if (isSetValueSupported() == false) {
try {
it.setValue(newValue1);
} catch (UnsupportedOperationException ex) {
}
return;
}
it.setValue(newValue1);
confirmed.put(key1, newValue1);
assertSame(key1, it.getKey());
assertSame(newValue1, it.getValue());
assertEquals(true, bidi.containsKey(key1));
assertEquals(true, bidi.containsValue(newValue1));
assertEquals(newValue1, bidi.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, bidi.containsKey(key1));
assertEquals(true, bidi.containsValue(newValue1));
assertEquals(newValue1, bidi.get(key1));
verify();
Object key2 = it.next();
it.setValue(newValue2);
confirmed.put(key2, newValue2);
assertSame(key2, it.getKey());
assertSame(newValue2, it.getValue());
assertEquals(true, bidi.containsKey(key2));
assertEquals(true, bidi.containsValue(newValue2));
assertEquals(newValue2, bidi.get(key2));
verify();
// at this point
// key1=newValue1, key2=newValue2
try {
it.setValue(newValue1); // should remove key1
} catch (IllegalArgumentException ex) {
return; // simplest way of dealing with tricky situation
}
confirmed.put(key2, newValue1);
AbstractTestBidiMap.this.confirmed.remove(key1);
assertEquals(newValue1, it.getValue());
assertEquals(true, bidi.containsKey(it.getKey()));
assertEquals(true, bidi.containsValue(newValue1));
assertEquals(newValue1, bidi.get(it.getKey()));
assertEquals(false, bidi.containsKey(key1));
assertEquals(false, bidi.containsValue(newValue2));
verify();
// check for ConcurrentModification
it.next(); // if you fail here, maybe you should be throwing an IAE, see above
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();
if (isPutChangeSupported() == false) {
try {
it.setValue(getOtherValues()[0]);
} catch (UnsupportedOperationException ex) {
}
return;
}
it.setValue(getOtherValues()[0]);
confirmed.put(key, getOtherValues()[0]);
assertEquals(getOtherValues()[0], bidi.get(key));
it.setValue(newValue1);
confirmed.put(key, newValue1);
verify();
it.remove();
confirmed.remove(key);
verify();
try {
it.setValue(getOtherValues()[0]);
it.setValue(newValue1);
} catch (IllegalStateException ex) {
}
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/Attic/AbstractTestMap.java,v 1.9 2003/10/31 01:24:32 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/AbstractTestMap.java,v 1.10 2003/11/01 18:47:18 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -98,6 +98,7 @@ import java.util.Set;
* <ul>
* <li> {@link #isPutAddSupported()}
* <li> {@link #isPutChangeSupported()}
* <li> {@link #isSetValueSupported()}
* <li> {@link #isRemoveSupported()}
* <li> {@link #isAllowDuplicateValues()}
* <li> {@link #isAllowNullKey()}
@ -152,7 +153,7 @@ import java.util.Set;
* @author Rodney Waldhoff
* @author Paul Jack
* @author Stephen Colebourne
* @version $Revision: 1.9 $ $Date: 2003/10/31 01:24:32 $
* @version $Revision: 1.10 $ $Date: 2003/11/01 18:47:18 $
*/
public abstract class AbstractTestMap extends AbstractTestObject {
@ -215,6 +216,19 @@ public abstract class AbstractTestMap extends AbstractTestObject {
return true;
}
/**
* Returns true if the maps produced by
* {@link #makeEmptyMap()} and {@link #makeFullMap()}
* support the <code>setValue</code> operation on entrySet entries.
* <p>
* Default implementation returns isPutChangeSupported().
* Override if your collection class does not support setValue but does
* support put changing.
*/
protected boolean isSetValueSupported() {
return isPutChangeSupported();
}
/**
* Returns true if the maps produced by
* {@link #makeEmptyMap()} and {@link #makeFullMap()}
@ -781,8 +795,10 @@ public abstract class AbstractTestMap extends AbstractTestObject {
}
} else {
try {
// two possible exception here, either valid
map.put(keys[0], newValues[0]);
fail("Expected UnsupportedOperationException on put (change)");
fail("Expected IllegalArgumentException or UnsupportedOperationException on put (change)");
} catch (IllegalArgumentException ex) {
} catch (UnsupportedOperationException ex) {}
}
@ -1085,9 +1101,9 @@ public abstract class AbstractTestMap extends AbstractTestObject {
return new TestMapEntrySet();
}
class TestMapEntrySet extends AbstractTestSet {
public class TestMapEntrySet extends AbstractTestSet {
public TestMapEntrySet() {
super("");
super("MapEntrySet");
}
// Have to implement manually; entrySet doesn't support addAll
@ -1139,6 +1155,90 @@ public abstract class AbstractTestMap extends AbstractTestObject {
TestMapEntrySet.this.confirmed = AbstractTestMap.this.confirmed.entrySet();
}
public void testMapEntrySetIteratorEntry() {
resetFull();
Iterator it = collection.iterator();
int count = 0;
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
assertEquals(true, AbstractTestMap.this.map.containsKey(entry.getKey()));
assertEquals(true, AbstractTestMap.this.map.containsValue(entry.getValue()));
assertEquals(AbstractTestMap.this.map.get(entry.getKey()), entry.getValue());
count++;
}
assertEquals(collection.size(), count);
}
public void testMapEntrySetIteratorEntrySetValue() {
Object key1 = getSampleKeys()[0];
Object key2 = getSampleKeys()[1];
Object newValue1 = getNewSampleValues()[0];
Object newValue2 = getNewSampleValues()[1];
resetFull();
// explicitly get entries as sample values/keys are connected for some maps
// such as BeanMap
Iterator it = TestMapEntrySet.this.collection.iterator();
Map.Entry entry1 = getEntry(it, key1);
it = TestMapEntrySet.this.collection.iterator();
Map.Entry entry2 = getEntry(it, key2);
Iterator itConfirmed = TestMapEntrySet.this.confirmed.iterator();
Map.Entry entryConfirmed1 = getEntry(itConfirmed, key1);
itConfirmed = TestMapEntrySet.this.confirmed.iterator();
Map.Entry entryConfirmed2 = getEntry(itConfirmed, key2);
verify();
if (isSetValueSupported() == false) {
try {
entry1.setValue(newValue1);
} catch (UnsupportedOperationException ex) {
}
return;
}
entry1.setValue(newValue1);
entryConfirmed1.setValue(newValue1);
assertEquals(newValue1, entry1.getValue());
assertEquals(true, AbstractTestMap.this.map.containsKey(entry1.getKey()));
assertEquals(true, AbstractTestMap.this.map.containsValue(newValue1));
assertEquals(newValue1, AbstractTestMap.this.map.get(entry1.getKey()));
verify();
entry1.setValue(newValue1);
entryConfirmed1.setValue(newValue1);
assertEquals(newValue1, entry1.getValue());
assertEquals(true, AbstractTestMap.this.map.containsKey(entry1.getKey()));
assertEquals(true, AbstractTestMap.this.map.containsValue(newValue1));
assertEquals(newValue1, AbstractTestMap.this.map.get(entry1.getKey()));
verify();
entry2.setValue(newValue2);
entryConfirmed2.setValue(newValue2);
assertEquals(newValue2, entry2.getValue());
assertEquals(true, AbstractTestMap.this.map.containsKey(entry2.getKey()));
assertEquals(true, AbstractTestMap.this.map.containsValue(newValue2));
assertEquals(newValue2, AbstractTestMap.this.map.get(entry2.getKey()));
verify();
}
protected Map.Entry getEntry(Iterator itConfirmed, Object key) {
Map.Entry entry = null;
while (itConfirmed.hasNext()) {
Map.Entry temp = (Map.Entry) itConfirmed.next();
if (temp.getKey() == null) {
if (key == null) {
entry = temp;
break;
}
} else if (temp.getKey().equals(key)) {
entry = temp;
break;
}
}
assertNotNull("No matching entry in map for key '" + key + "'", entry);
return entry;
}
protected void verify() {
super.verify();
AbstractTestMap.this.verify();
@ -1158,7 +1258,7 @@ public abstract class AbstractTestMap extends AbstractTestObject {
return new TestMapKeySet();
}
class TestMapKeySet extends AbstractTestSet {
public class TestMapKeySet extends AbstractTestSet {
public TestMapKeySet() {
super("");
}
@ -1226,7 +1326,7 @@ public abstract class AbstractTestMap extends AbstractTestObject {
return new TestMapValues();
}
class TestMapValues extends AbstractTestCollection {
public class TestMapValues extends AbstractTestCollection {
public TestMapValues() {
super("");
}
@ -1415,8 +1515,12 @@ public abstract class AbstractTestMap extends AbstractTestObject {
size, values.size());
assertEquals("values should be empty if HashMap is",
empty, values.isEmpty());
assertTrue("values should contain all HashMap's elements",
values.containsAll(confirmed.values()));
assertTrue("values should contain all HashMap's elements" +
"\nTest: " + test + "\nReal: " + known,
test.containsAll(known));
assertTrue("values should contain all HashMap's elements" +
"\nTest: " + test + "\nReal: " + known,
known.containsAll(test));
// originally coded to use a HashBag, but now separate jar so...
for (Iterator it = known.iterator(); it.hasNext();) {
boolean removed = test.remove(it.next());

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/Attic/AbstractTestSortedBidiMap.java,v 1.1 2003/10/31 01:26:25 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/AbstractTestSortedBidiMap.java,v 1.2 2003/11/01 18:47:18 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -74,7 +74,7 @@ import org.apache.commons.collections.pairs.DefaultMapEntry;
/**
* Abstract test class for {@link BidiMap} methods and contracts.
*
* @version $Revision: 1.1 $ $Date: 2003/10/31 01:26:25 $
* @version $Revision: 1.2 $ $Date: 2003/11/01 18:47:18 $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
@ -143,6 +143,7 @@ public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
assertSame(obj, sm.lastKey());
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
public void testBidiHeadMapContains() {
// extra test as other tests get complex
@ -166,6 +167,49 @@ public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
assertEquals(false, head.containsValue(secondValue));
}
//-----------------------------------------------------------------------
public void testBidiClearByHeadMap() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
Object first = it.next();
Object second = it.next();
Object toKey = it.next();
Object firstValue = sm.get(first);
Object secondValue = sm.get(second);
Object toKeyValue = sm.get(toKey);
SortedMap sub = sm.headMap(toKey);
int size = sm.size();
assertEquals(2, sub.size());
sub.clear();
assertEquals(0, sub.size());
assertEquals(size - 2, sm.size());
assertEquals(size - 2, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(first));
assertEquals(false, sm.containsValue(firstValue));
assertEquals(false, sm.inverseBidiMap().containsKey(firstValue));
assertEquals(false, sm.inverseBidiMap().containsValue(first));
assertEquals(false, sub.containsKey(first));
assertEquals(false, sub.containsValue(firstValue));
assertEquals(false, sm.containsKey(second));
assertEquals(false, sm.containsValue(secondValue));
assertEquals(false, sm.inverseBidiMap().containsKey(secondValue));
assertEquals(false, sm.inverseBidiMap().containsValue(second));
assertEquals(false, sub.containsKey(second));
assertEquals(false, sub.containsValue(secondValue));
assertEquals(true, sm.containsKey(toKey));
assertEquals(true, sm.containsValue(toKeyValue));
assertEquals(true, sm.inverseBidiMap().containsKey(toKeyValue));
assertEquals(true, sm.inverseBidiMap().containsValue(toKey));
assertEquals(false, sub.containsKey(toKey));
assertEquals(false, sub.containsValue(toKeyValue));
}
//-----------------------------------------------------------------------
public void testBidiRemoveByHeadMap() {
// extra test as other tests get complex
@ -175,30 +219,35 @@ public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
Object second = it.next();
Object toKey = it.next();
SortedMap head = sm.headMap(toKey);
assertEquals(2, head.size());
int size = sm.size();
SortedMap sub = sm.headMap(toKey);
assertEquals(2, sub.size());
assertEquals(true, sm.containsKey(first));
assertEquals(true, head.containsKey(first));
assertEquals(true, sub.containsKey(first));
assertEquals(true, sm.containsKey(second));
assertEquals(true, head.containsKey(second));
assertEquals(true, sub.containsKey(second));
Object firstValue = head.remove(first);
Object firstValue = sub.remove(first);
assertEquals(1, sub.size());
assertEquals(size - 1, sm.size());
assertEquals(size - 1, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(first));
assertEquals(false, sm.containsValue(firstValue));
assertEquals(false, sm.inverseBidiMap().containsKey(firstValue));
assertEquals(false, sm.inverseBidiMap().containsValue(first));
assertEquals(false, head.containsKey(first));
assertEquals(false, head.containsValue(firstValue));
assertEquals(1, head.size());
assertEquals(false, sub.containsKey(first));
assertEquals(false, sub.containsValue(firstValue));
Object secondValue = head.remove(second);
Object secondValue = sub.remove(second);
assertEquals(0, sub.size());
assertEquals(size - 2, sm.size());
assertEquals(size - 2, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(second));
assertEquals(false, sm.containsValue(secondValue));
assertEquals(false, sm.inverseBidiMap().containsKey(secondValue));
assertEquals(false, sm.inverseBidiMap().containsValue(second));
assertEquals(false, head.containsKey(second));
assertEquals(false, head.containsValue(secondValue));
assertEquals(0, head.size());
assertEquals(false, sub.containsKey(second));
assertEquals(false, sub.containsValue(secondValue));
}
//-----------------------------------------------------------------------
@ -206,46 +255,126 @@ public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
it.next();
it.next();
Object fromKey = it.next();
Object first = it.next();
Object second = it.next();
Object toKey = it.next();
SortedMap head = sm.headMap(toKey);
Set set = head.entrySet();
int size = sm.size();
SortedMap sub = sm.headMap(toKey);
Set set = sub.entrySet();
assertEquals(2, sub.size());
assertEquals(2, set.size());
Iterator it2 = set.iterator();
Object fromEntry = it2.next();
Map.Entry firstEntry = new DefaultMapEntry((Map.Entry) it2.next());
Map.Entry secondEntry = new DefaultMapEntry((Map.Entry) it2.next());
assertEquals(true, sm.containsKey(first));
assertEquals(true, head.containsKey(first));
assertEquals(true, sub.containsKey(first));
assertEquals(true, set.contains(firstEntry));
assertEquals(true, sm.containsKey(second));
assertEquals(true, head.containsKey(second));
assertEquals(true, sub.containsKey(second));
assertEquals(true, set.contains(secondEntry));
set.remove(firstEntry);
assertEquals(1, sub.size());
assertEquals(size - 1, sm.size());
assertEquals(size - 1, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(firstEntry.getKey()));
assertEquals(false, sm.containsValue(firstEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsKey(firstEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsValue(firstEntry.getKey()));
assertEquals(false, head.containsKey(firstEntry.getKey()));
assertEquals(false, head.containsValue(firstEntry.getValue()));
assertEquals(false, sub.containsKey(firstEntry.getKey()));
assertEquals(false, sub.containsValue(firstEntry.getValue()));
assertEquals(false, set.contains(firstEntry));
set.remove(secondEntry);
assertEquals(0, sub.size());
assertEquals(size - 2, sm.size());
assertEquals(size - 2, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(secondEntry.getKey()));
assertEquals(false, sm.containsValue(secondEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsKey(secondEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsValue(secondEntry.getKey()));
assertEquals(false, head.containsKey(secondEntry.getKey()));
assertEquals(false, head.containsValue(secondEntry.getValue()));
assertEquals(false, sub.containsKey(secondEntry.getKey()));
assertEquals(false, sub.containsValue(secondEntry.getValue()));
assertEquals(false, set.contains(secondEntry));
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
public void testBidiTailMapContains() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
Object first = it.next();
Object fromKey = it.next();
Object second = it.next();
Object firstValue = sm.get(first);
Object fromKeyValue = sm.get(fromKey);
Object secondValue = sm.get(second);
SortedMap sub = sm.tailMap(fromKey);
assertEquals(sm.size() - 1, sub.size());
assertEquals(true, sm.containsKey(first));
assertEquals(false, sub.containsKey(first));
assertEquals(true, sm.containsValue(firstValue));
assertEquals(false, sub.containsValue(firstValue));
assertEquals(true, sm.containsKey(fromKey));
assertEquals(true, sub.containsKey(fromKey));
assertEquals(true, sm.containsValue(fromKeyValue));
assertEquals(true, sub.containsValue(fromKeyValue));
assertEquals(true, sm.containsKey(second));
assertEquals(true, sub.containsKey(second));
assertEquals(true, sm.containsValue(secondValue));
assertEquals(true, sub.containsValue(secondValue));
}
//-----------------------------------------------------------------------
public void testBidiClearByTailMap() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
it.next();
it.next();
Object first = it.next();
Object fromKey = it.next();
Object second = it.next();
Object firstValue = sm.get(first);
Object fromKeyValue = sm.get(fromKey);
Object secondValue = sm.get(second);
SortedMap sub = sm.tailMap(fromKey);
int size = sm.size();
assertEquals(size - 3, sub.size());
sub.clear();
assertEquals(0, sub.size());
assertEquals(3, sm.size());
assertEquals(3, sm.inverseBidiMap().size());
assertEquals(true, sm.containsKey(first));
assertEquals(true, sm.containsValue(firstValue));
assertEquals(true, sm.inverseBidiMap().containsKey(firstValue));
assertEquals(true, sm.inverseBidiMap().containsValue(first));
assertEquals(false, sub.containsKey(first));
assertEquals(false, sub.containsValue(firstValue));
assertEquals(false, sm.containsKey(fromKey));
assertEquals(false, sm.containsValue(fromKeyValue));
assertEquals(false, sm.inverseBidiMap().containsKey(fromKeyValue));
assertEquals(false, sm.inverseBidiMap().containsValue(fromKey));
assertEquals(false, sub.containsKey(fromKey));
assertEquals(false, sub.containsValue(fromKeyValue));
assertEquals(false, sm.containsKey(second));
assertEquals(false, sm.containsValue(secondValue));
assertEquals(false, sm.inverseBidiMap().containsKey(secondValue));
assertEquals(false, sm.inverseBidiMap().containsValue(second));
assertEquals(false, sub.containsKey(second));
assertEquals(false, sub.containsValue(secondValue));
}
//-----------------------------------------------------------------------
public void testBidiRemoveByTailMap() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
@ -256,27 +385,34 @@ public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
Object first = it.next();
Object second = it.next();
SortedMap tail = sm.tailMap(fromKey);
int size = sm.size();
SortedMap sub = sm.tailMap(fromKey);
assertEquals(true, sm.containsKey(first));
assertEquals(true, tail.containsKey(first));
assertEquals(true, sub.containsKey(first));
assertEquals(true, sm.containsKey(second));
assertEquals(true, tail.containsKey(second));
assertEquals(true, sub.containsKey(second));
Object firstValue = tail.remove(first);
Object firstValue = sub.remove(first);
assertEquals(size - 3, sub.size());
assertEquals(size - 1, sm.size());
assertEquals(size - 1, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(first));
assertEquals(false, sm.containsValue(firstValue));
assertEquals(false, sm.inverseBidiMap().containsKey(firstValue));
assertEquals(false, sm.inverseBidiMap().containsValue(first));
assertEquals(false, tail.containsKey(first));
assertEquals(false, tail.containsValue(firstValue));
assertEquals(false, sub.containsKey(first));
assertEquals(false, sub.containsValue(firstValue));
Object secondValue = tail.remove(second);
Object secondValue = sub.remove(second);
assertEquals(size - 4, sub.size());
assertEquals(size - 2, sm.size());
assertEquals(size - 2, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(second));
assertEquals(false, sm.containsValue(secondValue));
assertEquals(false, sm.inverseBidiMap().containsKey(secondValue));
assertEquals(false, sm.inverseBidiMap().containsValue(second));
assertEquals(false, tail.containsKey(second));
assertEquals(false, tail.containsValue(secondValue));
assertEquals(false, sub.containsKey(second));
assertEquals(false, sub.containsValue(secondValue));
}
//-----------------------------------------------------------------------
@ -289,40 +425,135 @@ public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
Object fromKey = it.next();
Object first = it.next();
Object second = it.next();
Object toKey = it.next();
SortedMap tail = sm.tailMap(fromKey);
Set set = tail.entrySet();
int size = sm.size();
SortedMap sub = sm.tailMap(fromKey);
Set set = sub.entrySet();
Iterator it2 = set.iterator();
Object fromEntry = it2.next();
Map.Entry firstEntry = new DefaultMapEntry((Map.Entry) it2.next());
Map.Entry secondEntry = new DefaultMapEntry((Map.Entry) it2.next());
assertEquals(true, sm.containsKey(first));
assertEquals(true, tail.containsKey(first));
assertEquals(true, sub.containsKey(first));
assertEquals(true, set.contains(firstEntry));
assertEquals(true, sm.containsKey(second));
assertEquals(true, tail.containsKey(second));
assertEquals(true, sub.containsKey(second));
assertEquals(true, set.contains(secondEntry));
set.remove(firstEntry);
assertEquals(size - 3, sub.size());
assertEquals(size - 1, sm.size());
assertEquals(size - 1, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(firstEntry.getKey()));
assertEquals(false, sm.containsValue(firstEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsKey(firstEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsValue(firstEntry.getKey()));
assertEquals(false, tail.containsKey(firstEntry.getKey()));
assertEquals(false, tail.containsValue(firstEntry.getValue()));
assertEquals(false, sub.containsKey(firstEntry.getKey()));
assertEquals(false, sub.containsValue(firstEntry.getValue()));
assertEquals(false, set.contains(firstEntry));
set.remove(secondEntry);
assertEquals(size - 4, sub.size());
assertEquals(size - 2, sm.size());
assertEquals(size - 2, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(secondEntry.getKey()));
assertEquals(false, sm.containsValue(secondEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsKey(secondEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsValue(secondEntry.getKey()));
assertEquals(false, tail.containsKey(secondEntry.getKey()));
assertEquals(false, tail.containsValue(secondEntry.getValue()));
assertEquals(false, sub.containsKey(secondEntry.getKey()));
assertEquals(false, sub.containsValue(secondEntry.getValue()));
assertEquals(false, set.contains(secondEntry));
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
public void testBidiSubMapContains() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
Object first = it.next();
Object fromKey = it.next();
Object second = it.next();
Object toKey = it.next();
Object third = it.next();
Object firstValue = sm.get(first);
Object fromKeyValue = sm.get(fromKey);
Object secondValue = sm.get(second);
Object thirdValue = sm.get(third);
SortedMap sub = sm.subMap(fromKey, toKey);
assertEquals(2, sub.size());
assertEquals(true, sm.containsKey(first));
assertEquals(false, sub.containsKey(first));
assertEquals(true, sm.containsValue(firstValue));
assertEquals(false, sub.containsValue(firstValue));
assertEquals(true, sm.containsKey(fromKey));
assertEquals(true, sub.containsKey(fromKey));
assertEquals(true, sm.containsValue(fromKeyValue));
assertEquals(true, sub.containsValue(fromKeyValue));
assertEquals(true, sm.containsKey(second));
assertEquals(true, sub.containsKey(second));
assertEquals(true, sm.containsValue(secondValue));
assertEquals(true, sub.containsValue(secondValue));
assertEquals(true, sm.containsKey(third));
assertEquals(false, sub.containsKey(third));
assertEquals(true, sm.containsValue(thirdValue));
assertEquals(false, sub.containsValue(thirdValue));
}
//-----------------------------------------------------------------------
public void testBidiClearBySubMap() {
// extra test as other tests get complex
SortedBidiMap sm = (SortedBidiMap) makeFullMap();
Iterator it = sm.keySet().iterator();
it.next();
Object fromKey = it.next();
Object first = it.next();
Object second = it.next();
Object toKey = it.next();
Object fromKeyValue = sm.get(fromKey);
Object firstValue = sm.get(first);
Object secondValue = sm.get(second);
Object toKeyValue = sm.get(toKey);
SortedMap sub = sm.subMap(fromKey, toKey);
int size = sm.size();
assertEquals(3, sub.size());
sub.clear();
assertEquals(0, sub.size());
assertEquals(size - 3, sm.size());
assertEquals(size - 3, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(fromKey));
assertEquals(false, sm.containsValue(fromKeyValue));
assertEquals(false, sm.inverseBidiMap().containsKey(fromKeyValue));
assertEquals(false, sm.inverseBidiMap().containsValue(fromKey));
assertEquals(false, sub.containsKey(fromKey));
assertEquals(false, sub.containsValue(fromKeyValue));
assertEquals(false, sm.containsKey(first));
assertEquals(false, sm.containsValue(firstValue));
assertEquals(false, sm.inverseBidiMap().containsKey(firstValue));
assertEquals(false, sm.inverseBidiMap().containsValue(first));
assertEquals(false, sub.containsKey(first));
assertEquals(false, sub.containsValue(firstValue));
assertEquals(false, sm.containsKey(second));
assertEquals(false, sm.containsValue(secondValue));
assertEquals(false, sm.inverseBidiMap().containsKey(secondValue));
assertEquals(false, sm.inverseBidiMap().containsValue(second));
assertEquals(false, sub.containsKey(second));
assertEquals(false, sub.containsValue(secondValue));
assertEquals(true, sm.containsKey(toKey));
assertEquals(true, sm.containsValue(toKeyValue));
assertEquals(true, sm.inverseBidiMap().containsKey(toKeyValue));
assertEquals(true, sm.inverseBidiMap().containsValue(toKey));
assertEquals(false, sub.containsKey(toKey));
assertEquals(false, sub.containsValue(toKeyValue));
}
//-----------------------------------------------------------------------
public void testBidiRemoveBySubMap() {
// extra test as other tests get complex
@ -335,6 +566,7 @@ public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
Object second = it.next();
Object toKey = it.next();
int size = sm.size();
SortedMap sub = sm.subMap(fromKey, toKey);
assertEquals(true, sm.containsKey(first));
assertEquals(true, sub.containsKey(first));
@ -342,6 +574,9 @@ public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
assertEquals(true, sub.containsKey(second));
Object firstValue = sub.remove(first);
assertEquals(2, sub.size());
assertEquals(size - 1, sm.size());
assertEquals(size - 1, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(first));
assertEquals(false, sm.containsValue(firstValue));
assertEquals(false, sm.inverseBidiMap().containsKey(firstValue));
@ -350,6 +585,9 @@ public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
assertEquals(false, sub.containsValue(firstValue));
Object secondValue = sub.remove(second);
assertEquals(1, sub.size());
assertEquals(size - 2, sm.size());
assertEquals(size - 2, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(second));
assertEquals(false, sm.containsValue(secondValue));
assertEquals(false, sm.inverseBidiMap().containsKey(secondValue));
@ -370,6 +608,7 @@ public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
Object second = it.next();
Object toKey = it.next();
int size = sm.size();
SortedMap sub = sm.subMap(fromKey, toKey);
Set set = sub.entrySet();
assertEquals(3, set.size());
@ -385,6 +624,9 @@ public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
assertEquals(true, set.contains(secondEntry));
set.remove(firstEntry);
assertEquals(2, sub.size());
assertEquals(size - 1, sm.size());
assertEquals(size - 1, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(firstEntry.getKey()));
assertEquals(false, sm.containsValue(firstEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsKey(firstEntry.getValue()));
@ -394,6 +636,9 @@ public abstract class AbstractTestSortedBidiMap extends AbstractTestBidiMap {
assertEquals(false, set.contains(firstEntry));
set.remove(secondEntry);
assertEquals(1, sub.size());
assertEquals(size - 2, sm.size());
assertEquals(size - 2, sm.inverseBidiMap().size());
assertEquals(false, sm.containsKey(secondEntry.getKey()));
assertEquals(false, sm.containsValue(secondEntry.getValue()));
assertEquals(false, sm.inverseBidiMap().containsKey(secondEntry.getValue()));

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/TestBeanMap.java,v 1.13 2003/10/07 22:20:57 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestBeanMap.java,v 1.14 2003/11/01 18:47:18 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -62,19 +62,25 @@ import java.lang.reflect.Method;
import java.util.Map;
import junit.framework.Test;
import junit.textui.TestRunner;
/**
* Test cases for BeanMap
*
* @version $Revision: 1.13 $ $Date: 2003/10/07 22:20:57 $
* @version $Revision: 1.14 $ $Date: 2003/11/01 18:47:18 $
*
* @author Morgan Delagrange
* @author Stephen Colebourne
*/
public class TestBeanMap extends AbstractTestMap {
public TestBeanMap(String testName) {
super(testName);
}
public static void main(String[] args) {
TestRunner.run(suite());
}
public static Test suite() {
return BulkTest.makeSuite(TestBeanMap.class);
@ -252,6 +258,14 @@ public class TestBeanMap extends AbstractTestMap {
return values;
}
/**
* Values is a dead copy in BeanMap, so refresh each time.
*/
protected void verifyValues() {
values = map.values();
super.verifyValues();
}
/**
* The mappings in a BeanMap are fixed on the properties the underlying
* bean has. Adding and removing mappings is not possible, thus this

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/TestDoubleOrderedMap.java,v 1.10 2003/10/07 22:20:57 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestDoubleOrderedMap.java,v 1.11 2003/11/01 18:47:18 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -77,9 +77,10 @@ import junit.framework.Test;
* Map that does not support duplicate keys, duplicate
* values, or null values.
*
* @version $Revision: 1.10 $ $Date: 2003/10/07 22:20:57 $
* @version $Revision: 1.11 $ $Date: 2003/11/01 18:47:18 $
*
* @author Marc Johnson (marcj at users dot sourceforge dot net)
* @author Marc Johnson
* @author Stephen Colebourne
*/
public class TestDoubleOrderedMap extends AbstractTestMap {
@ -104,7 +105,7 @@ public class TestDoubleOrderedMap extends AbstractTestMap {
/**
* The default comparator in double ordered map does not allow null keys.
**/
public boolean isAllowNullKey() {
protected boolean isAllowNullKey() {
return false;
}
@ -112,14 +113,33 @@ public class TestDoubleOrderedMap extends AbstractTestMap {
* The default comparator in double ordered map does not allow null keys,
* and values are keys in this map.
**/
public boolean isAllowNullValue() {
protected boolean isAllowNullValue() {
return false;
}
/**
* Double ordered map does not support duplicate values
**/
public boolean isAllowDuplicateValues() {
protected boolean isAllowDuplicateValues() {
return false;
}
/**
* Change the Map.put() test because it tries put with the same key
* which is invalid in the modified double ordered map contract. (The
* DoubleOrderedMap documentation states that an IllegalArgumentException
* is thrown when a key is tried to be put into the map again. This
* differs from the standard Map contract which would replace the value
* for that key and return it.
*/
protected boolean isPutChangeSupported() {
return false;
}
/**
* setValue() is not supported as it can change the map.
*/
protected boolean isSetValueSupported() {
return false;
}
@ -2832,14 +2852,4 @@ public class TestDoubleOrderedMap extends AbstractTestMap {
junit.textui.TestRunner.run(TestDoubleOrderedMap.class);
}
/**
* Override the Map.put() test because it tries put with the same key
* which is invalid in the modified double ordered map contract. (The
* DoubleOrderedMap documentation states that an IllegalArgumentException
* is thrown when a key is tried to be put into the map again. This
* differs from the standard Map contract which would replace the value
* for that key and return it.
**/
public void testMapPut() {
}
}

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/TestLRUMap.java,v 1.25 2003/10/05 21:17:40 scolebourne Exp $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestLRUMap.java,v 1.26 2003/11/01 18:47:18 scolebourne Exp $
* ====================================================================
*
* The Apache Software License, Version 1.1
@ -68,10 +68,11 @@ import junit.framework.Test;
/**
* Tests LRUMap.
*
* @version $Revision: 1.25 $ $Date: 2003/10/05 21:17:40 $
* @version $Revision: 1.26 $ $Date: 2003/11/01 18:47:18 $
*
* @author James Strachan
* @author Morgan Delagrange
* @author Stephen Colebourne
*/
public class TestLRUMap extends TestSequencedHashMap {
@ -88,11 +89,20 @@ public class TestLRUMap extends TestSequencedHashMap {
junit.textui.TestRunner.main(testCaseName);
}
//-----------------------------------------------------------------------
public Map makeEmptyMap() {
LRUMap map = new LRUMap();
return map;
}
/**
* Override as test uses iterator() and getKey() in combination which doesn't work.
*/
protected String[] ignoredTests() {
return new String[] {"TestLRUMap.bulkTestMapEntrySet.testMapEntrySetIteratorEntry"};
}
//-----------------------------------------------------------------------
public void testRemoveLRU() {
LRUMap map2 = new LRUMap(3);
map2.put(new Integer(1),"foo");