Skip to content

Commit

Permalink
Introduce MemoizingRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
kcooney committed Jun 2, 2018
1 parent 550654a commit b2ce86a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
23 changes: 3 additions & 20 deletions src/main/java/org/junit/internal/requests/ClassRequest.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package org.junit.internal.requests;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.junit.internal.builders.AllDefaultPossibilitiesBuilder;
import org.junit.internal.builders.SuiteMethodBuilder;
import org.junit.runner.Request;
import org.junit.runner.Runner;
import org.junit.runners.model.RunnerBuilder;

public class ClassRequest extends Request {
private final Lock runnerLock = new ReentrantLock();

public class ClassRequest extends MemoizingRequest {
/*
* We have to use the f prefix, because IntelliJ's JUnit4IdeaTestRunner uses
* reflection to access this field. See
* https://github.com/junit-team/junit4/issues/960
*/
private final Class<?> fTestClass;
private final boolean canUseSuiteMethod;
private volatile Runner runner;

public ClassRequest(Class<?> testClass, boolean canUseSuiteMethod) {
this.fTestClass = testClass;
Expand All @@ -31,18 +24,8 @@ public ClassRequest(Class<?> testClass) {
}

@Override
public Runner getRunner() {
if (runner == null) {
runnerLock.lock();
try {
if (runner == null) {
runner = new CustomAllDefaultPossibilitiesBuilder().safeRunnerForClass(fTestClass);
}
} finally {
runnerLock.unlock();
}
}
return runner;
protected Runner createRunner() {
return new CustomAllDefaultPossibilitiesBuilder().safeRunnerForClass(fTestClass);
}

private class CustomAllDefaultPossibilitiesBuilder extends AllDefaultPossibilitiesBuilder {
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/org/junit/internal/requests/MemoizingRequest.java
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 src/main/java/org/junit/internal/requests/OrderingRequest.java
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;
}
}

0 comments on commit b2ce86a

Please sign in to comment.