CoreTestRunnablepublic class CoreTestRunnable extends Object implements RunnableA wrapper around a single test that allows to execute the test either in the
same thread, in a separate thread, or even in a different process. |
Fields Summary |
---|
private static boolean | IS_DALVIK | private TestCase | fTestThe test case we are supposed to run. | private TestResult | fResultThe TestResult we need to update after the run. | private Protectable | fProtectableThe Protectable that JUnit has created for us. | private boolean | fInvertReflects whether we need to invert the test result, which is used for
treating known failures. | private boolean | fIsolateReflects whether we need to isolate the test, which means we run it in
a separate process. | private Process | fProcessIf we are isolating the test case, this holds the process that is running
it. |
Constructors Summary |
---|
public CoreTestRunnable(TestCase test, TestResult result, Protectable protectable, boolean invert, boolean isolate)Creates a new CoreTestRunnable for the given parameters.
this.fTest = test;
this.fProtectable = protectable;
this.fResult = result;
this.fInvert = invert;
this.fIsolate = isolate;
|
Methods Summary |
---|
public void | run()Executes the test and stores the results. May be run from a secondary
Thread.
try {
if (fIsolate) {
runExternally();
} else {
runInternally();
}
if (fInvert) {
fInvert = false;
throw new AssertionFailedError(
"@KnownFailure expected to fail, but succeeded");
}
} catch (AssertionFailedError e) {
if (!fInvert) {
fResult.addFailure(fTest, e);
}
} catch (ThreadDeath e) { // don't catch ThreadDeath by accident
throw e;
} catch (Throwable e) {
if (!fInvert) {
fResult.addError(fTest, e);
}
}
| private void | runExternally()Runs the test case in a different process. This is what we do for
isolating test cases that have side effects or do suffer from them.
Throwable throwable = null;
File file = File.createTempFile("isolation", ".tmp");
fProcess = Runtime.getRuntime().exec(
(IS_DALVIK ? "dalvikvm" : "java") +
" -classpath " + System.getProperty("java.class.path") +
" -Djava.home=" + System.getProperty("java.home") +
" -Duser.home=" + System.getProperty("user.home") +
" -Djava.io.tmpdir=" + System.getProperty("user.home") +
" com.google.coretests.CoreTestIsolator" +
" " + fTest.getClass().getName() +
" " + fTest.getName() +
" " + file.getAbsolutePath());
int result = fProcess.waitFor();
if (result != TestRunner.SUCCESS_EXIT) {
try {
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
throwable = (Throwable)ois.readObject();
ois.close();
} catch (Exception ex) {
throwable = new RuntimeException("Error isolating test", ex);
}
}
file.delete();
if (throwable != null) {
throw throwable;
}
| private void | runInternally()Runs the test case in the same process. This is basically what a
run-of-the-mill JUnit does, except we might also do it in a secondary
thread.
fProtectable.protect();
| public void | stop()Tells the test case to stop. Only used with isolation. We need to kill
the external process in this case.
if (fProcess != null) {
fProcess.destroy();
}
|
|