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 c88983094 Add IterableUtilsSumSizesToIntTest, bullet-proofing, comments
c88983094 is described below

commit c889830940ea028a9657bd825871707dca2e8ccb
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Jul 10 17:19:49 2026 -0400

    Add IterableUtilsSumSizesToIntTest, bullet-proofing, comments
---
 pom.xml                                            |  10 ++
 .../apache/commons/collections4/IterableUtils.java |   4 +-
 .../collections4/multiset/AbstractMultiSet.java    |   2 +-
 .../IterableUtilsSumSizesToIntTest.java            | 109 +++++++++++++++++++++
 4 files changed, 123 insertions(+), 2 deletions(-)

diff --git a/pom.xml b/pom.xml
index 69dbf6b44..26aed186c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -53,6 +53,11 @@
       <artifactId>junit-pioneer</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-junit-jupiter</artifactId>
+      <scope>test</scope>
+    </dependency>    
     <dependency>
       <groupId>org.easymock</groupId>
       <artifactId>easymock</artifactId>
@@ -89,6 +94,11 @@
       <version>${commons.jmh.version}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-core</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <distributionManagement>
     <site>
diff --git a/src/main/java/org/apache/commons/collections4/IterableUtils.java 
b/src/main/java/org/apache/commons/collections4/IterableUtils.java
index 4475ffff6..04e7d2376 100644
--- a/src/main/java/org/apache/commons/collections4/IterableUtils.java
+++ b/src/main/java/org/apache/commons/collections4/IterableUtils.java
@@ -958,7 +958,9 @@ public class IterableUtils {
         int size = 0;
         try {
             for (final C item : iterable) {
-                size = Math.addExact(size, toIntFunction.applyAsInt(item));
+                if (item != null) {
+                    size = Math.addExact(size, toIntFunction.applyAsInt(item));
+                }
             }
         } catch (final ArithmeticException e) {
             size = Integer.MAX_VALUE;
diff --git 
a/src/main/java/org/apache/commons/collections4/multiset/AbstractMultiSet.java 
b/src/main/java/org/apache/commons/collections4/multiset/AbstractMultiSet.java
index ba6b739b9..e81972be8 100644
--- 
a/src/main/java/org/apache/commons/collections4/multiset/AbstractMultiSet.java
+++ 
b/src/main/java/org/apache/commons/collections4/multiset/AbstractMultiSet.java
@@ -479,7 +479,7 @@ public abstract class AbstractMultiSet<E> extends 
AbstractCollection<E> implemen
      */
     @Override
     public int size() {
-        // TODO reuse IterableUtils.sum(Iterable, ToIntFunction)
+        // TODO reuse IterableUtils.sumToInt(Iterable, ToIntFunction)
         int size = 0;
         try {
             for (final Entry<E> entry : entrySet()) {
diff --git 
a/src/test/java/org/apache/commons/collections4/IterableUtilsSumSizesToIntTest.java
 
b/src/test/java/org/apache/commons/collections4/IterableUtilsSumSizesToIntTest.java
new file mode 100644
index 000000000..0bbd2ae6a
--- /dev/null
+++ 
b/src/test/java/org/apache/commons/collections4/IterableUtilsSumSizesToIntTest.java
@@ -0,0 +1,109 @@
+/*
+ * 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
+ *
+ *      https://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.collections4;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+/**
+ * Tests {@link IterableUtils#sumSizesToInt(Iterable)}.
+ * <p>
+ * Uses Mockito to minimize overhead.
+ * </p>
+ */
+@ExtendWith(MockitoExtension.class)
+class IterableUtilsSumSizesToIntTest {
+
+    @Mock
+    private Iterable<? extends Collection<Object>> iterable;
+
+    @Mock
+    private Iterator<Collection<Object>> iterator;
+
+    @Mock
+    private Collection<Object> coll1;
+
+    @Mock
+    private Collection<Object> coll2;
+
+    @Mock
+    private Collection<Object> coll3;
+
+    @Test
+    void testEmptyIterable() {
+        doReturn(iterator).when(iterable).iterator();
+        when(iterator.hasNext()).thenReturn(false);
+        assertEquals(0, IterableUtils.sumSizesToInt(iterable));
+        verify(iterable).iterator();
+    }
+
+    @Test
+    void testIterableWithNullCollections() {
+        doReturn(iterator).when(iterable).iterator();
+        when(iterator.hasNext()).thenReturn(true, true, false);
+        when(iterator.next()).thenReturn(coll1, (Collection<Object>) null);
+        when(coll1.size()).thenReturn(5);
+        assertEquals(5, IterableUtils.sumSizesToInt(iterable));
+    }
+
+    @Test
+    void testLargeSizesOverflow() {
+        doReturn(iterator).when(iterable).iterator();
+        when(iterator.hasNext()).thenReturn(true, true, false);
+        when(iterator.next()).thenReturn(coll1, coll2);
+        when(coll1.size()).thenReturn(Integer.MAX_VALUE);
+        when(coll2.size()).thenReturn(10);
+        // int overflow behavior capping at Integer.MAX_VALUE.
+        assertEquals(Integer.MAX_VALUE, IterableUtils.sumSizesToInt(iterable));
+    }
+
+    @Test
+    void testMultipleCollections() {
+        doReturn(iterator).when(iterable).iterator();
+        when(iterator.hasNext()).thenReturn(true, true, true, false);
+        when(iterator.next()).thenReturn(coll1, coll2, coll3);
+        when(coll1.size()).thenReturn(10);
+        when(coll2.size()).thenReturn(20);
+        when(coll3.size()).thenReturn(30);
+        assertEquals(60, IterableUtils.sumSizesToInt(iterable));
+    }
+
+    @Test
+    void testNullIterable() {
+        assertThrows(NullPointerException.class, () -> 
IterableUtils.sumSizesToInt(null));
+    }
+
+    @Test
+    void testOnlyNullCollections() {
+        doReturn(iterator).when(iterable).iterator();
+        when(iterator.hasNext()).thenReturn(true, false);
+        when(iterator.next()).thenReturn((Collection<Object>) null);
+        assertEquals(0, IterableUtils.sumSizesToInt(iterable));
+    }
+}

Reply via email to