forked from junit-team/junit4
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request junit-team#293 from jglick/master
Sort test methods for predictability
- Loading branch information
Showing
8 changed files
with
79 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.junit.internal; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
|
||
public class MethodSorter { | ||
|
||
/** | ||
* Gets declared methods of a class in a predictable order. | ||
* Using the JVM order is unwise since the Java platform does not | ||
* specify any particular order, and in fact JDK 7 returns a more or less | ||
* random order; well-written test code would not assume any order, but some | ||
* does, and a predictable failure is better than a random failure on | ||
* certain platforms. Uses an unspecified but deterministic order. | ||
* @param clazz a class | ||
* @return same as {@link Class#getDeclaredMethods} but sorted | ||
* @see <a href="http://bugs.sun.com/view_bug.do?bug_id=7023180">JDK | ||
* (non-)bug #7023180</a> | ||
*/ | ||
public static Method[] getDeclaredMethods(Class<?> clazz) { | ||
Method[] methods = clazz.getDeclaredMethods(); | ||
Arrays.sort(methods, new Comparator<Method>() { | ||
@Override public int compare(Method m1, Method m2) { | ||
int i1 = m1.getName().hashCode(); | ||
int i2 = m2.getName().hashCode(); | ||
return i1 != i2 ? i1 - i2 : m1.toString().compareTo(m2.toString()); | ||
} | ||
}); | ||
return methods; | ||
} | ||
|
||
private MethodSorter() {} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.junit.internal; | ||
|
||
import java.util.Arrays; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
public class MethodSorterTest { | ||
|
||
@Test public void getDeclaredMethods() throws Exception { | ||
assertEquals("[void epsilon(), void beta(int[][]), java.lang.Object alpha(int,double,java.lang.Thread), void delta(), int gamma(), void gamma(boolean)]", declaredMethods(Dummy.class)); | ||
assertEquals("[void testOne()]", declaredMethods(Super.class)); | ||
assertEquals("[void testTwo()]", declaredMethods(Sub.class)); | ||
} | ||
|
||
private static String declaredMethods(Class<?> c) { | ||
return Arrays.toString(MethodSorter.getDeclaredMethods(c)).replace(c.getName() + '.', ""); | ||
} | ||
|
||
private static class Dummy { | ||
Object alpha(int i, double d, Thread t) {return null;} | ||
void beta(int[][] x) {} | ||
int gamma() {return 0;} | ||
void gamma(boolean b) {} | ||
void delta() {} | ||
void epsilon() {} | ||
} | ||
private static class Super { | ||
void testOne() {} | ||
} | ||
private static class Sub extends Super { | ||
void testTwo() {} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters