scolebourne 2003/11/01 10:47:18 Modified: collections/src/test/org/apache/commons/collections AbstractTestMap.java TestLRUMap.java AbstractTestSortedBidiMap.java AbstractTestBidiMap.java TestDoubleOrderedMap.java TestBeanMap.java collections/src/java/org/apache/commons/collections DualTreeBidiMap.java AbstractDualBidiMap.java Log: Add extra tests for maps Make the tests pass Revision Changes Path 1.10 +113 -9 jakarta-commons/collections/src/test/org/apache/commons/collections/AbstractTestMap.java Index: AbstractTestMap.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/AbstractTestMap.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- AbstractTestMap.java 31 Oct 2003 01:24:32 -0000 1.9 +++ AbstractTestMap.java 1 Nov 2003 18:47:18 -0000 1.10 @@ -98,6 +98,7 @@ * <ul> * <li> [EMAIL PROTECTED] #isPutAddSupported()} * <li> [EMAIL PROTECTED] #isPutChangeSupported()} + * <li> [EMAIL PROTECTED] #isSetValueSupported()} * <li> [EMAIL PROTECTED] #isRemoveSupported()} * <li> [EMAIL PROTECTED] #isAllowDuplicateValues()} * <li> [EMAIL PROTECTED] #isAllowNullKey()} @@ -218,6 +219,19 @@ /** * Returns true if the maps produced by * [EMAIL PROTECTED] #makeEmptyMap()} and [EMAIL PROTECTED] #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 + * [EMAIL PROTECTED] #makeEmptyMap()} and [EMAIL PROTECTED] #makeFullMap()} * support the <code>remove</code> and <code>clear</code> operations. * <p> * Default implementation returns true. @@ -781,8 +795,10 @@ } } 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 @@ 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 @@ 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 @@ return new TestMapKeySet(); } - class TestMapKeySet extends AbstractTestSet { + public class TestMapKeySet extends AbstractTestSet { public TestMapKeySet() { super(""); } @@ -1226,7 +1326,7 @@ return new TestMapValues(); } - class TestMapValues extends AbstractTestCollection { + public class TestMapValues extends AbstractTestCollection { public TestMapValues() { super(""); } @@ -1415,8 +1515,12 @@ 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()); 1.26 +12 -2 jakarta-commons/collections/src/test/org/apache/commons/collections/TestLRUMap.java Index: TestLRUMap.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestLRUMap.java,v retrieving revision 1.25 retrieving revision 1.26 diff -u -r1.25 -r1.26 --- TestLRUMap.java 5 Oct 2003 21:17:40 -0000 1.25 +++ TestLRUMap.java 1 Nov 2003 18:47:18 -0000 1.26 @@ -72,6 +72,7 @@ * * @author James Strachan * @author Morgan Delagrange + * @author Stephen Colebourne */ public class TestLRUMap extends TestSequencedHashMap { @@ -88,11 +89,20 @@ 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"); 1.2 +289 -44 jakarta-commons/collections/src/test/org/apache/commons/collections/AbstractTestSortedBidiMap.java Index: AbstractTestSortedBidiMap.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/AbstractTestSortedBidiMap.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- AbstractTestSortedBidiMap.java 31 Oct 2003 01:26:25 -0000 1.1 +++ AbstractTestSortedBidiMap.java 1 Nov 2003 18:47:18 -0000 1.2 @@ -144,6 +144,7 @@ } //----------------------------------------------------------------------- + //----------------------------------------------------------------------- public void testBidiHeadMapContains() { // extra test as other tests get complex SortedBidiMap sm = (SortedBidiMap) makeFullMap(); @@ -167,6 +168,49 @@ } //----------------------------------------------------------------------- + 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 SortedBidiMap sm = (SortedBidiMap) makeFullMap(); @@ -175,30 +219,35 @@ 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 @@ // 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 @@ 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,41 +425,136 @@ 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 SortedBidiMap sm = (SortedBidiMap) makeFullMap(); @@ -335,6 +566,7 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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())); 1.4 +159 -9 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.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- AbstractTestBidiMap.java 31 Oct 2003 01:25:24 -0000 1.3 +++ AbstractTestBidiMap.java 1 Nov 2003 18:47:18 -0000 1.4 @@ -57,10 +57,12 @@ */ 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 [EMAIL PROTECTED] BidiMap} methods and contracts. @@ -184,6 +186,16 @@ 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 @@ !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 @@ protected boolean isPutChangeSupported() { return main.isPutChangeSupported(); } + protected boolean isSetValueSupported() { + return main.isSetValueSupported(); + } protected boolean isRemoveSupported() { return main.isRemoveSupported(); } @@ -468,29 +549,98 @@ //----------------------------------------------------------------------- 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 key = it.next(); + Object key1 = it.next(); - if (isPutChangeSupported() == false) { + if (isSetValueSupported() == false) { try { - it.setValue(getOtherValues()[0]); + it.setValue(newValue1); } catch (UnsupportedOperationException ex) { } return; } - it.setValue(getOtherValues()[0]); - confirmed.put(key, getOtherValues()[0]); - assertEquals(getOtherValues()[0], bidi.get(key)); + 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(); + + 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(); 1.11 +26 -16 jakarta-commons/collections/src/test/org/apache/commons/collections/TestDoubleOrderedMap.java Index: TestDoubleOrderedMap.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestDoubleOrderedMap.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- TestDoubleOrderedMap.java 7 Oct 2003 22:20:57 -0000 1.10 +++ TestDoubleOrderedMap.java 1 Nov 2003 18:47:18 -0000 1.11 @@ -79,7 +79,8 @@ * * @version $Revision$ $Date$ * - * @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 @@ /** * The default comparator in double ordered map does not allow null keys. **/ - public boolean isAllowNullKey() { + protected boolean isAllowNullKey() { return false; } @@ -112,14 +113,33 @@ * 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 @@ 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() { - } } 1.14 +16 -2 jakarta-commons/collections/src/test/org/apache/commons/collections/TestBeanMap.java Index: TestBeanMap.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/test/org/apache/commons/collections/TestBeanMap.java,v retrieving revision 1.13 retrieving revision 1.14 diff -u -r1.13 -r1.14 --- TestBeanMap.java 7 Oct 2003 22:20:57 -0000 1.13 +++ TestBeanMap.java 1 Nov 2003 18:47:18 -0000 1.14 @@ -62,6 +62,7 @@ import java.util.Map; import junit.framework.Test; +import junit.textui.TestRunner; /** * Test cases for BeanMap @@ -69,12 +70,17 @@ * @version $Revision$ $Date$ * * @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); @@ -250,6 +256,14 @@ null, }; return values; + } + + /** + * Values is a dead copy in BeanMap, so refresh each time. + */ + protected void verifyValues() { + values = map.values(); + super.verifyValues(); } /** 1.2 +14 -2 jakarta-commons/collections/src/java/org/apache/commons/collections/DualTreeBidiMap.java Index: DualTreeBidiMap.java =================================================================== RCS file: /home/cvs/jakarta-commons/collections/src/java/org/apache/commons/collections/DualTreeBidiMap.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- DualTreeBidiMap.java 31 Oct 2003 01:26:25 -0000 1.1 +++ DualTreeBidiMap.java 1 Nov 2003 18:47:18 -0000 1.2 @@ -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; @@ -165,6 +166,9 @@ 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; } @@ -172,6 +176,14 @@ public boolean containsValue(Object value) { // override as default implementation jumps to [1] 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) { 1.6 +13 -14 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.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- AbstractDualBidiMap.java 31 Oct 2003 01:26:25 -0000 1.5 +++ AbstractDualBidiMap.java 1 Nov 2003 18:47:18 -0000 1.6 @@ -510,12 +510,13 @@ } 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 @@ 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); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]