This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git
The following commit(s) were added to refs/heads/master by this push:
new 4390163b8 Fix inverted fixed-size guard in FixedSizeSortedMap.putAll
(#722)
4390163b8 is described below
commit 4390163b87f556c3c3c6fff22613d323909e5aa9
Author: Naveed Khan <[email protected]>
AuthorDate: Sat Aug 8 13:18:33 2026 +0000
Fix inverted fixed-size guard in FixedSizeSortedMap.putAll (#722)
* fix inverted guard in FixedSizeSortedMap.putAll
* Update FixedSizeSortedMapTest.java
---------
Co-authored-by: Gary Gregory <[email protected]>
---
.../collections4/map/FixedSizeSortedMap.java | 2 +-
.../collections4/map/FixedSizeSortedMapTest.java | 24 ++++++++++++++++++++++
2 files changed, 25 insertions(+), 1 deletion(-)
diff --git
a/src/main/java/org/apache/commons/collections4/map/FixedSizeSortedMap.java
b/src/main/java/org/apache/commons/collections4/map/FixedSizeSortedMap.java
index 6eec237d2..3c4adc70d 100644
--- a/src/main/java/org/apache/commons/collections4/map/FixedSizeSortedMap.java
+++ b/src/main/java/org/apache/commons/collections4/map/FixedSizeSortedMap.java
@@ -144,7 +144,7 @@ public class FixedSizeSortedMap<K, V>
@Override
public void putAll(final Map<? extends K, ? extends V> mapToCopy) {
- if (CollectionUtils.isSubCollection(mapToCopy.keySet(), keySet())) {
+ if (!CollectionUtils.isSubCollection(mapToCopy.keySet(), keySet())) {
throw new IllegalArgumentException("Cannot put new key/value pair
- Map is fixed size");
}
map.putAll(mapToCopy);
diff --git
a/src/test/java/org/apache/commons/collections4/map/FixedSizeSortedMapTest.java
b/src/test/java/org/apache/commons/collections4/map/FixedSizeSortedMapTest.java
index 706edcbb4..4d82c6616 100644
---
a/src/test/java/org/apache/commons/collections4/map/FixedSizeSortedMapTest.java
+++
b/src/test/java/org/apache/commons/collections4/map/FixedSizeSortedMapTest.java
@@ -16,9 +16,16 @@
*/
package org.apache.commons.collections4.map;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.Collections;
import java.util.SortedMap;
import java.util.TreeMap;
+import org.junit.jupiter.api.Test;
+
/**
* Extension of {@link AbstractSortedMapTest} for exercising the {@link
FixedSizeSortedMap}
* implementation.
@@ -33,6 +40,23 @@ public class FixedSizeSortedMapTest<K, V> extends
AbstractSortedMapTest<K, V> {
return "4";
}
+ @Test
+ void testPutAllAllowsUpdatesRejectsNewKeys() {
+ final SortedMap<String, String> base = new TreeMap<>();
+ base.put("a", "1");
+ final SortedMap<String, String> fixed =
FixedSizeSortedMap.fixedSizeSortedMap(base);
+ // updating the value of an existing key is allowed
+ fixed.putAll(Collections.singletonMap("a", "2"));
+ assertEquals("2", fixed.get("a"));
+ // an empty map is a no-op, not a rejection
+ fixed.putAll(Collections.emptyMap());
+ assertEquals(1, fixed.size());
+ // a new key must be rejected and must not grow the map
+ assertThrows(IllegalArgumentException.class, () ->
fixed.putAll(Collections.singletonMap("b", "9")));
+ assertEquals(1, fixed.size());
+ assertFalse(fixed.containsKey("b"));
+ }
+
@Override
public boolean isPutAddSupported() {
return false;