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 extends AbstractCollectionTest { 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 extends AbstractBagTest { return "4"; } + @Override + protected int getIterationBehaviour(){ + return UNORDERED; + } + // public void testCreate() throws Exception { // Bag 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; *
  • {@link #isFailFastSupported()} * *

    + * Indicate Collection Behaviour + *

    + * Override these if your collection makes specific behaviour guarantees: + *

      + *
    • {@link #getIterationBehaviour()}
    • + *
    + *

    * Fixture Methods *

    * Fixtures are used to verify that the operation results in correct state @@ -134,6 +141,13 @@ public abstract class AbstractCollectionTest 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 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 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> classes = new HashSet<>(); @@ -1116,9 +1147,16 @@ public abstract class AbstractCollectionTest 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(); }