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 167b284e4 Reduce copy-pasta in tests.
167b284e4 is described below

commit 167b284e44bc969bfe91a31cedb52e45d8da3f7f
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Jun 27 14:34:19 2026 +0000

    Reduce copy-pasta in tests.
---
 .../commons/collections4/AbstractObjectTest.java   | 14 +---------
 .../org/apache/commons/collections4/BulkTest.java  | 31 ++++++++++++++++++++++
 .../org/apache/commons/collections4/TestUtils.java | 19 +------------
 .../commons/collections4/bag/HashBagTest.java      | 19 +++----------
 .../collections4/map/MultiValueMapTest.java        | 20 --------------
 .../collections4/multiset/HashMultiSetTest.java    | 17 +++---------
 .../splitmap/TransformedSplitMapTest.java          | 17 ++++--------
 7 files changed, 45 insertions(+), 92 deletions(-)

diff --git 
a/src/test/java/org/apache/commons/collections4/AbstractObjectTest.java 
b/src/test/java/org/apache/commons/collections4/AbstractObjectTest.java
index 068945e98..efc608e76 100644
--- a/src/test/java/org/apache/commons/collections4/AbstractObjectTest.java
+++ b/src/test/java/org/apache/commons/collections4/AbstractObjectTest.java
@@ -51,17 +51,6 @@ public abstract class AbstractObjectTest extends BulkTest {
     /** Current major release for Collections */
     public static final int COLLECTIONS_MAJOR_VERSION = 4;
 
-    @SuppressWarnings("unchecked")
-    public static <T> T serializeDeserialize(final T obj) throws IOException, 
ClassNotFoundException {
-        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-        try (ObjectOutputStream out = new ObjectOutputStream(buffer)) {
-            out.writeObject(obj);
-        }
-        try (ObjectInputStream in = new ObjectInputStream(new 
ByteArrayInputStream(buffer.toByteArray()))) {
-            return (T) in.readObject();
-        }
-    }
-
     protected String getCanonicalEmptyCollectionName(final Object object) {
         final StringBuilder retval = new StringBuilder();
         retval.append(TEST_DATA_PATH);
@@ -148,8 +137,7 @@ public abstract class AbstractObjectTest extends BulkTest {
 
     // private implementation
     private Object readExternalFormFromStream(final InputStream stream) throws 
IOException, ClassNotFoundException {
-        final ObjectInputStream oStream = new ObjectInputStream(stream);
-        return oStream.readObject();
+        return new ObjectInputStream(stream).readObject();
     }
 
     protected boolean skipSerializedCanonicalTests() {
diff --git a/src/test/java/org/apache/commons/collections4/BulkTest.java 
b/src/test/java/org/apache/commons/collections4/BulkTest.java
index a5601afe3..fa322e9a9 100644
--- a/src/test/java/org/apache/commons/collections4/BulkTest.java
+++ b/src/test/java/org/apache/commons/collections4/BulkTest.java
@@ -16,6 +16,14 @@
  */
 package org.apache.commons.collections4;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
 /**
  * This class is left over from the JUnit 3 implementation.
  */
@@ -27,6 +35,29 @@ public class BulkTest {
     /** Path to test properties resources. */
     public static final String TEST_PROPERTIES_PATH = 
"src/test/resources/org/apache/commons/collections4/properties/";
 
+    @SuppressWarnings("unchecked")
+    public static <T> T deserialize(final byte[] serialized) throws 
IOException, ClassNotFoundException {
+        try (ObjectInputStream in = new ObjectInputStream(new 
ByteArrayInputStream(serialized))) {
+            return (T) in.readObject();
+        }
+    }
+
+    public static Object deserialize(final Path path) throws IOException, 
ClassNotFoundException {
+        return deserialize(Files.readAllBytes(path));
+    }
+
+    public static byte[] serialize(final Object object) throws IOException {
+        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
+            oos.writeObject(object);
+        }
+        return baos.toByteArray();
+    }
+
+    public static <T> T serializeDeserialize(final T obj) throws IOException, 
ClassNotFoundException {
+        return deserialize(serialize(obj));
+    }
+
     /**
      * The full name of this bulk test instance.
      */
diff --git a/src/test/java/org/apache/commons/collections4/TestUtils.java 
b/src/test/java/org/apache/commons/collections4/TestUtils.java
index b2e910273..b02ccb2cf 100644
--- a/src/test/java/org/apache/commons/collections4/TestUtils.java
+++ b/src/test/java/org/apache/commons/collections4/TestUtils.java
@@ -18,12 +18,7 @@ package org.apache.commons.collections4;
 
 import static org.junit.jupiter.api.Assertions.assertSame;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
 
 public final class TestUtils {
 
@@ -61,20 +56,8 @@ public final class TestUtils {
      * @see #assertSameAfterSerialization(Object)
      */
     public static void assertSameAfterSerialization(final String msg, final 
Object o) throws IOException, ClassNotFoundException {
-        // write object to byte buffer
-        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        final ObjectOutputStream oos = new ObjectOutputStream(baos);
-        oos.writeObject(o);
-        oos.close();
-
-        // read same object from byte buffer
-        final InputStream is = new ByteArrayInputStream(baos.toByteArray());
-        final ObjectInputStream ois = new ObjectInputStream(is);
-        final Object object = ois.readObject();
-        ois.close();
-
         // assert that original object and deserialized objects are the same
-        assertSame(o, object, msg);
+        assertSame(o, AbstractObjectTest.serializeDeserialize(o), msg);
     }
 
     private TestUtils() {
diff --git a/src/test/java/org/apache/commons/collections4/bag/HashBagTest.java 
b/src/test/java/org/apache/commons/collections4/bag/HashBagTest.java
index 0f1cf262e..31837664c 100644
--- a/src/test/java/org/apache/commons/collections4/bag/HashBagTest.java
+++ b/src/test/java/org/apache/commons/collections4/bag/HashBagTest.java
@@ -18,11 +18,7 @@ package org.apache.commons.collections4.bag;
 
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.InvalidObjectException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
 
 import org.apache.commons.collections4.Bag;
 import org.junit.jupiter.api.Test;
@@ -53,18 +49,11 @@ public class HashBagTest<T> extends AbstractBagTest<T> {
         final int marker = 0x11223344;
         final HashBag<String> bag = new HashBag<>();
         bag.add("X", marker);
-        final ByteArrayOutputStream out = new ByteArrayOutputStream();
-        try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
-            oos.writeObject(bag);
-        }
-        for (final int count : new int[] {0, -7}) {
-            final byte[] bytes = out.toByteArray();
+        final byte[] byteArray = serialize(bag);
+        for (final int count : new int[] { 0, -7 }) {
+            final byte[] bytes = byteArray.clone();
             replaceInt(bytes, marker, count);
-            assertThrows(InvalidObjectException.class, () -> {
-                try (ObjectInputStream ois = new ObjectInputStream(new 
ByteArrayInputStream(bytes))) {
-                    ois.readObject();
-                }
-            });
+            assertThrows(InvalidObjectException.class, () -> 
deserialize(bytes));
         }
     }
 
diff --git 
a/src/test/java/org/apache/commons/collections4/map/MultiValueMapTest.java 
b/src/test/java/org/apache/commons/collections4/map/MultiValueMapTest.java
index 406783d02..7b3d263a2 100644
--- a/src/test/java/org/apache/commons/collections4/map/MultiValueMapTest.java
+++ b/src/test/java/org/apache/commons/collections4/map/MultiValueMapTest.java
@@ -23,11 +23,6 @@ import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
@@ -68,13 +63,6 @@ public class MultiValueMapTest<K, V> extends 
AbstractObjectTest {
         return map;
     }
 
-    private Object deserialize(final byte[] data) throws IOException, 
ClassNotFoundException {
-        final ByteArrayInputStream bais = new ByteArrayInputStream(data);
-        final ObjectInputStream iis = new ObjectInputStream(bais);
-
-        return iis.readObject();
-    }
-
     @Override
     public String getCompatibilityVersion() {
         return "4";
@@ -98,14 +86,6 @@ public class MultiValueMapTest<K, V> extends 
AbstractObjectTest {
         return m;
     }
 
-    private byte[] serialize(final Object object) throws IOException {
-        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-        try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
-            oos.writeObject(object);
-        }
-        return baos.toByteArray();
-    }
-
     @Test
     @SuppressWarnings("unchecked")
     void testContainsValue() {
diff --git 
a/src/test/java/org/apache/commons/collections4/multiset/HashMultiSetTest.java 
b/src/test/java/org/apache/commons/collections4/multiset/HashMultiSetTest.java
index a651654da..f8e47428d 100644
--- 
a/src/test/java/org/apache/commons/collections4/multiset/HashMultiSetTest.java
+++ 
b/src/test/java/org/apache/commons/collections4/multiset/HashMultiSetTest.java
@@ -18,11 +18,7 @@ package org.apache.commons.collections4.multiset;
 
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.InvalidObjectException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
 
 import org.apache.commons.collections4.MultiSet;
 import org.junit.jupiter.api.Test;
@@ -53,18 +49,11 @@ public class HashMultiSetTest<T> extends 
AbstractMultiSetTest<T> {
         final int marker = 0x11223344;
         final HashMultiSet<String> set = new HashMultiSet<>();
         set.add("Y", marker);
-        final ByteArrayOutputStream out = new ByteArrayOutputStream();
-        try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
-            oos.writeObject(set);
-        }
+        final byte[] byteArray = serialize(set);
         for (final int count : new int[] {0, -7}) {
-            final byte[] bytes = out.toByteArray();
+            final byte[] bytes = byteArray.clone();
             replaceInt(bytes, marker, count);
-            assertThrows(InvalidObjectException.class, () -> {
-                try (ObjectInputStream ois = new ObjectInputStream(new 
ByteArrayInputStream(bytes))) {
-                    ois.readObject();
-                }
-            });
+            assertThrows(InvalidObjectException.class, () -> 
deserialize(bytes));
         }
     }
 
diff --git 
a/src/test/java/org/apache/commons/collections4/splitmap/TransformedSplitMapTest.java
 
b/src/test/java/org/apache/commons/collections4/splitmap/TransformedSplitMapTest.java
index ddc7b50b5..ddeb9794f 100644
--- 
a/src/test/java/org/apache/commons/collections4/splitmap/TransformedSplitMapTest.java
+++ 
b/src/test/java/org/apache/commons/collections4/splitmap/TransformedSplitMapTest.java
@@ -22,9 +22,8 @@ import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.IOException;
-import java.io.ObjectInputStream;
 import java.math.BigInteger;
-import java.nio.file.Files;
+import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.util.HashMap;
 
@@ -53,11 +52,8 @@ class TransformedSplitMapTest extends BulkTest {
                 TransformedSplitMap.transformingMap(new HashMap<>(),
                                                     
NOPTransformer.<String>nopTransformer(),
                                                     
NOPTransformer.<String>nopTransformer());
-
-        final ObjectInputStream in = new 
ObjectInputStream(Files.newInputStream(Paths.get(TEST_DATA_PATH + 
"/TransformedSplitMap.emptyCollection.version4.obj")));
-        final Object readObject = in.readObject();
-        in.close();
-
+        final Path path = Paths.get(TEST_DATA_PATH + 
"/TransformedSplitMap.emptyCollection.version4.obj");
+        final Object readObject = deserialize(path);
         final TransformedSplitMap<?, ?, ?, ?> readMap = 
(TransformedSplitMap<?, ?, ?, ?>) readObject;
         assertTrue(readMap.isEmpty(), "Map should be empty");
         assertEquals(map.entrySet(), readMap.entrySet());
@@ -71,11 +67,8 @@ class TransformedSplitMapTest extends BulkTest {
         map.put("c", "d");
         map.put("e", "f");
         map.put("g", "h");
-
-        final ObjectInputStream in = new 
ObjectInputStream(Files.newInputStream(Paths.get(TEST_DATA_PATH + 
"TransformedSplitMap.fullCollection.version4.obj")));
-        final Object readObject = in.readObject();
-        in.close();
-
+        final Path path = Paths.get(TEST_DATA_PATH + 
"TransformedSplitMap.fullCollection.version4.obj");
+        final Object readObject = deserialize(path);
         final TransformedSplitMap<?, ?, ?, ?> readMap = 
(TransformedSplitMap<?, ?, ?, ?>) readObject;
         assertFalse(readMap.isEmpty(), "Map should not be empty");
         assertEquals(map.entrySet(), readMap.entrySet());

Reply via email to