diff --git a/libraries/pom.xml b/libraries/pom.xml
index a3b78f1695..94ca015ae6 100644
--- a/libraries/pom.xml
+++ b/libraries/pom.xml
@@ -59,7 +59,8 @@
${basedir}/datanucleus.properties
${basedir}/log4j.properties
true
- false
+ false
+
@@ -372,6 +373,12 @@
${awaitility.version}
test
+
+ org.hamcrest
+ java-hamcrest
+ ${org.hamcrest.version}
+ test
+
0.7.0
@@ -404,6 +411,7 @@
0.10
3.5.0
3.0.0
+ 2.0.0.0
diff --git a/libraries/src/test/java/com/baeldung/commons/collections/MapUtilsTest.java b/libraries/src/test/java/com/baeldung/commons/collections/MapUtilsTest.java
new file mode 100644
index 0000000000..4954f2574a
--- /dev/null
+++ b/libraries/src/test/java/com/baeldung/commons/collections/MapUtilsTest.java
@@ -0,0 +1,147 @@
+package com.baeldung.commons.collections;
+
+import org.apache.commons.collections4.MapIterator;
+import org.apache.commons.collections4.MapUtils;
+import org.apache.commons.collections4.PredicateUtils;
+import org.apache.commons.collections4.TransformerUtils;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.collection.IsMapContaining.hasEntry;
+import static org.hamcrest.collection.IsMapWithSize.aMapWithSize;
+import static org.hamcrest.collection.IsMapWithSize.anEmptyMap;
+import static org.junit.Assert.*;
+
+public class MapUtilsTest {
+
+ private String[][] color2DArray = new String[][] {
+ {"RED", "#FF0000"},
+ {"GREEN", "#00FF00"},
+ {"BLUE", "#0000FF"}
+ };
+ private String[] color1DArray = new String[] {
+ "RED", "#FF0000",
+ "GREEN", "#00FF00",
+ "BLUE", "#0000FF"
+ };
+ private Map colorMap;
+
+ @Before
+ public void createMap() {
+ this.colorMap = MapUtils.putAll(new HashMap(), this.color2DArray);
+ }
+
+ @Test
+ public void whenCreateMapFrom2DArray_theMapIsCreated() {
+ this.colorMap = MapUtils.putAll(new HashMap(), this.color2DArray);
+
+ assertThat(this.colorMap, is(aMapWithSize(this.color2DArray.length)));
+
+ assertThat(this.colorMap, hasEntry("RED", "#FF0000"));
+ assertThat(this.colorMap, hasEntry("GREEN", "#00FF00"));
+ assertThat(this.colorMap, hasEntry("BLUE", "#0000FF"));
+ }
+
+ @Test
+ public void whenCreateMapFrom1DArray_theMapIsCreated() {
+ this.colorMap = MapUtils.putAll(new HashMap(), this.color1DArray);
+
+ assertThat(this.colorMap, is(aMapWithSize(this.color1DArray.length / 2)));
+
+ assertThat(this.colorMap, hasEntry("RED", "#FF0000"));
+ assertThat(this.colorMap, hasEntry("GREEN", "#00FF00"));
+ assertThat(this.colorMap, hasEntry("BLUE", "#0000FF"));
+ }
+
+ @Test
+ public void whenVerbosePrintMap_thenMustPrintFormattedMap() {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ PrintStream outPrint = new PrintStream(out);
+
+ outPrint.println("Optional Label = ");
+ outPrint.println("{");
+ outPrint.println(" RED = #FF0000");
+ outPrint.println(" BLUE = #0000FF");
+ outPrint.println(" GREEN = #00FF00");
+ outPrint.println("}");
+
+ String expectedOut = out.toString();
+
+ out.reset();
+
+ MapUtils.verbosePrint(outPrint, "Optional Label", this.colorMap);
+ assertEquals(expectedOut, out.toString());
+ }
+
+ @Test
+ public void whenGetKeyNotPresent_thenMustReturnDefaultValue() {
+ String defaultColorStr = "COLOR_NOT_FOUND";
+ String color = MapUtils.getString(this.colorMap, "BLACK", defaultColorStr);
+
+ assertEquals(color, defaultColorStr);
+ }
+
+ @Test
+ public void whenGetOnNullMap_thenMustReturnDefaultValue() {
+ String defaultColorStr = "COLOR_NOT_FOUND";
+ String color = MapUtils.getString(null, "RED", defaultColorStr);
+
+ assertEquals(color, defaultColorStr);
+ }
+
+ @Test
+ public void whenInvertMap_thenMustReturnInvertedMap() {
+ Map invColorMap = MapUtils.invertMap(this.colorMap);
+ assertEquals(this.colorMap.size(), invColorMap.size());
+
+ MapIterator itColorMap
+ = MapUtils.iterableMap(this.colorMap).mapIterator();
+
+ while (itColorMap.hasNext()) {
+ String colorMapKey = itColorMap.next();
+ String colorMapValue = itColorMap.getValue();
+
+ String invColorMapValue = MapUtils.getString(invColorMap, colorMapValue);
+
+ assertTrue(invColorMapValue.equals(colorMapKey));
+ }
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void whenCreateFixedSizedMapAndAdd_thenMustThrowException() {
+ Map rgbMap = MapUtils.fixedSizeMap(MapUtils.putAll(
+ new HashMap(),
+ this.color1DArray));
+
+ rgbMap.put("ORANGE", "#FFA500");
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void whenAddDuplicateToUniqueValuesPredicateMap_thenMustThrowException() {
+ Map uniqValuesMap
+ = MapUtils.predicatedMap(this.colorMap, null, PredicateUtils.uniquePredicate());
+
+ uniqValuesMap.put("NEW_RED", "#FF0000");
+ }
+
+ @Test
+ public void whenCreateLazyMap_theMapIsCreated() {
+ Map intStrMap = MapUtils.lazyMap(
+ new HashMap(),
+ TransformerUtils.stringValueTransformer());
+
+ assertThat(intStrMap, is(anEmptyMap()));
+
+ intStrMap.get(1);
+ intStrMap.get(2);
+ intStrMap.get(3);
+
+ assertThat(intStrMap, is(aMapWithSize(3)));
+ }
+}