From 5d71ff3d29d63e677dfdaf53e08ecdf086cca665 Mon Sep 17 00:00:00 2001
From: Chen <50514813+dota17@users.noreply.github.com>
Date: Tue, 28 Apr 2020 21:41:39 +0800
Subject: [PATCH] Improve MapUtils with the null checks, add JUnit for it and
add Javadoc for the parameter indent. (#126)
* Improve MapUtils with the null checks, add JUnit for it and add Javadoc for the parameter indent.
* Standardize on American English spelling of 'behavior'.
* Tested the NPE exceptions with the JUnit 5 APIs.
* Fixed the failure of CI with the ParameterResolutionException.
* Remove unused imports.
---
pom.xml | 9 ++
.../apache/commons/collections4/MapUtils.java | 12 +-
.../commons/collections4/MapUtilsTest.java | 142 ++++++++++--------
3 files changed, 98 insertions(+), 65 deletions(-)
diff --git a/pom.xml b/pom.xml
index e632c7c9d..1da75352c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -441,9 +441,18 @@
Claude Warren
+
+ Chen Guoping
+
+
+ org.junit.jupiter
+ junit-jupiter-api
+ 5.6.0
+ test
+ org.junit.jupiterjunit-jupiter-engine
diff --git a/src/main/java/org/apache/commons/collections4/MapUtils.java b/src/main/java/org/apache/commons/collections4/MapUtils.java
index e73474e3e..ee66d238a 100644
--- a/src/main/java/org/apache/commons/collections4/MapUtils.java
+++ b/src/main/java/org/apache/commons/collections4/MapUtils.java
@@ -1187,11 +1187,12 @@ public class MapUtils {
*
* @param the key type
* @param the value type
- * @param map the map to invert, may not be null
+ * @param map the map to invert, must not be null
* @return a new HashMap containing the inverted data
* @throws NullPointerException if the map is null
*/
public static Map invertMap(final Map map) {
+ Objects.requireNonNull(map, "map");
final Map out = new HashMap<>(map.size());
for (final Entry entry : map.entrySet()) {
out.put(entry.getValue(), entry.getKey());
@@ -1614,6 +1615,7 @@ public class MapUtils {
* Writes indentation to the given stream.
*
* @param out the stream to indent
+ * @param indent the index of the indentation
*/
private static void printIndent(final PrintStream out, final int indent) {
for (int i = 0; i < indent; i++) {
@@ -1722,13 +1724,14 @@ public class MapUtils {
*
*
* @param the key type
- * @param map the map to add to, may not be null
+ * @param map the map to add to, must not be null
* @param key the key
* @param value the value, null converted to ""
* @throws NullPointerException if the map is null
*/
public static void safeAddToMap(final Map super K, Object> map, final K key, final Object value)
throws NullPointerException {
+ Objects.requireNonNull(map, "map");
map.put(key, value == null ? "" : value);
}
@@ -1808,11 +1811,12 @@ public class MapUtils {
/**
* Creates a new HashMap using data copied from a ResourceBundle.
*
- * @param resourceBundle the resource bundle to convert, may not be null
- * @return the hashmap containing the data
+ * @param resourceBundle the resource bundle to convert, must not be null
+ * @return the HashMap containing the data
* @throws NullPointerException if the bundle is null
*/
public static Map toMap(final ResourceBundle resourceBundle) {
+ Objects.requireNonNull(resourceBundle, "resourceBundle");
final Enumeration enumeration = resourceBundle.getKeys();
final Map map = new HashMap<>();
diff --git a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java
index 17428fe73..1703bcf97 100644
--- a/src/test/java/org/apache/commons/collections4/MapUtilsTest.java
+++ b/src/test/java/org/apache/commons/collections4/MapUtilsTest.java
@@ -16,13 +16,14 @@
*/
package org.apache.commons.collections4;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotSame;
-import static org.junit.Assert.assertSame;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
@@ -36,7 +37,6 @@ import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListResourceBundle;
-import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
@@ -44,25 +44,21 @@ 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;
import org.apache.commons.collections4.keyvalue.DefaultKeyValue;
import org.apache.commons.collections4.keyvalue.DefaultMapEntry;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.collections4.map.LazyMap;
import org.apache.commons.collections4.map.MultiValueMap;
import org.apache.commons.collections4.map.PredicatedMap;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
/**
* Tests for MapUtils.
*/
@SuppressWarnings("boxing")
-public class MapUtilsTest extends AbstractAvailableLocalesTest {
+public class MapUtilsTest {
private static final String THREE = "Three";
-
- public MapUtilsTest(final Locale locale) {
- super(locale);
- }
+ private static final String TWO = "Two";
public Predicate