-
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.
- Loading branch information
Showing
3 changed files
with
62 additions
and
20 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
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; | ||
} | ||
} |