cbeust/testng — Issue #116: org.testng.internal.BaseTestMethod does respect general contract of Comparable
Author: romlom
Created: 2011-10-26 09:54:47+00:00
State: open

from int java.lang.Comparable.compareTo(T o) javadoc:

The implementor must ensure sgn(x.compareTo(y)) == -sgn(y.compareTo(x)) for all x and y.

So if we put y = x we got:

sgn(x.compare(x)) == -sgn(x.compare(x))

So either x.compare(x) throws exception either return 0

!BUT!

org.testng.internal.BaseTestMethod:

public int compareTo(Object o) {
int result = -2;
Class thisClass = getRealClass();
Class otherClass = ((ITestNGMethod) o).getRealClass();
if (thisClass.isAssignableFrom(otherClass)) {
result = -1;
} else if (otherClass.isAssignableFrom(thisClass)) {
result = 1;
} else if (equals(o)) {
result = 0;
}

return result;
}

will return -1 when comparing object to itself.

I am reporting this because i got following exception:

java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.ComparableTimSort.mergeHi(ComparableTimSort.java:835)
at java.util.ComparableTimSort.mergeAt(ComparableTimSort.java:453)
at java.util.ComparableTimSort.mergeCollapse(ComparableTimSort.java:374)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:182)
at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
at java.util.Arrays.sort(Arrays.java:472)
at java.util.Collections.sort(Collections.java:155)
at org.testng.internal.MethodInheritance.sortMethodsByInheritance(MethodInheritance.java:163)
at org.testng.internal.MethodInheritance.fixMethodInheritance(MethodInheritance.java:92)
at org.testng.internal.MethodHelper.sortMethods(MethodHelper.java:313)
at org.testng.internal.MethodHelper.collectAndOrderMethods(MethodHelper.java:51)
at org.testng.TestRunner.initMethods(TestRunner.java:463)
at org.testng.TestRunner.init(TestRunner.java:231)
at org.testng.TestRunner.init(TestRunner.java:201)
at org.testng.TestRunner.(TestRunner.java:157)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:137)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:266)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:568)
at org.testng.SuiteRunner.init(SuiteRunner.java:158)
at org.testng.SuiteRunner.(SuiteRunner.java:112)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1246)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1233)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1085)
at org.testng.TestNG.run(TestNG.java:1006)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:107)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:199)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:170)

Unfortunately, I did not manage to extract small example for testing purposes. But I hope issue is detailed enough.