FileDocCategorySizeDatePackage
CoreTestRunnable.javaAPI DocAndroid 1.5 API5290Wed May 06 22:41:04 BST 2009com.google.coretests

CoreTestRunnable

public class CoreTestRunnable extends Object implements Runnable
A 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
fTest
The test case we are supposed to run.
private TestResult
fResult
The TestResult we need to update after the run.
private Protectable
fProtectable
The Protectable that JUnit has created for us.
private boolean
fInvert
Reflects whether we need to invert the test result, which is used for treating known failures.
private boolean
fIsolate
Reflects whether we need to isolate the test, which means we run it in a separate process.
private Process
fProcess
If 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 voidrun()
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 voidrunExternally()
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 voidrunInternally()
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 voidstop()
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();
        }