From 5e313d14b2a10ce0aea529ac4db50e183f0b1e49 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sat, 28 Oct 2006 12:27:01 +0000 Subject: [PATCH] COLLECTIONS-228 - MultiValueMap put and putAll do not return the correct values git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@468684 13f79535-47bb-0310-9956-ffa450edef68 --- RELEASE-NOTES.html | 1 + .../collections/map/MultiValueMap.java | 17 +++++------ .../collections/map/TestMultiValueMap.java | 28 +++++++++++++++---- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/RELEASE-NOTES.html b/RELEASE-NOTES.html index 645b52368..abe473d08 100644 --- a/RELEASE-NOTES.html +++ b/RELEASE-NOTES.html @@ -57,6 +57,7 @@ If this causes major headaches to anyone please contact commons-dev at jakarta.a
  • Flat3Map - Fix setValue in MapIterator and EntrySetIterator to work correctly [COLLECTIONS-217]
  • ExtendedProperties - Include property name had confused static/instance semantics [COLLECTIONS-214]
  • CollectionUtils - Fix removeAll() method which was completely broken [COLLECTIONS-219]
  • +
  • MultiValueMap - Fix put() and putAll() to return correct results [COLLECTIONS-228]
  • JAVADOC

    diff --git a/src/java/org/apache/commons/collections/map/MultiValueMap.java b/src/java/org/apache/commons/collections/map/MultiValueMap.java index 81e86be93..8bddee9fc 100644 --- a/src/java/org/apache/commons/collections/map/MultiValueMap.java +++ b/src/java/org/apache/commons/collections/map/MultiValueMap.java @@ -205,12 +205,12 @@ public class MultiValueMap extends AbstractMapDecorator implements MultiMap { boolean result = false; Collection coll = getCollection(key); if (coll == null) { - coll = createCollection(1); - result = coll.add(value); + coll = createCollection(1); // might produce a non-empty collection + coll.add(value); if (coll.size() > 0) { // only add if non-zero size to maintain class state getMap().put(key, coll); - result = false; + result = true; // map definitely changed } } else { result = coll.add(value); @@ -307,19 +307,20 @@ public class MultiValueMap extends AbstractMapDecorator implements MultiMap { if (values == null || values.size() == 0) { return false; } + boolean result = false; Collection coll = getCollection(key); if (coll == null) { - coll = createCollection(values.size()); - boolean result = coll.addAll(values); + coll = createCollection(values.size()); // might produce a non-empty collection + coll.addAll(values); if (coll.size() > 0) { // only add if non-zero size to maintain class state getMap().put(key, coll); - result = false; + result = true; // map definitely changed } - return result; } else { - return coll.addAll(values); + result = coll.addAll(values); } + return result; } /** diff --git a/src/test/org/apache/commons/collections/map/TestMultiValueMap.java b/src/test/org/apache/commons/collections/map/TestMultiValueMap.java index d4abcf9a8..3645ce9c4 100644 --- a/src/test/org/apache/commons/collections/map/TestMultiValueMap.java +++ b/src/test/org/apache/commons/collections/map/TestMultiValueMap.java @@ -30,7 +30,6 @@ import junit.framework.TestSuite; import org.apache.commons.collections.IteratorUtils; import org.apache.commons.collections.MultiMap; -import org.apache.commons.collections.TestMultiHashMap; /** * TestMultiValueMap. @@ -46,11 +45,11 @@ public class TestMultiValueMap extends TestCase { } public static Test suite() { - return new TestSuite(TestMultiHashMap.class); + return new TestSuite(TestMultiValueMap.class); } public static void main(String args[]) { - String[] testCaseName = { TestMultiHashMap.class.getName()}; + String[] testCaseName = { TestMultiValueMap.class.getName()}; junit.textui.TestRunner.main(testCaseName); } @@ -201,9 +200,9 @@ public class TestMultiValueMap extends TestCase { map.put("B", "BC"); assertEquals(2, map.size()); map.remove("A"); - assertEquals(2, map.size()); + assertEquals(1, map.size()); map.remove("B", "BC"); - assertEquals(2, map.size()); + assertEquals(1, map.size()); } public void testSize_Key() { @@ -249,6 +248,25 @@ public class TestMultiValueMap extends TestCase { assertEquals(false, map.containsValue("A", "AB")); } + public void testPutWithList() { + MultiValueMap test = MultiValueMap.decorate(new HashMap(), ArrayList.class); + assertEquals("a", test.put("A", "a")); + assertEquals("b", test.put("A", "b")); + assertEquals(1, test.size()); + assertEquals(2, test.size("A")); + assertEquals(2, test.totalSize()); + } + + public void testPutWithSet() { + MultiValueMap test = MultiValueMap.decorate(new HashMap(), HashSet.class); + assertEquals("a", test.put("A", "a")); + assertEquals("b", test.put("A", "b")); + assertEquals(null, test.put("A", "a")); + assertEquals(1, test.size()); + assertEquals(2, test.size("A")); + assertEquals(2, test.totalSize()); + } + public void testPutAll_Map1() { MultiMap original = new MultiValueMap(); original.put("key", "object1");