From 9beb1e0b115cb56b02a3baee4fdb2cdee2bdddb2 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sat, 18 Apr 2020 17:29:54 +0530 Subject: [PATCH 1/2] [COLLECTIONS-760]: Add tests for MapUtils. --- .../commons/collections4/MapUtilsTest.java | 111 +++++++++++++++++- 1 file changed, 105 insertions(+), 6 deletions(-) diff --git a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java index c131fa53d..2e2f806fe 100644 --- a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java @@ -28,6 +28,7 @@ import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.text.DecimalFormat; import java.text.NumberFormat; +import java.util.AbstractMap; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -40,6 +41,7 @@ import java.util.Map; import java.util.Properties; import java.util.ResourceBundle; import java.util.Set; +import java.util.SortedMap; import java.util.TreeMap; import org.apache.commons.collections4.collection.TransformedCollectionTest; import org.apache.commons.collections4.junit.AbstractAvailableLocalesTest; @@ -56,6 +58,7 @@ import org.junit.Test; */ @SuppressWarnings("boxing") public class MapUtilsTest extends AbstractAvailableLocalesTest { + private static final String THREE = "Three"; public MapUtilsTest(final Locale locale) { super(locale); @@ -891,6 +894,45 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest { assertSame(iMap, MapUtils.iterableMap(iMap)); } + @Test + public void testLazyMap() { + final Map lazyMap = MapUtils.lazyMap(new HashMap<>(), () -> 1); + lazyMap.put("Two", 2); + + assertEquals(Integer.valueOf(2), lazyMap.get("Two")); + assertEquals(Integer.valueOf(1), lazyMap.get(THREE)); + } + + @Test + public void testLazySortedMapFactory() { + final SortedMap lazySortedMap = MapUtils.lazySortedMap(new TreeMap<>(), () -> 1); + lazySortedMap.put("Two", 2); + + assertEquals(Integer.valueOf(2), lazySortedMap.get("Two")); + assertEquals(Integer.valueOf(1), lazySortedMap.get(THREE)); + + final Set> entrySet = new HashSet<>(); + entrySet.add(new AbstractMap.SimpleEntry<>(THREE, 1)); + entrySet.add(new AbstractMap.SimpleEntry<>("Two", 2)); + + assertEquals(entrySet, lazySortedMap.entrySet()); + } + + @Test + public void testLazySortedMapTransformer() { + final SortedMap lazySortedMap = MapUtils.lazySortedMap(new TreeMap<>(), s -> 1); + lazySortedMap.put("Two", 2); + + assertEquals(Integer.valueOf(2), lazySortedMap.get("Two")); + assertEquals(Integer.valueOf(1), lazySortedMap.get(THREE)); + + final Set> entrySet = new HashSet<>(); + entrySet.add(new AbstractMap.SimpleEntry<>(THREE, 1)); + entrySet.add(new AbstractMap.SimpleEntry<>("Two", 2)); + + assertEquals(entrySet, lazySortedMap.entrySet()); + } + @Test public void testSize0() { assertEquals(0, MapUtils.size(new HashMap<>())); @@ -931,6 +973,62 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest { assertEquals(out.size(), 0); } + @Test + public void testTransformedMap() { + final Map map = new HashMap<>(); + + final Map transformedMap = MapUtils.transformedMap(map, i -> i + 1, i -> i + 10); + transformedMap.put(1L, 100L); + + final Set> entrySet = new HashSet<>(); + entrySet.add(new AbstractMap.SimpleEntry<>(2L, 110L)); + + assertEquals(entrySet, transformedMap.entrySet()); + } + + @Test + public void testTransformedSortedMap() { + final SortedMap sortedMap = new TreeMap<>(); + + final SortedMap transformedSortedMap = MapUtils.transformedSortedMap(sortedMap, i -> i + 1, i -> i + 10); + transformedSortedMap.put(2L, 200L); + transformedSortedMap.put(1L, 100L); + + final Set> entrySet = new HashSet<>(); + entrySet.add(new AbstractMap.SimpleEntry<>(2L, 110L)); + entrySet.add(new AbstractMap.SimpleEntry<>(3L, 210L)); + + assertEquals(entrySet, transformedSortedMap.entrySet()); + } + + @Test(expected = UnsupportedOperationException.class) + public void testUnmodifiableMap() { + MapUtils.unmodifiableMap(new HashMap<>()).clear(); + } + + @Test(expected = UnsupportedOperationException.class) + public void testUnmodifiableSortedMap() { + MapUtils.unmodifiableSortedMap(new TreeMap<>()).clear(); + } + + @Test(expected = IllegalArgumentException.class) + public void testFixedSizeMap() { + MapUtils.fixedSizeMap(new HashMap<>()).put(new Object(), new Object()); + } + + @Test(expected = IllegalArgumentException.class) + public void testFixedSizeSortedMap() { + MapUtils.fixedSizeSortedMap(new TreeMap()).put(1L, 1L); + } + + @Test + public void testGetNumberValueWithInvalidString() { + final Map map = new HashMap<>(); + map.put("key", "one"); + + assertNull(MapUtils.getNumber(map, "key")); + } + @Test public void testgetDoubleValue() { final Map in = new HashMap<>(); @@ -992,15 +1090,16 @@ public class MapUtilsTest extends AbstractAvailableLocalesTest { assertEquals(2.0, MapUtils.getLongValue(in, "key", 0L), 0); assertEquals(2.0, MapUtils.getLongValue(in, "key"), 0); assertEquals(1, MapUtils.getLongValue(in, "noKey", 1L), 0); - assertEquals(1, MapUtils.getLongValue(in, "noKey", key -> { - return 1L; - }), 0); + assertEquals(1, MapUtils.getLongValue(in, "noKey", key -> 1L), 0); assertEquals(0, MapUtils.getLongValue(in, "noKey"), 0); assertEquals(2.0, MapUtils.getLong(in, "key", 0L), 0); assertEquals(1, MapUtils.getLong(in, "noKey", 1L), 0); - assertEquals(1, MapUtils.getLong(in, "noKey", key -> { - return 1L; - }), 0); + assertEquals(1, MapUtils.getLong(in, "noKey", key -> 1L), 0); + + final Map in1 = new HashMap<>(); + in1.put("key", 2); + + assertEquals(Long.valueOf(2), MapUtils.getLong(in1, "key")); final Map inStr = new HashMap<>(); inStr.put("str1", "2"); From e3aa7625b3914b17b091044847260c8a81ea8135 Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Sun, 19 Apr 2020 11:36:35 +1200 Subject: [PATCH 2/2] [COLLECTIONS-760]: add changelog --- src/changes/changes.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b5fb9853c..ee57d56b0 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -21,6 +21,9 @@ + + Add tests for MapUtils + Return 0 immediately if the given iterable is null in IterableUtils#size. Update tests.