Skip to content

Commit

Permalink
Open-source the JUnit test runner.
Browse files Browse the repository at this point in the history
--
MOS_MIGRATED_REVID=112027454
  • Loading branch information
damienmg committed Jan 13, 2016
1 parent 2665d68 commit eea8efa
Show file tree
Hide file tree
Showing 57 changed files with 4,971 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ genrule(
"//src/java_tools/buildjar:JavaBuilder_deploy.jar",
"//src/java_tools/singlejar:SingleJar_deploy.jar",
"//src/java_tools/buildjar/java/com/google/devtools/build/buildjar/genclass:GenClass_deploy.jar",
"//src/java_tools/junitrunner/java/com/google/testing/junit/runner:Runner_deploy.jar",
"//third_party/ijar",
"//third_party/java/apkbuilder:embedded_tools",
] + select({
Expand Down
1 change: 1 addition & 0 deletions src/create_embedded_tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ for i in $*; do
*JavaBuilder_deploy.jar) OUTPUT_PATH=tools/jdk/JavaBuilder_deploy.jar ;;
*SingleJar_deploy.jar) OUTPUT_PATH=tools/jdk/SingleJar_deploy.jar ;;
*GenClass_deploy.jar) OUTPUT_PATH=tools/jdk/GenClass_deploy.jar ;;
*Runner_deploy.jar) OUTPUT_PATH=tools/jdk/TestRunner_deploy.jar ;;
*ijar) OUTPUT_PATH=tools/jdk/ijar ;;
*src/objc_tools/*) OUTPUT_PATH=tools/objc/precomp_${i##*/} ;;
*xcode*StdRedirect.dylib) OUTPUT_PATH=tools/objc/StdRedirect.dylib ;;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Description:
#
# JUnit 4.x extensions

package(
default_testonly = 1,
default_visibility = ["//src:__subpackages__"],
)

# Libraries
# =========================================================

# Extensions for writing custom JUnit4 runners
java_library(
name = "runner",
testonly = 0, # TODO(bazel-team): make it testonly
srcs = glob(["runner/*.java"]),
deps = [
"//third_party:guava",
"//third_party:junit4",
],
)

filegroup(
name = "srcs",
srcs = glob(["**"]),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2015 The Bazel Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.testing.junit.junit4.runner;

import static com.google.common.base.Preconditions.checkNotNull;

import org.junit.runner.Description;
import org.junit.runner.manipulation.Filter;

/**
* A filter that returns {@code true} if both of its components return {@code
* true}.
*/
@Deprecated
class AndFilter extends Filter {
private final Filter filter1;
private final Filter filter2;

public AndFilter(Filter filter1, Filter filter2) {
this.filter1 = checkNotNull(filter1);
this.filter2 = checkNotNull(filter2);
}

@Override
public boolean shouldRun(Description description) {
return filter1.shouldRun(description) && filter2.shouldRun(description);
}

@Override
public String describe() {
return String.format("%s && %s", filter1.describe(), filter2.describe());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2015 The Bazel Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.testing.junit.junit4.runner;

import com.google.common.base.Preconditions;

import org.junit.runner.Description;

/**
* The test runner may throw a {@code DynamicTestFailureException} to indicate a
* test case failed due to a failure in a dynamically-discovered test within
* a JUnit test case.
*/
public class DynamicTestException extends Exception {
private final Description test;

/**
* Constructs a {@code DynamicTestFailureException} that indicates a
* dynamically-discovered test, specified as a (@link Description}, failed
* due to the specified {@code cause}.
*/
public DynamicTestException(Description test, Throwable cause) {
super(cause);
Preconditions.checkArgument(test.isTest());
this.test = test;
}

/**
* Returns the description of the dynamically-added test case.
*/
public final Description getTest() {
return test;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2015 The Bazel Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.testing.junit.junit4.runner;

import org.junit.runner.Request;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.NoTestsRemainException;

/**
* Common filters.
*/
@Deprecated
public final class Filters {

private Filters() {}

/**
* Returns a filter that evaluates to {@code true} if both of its
* components evaluates to {@code true}. The filters are evaluated in
* order, and evaluation will be "short-circuited" if the first filter
* returns {@code false}.
*/
public static Filter and(Filter delegate1, Filter delegate2) {
return delegate1 == Filter.ALL ? delegate2
: (delegate2 == Filter.ALL ? delegate1
: new AndFilter(delegate1, delegate2));
}

/**
* Returns a Request that only contains those tests that should run when
* a filter is applied, filtering out all empty suites.<p>
*
* Note that if the request passed into this method caches its runner,
* that runner will be modified to use the given filter. To be safe,
* do not use the passed-in request after calling this method.
*
* @param request Request to filter
* @param filter Filter to apply
* @return request
* @throws NoTestsRemainException if the applying the filter removes all tests
*/
public static Request apply(Request request, Filter filter) throws NoTestsRemainException {
filter = new SuiteTrimmingFilter(filter);
Runner runner = request.getRunner();
filter.apply(runner);

return Request.runner(runner);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2015 The Bazel Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.testing.junit.junit4.runner;

import org.junit.runner.Request;
import org.junit.runner.Runner;

/**
* A {@link Request} that memoizies another {@code Request}.
* This class is meant to be overridden to modify some behaviors.
*/
@Deprecated
public class MemoizingRequest extends Request {
private final Request requestDelegate;
private Runner runnerDelegate;

public MemoizingRequest(Request delegate) {
this.requestDelegate = delegate;
}

@Override
public final synchronized Runner getRunner() {
if (runnerDelegate == null) {
runnerDelegate = createRunner(requestDelegate);
}
return runnerDelegate;
}

/**
* Creates the runner. This method is called at most once.
* Subclasses can override this method for different behavior.
* The default implementation returns the runner created by the delegate.
*
* @param delegate request to delegate to
* @return runner
*/
protected Runner createRunner(Request delegate) {
return delegate.getRunner();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2015 The Bazel Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.testing.junit.junit4.runner;

import org.junit.runner.Description;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;
import org.junit.runner.notification.RunNotifier;
import org.junit.runner.notification.StoppedByUserException;

/**
* A {@link RunNotifier} that delegates all its operations to another {@code RunNotifier}.
* This class is meant to be overridden to modify some behaviors.
*/
public abstract class RunNotifierWrapper extends RunNotifier {
private final RunNotifier delegate;

/**
* Creates a new instance.
*
* @param delegate notifier to delegate to
*/
public RunNotifierWrapper(RunNotifier delegate) {
this.delegate = delegate;
}

/**
* @return the delegate
*/
protected final RunNotifier getDelegate() {
return delegate;
}

@Override
public void addFirstListener(RunListener listener) {
delegate.addFirstListener(listener);
}

@Override
public void addListener(RunListener listener) {
delegate.addListener(listener);
}

@Override
public void removeListener(RunListener listener) {
delegate.removeListener(listener);
}

@Override
public void fireTestRunStarted(Description description) {
delegate.fireTestRunStarted(description);
}

@Override
public void fireTestStarted(Description description) throws StoppedByUserException {
delegate.fireTestStarted(description);
}

@Override
public void fireTestIgnored(Description description) {
delegate.fireTestIgnored(description);
}

@Override
public void fireTestAssumptionFailed(Failure failure) {
delegate.fireTestAssumptionFailed(failure);
}

@Override
public void fireTestFailure(Failure failure) {
delegate.fireTestFailure(failure);
}

@Override
public void fireTestFinished(Description description) {
delegate.fireTestFinished(description);
}

@Override
public void fireTestRunFinished(Result result) {
delegate.fireTestRunFinished(result);
}

@Override
public void pleaseStop() {
delegate.pleaseStop();
}
}
Loading

0 comments on commit eea8efa

Please sign in to comment.