From 37b6aedcd013f7f01502e46f0528c9552dc52d96 Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Sat, 7 May 2005 17:13:40 +0000 Subject: [PATCH] Fix so putAll works with an empty map bug 34686, reported by Marc Lottman git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@169097 13f79535-47bb-0310-9956-ffa450edef68 --- .../collections/map/TransformedMap.java | 3 +++ .../collections/map/AbstractTestMap.java | 27 +++++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/commons/collections/map/TransformedMap.java b/src/java/org/apache/commons/collections/map/TransformedMap.java index 383673f07..36ef470f3 100644 --- a/src/java/org/apache/commons/collections/map/TransformedMap.java +++ b/src/java/org/apache/commons/collections/map/TransformedMap.java @@ -150,6 +150,9 @@ public class TransformedMap * @throws the transformed object */ protected Map transformMap(Map map) { + if (map.isEmpty()) { + return map; + } Map result = new LinkedMap(map.size()); for (Iterator it = map.entrySet().iterator(); it.hasNext(); ) { Map.Entry entry = (Map.Entry) it.next(); diff --git a/src/test/org/apache/commons/collections/map/AbstractTestMap.java b/src/test/org/apache/commons/collections/map/AbstractTestMap.java index a854752b0..4bf996c33 100644 --- a/src/test/org/apache/commons/collections/map/AbstractTestMap.java +++ b/src/test/org/apache/commons/collections/map/AbstractTestMap.java @@ -907,23 +907,46 @@ public abstract class AbstractTestMap extends AbstractTestObject { return; } + // check putAll OK adding empty map to empty map resetEmpty(); + assertEquals(0, map.size()); + map.putAll(new HashMap()); + assertEquals(0, map.size()); + // check putAll OK adding empty map to non-empty map + resetFull(); + int size = map.size(); + map.putAll(new HashMap()); + assertEquals(size, map.size()); + + // check putAll OK adding non-empty map to empty map + resetEmpty(); Map m2 = makeFullMap(); - map.putAll(m2); confirmed.putAll(m2); verify(); + // check putAll OK adding non-empty JDK map to empty map resetEmpty(); - m2 = makeConfirmedMap(); Object[] keys = getSampleKeys(); Object[] values = getSampleValues(); for(int i = 0; i < keys.length; i++) { m2.put(keys[i], values[i]); } + map.putAll(m2); + confirmed.putAll(m2); + verify(); + // check putAll OK adding non-empty JDK map to non-empty map + resetEmpty(); + m2 = makeConfirmedMap(); + map.put(keys[0], values[0]); + confirmed.put(keys[0], values[0]); + verify(); + for(int i = 1; i < keys.length; i++) { + m2.put(keys[i], values[i]); + } map.putAll(m2); confirmed.putAll(m2); verify();