[VALIDATOR-492] ValidatorUtils.copyFastHashMap is broken
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 5f935f0..1a6409a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -65,6 +65,8 @@
<body>
<release version="1.8.1" date="202Y-MM-DD" description="Maintenance and bug fix release.">
+ <!-- FIX -->
+ <action type="fix" issue="VALIDATOR-492" dev="ggregory" due-to="Tobias Wildgruber, Gary Gregory">ValidatorUtils.copyFastHashMap is broken.</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot">Bump org.apache.commons:commons-parent from 65 to 67.</action>
</release>
diff --git a/src/main/java/org/apache/commons/validator/util/ValidatorUtils.java b/src/main/java/org/apache/commons/validator/util/ValidatorUtils.java
index cc8a2b9..552b3da 100644
--- a/src/main/java/org/apache/commons/validator/util/ValidatorUtils.java
+++ b/src/main/java/org/apache/commons/validator/util/ValidatorUtils.java
@@ -18,7 +18,9 @@
import java.util.Collection;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.Map;
+import java.util.Map.Entry;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.collections.FastHashMap; // DEPRECATED
@@ -53,10 +55,13 @@
*/
@Deprecated
public static FastHashMap copyFastHashMap(final FastHashMap fastHashMap) {
- final FastHashMap results = new FastHashMap();
+ FastHashMap results = new FastHashMap();
@SuppressWarnings("unchecked") // FastHashMap is not generic
- final HashMap<String, ?> map = fastHashMap;
- map.forEach((key, value) -> {
+ Iterator<Entry<String, ?>> iterator = fastHashMap.entrySet().iterator();
+ while (iterator.hasNext()) {
+ Entry<String, ?> entry = iterator.next();
+ String key = entry.getKey();
+ Object value = entry.getValue();
if (value instanceof Msg) {
results.put(key, ((Msg) value).clone());
} else if (value instanceof Arg) {
@@ -66,7 +71,7 @@
} else {
results.put(key, value);
}
- });
+ }
results.setFast(true);
return results;
}
@@ -81,7 +86,7 @@
* @return A copy of the <code>Map</code> that was passed in.
*/
public static Map<String, Object> copyMap(final Map<String, Object> map) {
- final Map<String, Object> results = new HashMap<>();
+ final Map<String, Object> results = new HashMap<>(map.size());
map.forEach((key, value) -> {
if (value instanceof Msg) {
results.put(key, ((Msg) value).clone());
diff --git a/src/test/java/org/apache/commons/validator/util/ValidatorUtilsTest.java b/src/test/java/org/apache/commons/validator/util/ValidatorUtilsTest.java
new file mode 100644
index 0000000..d64414f
--- /dev/null
+++ b/src/test/java/org/apache/commons/validator/util/ValidatorUtilsTest.java
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.validator.util;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.apache.commons.collections.FastHashMap;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link ValidatorUtilsTest}.
+ */
+public class ValidatorUtilsTest {
+
+ @Test
+ public void testCopyFastHashMap() {
+ final FastHashMap original = new FastHashMap();
+ original.put("key1", "value1");
+ original.put("key2", "value2");
+ original.put("key3", "value3");
+ original.setFast(true);
+ final FastHashMap copy = ValidatorUtils.copyFastHashMap(original);
+ assertEquals(original, copy);
+ }
+
+}