You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2026/06/19 13:02:12 UTC
(commons-collections) branch master updated (6cc1adac1 -> 6742fee39)
This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git
from 6cc1adac1 validate order and uniqueness invariants in decorator readObject (#684)
new e827e7940 Validate order and uniqueness invariants in decorator readObject() (#684).
new 267158997 Javadoc
new 6742fee39 Refactor tests for PR copy-pasta.
The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/changes/changes.xml | 1 +
.../commons/collections4/AbstractObjectTest.java | 128 ++++++++-------------
.../collections4/list/SetUniqueListTest.java | 15 +--
.../collections4/map/ListOrderedMapTest.java | 14 +--
.../collections4/set/ListOrderedSetTest.java | 14 +--
5 files changed, 50 insertions(+), 122 deletions(-)
(commons-collections) 02/03: Javadoc
Posted by gg...@apache.org.
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
commit 267158997f4af7d51b31cbd0704ea0e9aaa4858e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jun 19 13:01:56 2026 +0000
Javadoc
---
.../commons/collections4/AbstractObjectTest.java | 128 ++++++++-------------
1 file changed, 45 insertions(+), 83 deletions(-)
diff --git a/src/test/java/org/apache/commons/collections4/AbstractObjectTest.java b/src/test/java/org/apache/commons/collections4/AbstractObjectTest.java
index 4a31e6e84..b364f60cc 100644
--- a/src/test/java/org/apache/commons/collections4/AbstractObjectTest.java
+++ b/src/test/java/org/apache/commons/collections4/AbstractObjectTest.java
@@ -14,6 +14,7 @@
* 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;
@@ -38,12 +39,12 @@ import org.junit.jupiter.api.Test;
/**
* Tests {@link Object}.
* <p>
- * To use, simply extend this class, and implement
- * the {@link #makeObject()} method.
+ * To use, simply extend this class, and implement the {@link #makeObject()} method.
+ * </p>
* <p>
- * If your {@link Object} fails one of these tests by design,
- * you may still use this base set of cases. Simply override the
- * test case (method) your {@link Object} fails.
+ * If your {@link Object} fails one of these tests by design, you may still use this base set of cases. Simply override the test case (method) your
+ * {@link Object} fails.
+ * </p>
*/
public abstract class AbstractObjectTest extends BulkTest {
@@ -75,35 +76,26 @@ public abstract class AbstractObjectTest extends BulkTest {
}
/**
- * Gets the version of Collections that this object tries to
- * maintain serialization compatibility with. Defaults to 4, due to
- * the package change to collections4 introduced in version 4.
- *
- * This constant makes it possible for TestMap (and other subclasses,
- * if necessary) to automatically check SCM for a versionX copy of a
- * Serialized object, so we can make sure that compatibility is maintained.
- * See, for example, TestMap.getCanonicalFullMapName(Map map).
- * Subclasses can override this variable, indicating compatibility
- * with earlier Collections versions.
+ * Gets the version of Collections that this object tries to maintain serialization compatibility with. Defaults to 4, due to the package change to
+ * collections4 introduced in version 4. This constant makes it possible for TestMap (and other subclasses, if necessary) to automatically check SCM for a
+ * versionX copy of a Serialized object, so we can make sure that compatibility is maintained. See, for example, TestMap.getCanonicalFullMapName(Map map).
+ * Subclasses can override this variable, indicating compatibility with earlier Collections versions.
*
- * @return The version, or {@code null} if this object shouldn't be
- * tested for compatibility with previous versions.
+ * @return The version, or {@code null} if this object shouldn't be tested for compatibility with previous versions.
*/
public String getCompatibilityVersion() {
return "4";
}
/**
- * Returns true to indicate that the collection supports equals() comparisons.
- * This implementation returns true;
+ * Returns true to indicate that the collection supports equals() comparisons. This implementation returns true;
*/
public boolean isEqualsCheckable() {
return true;
}
/**
- * Is serialization testing supported.
- * Default is true.
+ * Is serialization testing supported. Default is true.
*/
public boolean isTestSerialization() {
return true;
@@ -112,16 +104,15 @@ public abstract class AbstractObjectTest extends BulkTest {
/**
* Implement this method to return the object to test.
*
- * @return the object to test
+ * @return the object to test.
*/
public abstract Object makeObject();
/**
- * Reads a Serialized or Externalized Object from bytes.
- * Useful for verifying serialization in memory.
+ * Reads a Serialized or Externalized Object from bytes. Useful for verifying serialization in memory.
*
- * @param b byte array containing a serialized Object
- * @return Object contained in the bytes
+ * @param b byte array containing a serialized Object.
+ * @return Object contained in the bytes.
* @throws IOException
* @throws ClassNotFoundException
*/
@@ -131,12 +122,10 @@ public abstract class AbstractObjectTest extends BulkTest {
}
/**
- * Reads a Serialized or Externalized Object from disk.
- * Useful for creating compatibility tests between
- * different SCM versions of the same class
+ * Reads a Serialized or Externalized Object from disk. Useful for creating compatibility tests between different SCM versions of the same class
*
- * @param path path to the serialized Object
- * @return the Object at the given path
+ * @param path path to the serialized Object.
+ * @return the Object at the given path.
* @throws IOException
* @throws ClassNotFoundException
*/
@@ -152,17 +141,14 @@ public abstract class AbstractObjectTest extends BulkTest {
return oStream.readObject();
}
- protected Object serializeDeserialize(final Object obj) throws Exception {
+ protected Object serializeDeserialize(final Object obj) throws IOException, ClassNotFoundException {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
- final ObjectOutputStream out = new ObjectOutputStream(buffer);
- out.writeObject(obj);
- out.close();
-
- final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()));
- final Object dest = in.readObject();
- in.close();
-
- return dest;
+ try (ObjectOutputStream out = new ObjectOutputStream(buffer)) {
+ out.writeObject(obj);
+ }
+ try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray()))) {
+ return in.readObject();
+ }
}
protected boolean skipSerializedCanonicalTests() {
@@ -170,9 +156,7 @@ public abstract class AbstractObjectTest extends BulkTest {
}
/**
- * Override this method if a subclass is testing an object
- * that cannot serialize an "empty" Collection.
- * (for example Comparators have no contents)
+ * Override this method if a subclass is testing an object that cannot serialize an "empty" Collection. (for example Comparators have no contents)
*
* @return true
*/
@@ -181,9 +165,7 @@ public abstract class AbstractObjectTest extends BulkTest {
}
/**
- * Override this method if a subclass is testing an object
- * that cannot serialize a "full" Collection.
- * (for example Comparators have no contents)
+ * Override this method if a subclass is testing an object that cannot serialize a "full" Collection. (for example Comparators have no contents)
*
* @return true
*/
@@ -192,8 +174,7 @@ public abstract class AbstractObjectTest extends BulkTest {
}
/**
- * Tests serialization by comparing against a previously stored version in SCM.
- * If the test object is serializable, confirm that a canonical form exists.
+ * Tests serialization by comparing against a previously stored version in SCM. If the test object is serializable, confirm that a canonical form exists.
*/
@Test
public void testCanonicalEmptyCollectionExists() {
@@ -201,16 +182,13 @@ public abstract class AbstractObjectTest extends BulkTest {
final Object object = makeObject();
if (object instanceof Serializable) {
final String name = getCanonicalEmptyCollectionName(object);
- assertTrue(
- new File(name).exists(),
- "Canonical empty collection (" + name + ") is not in SCM");
+ assertTrue(new File(name).exists(), "Canonical empty collection (" + name + ") is not in SCM");
}
}
}
/**
- * Tests serialization by comparing against a previously stored version in SCM.
- * If the test object is serializable, confirm that a canonical form exists.
+ * Tests serialization by comparing against a previously stored version in SCM. If the test object is serializable, confirm that a canonical form exists.
*/
@Test
public void testCanonicalFullCollectionExists() {
@@ -218,9 +196,7 @@ public abstract class AbstractObjectTest extends BulkTest {
final Object object = makeObject();
if (object instanceof Serializable) {
final String name = getCanonicalFullCollectionName(object);
- assertTrue(
- new File(name).exists(),
- "Canonical full collection (" + name + ") is not in SCM");
+ assertTrue(new File(name).exists(), "Canonical full collection (" + name + ") is not in SCM");
}
}
}
@@ -241,15 +217,11 @@ public abstract class AbstractObjectTest extends BulkTest {
void testObjectHashCodeEqualsContract() {
final Object obj1 = makeObject();
if (obj1.equals(obj1)) {
- assertEquals(
- obj1.hashCode(), obj1.hashCode(),
- "[1] When two objects are equal, their hashCodes should be also.");
+ assertEquals(obj1.hashCode(), obj1.hashCode(), "[1] When two objects are equal, their hashCodes should be also.");
}
final Object obj2 = makeObject();
if (obj1.equals(obj2)) {
- assertEquals(
- obj1.hashCode(), obj2.hashCode(),
- "[2] When two objects are equal, their hashCodes should be also.");
+ assertEquals(obj1.hashCode(), obj2.hashCode(), "[2] When two objects are equal, their hashCodes should be also.");
assertEquals(obj2, obj1, "When obj1.equals(obj2) is true, then obj2.equals(obj1) should also be true");
}
}
@@ -272,9 +244,7 @@ public abstract class AbstractObjectTest extends BulkTest {
}
/**
- * Sanity check method, makes sure that any Serializable
- * class can be serialized and de-serialized in memory,
- * using the handy makeObject() method
+ * Sanity check method, makes sure that any Serializable class can be serialized and de-serialized in memory, using the handy makeObject() method
*
* @throws IOException
* @throws ClassNotFoundException
@@ -283,17 +253,15 @@ public abstract class AbstractObjectTest extends BulkTest {
public void testSimpleSerialization() throws Exception {
final Object o = makeObject();
if (o instanceof Serializable && isTestSerialization()) {
- final byte[] object = writeExternalFormToBytes((Serializable) o);
- readExternalFormFromBytes(object);
+ readExternalFormFromBytes(writeExternalFormToBytes((Serializable) o));
}
}
/**
- * Converts a Serializable or Externalizable object to
- * bytes. Useful for in-memory tests of serialization
+ * Converts a Serializable or Externalizable object to bytes. Useful for in-memory tests of serialization
*
- * @param o Object to convert to bytes
- * @return serialized form of the Object
+ * @param o Object to convert to bytes.
+ * @return serialized form of the Object.
* @throws IOException
*/
protected byte[] writeExternalFormToBytes(final Serializable o) throws IOException {
@@ -303,15 +271,11 @@ public abstract class AbstractObjectTest extends BulkTest {
}
/**
- * Writes a Serializable or Externalizable object as
- * a file at the given path. NOT USEFUL as part
- * of a unit test; this is just a utility method
- * for creating disk-based objects in SCM that can become
- * the basis for compatibility tests using
- * readExternalFormFromDisk(String path)
+ * Writes a Serializable or Externalizable object as a file at the given path. NOT USEFUL as part of a unit test; this is just a utility method for creating
+ * disk-based objects in SCM that can become the basis for compatibility tests using readExternalFormFromDisk(String path)
*
- * @param o Object to serialize
- * @param path path to write the serialized Object
+ * @param o Object to serialize.
+ * @param path path to write the serialized Object.
* @throws IOException
*/
protected void writeExternalFormToDisk(final Serializable o, final String path) throws IOException {
@@ -321,8 +285,6 @@ public abstract class AbstractObjectTest extends BulkTest {
}
private void writeExternalFormToStream(final Serializable o, final OutputStream stream) throws IOException {
- final ObjectOutputStream oStream = new ObjectOutputStream(stream);
- oStream.writeObject(o);
+ new ObjectOutputStream(stream).writeObject(o);
}
-
}
(commons-collections) 03/03: Refactor tests for PR copy-pasta.
Posted by gg...@apache.org.
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
commit 6742fee39960967a679468e118e4185dbc9aeb2a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jun 19 13:02:05 2026 +0000
Refactor tests for PR copy-pasta.
---
.../commons/collections4/list/SetUniqueListTest.java | 15 ++-------------
.../commons/collections4/map/ListOrderedMapTest.java | 14 +-------------
.../commons/collections4/set/ListOrderedSetTest.java | 14 +-------------
3 files changed, 4 insertions(+), 39 deletions(-)
diff --git a/src/test/java/org/apache/commons/collections4/list/SetUniqueListTest.java b/src/test/java/org/apache/commons/collections4/list/SetUniqueListTest.java
index d03ac8a3e..8232646bd 100644
--- a/src/test/java/org/apache/commons/collections4/list/SetUniqueListTest.java
+++ b/src/test/java/org/apache/commons/collections4/list/SetUniqueListTest.java
@@ -22,11 +22,7 @@ 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.InvalidObjectException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -113,15 +109,8 @@ public class SetUniqueListTest<E> extends AbstractListTest<E> {
list.add("beta");
// push a duplicate straight onto the decorated list, bypassing the uniqueness set
list.decorated().add("alpha");
- final ByteArrayOutputStream out = new ByteArrayOutputStream();
- try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
- oos.writeObject(list);
- }
- assertThrows(InvalidObjectException.class, () -> {
- try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()))) {
- ois.readObject();
- }
- });
+ assertThrows(InvalidObjectException.class, () -> serializeDeserialize(list));
+
}
@Test
diff --git a/src/test/java/org/apache/commons/collections4/map/ListOrderedMapTest.java b/src/test/java/org/apache/commons/collections4/map/ListOrderedMapTest.java
index 9b35f81df..699b756e1 100644
--- a/src/test/java/org/apache/commons/collections4/map/ListOrderedMapTest.java
+++ b/src/test/java/org/apache/commons/collections4/map/ListOrderedMapTest.java
@@ -21,11 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
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 java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -178,15 +174,7 @@ public class ListOrderedMapTest<K, V> extends AbstractOrderedMapTest<K, V> {
map.put("two", "2");
// drop a key straight from the backing map; the insert-order list still names it
map.decorated().remove("one");
- final ByteArrayOutputStream out = new ByteArrayOutputStream();
- try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
- oos.writeObject(map);
- }
- assertThrows(InvalidObjectException.class, () -> {
- try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()))) {
- ois.readObject();
- }
- });
+ assertThrows(InvalidObjectException.class, () -> serializeDeserialize(map));
}
@Test
diff --git a/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java b/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java
index c08b1bb8d..7794af1bd 100644
--- a/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java
+++ b/src/test/java/org/apache/commons/collections4/set/ListOrderedSetTest.java
@@ -21,11 +21,7 @@ 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.InvalidObjectException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@@ -104,15 +100,7 @@ public class ListOrderedSetTest<E>
set.add("green");
// remove an element from the decorated set only; the order list keeps naming it
set.decorated().remove("red");
- final ByteArrayOutputStream out = new ByteArrayOutputStream();
- try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
- oos.writeObject(set);
- }
- assertThrows(InvalidObjectException.class, () -> {
- try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()))) {
- ois.readObject();
- }
- });
+ assertThrows(InvalidObjectException.class, () -> serializeDeserialize(set));
}
@Test
(commons-collections) 01/03: Validate order and uniqueness invariants in decorator readObject() (#684).
Posted by gg...@apache.org.
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
commit e827e7940fdbfb369ed7319066029bcd81006172
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jun 19 12:37:13 2026 +0000
Validate order and uniqueness invariants in decorator readObject()
(#684).
---
src/changes/changes.xml | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 3f18bf12d..9c38c53c6 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -52,6 +52,7 @@
<action type="fix" dev="ggregory" due-to="Dexter.k, Gary Gregory">Re-validate entries in PredicatedMap/PredicatedCollection readObject (#682).</action>
<action type="fix" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory" issue="COLLECTIONS-889">IndexedCollection.remove(Object) removes all values for a non-unique index key.</action>
<action type="fix" dev="ggregory" due-to="Maxim Safronov, Gary Gregory" issue="COLLECTIONS-878">MapUtils.invertMap() improves HashMap construction (#652).</action>
+ <action type="fix" dev="ggregory" due-to="Dexter.k, Gary Gregory">Validate order and uniqueness invariants in decorator readObject() (#684).</action>
<!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add generics to UnmodifiableIterator for the wrapped type.</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add a Maven benchmark profile for JMH.</action>