Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

org.testng.internal.BaseTestMethod does respect general contract of Comparable #116

Open
romlom opened this Issue · 3 comments

3 participants

@romlom

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.

@triceo

Would it be possible to merge this? The fix looks simple enough and the issue is affecting people, as can be seen when querying Google.

@cbeust
Owner

Hi Lukáš,

Sure, but do you have a pull request? I don't see one.

@triceo

Ah, my bad, I misread the page - I thought this was a pull request. I guess I'll have to provide one now, then. ;-)

@triceo triceo referenced this issue from a commit
@triceo triceo Fixes issue #116: org.testng.internal.BaseTestMethod does respect gen…
…eral contract of Comparable
7e5c60a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.