You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2022/09/23 11:49:12 UTC

[commons-collections] branch master updated (16710abc9 -> b284bc8e2)

This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git


    from 16710abc9 Bump commons-parent from 53 to 54 #339
     new 30a9ab835 Fix flaky test failure in SynchronizedBagTest#testCollectionToArray2
     new c35d8c6fd Update test of unordered arrays
     new b284bc8e2 Track changes

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                            |  6 +-
 .../commons/collections4/bag/AbstractBagTest.java  |  4 ++
 .../collections4/bag/SynchronizedBagTest.java      |  5 ++
 .../collection/AbstractCollectionTest.java         | 78 ++++++++++++++++++++--
 4 files changed, 87 insertions(+), 6 deletions(-)


[commons-collections] 02/03: Update test of unordered arrays

Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git

commit c35d8c6fd1ae28680c9912acf031173db389b80c
Author: aherbert <a....@sussex.ac.uk>
AuthorDate: Fri Sep 23 12:24:08 2022 +0100

    Update test of unordered arrays
    
    Matching the length and then matching items in a Set will not detect a
    count mismatch of duplicates. This test now explicitly matches each item
    once and only once.
---
 .../collection/AbstractCollectionTest.java         | 42 ++++++++++++++++++----
 1 file changed, 36 insertions(+), 6 deletions(-)

diff --git a/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java b/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java
index fc7b84edb..fa6e20122 100644
--- a/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java
+++ b/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java
@@ -39,6 +39,7 @@ import java.util.Objects;
 import java.util.function.Predicate;
 
 import org.apache.commons.collections4.AbstractObjectTest;
+import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 /**
@@ -1123,9 +1124,7 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
         a = getCollection().toArray();
 
         if ((getIterationBehaviour() & UNORDERED) != 0) {
-            assertTrue("toArrays should contain the same elements",
-                    array.length == a.length &&
-                    (new HashSet<>(Arrays.asList(array)).equals(new HashSet<>(Arrays.asList(a)))));
+            assertUnorderedArrayEquals(array, a, "toArray(Object[]) and toArray()");
         } else {
             assertEquals("toArrays should be equal", Arrays.asList(array), Arrays.asList(a));
         }
@@ -1149,9 +1148,7 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
                 a.getClass(), array.getClass());
 
         if ((getIterationBehaviour() & UNORDERED) != 0) {
-            assertTrue("type-specific toArrays should contain the same elements",
-                    array.length == getCollection().toArray().length &&
-                    (new HashSet<>(Arrays.asList(array))).equals(new HashSet<>(Arrays.asList(getCollection().toArray()))));
+            assertUnorderedArrayEquals(array, getCollection().toArray(), "type-specific toArray(T[]) and toArray()");
         } else {
             assertEquals("type-specific toArrays should be equal",
                     Arrays.asList(array),
@@ -1160,6 +1157,39 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
         verify();
     }
 
+    /**
+     * Assert the arrays contain the same elements, ignoring the order.
+     *
+     * <p>Note this does not test the arrays are deeply equal. Array elements are compared
+     * using {@link Object#equals(Object)}.
+     *
+     * @param a1 First array
+     * @param a2 Second array
+     * @param msg Failure message prefix
+     */
+    private static void assertUnorderedArrayEquals(Object[] a1, Object[] a2, String msg) {
+        Assertions.assertEquals(a1.length, a2.length, () -> msg + ": length");
+        final int size = a1.length;
+        // Track values that have been matched once (and only once)
+        final boolean[] matched = new boolean[size];
+        NEXT_OBJECT:
+        for (final Object o : a1) {
+            for (int i = 0; i < size; i++) {
+                if (matched[i]) {
+                    // skip values already matched
+                    continue;
+                }
+                if (Objects.equals(o, a2[i])) {
+                    // values matched
+                    matched[i] = true;
+                    // continue to the outer loop
+                    continue NEXT_OBJECT;
+                }
+            }
+            fail(msg + ": array 2 does not have object: " + o);
+        }
+    }
+
     /**
      *  Tests {@code toString} on a collection.
      */


[commons-collections] 01/03: Fix flaky test failure in SynchronizedBagTest#testCollectionToArray2

Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git

commit 30a9ab835dadfde94d0daa15e9a8447843076abf
Author: Partha-SUST16 <pp...@gmail.com>
AuthorDate: Fri Sep 23 12:07:45 2022 +0100

    Fix flaky test failure in SynchronizedBagTest#testCollectionToArray2
    
    Closes #336
---
 .../commons/collections4/bag/AbstractBagTest.java  |  4 ++
 .../collections4/bag/SynchronizedBagTest.java      |  5 +++
 .../collection/AbstractCollectionTest.java         | 48 +++++++++++++++++++---
 3 files changed, 52 insertions(+), 5 deletions(-)

diff --git a/src/test/java/org/apache/commons/collections4/bag/AbstractBagTest.java b/src/test/java/org/apache/commons/collections4/bag/AbstractBagTest.java
index 05e93dc98..62e6df003 100644
--- a/src/test/java/org/apache/commons/collections4/bag/AbstractBagTest.java
+++ b/src/test/java/org/apache/commons/collections4/bag/AbstractBagTest.java
@@ -674,6 +674,10 @@ public abstract class AbstractBagTest<T> extends AbstractCollectionTest<T> {
             super.verify();
         }
 
+        @Override
+        protected int getIterationBehaviour(){
+            return AbstractBagTest.this.getIterationBehaviour();
+        }
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/collections4/bag/SynchronizedBagTest.java b/src/test/java/org/apache/commons/collections4/bag/SynchronizedBagTest.java
index 06cd875d4..e01b2d56e 100644
--- a/src/test/java/org/apache/commons/collections4/bag/SynchronizedBagTest.java
+++ b/src/test/java/org/apache/commons/collections4/bag/SynchronizedBagTest.java
@@ -46,6 +46,11 @@ public class SynchronizedBagTest<T> extends AbstractBagTest<T> {
         return "4";
     }
 
+    @Override
+    protected int getIterationBehaviour(){
+        return UNORDERED;
+    }
+
 //    public void testCreate() throws Exception {
 //        Bag<T> bag = makeObject();
 //        writeExternalFormToDisk((java.io.Serializable) bag, "src/test/resources/data/test/SynchronizedBag.emptyCollection.version4.obj");
diff --git a/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java b/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java
index 6896fb5f6..fc7b84edb 100644
--- a/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java
+++ b/src/test/java/org/apache/commons/collections4/collection/AbstractCollectionTest.java
@@ -70,6 +70,13 @@ import org.junit.jupiter.api.Test;
  * <li>{@link #isFailFastSupported()}
  * </ul>
  * <p>
+ * <b>Indicate Collection Behaviour</b>
+ * <p>
+ * Override these if your collection makes specific behaviour guarantees:
+ * <ul>
+ * <li>{@link #getIterationBehaviour()}</li>
+ * </ul>
+ * <p>
  * <b>Fixture Methods</b>
  * <p>
  * Fixtures are used to verify that the operation results in correct state
@@ -134,6 +141,13 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
     // tests on Collection.equals nor any for Collection.hashCode.
     //
 
+    /**
+     * Flag to indicate the collection makes no ordering guarantees for the iterator. If this is not used
+     * then the behaviour is assumed to be ordered and the output order of the iterator is matched by
+     * the toArray method.
+     */
+    protected static final int UNORDERED = 0x1;
+
     // These fields are used by reset() and verify(), and any test
     // method that tests a modification.
 
@@ -487,6 +501,18 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
         };
     }
 
+    /**
+     * Return a flag specifying the iteration behaviour of the collection.
+     * This is used to change the assertions used by specific tests.
+     * The default implementation returns 0 which indicates ordered iteration behaviour.
+     *
+     * @return the iteration behaviour
+     * @see #UNORDERED
+     */
+    protected int getIterationBehaviour(){
+        return 0;
+    }
+
     // Tests
     /**
      *  Tests {@link Collection#add(Object)}.
@@ -1095,9 +1121,14 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
 
         array = getCollection().toArray(new Object[0]);
         a = getCollection().toArray();
-        assertEquals("toArrays should be equal",
-                     Arrays.asList(array), Arrays.asList(a));
 
+        if ((getIterationBehaviour() & UNORDERED) != 0) {
+            assertTrue("toArrays should contain the same elements",
+                    array.length == a.length &&
+                    (new HashSet<>(Arrays.asList(array)).equals(new HashSet<>(Arrays.asList(a)))));
+        } else {
+            assertEquals("toArrays should be equal", Arrays.asList(array), Arrays.asList(a));
+        }
         // Figure out if they're all the same class
         // TODO: It'd be nicer to detect a common superclass
         final HashSet<Class<?>> classes = new HashSet<>();
@@ -1116,9 +1147,16 @@ public abstract class AbstractCollectionTest<E> extends AbstractObjectTest {
         array = getCollection().toArray(a);
         assertEquals("toArray(Object[]) should return correct array type",
                 a.getClass(), array.getClass());
-        assertEquals("type-specific toArrays should be equal",
-                Arrays.asList(array),
-                Arrays.asList(getCollection().toArray()));
+
+        if ((getIterationBehaviour() & UNORDERED) != 0) {
+            assertTrue("type-specific toArrays should contain the same elements",
+                    array.length == getCollection().toArray().length &&
+                    (new HashSet<>(Arrays.asList(array))).equals(new HashSet<>(Arrays.asList(getCollection().toArray()))));
+        } else {
+            assertEquals("type-specific toArrays should be equal",
+                    Arrays.asList(array),
+                    Arrays.asList(getCollection().toArray()));
+        }
         verify();
     }
 


[commons-collections] 03/03: Track changes

Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

aherbert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-collections.git

commit b284bc8e20c3802c9888956489bbbcfcc5845b51
Author: aherbert <a....@sussex.ac.uk>
AuthorDate: Fri Sep 23 12:31:03 2022 +0100

    Track changes
---
 src/changes/changes.xml | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 74d686548..b7a36d111 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -319,7 +319,11 @@
       Bump japicmp from 0.15.4 to 0.15.7.
     </action>
     <action type="update" dev="kinow" due-to="Dependabot">
-       Bump commons.pmd-impl.version from 6.46.0 to 6.49.0 #318, #327, #333.
+      Bump commons.pmd-impl.version from 6.46.0 to 6.49.0 #318, #327, #333.
+    </action>
+    <action type="update" dev="aherbert" due-to="Partha Protim Paul">
+      Correct test of Collection toArray(Object[]) vs toArray() to optionally ignore array order.
+      Ordering is not specified for some collections such as Bags.
     </action>
   </release>
   <release version="4.4" date="2019-07-05" description="Maintenance release.">