aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java_tools/junitrunner/java/com/google/testing
diff options
context:
space:
mode:
authorGravatar Kush Chakraborty <kush@google.com>2017-02-24 00:17:21 +0000
committerGravatar Irina Iancu <elenairina@google.com>2017-02-24 08:31:30 +0000
commitc67080c787cf0d176a91564c981b2366242d6c19 (patch)
tree873ede500711beb21e9b96a67310cc4e3de5c2ca /src/java_tools/junitrunner/java/com/google/testing
parent5e945570ec0b9079596756bf89437ac37e031c36 (diff)
*** Reason for rollback *** Breaks dagger []: [] *** Original change description *** Separate the classpaths of the TestRunner with the test target, and use a separate Classloader to load the test target's classes. This enables a clean separation of the classes of the TestRunner with the target under test. This is achieved with the following steps: 1. Start the test runner with only the bare bones classpaths to the Test Runner's classes which are used by the system ClassLoader. 2. Have all the classpaths required to load the test target's classes in a TEST_TARGET_CLASSPATH envi... *** -- PiperOrigin-RevId: 148405598 MOS_MIGRATED_REVID=148405598
Diffstat (limited to 'src/java_tools/junitrunner/java/com/google/testing')
-rw-r--r--src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD1
-rw-r--r--src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java123
2 files changed, 10 insertions, 114 deletions
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD
index 71a715a85a..1699cb882b 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD
@@ -17,7 +17,6 @@ java_library(
"//src/java_tools/junitrunner/java/com/google/testing/junit/runner/model",
"//src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding",
"//src/java_tools/junitrunner/java/com/google/testing/junit/runner/util",
- "//src/main/protobuf:worker_protocol_java_proto",
"//third_party:junit4",
],
)
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java
index 3f4c966d94..74a6826f88 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java
@@ -14,20 +14,13 @@
package com.google.testing.junit.runner;
-import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest;
-import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse;
import com.google.testing.junit.runner.internal.StackTraces;
import com.google.testing.junit.runner.junit4.JUnit4InstanceModules.Config;
import com.google.testing.junit.runner.junit4.JUnit4InstanceModules.SuiteClass;
import com.google.testing.junit.runner.junit4.JUnit4Runner;
import com.google.testing.junit.runner.model.AntXmlResultWriter;
import com.google.testing.junit.runner.model.XmlResultWriter;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
import java.io.PrintStream;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLClassLoader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -55,9 +48,6 @@ public class BazelTestRunner {
*/
static final String TEST_SUITE_PROPERTY_NAME = "bazel.test_suite";
- private static URL[] classpaths = null;
- private static URLClassLoader targetClassLoader;
-
private BazelTestRunner() {
// utility class; should not be instantiated
}
@@ -82,8 +72,9 @@ public class BazelTestRunner {
String suiteClassName = System.getProperty(TEST_SUITE_PROPERTY_NAME);
- if ("true".equals(System.getenv("PERSISTENT_TEST_RUNNER"))) {
- System.exit(runPersistentTestRunner(suiteClassName));
+ if (args.length >= 1 && args[args.length - 1].equals("--persistent_test_runner")) {
+ System.err.println("Requested test strategy is currently unsupported.");
+ System.exit(1);
}
if (!checkTestSuiteProperty(suiteClassName)) {
@@ -135,7 +126,7 @@ public class BazelTestRunner {
}
private static int runTestsInSuite(String suiteClassName, String[] args) {
- Class<?> suite = getTargetSuiteClass(suiteClassName);
+ Class<?> suite = getTestClass(suiteClassName);
if (suite == null) {
// No class found corresponding to the system property passed in from Bazel
@@ -145,123 +136,29 @@ public class BazelTestRunner {
}
}
+ // TODO(kush): Use a new classloader for the following instantiation.
JUnit4Runner runner =
JUnit4Bazel.builder()
.suiteClass(new SuiteClass(suite))
.config(new Config(args))
.build()
.runner();
-
- // Some frameworks such as Mockito use the Thread's context classloader.
- Thread.currentThread().setContextClassLoader(targetClassLoader);
-
- int result = 1;
- try {
- result = runner.run().wasSuccessful() ? 0 : 1;
- } catch (RuntimeException e) {
- System.err.println("Test run failed with exception");
- e.printStackTrace();
- }
- return result;
+ return runner.run().wasSuccessful() ? 0 : 1;
}
- /**
- * Run in a loop awaiting instructions for the next test run.
- *
- * @param suiteClassName name of the class which is passed on to JUnit to determine the test suite
- * @return 0 when we encounter an EOF from input, or non-zero values if we encounter an
- * unrecoverable error.
- */
- private static int runPersistentTestRunner(String suiteClassName) {
- PrintStream originalStdOut = System.out;
- PrintStream originalStdErr = System.err;
-
- while (true) {
- try {
- WorkRequest request = WorkRequest.parseDelimitedFrom(System.in);
-
- if (request == null) {
- break;
- }
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- PrintStream printStream = new PrintStream(outputStream, true);
- System.setOut(printStream);
- System.setErr(printStream);
- String[] arguments = request.getArgumentsList().toArray(new String[0]);
- int exitCode = -1;
- try {
- exitCode = runTestsInSuite(suiteClassName, arguments);
- } finally {
- System.setOut(originalStdOut);
- System.setErr(originalStdErr);
- }
-
- WorkResponse response =
- WorkResponse
- .newBuilder()
- .setOutput(outputStream.toString())
- .setExitCode(exitCode)
- .build();
- response.writeDelimitedTo(System.out);
- System.out.flush();
-
- } catch (IOException e) {
- e.printStackTrace();
- return 1;
- }
- }
- return 0;
- }
-
- /**
- * Get the actual Test Suite class corresponding to the given name.
- */
- private static Class<?> getTargetSuiteClass(String suiteClassName) {
- if (suiteClassName == null) {
+ private static Class<?> getTestClass(String name) {
+ if (name == null) {
return null;
}
try {
- targetClassLoader = new URLClassLoader(getClasspaths());
- Class<?> targetSuiteClass = targetClassLoader.loadClass(suiteClassName);
- System.out.printf(
- "Running test suites for class: %s, created by classLoader: %s%n",
- targetSuiteClass, targetSuiteClass.getClassLoader());
- return targetSuiteClass;
- } catch (ClassNotFoundException | MalformedURLException e) {
- System.err.println("Exception in loading class:" + e.getMessage());
+ return Class.forName(name);
+ } catch (ClassNotFoundException e) {
return null;
}
}
/**
- * Used to get the classpaths which should be used to load the classes of the test target.
- *
- * @throws MalformedURLException when we are unable to create a given classpath.
- * @return array of URLs containing the classpaths or null if classpaths could not be located.
- */
- private static URL[] getClasspaths() throws MalformedURLException {
- // TODO(kush): WARNING THIS DOES NOT RELOAD CLASSPATHS FOR EVERY TEST RUN. b/34712039
- if (classpaths != null) {
- return classpaths;
- }
- String testTargetsClaspaths = System.getenv("TEST_TARGET_CLASSPATH");
- if (testTargetsClaspaths == null || testTargetsClaspaths.isEmpty()) {
- throw new IllegalStateException(
- "Target's classpath not present in TEST_TARGET_CLASSPATH environment variable");
- }
-
- String[] targetClassPaths = testTargetsClaspaths.split(":");
-
- classpaths = new URL[targetClassPaths.length];
- String workingDir = System.getProperty("user.dir");
- for (int index = 0; index < targetClassPaths.length; index++) {
- classpaths[index] = new URL("file://" + workingDir + "/" + targetClassPaths[index]);
- }
- return classpaths;
- }
-
- /**
* Prints out stack traces if the JVM does not exit quickly. This can help detect shutdown hooks
* that are preventing the JVM from exiting quickly.
*