[
https://issues.apache.org/jira/browse/COLLECTIONS-804?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17492282#comment-17492282
]
Alex Herbert commented on COLLECTIONS-804:
------------------------------------------
The testCollectionToArray2 method is in AbstractCollectionTest and is also used
for collections that are ordered.
>From the java.util.Collection toArray method:
{noformat}
If this collection makes any guarantees as to what order its elements are
returned by its iterator, this method must return the elements in the same
order.{noformat}
This would suggest the test requires a property to determine if the collection
under test should have an ordered iterator or not. If not then the two arrays
can be compared using a HashBag. Otherwise the toArray output should be checked
against the iterator.
> testCollectionToArray2() is non deterministic
> ---------------------------------------------
>
> Key: COLLECTIONS-804
> URL: https://issues.apache.org/jira/browse/COLLECTIONS-804
> Project: Commons Collections
> Issue Type: Bug
> Components: Collection
> Affects Versions: 4.4
> Reporter: Vivek Gupta
> Priority: Minor
>
> The test
> {{org.apache.commons.collections4.collection.AbstractCollectionTest.testCollectionToArray2()}}
> can fail if two {{toArray()}} calls return elements in a different order.
> One can check for that using the NonDex tool
> ([https://github.com/TestingResearchIllinois/NonDex]).
> Bug repoduction:
> # Clone the repo and cd into it.
> # {{mvn test
> -Dtest=org.apache.commons.collections4.bidimap.DualHashBidiMapTest#testCollectionToArray2
> -Drat.skip}} This test will pass.
> # {{mvn edu.illinois:nondex-maven-plugin:1.1.2:nondex
> -Dtest=org.apache.commons.collections4.bidimap.DualHashBidiMapTest#testCollectionToArray2
> -Drat.skip}} This command runs the NonDex tool on the the test. Near the end
> of the output, you will see a summary of test failures, where you will find
> {{testCollectionToArray2()}} function listed.
> The source of the bug:
> The test gets an array representation of the collections and then compares
> them, taking the order into account. However, Java Collections need not be
> necessarily ordered. Its member function {{toArray()}} also does not
> guarantee a deterministic order of the elements in its returned list. As
> such, we should ignore the order of the elements in the assertion.
> Proposed fix:
> We ignore the order of array lists by putting its elements into separate
> multisets and then comparing those multisets for equality. Multiset is not a
> part of the standard Java library. However, conveniently enough, this
> repository already had an implementation of multiset called HashBags, which I
> used.
--
This message was sent by Atlassian Jira
(v8.20.1#820001)