Skip to content

Commit

Permalink
make MethodSorter.NAME_ASCENDING a deterministic comparator, with Met…
Browse files Browse the repository at this point in the history
…hod.toString used as a tiebreaker
  • Loading branch information
ibeauregard committed May 30, 2012
1 parent b508fe7 commit 32330b1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/junit/internal/MethodSorter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ public int compare(Method m1, Method m2) {
};

/**
* Method name ascending lexicographic sort order
* Method name ascending lexicographic sort order, with toString as a tiebreaker
*/
public static Comparator<Method> NAME_ASCENDING= new Comparator<Method>() {
public int compare(Method m1, Method m2) {
return MethodSorter.compare(m1.getName(), m2.getName());
return MethodSorter.compare(m1.getName() + m1.toString(), m2.getName() + m2.toString());
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/junit/runners/MethodSorters.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Defines common {@link MethodSorter} implementations.
*/
public enum MethodSorters {
/** Sorts the test methods by the method name, in lexicographic order */
/** Sorts the test methods by the method name, in lexicographic order, with Method.toString used as a tiebreaker */
NAME_ASCENDING(MethodSorter.NAME_ASCENDING),
/** Leaves the test methods in the order returned by the JVM.
* Note that the order from the JVM my vary from run to run */
Expand Down
7 changes: 2 additions & 5 deletions src/test/java/org/junit/internal/MethodSorterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,7 @@ void epsilon() {}
}

@Test public void testNameAsc() {
// see http://bugs.sun.com/view_bug.do?bug_id=7023180 for why two alternatives are possible
final String expectedAlternative1 = Arrays.asList(ALPHA, BETA, DELTA, EPSILON, GAMMA_VOID, GAMMA_BOOLEAN).toString();
final String expectedAlternative2 = Arrays.asList(ALPHA, BETA, DELTA, EPSILON, GAMMA_BOOLEAN, GAMMA_VOID).toString();

assertThat(declaredMethods(DummySortWithNameAsc.class), anyOf(equalTo(expectedAlternative1), equalTo(expectedAlternative2)));
String[] expected= new String[] { ALPHA, BETA, DELTA, EPSILON, GAMMA_VOID, GAMMA_BOOLEAN };
assertEquals(Arrays.asList(expected).toString(), declaredMethods(DummySortWithNameAsc.class));
}
}

0 comments on commit 32330b1

Please sign in to comment.