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.
Add Ordering, Orderable and @OrderWith (junit-team#1130)
* Add Ordering, Orderable and @OrderWith. These APIs allow arbitrary ordering of tests, including randomization.
- Loading branch information
Showing
24 changed files
with
1,292 additions
and
56 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
30 changes: 30 additions & 0 deletions
30
src/main/java/org/junit/internal/requests/MemoizingRequest.java
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,30 @@ | ||
package org.junit.internal.requests; | ||
|
||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
import org.junit.runner.Request; | ||
import org.junit.runner.Runner; | ||
|
||
abstract class MemoizingRequest extends Request { | ||
private final Lock runnerLock = new ReentrantLock(); | ||
private volatile Runner runner; | ||
|
||
@Override | ||
public final Runner getRunner() { | ||
if (runner == null) { | ||
runnerLock.lock(); | ||
try { | ||
if (runner == null) { | ||
runner = createRunner(); | ||
} | ||
} finally { | ||
runnerLock.unlock(); | ||
} | ||
} | ||
return runner; | ||
} | ||
|
||
/** Creates the {@link Runner} to return from {@link #getRunner()}. Called at most once. */ | ||
protected abstract Runner createRunner(); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/org/junit/internal/requests/OrderingRequest.java
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,29 @@ | ||
package org.junit.internal.requests; | ||
|
||
import org.junit.internal.runners.ErrorReportingRunner; | ||
import org.junit.runner.Request; | ||
import org.junit.runner.Runner; | ||
import org.junit.runner.manipulation.InvalidOrderingException; | ||
import org.junit.runner.manipulation.Ordering; | ||
|
||
/** @since 4.13 */ | ||
public class OrderingRequest extends MemoizingRequest { | ||
private final Request request; | ||
private final Ordering ordering; | ||
|
||
public OrderingRequest(Request request, Ordering ordering) { | ||
this.request = request; | ||
this.ordering = ordering; | ||
} | ||
|
||
@Override | ||
protected Runner createRunner() { | ||
Runner runner = request.getRunner(); | ||
try { | ||
ordering.apply(runner); | ||
} catch (InvalidOrderingException e) { | ||
return new ErrorReportingRunner(ordering.getClass(), e); | ||
} | ||
return runner; | ||
} | ||
} |
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,26 @@ | ||
package org.junit.runner; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Inherited; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import org.junit.runner.manipulation.Ordering; | ||
|
||
/** | ||
* When a test class is annotated with <code>@OrderWith</code> or extends a class annotated | ||
* with <code>@OrderWith</code>, JUnit will order the tests in the test class (and child | ||
* test classes, if any) using the ordering defined by the {@link Ordering} class. | ||
* | ||
* @since 4.13 | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.TYPE) | ||
@Inherited | ||
public @interface OrderWith { | ||
/** | ||
* Gets a class that extends {@link Ordering}. The class must have a public no-arg constructor. | ||
*/ | ||
Class<? extends Ordering.Factory> value(); | ||
} |
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
27 changes: 27 additions & 0 deletions
27
src/main/java/org/junit/runner/manipulation/Alphanumeric.java
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,27 @@ | ||
package org.junit.runner.manipulation; | ||
|
||
import java.util.Comparator; | ||
|
||
import org.junit.runner.Description; | ||
|
||
/** | ||
* A sorter that orders tests alphanumerically by test name. | ||
* | ||
* @since 4.13 | ||
*/ | ||
public final class Alphanumeric extends Sorter implements Ordering.Factory { | ||
|
||
public Alphanumeric() { | ||
super(COMPARATOR); | ||
} | ||
|
||
public Ordering create(Context context) { | ||
return this; | ||
} | ||
|
||
private static final Comparator<Description> COMPARATOR = new Comparator<Description>() { | ||
public int compare(Description o1, Description o2) { | ||
return o1.getDisplayName().compareTo(o2.getDisplayName()); | ||
} | ||
}; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/junit/runner/manipulation/InvalidOrderingException.java
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,21 @@ | ||
package org.junit.runner.manipulation; | ||
|
||
/** | ||
* Thrown when an ordering does something invalid (like remove or add children) | ||
* | ||
* @since 4.13 | ||
*/ | ||
public class InvalidOrderingException extends Exception { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public InvalidOrderingException() { | ||
} | ||
|
||
public InvalidOrderingException(String message) { | ||
super(message); | ||
} | ||
|
||
public InvalidOrderingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/junit/runner/manipulation/Orderable.java
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,21 @@ | ||
package org.junit.runner.manipulation; | ||
|
||
/** | ||
* Interface for runners that allow ordering of tests. | ||
* | ||
* <p>Beware of using this interface to cope with order dependencies between tests. | ||
* Tests that are isolated from each other are less expensive to maintain and | ||
* can be run individually. | ||
* | ||
* @since 4.13 | ||
*/ | ||
public interface Orderable extends Sortable { | ||
|
||
/** | ||
* Orders the tests using <code>orderer</code> | ||
* | ||
* @throws InvalidOrderingException if orderer does something invalid (like remove or add | ||
* children) | ||
*/ | ||
void order(Orderer orderer) throws InvalidOrderingException; | ||
} |
Oops, something went wrong.