Permalink
Browse files

Changes after review by kcooney

  • Loading branch information...
1 parent c610a49 commit 35916619de9d9c3e7ec3d793ab3072c9e78db258 @matthewfarwell matthewfarwell committed Apr 4, 2012
@@ -8,25 +8,19 @@
import org.junit.runners.MethodSorters;
/**
- * SortMethodsWith allows the user to choose the order of execution of the methods within a test class.
+ * This class allows the user to choose the order of execution of the methods within a test class.
* <br/>
* <br/>
* The default order of execution of JUnit tests within a class is deterministic but not predictable.
- * Before 4.11, the behaviour was to run the test methods in byte code order, which pre-Java 7 was mostly predictable.
- * Java 7 (and some previous versions), does not guaranteee the order of execution, which can change from run to run,
- * so a deterministic sort was introduced.
+ * The order of execution is not guaranteed for Java 7 (and some previous versions), and can even change
+ * from run to run, so the order of execution was changed to be deterministic (in JUnit 4.11)
* <br/>
- * As a rule, test method execution should be independent of one another. However, there may be a number of dependent tests
- * either through error or by design. This class allows the user to specify the order of execution of test methods.
+ * It is recommended that test methods be written so that they are independent of the order that they are executed.
+ * However, there may be a number of dependent tests either through error or by design.
+ * This class allows the user to specify the order of execution of test methods.
* <br/>
- * There are four possibilities:
- * <ul>
- * <li>MethodSorters.DEFAULT: the default value, deterministic, but not predictable</li>
- * <li>MethodSorters.JVM: the order in which the tests are returned by the JVM, i.e. there is no sorting done</li>
- * <li>MethodSorters.NAME_ASC: sorted in order of method name, ascending</li>
- * <li>MethodSorters.NAME_DESC: sorter in order of method name, descending</li>
- * </ul>
- *
+ * For possibilities, see {@link MethodSorters}
+ *
* Here is an example:
*
* <pre>
@@ -41,7 +35,7 @@
@Target({ElementType.TYPE})
public @interface SortMethodsWith {
/**
- * Optionally specify <code>sorter</code> to have the methods executed in a particular order
+ * Optionally specify <code>value</code> to have the methods executed in a particular order
*/
MethodSorters value() default MethodSorters.DEFAULT;
}
@@ -7,21 +7,21 @@
import org.junit.SortMethodsWith;
public class MethodSorter {
- /**
- * DEFAULT sort order
- */
- public static Comparator<Method> DEFAULT = new Comparator<Method>() {
+ /**
+ * DEFAULT sort order
+ */
+ public static Comparator<Method> DEFAULT= new Comparator<Method>() {
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 i1 != i2 ? i1 - i2 : MethodSorter.compare(m1.toString(), m2.toString());
}
};
/**
* Method name ascending sort order
*/
- public static Comparator<Method> NAME_ASC = new Comparator<Method>() {
+ public static Comparator<Method> NAME_ASCENDING= new Comparator<Method>() {
public int compare(Method m1, Method m2) {
return MethodSorter.compare(m1.getName(), m2.getName());
}
@@ -34,7 +34,7 @@ private static int compare(String s1, String s2) {
/**
* Method name descending sort order
*/
- public static Comparator<Method> NAME_DESC = new Comparator<Method>() {
+ public static Comparator<Method> NAME_DESCENDING= new Comparator<Method>() {
public int compare(Method m1, Method m2) {
return MethodSorter.compare(m1.getName(), m2.getName()) * -1;
}
@@ -54,23 +54,23 @@ public int compare(Method m1, Method m2) {
* (non-)bug #7023180</a>
*/
public static Method[] getDeclaredMethods(Class<?> clazz) {
- Comparator<Method> comparator = getSorter(clazz.getAnnotation(SortMethodsWith.class));
-
- Method[] methods = clazz.getDeclaredMethods();
+ Comparator<Method> comparator= getSorter(clazz.getAnnotation(SortMethodsWith.class));
+
+ Method[] methods= clazz.getDeclaredMethods();
if (comparator != null) {
- Arrays.sort(methods, comparator);
+ Arrays.sort(methods, comparator);
}
return methods;
}
private MethodSorter() {}
- private static Comparator<Method> getSorter(SortMethodsWith sortMethodsWith) {
- if (sortMethodsWith == null) {
- return DEFAULT;
- }
+ private static Comparator<Method> getSorter(SortMethodsWith sortMethodsWith) {
+ if (sortMethodsWith == null) {
+ return DEFAULT;
+ }
- return sortMethodsWith.value().getComparator();
- }
+ return sortMethodsWith.value().getComparator();
+ }
}
@@ -6,16 +6,17 @@
import org.junit.internal.MethodSorter;
/**
- * Sort the methods into a specified execution order
+ * Sort the methods into a specified execution order.
+ * Defines common {@link MethodSorter} implementations.
*/
public enum MethodSorters {
- /** Name ascending */
- NAME_ASC(MethodSorter.NAME_ASC),
- /** Name descending */
- NAME_DESC(MethodSorter.NAME_DESC),
- /** default JVM, (no sort) */
+ /** Sorts the test methods by the method name, in lexicographic order */
+ NAME_ASCENDING(MethodSorter.NAME_ASCENDING),
+ /** Sorts the test methods by the method name, in reverse lexicographic order */
+ NAME_DESCENDING(MethodSorter.NAME_DESCENDING),
+ /** the order in which the tests are returned by the JVM, i.e. there is no sorting done */
JVM(null),
- /** Default, deterministic but not predictable */
+ /** the default value, deterministic, but not predictable */
DEFAULT(MethodSorter.DEFAULT);
private final Comparator<Method> fComparator;
@@ -25,7 +25,7 @@ void testTwo() {}
}
private String toString(Class<?> clazz, Method[] methods) {
- return Arrays.toString(methods).replace(clazz.getName() + '.', "");
+ return Arrays.toString(methods).replace(clazz.getName() + '.', "");
}
private String declaredMethods(Class<?> clazz) {
@@ -37,7 +37,7 @@ private String declaredMethods(Class<?> clazz) {
assertEquals("[void testOne()]", declaredMethods(Super.class));
assertEquals("[void testTwo()]", declaredMethods(Sub.class));
}
-
+
@SortMethodsWith(MethodSorters.DEFAULT)
private static class DummySortWithDefault {
Object alpha(int i, double d, Thread t) {return null;}
@@ -48,10 +48,10 @@ void delta() {}
void epsilon() {}
}
- @Test public void testDefaultSorter() {
+ @Test public void testDefaultSorter() {
assertEquals("[void epsilon(), void beta(int[][]), java.lang.Object alpha(int,double,java.lang.Thread), void delta(), int gamma(), void gamma(boolean)]", declaredMethods(DummySortWithDefault.class));
- }
-
+ }
+
@SortMethodsWith(MethodSorters.JVM)
private static class DummySortJvm {
Object alpha(int i, double d, Thread t) {return null;}
@@ -62,14 +62,14 @@ void delta() {}
void epsilon() {}
}
- @Test public void testSortWithJvm() {
- Class<?> clazz = DummySortJvm.class;
+ @Test public void testSortWithJvm() {
+ Class<?> clazz = DummySortJvm.class;
String actual = toString(clazz, clazz.getDeclaredMethods());
assertEquals(actual, declaredMethods(clazz));
- }
+ }
- @SortMethodsWith(MethodSorters.NAME_ASC)
+ @SortMethodsWith(MethodSorters.NAME_ASCENDING)
private static class DummySortWithNameAsc {
Object alpha(int i, double d, Thread t) {return null;}
void beta(int[][] x) {}
@@ -79,11 +79,11 @@ void delta() {}
void epsilon() {}
}
- @Test public void testNameAsc() {
+ @Test public void testNameAsc() {
assertEquals("[java.lang.Object alpha(int,double,java.lang.Thread), void beta(int[][]), void delta(), void epsilon(), int gamma(), void gamma(boolean)]", declaredMethods(DummySortWithNameAsc.class));
- }
+ }
- @SortMethodsWith(MethodSorters.NAME_DESC)
+ @SortMethodsWith(MethodSorters.NAME_DESCENDING)
private static class DummySortWithNameDesc {
Object alpha(int i, double d, Thread t) {return null;}
void beta(int[][] x) {}
@@ -93,7 +93,7 @@ void delta() {}
void epsilon() {}
}
- @Test public void testNameDesc() {
+ @Test public void testNameDesc() {
assertEquals("[int gamma(), void gamma(boolean), void epsilon(), void delta(), void beta(int[][]), java.lang.Object alpha(int,double,java.lang.Thread)]", declaredMethods(DummySortWithNameDesc.class));
- }
+ }
}

0 comments on commit 3591661

Please sign in to comment.