Uploaded image for project: 'Confluence Data Center'
  1. Confluence Data Center
  2. CONFSERVER-58059

NaturalStringComparator causes "Comparison method violates its general contract!" when specific order of strings given

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Low Low
    • None
    • 6.14.2, 6.14.3
    • None

      Issue Summary

      TimSort throws "Comparison method violates its general contract!" error using NaturalStringComparator.

      Environment

      This was confirmed by unit testing against a source code of 6.14.2.

      Steps to Reproduce

      Add a following testing method against test class for NaturalStringComparator

          @Test
          public void testContractViolationError() {
              final List<String> list = Arrays.asList(
                      "a",
                      "a",
                      "亜",
                      "a",
                      "a",
                      "a",
                      "a",
                      "a",
                      "a",
                      "a",
                      "1",
                      "a",
                      "a",
                      "1",
                      "1",
                      "a",
                      "a",
                      "a",
                      "亜",
                      "1",
                      "a",
                      "亜",
                      "a",
                      "亜",
                      "10",
                      "5",
                      "5",
                      "a",
                      "あ",
                      "亜",
                      "a",
                      "a",
                      "亜",
                      "a",
                      "a",
                      "a",
                      "1",
                      "1",
                      "1",
                      "a",
                      "a",
                      "1",
                      "a",
                      "1",
                      "a",
                      "亜"
              );
              try {
                  list.sort(comparator);
              }catch(Exception e)
              {
                  e.printStackTrace();
                  Assert.fail();
              }
          }
      

      Expected Results

      No exception raised and sorted correctly.

      Actual Results

      Test fails with following Exception.

      java.lang.IllegalArgumentException: Comparison method violates its general contract!
      	at java.util.TimSort.mergeLo(TimSort.java:777)
      	at java.util.TimSort.mergeAt(TimSort.java:514)
      	at java.util.TimSort.mergeCollapse(TimSort.java:441)
      	at java.util.TimSort.sort(TimSort.java:245)
      	at java.util.Arrays.sort(Arrays.java:1438)
      	at java.util.Arrays$ArrayList.sort(Arrays.java:3895)
      	at com.atlassian.confluence.pages.TestNaturalStringComparator.testContractViolationError(TestNaturalStringComparator.java:275)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.lang.reflect.Method.invoke(Method.java:498)
      	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
      	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
      	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      	at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
      	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
      	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
      	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
      	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
      
      

      Notes

      This was confirmed by unit testing in my end, however a customer faced this error in page tree sorting.

      Workaround

      Adding this JVM system property fixes the issue:
      -Djava.util.Arrays.useLegacyMergeSort=true

              Unassigned Unassigned
              nmukai Nobuyuki Mukai
              Votes:
              2 Vote for this issue
              Watchers:
              5 Start watching this issue

                Created:
                Updated: