Fix flaky test failure in SynchronizedBagTest#testCollectionToArray2

Closes #336
This commit is contained in:
Partha-SUST16 2022-09-23 12:07:45 +01:00 committed by aherbert
parent 16710abc9a
commit 30a9ab835d
3 changed files with 52 additions and 5 deletions

View File

@ -674,6 +674,10 @@ public abstract class AbstractBagTest<T> extends AbstractCollectionTest<T> {
super.verify();
}
@Override
protected int getIterationBehaviour(){
return AbstractBagTest.this.getIterationBehaviour();
}
}
/**

View File

@ -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");

View File

@ -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();
}