From 9a24ed01f79153d1e0ed85abf15547c76f2f0491 Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Mon, 14 Feb 2011 21:21:55 +0000 Subject: [PATCH] address some testing TODOs git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@1070673 13f79535-47bb-0310-9956-ffa450edef68 --- .../collections/map/AbstractTestMap.java | 264 +++++++++++++++++- 1 file changed, 259 insertions(+), 5 deletions(-) diff --git a/src/test/org/apache/commons/collections/map/AbstractTestMap.java b/src/test/org/apache/commons/collections/map/AbstractTestMap.java index 07f04196d..935e9a164 100644 --- a/src/test/org/apache/commons/collections/map/AbstractTestMap.java +++ b/src/test/org/apache/commons/collections/map/AbstractTestMap.java @@ -18,17 +18,22 @@ package org.apache.commons.collections.map; import java.io.Serializable; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; -import java.util.Set; import java.util.Map.Entry; +import java.util.Set; import org.apache.commons.collections.AbstractTestObject; import org.apache.commons.collections.BulkTest; +import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.collection.AbstractTestCollection; +import org.apache.commons.collections.keyvalue.DefaultMapEntry; import org.apache.commons.collections.set.AbstractTestSet; /** @@ -1207,6 +1212,88 @@ public abstract class AbstractTestMap extends AbstractTestObject { } } + /** + * Tests values.removeAll. + */ + public void testValuesRemoveAll() { + resetFull(); + Collection values = getMap().values(); + List sampleValuesAsList = Arrays.asList(getSampleValues()); + if (!values.equals(sampleValuesAsList)) { + return; + } + try { + assertFalse(values.removeAll(Collections. emptySet())); + } catch (UnsupportedOperationException e) { + // if values.removeAll is unsupported, just skip this test + return; + } + assertEquals(sampleValuesAsList.size(), getMap().size()); + try { + assertTrue(values.removeAll(sampleValuesAsList)); + } catch (UnsupportedOperationException e) { + // if values.removeAll is unsupported, just skip this test + return; + } + assertTrue(getMap().isEmpty()); + } + + /** + * Test values.retainAll. + */ + public void testValuesRetainAll() { + resetFull(); + Collection values = getMap().values(); + List sampleValuesAsList = Arrays.asList(getSampleValues()); + if (!values.equals(sampleValuesAsList)) { + return; + } + try { + assertFalse(values.retainAll(sampleValuesAsList)); + } catch (UnsupportedOperationException e) { + // if values.retainAll is unsupported, just skip this test + return; + } + assertEquals(sampleValuesAsList.size(), getMap().size()); + try { + assertTrue(values.retainAll(Collections. emptySet())); + } catch (UnsupportedOperationException e) { + // if values.retainAll is unsupported, just skip this test + return; + } + assertTrue(getMap().isEmpty()); + } + + /** + * Verifies that values.iterator.remove changes the underlying map. + */ + public void testValuesIteratorRemoveChangesMap() { + resetFull(); + List sampleValuesAsList = Arrays.asList(getSampleValues()); + Map cardinality = CollectionUtils.getCardinalityMap(sampleValuesAsList); + Collection values = getMap().values(); + for (Iterator iter = values.iterator(); iter.hasNext();) { + V value = iter.next(); + Integer count = cardinality.get(value); + if (count == null) { + return; + } + try { + iter.remove(); + cardinality.put(value, --count); + } catch (UnsupportedOperationException e) { + // if values.iterator.remove is unsupported, just skip this test + return; + } + boolean expected = count > 0; + StringBuilder msg = new StringBuilder("Value should "); + msg.append(expected ? "yet " : "no longer "); + msg.append("be present in the underlying map"); + assertEquals(msg.toString(), expected, getMap().containsValue(value)); + } + assertTrue(getMap().isEmpty()); + } + /** * Tests that the {@link Map#keySet} set is backed by * the underlying map by removing from the keySet set @@ -1229,11 +1316,178 @@ public abstract class AbstractTestMap extends AbstractTestObject { } } - // TODO: Need: - // testValuesRemovedFromEntrySetAreRemovedFromMap - // same for EntrySet/KeySet/values's - // Iterator.remove, removeAll, retainAll + /** + * Test keySet.removeAll. + */ + public void testKeySetRemoveAll() { + resetFull(); + Set keys = getMap().keySet(); + List sampleKeysAsList = Arrays.asList(getSampleKeys()); + if (!keys.equals(sampleKeysAsList)) { + return; + } + try { + assertFalse(keys.removeAll(Collections. emptySet())); + } catch (UnsupportedOperationException e) { + return; + } + assertEquals(sampleKeysAsList, keys); + try { + assertTrue(keys.removeAll(sampleKeysAsList)); + } catch (UnsupportedOperationException e) { + return; + } + assertTrue(getMap().isEmpty()); + } + /** + * Test keySet.retainAll. + */ + public void testKeySetRetainAll() { + resetFull(); + Set keys = getMap().keySet(); + List sampleKeysAsList = Arrays.asList(getSampleKeys()); + if (!keys.equals(sampleKeysAsList)) { + return; + } + try { + assertFalse(keys.retainAll(sampleKeysAsList)); + } catch (UnsupportedOperationException e) { + return; + } + assertEquals(sampleKeysAsList, keys); + try { + assertTrue(keys.retainAll(Collections. emptySet())); + } catch (UnsupportedOperationException e) { + return; + } + assertTrue(getMap().isEmpty()); + } + + /** + * Verify that keySet.iterator.remove changes the underlying map. + */ + public void testKeySetIteratorRemoveChangesMap() { + resetFull(); + for (Iterator iter = getMap().keySet().iterator(); iter.hasNext();) { + K key = iter.next(); + try { + iter.remove(); + } catch (UnsupportedOperationException e) { + return; + } + assertFalse(getMap().containsKey(key)); + } + } + + /** + * Tests that the {@link Map#entrySet} set is backed by + * the underlying map by removing from the entrySet set + * and testing if the entry was removed from the map. + */ + public void testEntrySetRemoveChangesMap() { + resetFull(); + K[] sampleKeys = getSampleKeys(); + V[] sampleValues = getSampleValues(); + Set> entrySet = getMap().entrySet(); + for (int i = 0; i < sampleKeys.length; i++) { + try { + entrySet.remove(new DefaultMapEntry(sampleKeys[i], sampleValues[i])); + } catch (UnsupportedOperationException e) { + // if entrySet removal is unsupported, just skip this test + return; + } + assertTrue( + "Entry should have been removed from the underlying map.", + !getMap().containsKey(sampleKeys[i])); + } + } + + /** + * Test entrySet.removeAll. + */ + public void testEntrySetRemoveAll() { + resetFull(); + K[] sampleKeys = getSampleKeys(); + V[] sampleValues = getSampleValues(); + //verify map looks as expected: + for (int i = 0; i < sampleKeys.length; i++) { + if (!getMap().containsKey(sampleKeys[i])) { + return; + } + V value = sampleValues[i]; + V test = getMap().get(sampleKeys[i]); + if (value == test || value != null && value.equals(test)) { + continue; + } + return; + } + Set> entrySet = getMap().entrySet(); + HashSet> comparisonSet = new HashSet>(entrySet); + try { + assertFalse(entrySet.removeAll(Collections.> emptySet())); + } catch (UnsupportedOperationException e) { + return; + } + assertEquals(sampleKeys.length, getMap().size()); + try { + assertTrue(entrySet.removeAll(comparisonSet)); + } catch (UnsupportedOperationException e) { + return; + } + assertTrue(getMap().isEmpty()); + } + + /** + * Test entrySet.retainAll. + */ + public void testEntrySetRetainAll() { + resetFull(); + K[] sampleKeys = getSampleKeys(); + V[] sampleValues = getSampleValues(); + //verify map looks as expected: + for (int i = 0; i < sampleKeys.length; i++) { + if (!getMap().containsKey(sampleKeys[i])) { + return; + } + V value = sampleValues[i]; + V test = getMap().get(sampleKeys[i]); + if (value == test || value != null && value.equals(test)) { + continue; + } + return; + } + Set> entrySet = getMap().entrySet(); + HashSet> comparisonSet = new HashSet>(entrySet); + try { + assertFalse(entrySet.retainAll(comparisonSet)); + } catch (UnsupportedOperationException e) { + return; + } + assertEquals(sampleKeys.length, getMap().size()); + try { + assertTrue(entrySet.retainAll(Collections.> emptySet())); + } catch (UnsupportedOperationException e) { + return; + } + assertTrue(getMap().isEmpty()); + } + + /** + * Verify that entrySet.iterator.remove changes the underlying map. + */ + public void testEntrySetIteratorRemoveChangesMap() { + resetFull(); + for (Iterator> iter = getMap().entrySet().iterator(); iter.hasNext();) { + K key = iter.next().getKey(); + try { + iter.remove(); + } catch (UnsupportedOperationException e) { + return; + } + assertFalse(getMap().containsKey(key)); + } + } /** * Utility methods to create an array of Map.Entry objects