Skip to content

Commit

Permalink
Change Sorter to no longer extend Ordering.
Browse files Browse the repository at this point in the history
Change Orderable to no longer extend Sortable.
  • Loading branch information
kcooney committed Apr 30, 2015
1 parent 4770fff commit a19a3ab
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 130 deletions.
7 changes: 4 additions & 3 deletions src/main/java/junit/framework/JUnit4TestAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.Filterable;
import org.junit.runner.manipulation.GenericOrdering;
import org.junit.runner.manipulation.InvalidOrderingException;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.manipulation.Orderable;
import org.junit.runner.manipulation.Ordering;
import org.junit.runner.manipulation.Sortable;
import org.junit.runner.manipulation.Sorter;

public class JUnit4TestAdapter implements Test, Filterable, Orderable, Describable {
public class JUnit4TestAdapter implements Test, Filterable, Sortable, Orderable, Describable {
private final Class<?> fNewTestClass;

private final Runner fRunner;
Expand Down Expand Up @@ -86,7 +87,7 @@ public void sort(Sorter sorter) {
sorter.apply(fRunner);
}

public void order(GenericOrdering ordering) throws InvalidOrderingException {
public void order(Ordering ordering) throws InvalidOrderingException {
ordering.apply(fRunner);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.Filterable;
import org.junit.runner.manipulation.GenericOrdering;
import org.junit.runner.manipulation.InvalidOrderingException;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.manipulation.Orderable;
import org.junit.runner.manipulation.Ordering;
import org.junit.runner.manipulation.Sortable;
import org.junit.runner.manipulation.Sorter;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;

public class JUnit38ClassRunner extends Runner implements Filterable, Orderable {
public class JUnit38ClassRunner extends Runner implements Filterable, Sortable, Orderable {
private static final class OldTestClassAdaptingListener implements
TestListener {
private final RunNotifier notifier;
Expand Down Expand Up @@ -174,7 +174,7 @@ public void sort(Sorter sorter) {
}
}

public void order(GenericOrdering ordering) throws InvalidOrderingException {
public void order(Ordering ordering) throws InvalidOrderingException {
if (getTest() instanceof Orderable) {
Orderable adapter = (Orderable) getTest();
adapter.order(ordering);
Expand Down
36 changes: 0 additions & 36 deletions src/main/java/org/junit/runner/manipulation/GenericOrdering.java

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/org/junit/runner/manipulation/Orderable.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
*
* @since 4.13
*/
public interface Orderable extends Sortable {
public interface Orderable {

/**
* Orders the tests using <code>ordering</code>
*
* @throws InvalidOrderingException if ordering does something invalid (like remove or add children)
*/
void order(GenericOrdering ordering) throws InvalidOrderingException;
void order(Ordering ordering) throws InvalidOrderingException;
}
9 changes: 1 addition & 8 deletions src/main/java/org/junit/runner/manipulation/Ordering.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,9 @@ public static Ordering definedBy(Class<? extends Ordering> orderingClass)
* @throws InvalidOrderingException if ordering does something invalid (like remove or add children)
*/
public void apply(Object runner) throws InvalidOrderingException {
/*
* If the runner is Sortable but not Orderable and this Ordering is a
* Sorter, then the Sorter subclass overrides apply() to apply the sort.
*
* Note that GenericOrdering also overrides apply() to avoid having a
* GenericOrdering wrap another GenericOrdering.
*/
if (runner instanceof Orderable) {
Orderable orderable = (Orderable) runner;
orderable.order(new GenericOrdering(this));
orderable.order(this);
}
}

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/org/junit/runner/manipulation/SortBasedOrdering.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.junit.runner.manipulation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.junit.runner.Description;

class SortBasedOrdering extends Ordering {
private final Sorter sorter;

public SortBasedOrdering(Sorter sorter) {
this.sorter = sorter;
}

@Override
public void apply(Object runner) {
if (runner instanceof Sortable) {
// Sorting is more efficient than ordering, so apply the sorter.
sorter.apply(runner);
return;
}

try {
super.apply(runner);
} catch (InvalidOrderingException e) {
throw new AssertionError("SortBasedOrdering should always produce a valid ordering");
}
}

@Override
public List<Description> order(Collection<Description> siblings) {
List<Description> sorted = new ArrayList<Description>(siblings);
Collections.sort(sorted, sorter);
return sorted;
}
}
29 changes: 5 additions & 24 deletions src/main/java/org/junit/runner/manipulation/Sorter.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package org.junit.runner.manipulation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.runner.Description;

Expand All @@ -14,7 +10,7 @@
*
* @since 4.0
*/
public class Sorter extends Ordering implements Comparator<Description> {
public class Sorter implements Comparator<Description> {
/**
* NULL is a <code>Sorter</code> that leaves elements in an undefined order
*/
Expand All @@ -39,33 +35,18 @@ public Sorter(Comparator<Description> comparator) {
/**
* Sorts the test in <code>runner</code> using <code>comparator</code>.
*/
@Override
public void apply(Object runner) {
/*
* Note that all runners that are Orderable are also Sortable (because
* Orderable extends Sortable). Sorting is more efficient than ordering,
* so we override the parent behavior so we sort instead.
*/
if (runner instanceof Sortable) {
Sortable sortable = (Sortable) runner;
sortable.sort(this);
} else if (runner instanceof Orderable) {
SortBasedOrdering ordering = new SortBasedOrdering(this);
ordering.apply(runner);
}
}

public int compare(Description o1, Description o2) {
return comparator.compare(o1, o2);
}

@Override
public final List<Description> order(Collection<Description> siblings) {
/*
* In practice, we will never get here--Sorters do their work in the
* compare() method--but the Liskov substitution principle demands that
* we obey the general contract of Orderable. Luckily, it's trivial to
* implement.
*/
List<Description> sorted = new ArrayList<Description>(siblings);
Collections.sort(sorted, this); // Note: it would be incorrect to pass in "comparator"
return sorted;
}
}

7 changes: 4 additions & 3 deletions src/main/java/org/junit/runners/ParentRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.Filterable;
import org.junit.runner.manipulation.GenericOrdering;
import org.junit.runner.manipulation.InvalidOrderingException;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.manipulation.Orderable;
import org.junit.runner.manipulation.Ordering;
import org.junit.runner.manipulation.Sortable;
import org.junit.runner.manipulation.Sorter;
import org.junit.runner.notification.RunNotifier;
import org.junit.runner.notification.StoppedByUserException;
Expand All @@ -61,7 +62,7 @@
* @since 4.5
*/
public abstract class ParentRunner<T> extends Runner implements Filterable,
Orderable {
Sortable, Orderable {
private static final List<TestClassValidator> VALIDATORS = Arrays.asList(
new AnnotationsValidator(), new PublicClassValidator());

Expand Down Expand Up @@ -417,7 +418,7 @@ public void sort(Sorter sorter) {
*
* @since 4.13
*/
public void order(GenericOrdering ordering) throws InvalidOrderingException {
public void order(Ordering ordering) throws InvalidOrderingException {
synchronized (childrenLock) {
List<T> children = getFilteredChildren();
Map<Description, List<T>> childMap = new LinkedHashMap<Description, List<T>>(
Expand Down
98 changes: 49 additions & 49 deletions src/test/java/org/junit/tests/manipulation/OrderWithTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,55 +127,55 @@ public void orderingBackwardWorksOnSuite() {
}
}

public static class TestClassRunnerIsSortableViaOrderWith {
private static String log = "";

public static class Unordered {
@Test
public void a() {
log += "a";
}

@Test
public void b() {
log += "b";
}

@Test
public void c() {
log += "c";
}
}

@Before
public void resetLog() {
log = "";
}

@OrderWith(AlphanumericSorter.class)
public static class SortedAlphanumerically extends Unordered {
}

@OrderWith(ReverseAlphanumericSorter.class)
public static class SortedReverseAlphanumerically extends Unordered {
}

@Test
public void sortingForwardWorksOnTestClassRunner() {
Request forward = Request.aClass(SortedAlphanumerically.class);

new JUnitCore().run(forward);
assertEquals("abc", log);
}

@Test
public void sortingBackwardWorksOnTestClassRunner() {
Request backward = Request.aClass(SortedReverseAlphanumerically.class);

new JUnitCore().run(backward);
assertEquals("cba", log);
}
}
// public static class TestClassRunnerIsSortableViaOrderWith {
// private static String log = "";
//
// public static class Unordered {
// @Test
// public void a() {
// log += "a";
// }
//
// @Test
// public void b() {
// log += "b";
// }
//
// @Test
// public void c() {
// log += "c";
// }
// }
//
// @Before
// public void resetLog() {
// log = "";
// }
//
// @OrderWith(AlphanumericSorter.class)
// public static class SortedAlphanumerically extends Unordered {
// }
//
// @OrderWith(ReverseAlphanumericSorter.class)
// public static class SortedReverseAlphanumerically extends Unordered {
// }
//
// @Test
// public void sortingForwardWorksOnTestClassRunner() {
// Request forward = Request.aClass(SortedAlphanumerically.class);
//
// new JUnitCore().run(forward);
// assertEquals("abc", log);
// }
//
// @Test
// public void sortingBackwardWorksOnTestClassRunner() {
// Request backward = Request.aClass(SortedReverseAlphanumerically.class);
//
// new JUnitCore().run(backward);
// assertEquals("cba", log);
// }
// }

public static class TestClassRunnerIsOrderableWithSuiteMethod {
private static String log = "";
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/junit/tests/manipulation/OrderableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import org.junit.runner.Request;
import org.junit.runner.RunWith;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.GenericOrdering;
import org.junit.runner.manipulation.InvalidOrderingException;
import org.junit.runner.manipulation.Orderable;
import org.junit.runner.manipulation.Ordering;
import org.junit.runner.manipulation.Sorter;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
Expand Down Expand Up @@ -138,7 +138,7 @@ public Description getDescription() {
return delegate.getDescription();
}

public void order(GenericOrdering ordering) throws InvalidOrderingException {
public void order(Ordering ordering) throws InvalidOrderingException {
delegate.order(ordering);
}

Expand Down

0 comments on commit a19a3ab

Please sign in to comment.