This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-validator.git
The following commit(s) were added to refs/heads/master by this push: new 4015f7e0 [VALIDATOR-492] ValidatorUtils.copyFastHashMap is broken 4015f7e0 is described below commit 4015f7e05f3e08103000b8e2c50bf3f31d76e592 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed Mar 20 22:40:02 2024 -0400 [VALIDATOR-492] ValidatorUtils.copyFastHashMap is broken --- src/changes/changes.xml | 2 ++ .../commons/validator/util/ValidatorUtils.java | 15 +++++--- .../commons/validator/util/ValidatorUtilsTest.java | 41 ++++++++++++++++++++++ 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 5f935f0a..1a6409a8 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -65,6 +65,8 @@ The <action> type attribute can be add,update,fix,remove. <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 cc8a2b99..552b3da6 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 @@ package org.apache.commons.validator.util; 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 @@ public class ValidatorUtils { */ @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 @@ public class ValidatorUtils { } else { results.put(key, value); } - }); + } results.setFast(true); return results; } @@ -81,7 +86,7 @@ public class ValidatorUtils { * @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 00000000..d64414f5 --- /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); + } + +}