1010
1111package org .junit .platform .engine .support .hierarchical ;
1212
13- import static java .util .Comparator .naturalOrder ;
14- import static java .util .Comparator .reverseOrder ;
13+ import static java .util .Comparator .comparing ;
1514import static java .util .Objects .requireNonNull ;
1615import static java .util .concurrent .CompletableFuture .completedFuture ;
1716import static java .util .concurrent .TimeUnit .SECONDS ;
2322import java .util .ArrayDeque ;
2423import java .util .ArrayList ;
2524import java .util .Collection ;
25+ import java .util .Comparator ;
2626import java .util .Deque ;
2727import java .util .EnumMap ;
2828import java .util .Iterator ;
@@ -361,13 +361,13 @@ else if (child.getExecutionMode() == SAME_THREAD) {
361361 if (!queueEntries .isEmpty ()) {
362362 if (sameThreadTasks .isEmpty ()) {
363363 // hold back one task for this thread
364- var lastEntry = queueEntries .stream ().max (naturalOrder () ).orElseThrow ();
364+ var lastEntry = queueEntries .stream ().max (WorkQueue . Entry . COMPARATOR ).orElseThrow ();
365365 queueEntries .remove (lastEntry );
366366 sameThreadTasks .add (lastEntry .task );
367367 }
368368 forkAll (queueEntries );
369369 }
370- queueEntries .sort (reverseOrder ());
370+ queueEntries .sort (WorkQueue . Entry . COMPARATOR . reversed ());
371371 return queueEntries ;
372372 }
373373
@@ -653,7 +653,8 @@ private enum BlockingMode {
653653 }
654654
655655 private static class WorkQueue implements Iterable <WorkQueue .Entry > {
656- private final Set <Entry > queue = new ConcurrentSkipListSet <>();
656+
657+ private final Set <Entry > queue = new ConcurrentSkipListSet <>(Entry .COMPARATOR );
657658
658659 Entry add (TestTask task , int index ) {
659660 Entry entry = new Entry (task , index );
@@ -691,7 +692,15 @@ public Iterator<Entry> iterator() {
691692 return queue .iterator ();
692693 }
693694
694- private static final class Entry implements Comparable <Entry > {
695+ private static final class Entry {
696+
697+ private static final Comparator <Entry > SAME_LENGTH_UNIQUE_ID_COMPARATOR //
698+ = (e1 , e2 ) -> compareBy (e1 .uniqueId (), e2 .uniqueId ());
699+
700+ private static final Comparator <Entry > COMPARATOR = comparing (Entry ::level ).reversed () //
701+ .thenComparing (Entry ::isContainer ) // tests before containers
702+ .thenComparing (comparing (Entry ::index ).reversed ()) //
703+ .thenComparing (SAME_LENGTH_UNIQUE_ID_COMPARATOR .reversed ());
695704
696705 private final TestTask task ;
697706 private final CompletableFuture <@ Nullable Void > future ;
@@ -712,24 +721,7 @@ private static final class Entry implements Comparable<Entry> {
712721 this .index = index ;
713722 }
714723
715- @ Override
716- public int compareTo (Entry that ) {
717- var result = Integer .compare (that .getLevel (), getLevel ());
718- if (result != 0 ) {
719- return result ;
720- }
721- result = Boolean .compare (this .isContainer (), that .isContainer ());
722- if (result != 0 ) {
723- return result ;
724- }
725- result = Integer .compare (that .index , index );
726- if (result != 0 ) {
727- return result ;
728- }
729- return compareBy (that .uniqueId (), this .uniqueId ());
730- }
731-
732- private int compareBy (UniqueId a , UniqueId b ) {
724+ private static int compareBy (UniqueId a , UniqueId b ) {
733725 var aIterator = a .getSegments ().iterator ();
734726 var bIterator = b .getSegments ().iterator ();
735727
@@ -745,15 +737,19 @@ private int compareBy(UniqueId a, UniqueId b) {
745737 return 0 ;
746738 }
747739
748- private int compareBy (UniqueId .Segment a , UniqueId .Segment b ) {
740+ private static int compareBy (UniqueId .Segment a , UniqueId .Segment b ) {
749741 int result = a .getType ().compareTo (b .getType ());
750742 if (result != 0 ) {
751743 return result ;
752744 }
753745 return a .getValue ().compareTo (b .getValue ());
754746 }
755747
756- private int getLevel () {
748+ private int index () {
749+ return this .index ;
750+ }
751+
752+ private int level () {
757753 return uniqueId ().getSegments ().size ();
758754 }
759755
0 commit comments