-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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.
These APIs allow arbitrary ordernig of tests, including randomization.
- Loading branch information
Showing
21 changed files
with
1,059 additions
and
37 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,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 { | ||
/** | ||
* @return a class that extends {@link Ordering} (must have a public no-argument constructor) | ||
*/ | ||
Class<? extends Ordering> 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
32 changes: 32 additions & 0 deletions
32
src/main/java/org/junit/runner/manipulation/GenericOrdering.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,32 @@ | ||
package org.junit.runner.manipulation; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import org.junit.runner.Description; | ||
|
||
/** | ||
* An {@link Ordering} that is not a {@link Sorter}. | ||
* | ||
* @since 4.13 | ||
*/ | ||
public final class GenericOrdering extends Ordering { | ||
private final Ordering delegate; | ||
|
||
GenericOrdering(Ordering delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public List<Description> order(Collection<Description> siblings) { | ||
return delegate.order(siblings); | ||
} | ||
|
||
@Override | ||
public void apply(Object runner) throws InvalidOrderingException { | ||
if (runner instanceof Orderable) { | ||
Orderable orderable = (Orderable) runner; | ||
orderable.order(this); | ||
} | ||
} | ||
} |
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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
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,20 @@ | ||
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>ordering</code> | ||
* | ||
* @throws InvalidOrderingException if ordering does something invalid (like remove or add children) | ||
*/ | ||
void order(GenericOrdering ordering) throws InvalidOrderingException; | ||
} |
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,71 @@ | ||
package org.junit.runner.manipulation; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Random; | ||
|
||
import org.junit.runner.Description; | ||
|
||
/** | ||
* Reorders tests. An {@code Ordering} can reverse the order of tests, sort the | ||
* order or even shuffle the order. | ||
* | ||
* <p>In general you will not need to use a <code>Ordering</code> directly. | ||
* Instead, use {@link org.junit.runner.Request#orderWith(Ordering)}. | ||
* | ||
* @since 4.13 | ||
*/ | ||
public abstract class Ordering { | ||
|
||
/** | ||
* Creates an {@link Ordering} that shuffles the items using the given | ||
* {@link Random} instance. | ||
*/ | ||
public static Ordering shuffledBy(final Random random) { | ||
return new Ordering() { | ||
@Override | ||
public List<Description> order(Collection<Description> siblings) { | ||
List<Description> shuffled = new ArrayList<Description>(siblings); | ||
Collections.shuffle(shuffled, random); | ||
return shuffled; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Creates an {@link Ordering} from the given class. The class must have a public no-argument constructor. | ||
* | ||
* @throws InvalidOrderingException if the instance could not be created | ||
*/ | ||
public static Ordering definedBy(Class<? extends Ordering> orderingClass) | ||
throws InvalidOrderingException { | ||
try { | ||
return orderingClass.newInstance(); | ||
} catch (InstantiationException e) { | ||
throw new InvalidOrderingException("Could not create ordering", e); | ||
} catch (IllegalAccessException e) { | ||
throw new InvalidOrderingException("Could not create ordering", e); | ||
} | ||
} | ||
|
||
/** | ||
* Order the tests in <code>runner</code> using this ordering. | ||
* | ||
* @throws InvalidOrderingException if ordering does something invalid (like remove or add children) | ||
*/ | ||
public void apply(Object runner) throws InvalidOrderingException { | ||
if (runner instanceof Orderable) { | ||
Orderable orderable = (Orderable) runner; | ||
orderable.order(new GenericOrdering(this)); | ||
} | ||
} | ||
|
||
/** | ||
* Orders the given descriptions (all of which have the same parent). | ||
* | ||
* @param siblings unmodifiable collection of descriptions to order | ||
*/ | ||
public abstract List<Description> order(Collection<Description> siblings); | ||
} |
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
Oops, something went wrong.