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

CoreTestIsolator

public class CoreTestIsolator extends TestRunner
A minimalistic TestRunner implementation that silently executes a single test method and writes a possible stack trace to a temporary file. Used for isolating tests.

Fields Summary
Constructors Summary
public CoreTestIsolator()
Creates a new CoreTestIsolator. The superclass needs to be able to build a proper ResultPrinter, so we pass it a null device for printing stuff.

        super(new PrintStream(new OutputStream() {
            @Override
            public void write(int oneByte) throws IOException {
                // Null device
            }
        }));
    
Methods Summary
protected junit.framework.TestResultcreateTestResult()

        return new TestResult();
    
public static voidmain(java.lang.String[] args)
Provides the main entry point. First and second argument are class and method name, respectively. Third argument is the temporary file name for the result. Exits with one of the usual JUnit exit values.

        Logger.global.setLevel(Level.OFF);
        
        CoreTestIsolator testRunner = new CoreTestIsolator();
        try {
            TestResult r = testRunner.start(args);
            
            if (!r.wasSuccessful()) {
                // Store failure or error - we know there must be one
                Throwable failure = r.failureCount() != 0 ? 
                        ((TestFailure)r.failures().nextElement()).
                                thrownException() :
                        ((TestFailure)r.errors().nextElement()).
                                thrownException();

                saveStackTrace(failure, args[2]);
                
                System.exit(FAILURE_EXIT);
            } else {
                // Nothing to see here, please get along
                System.exit(SUCCESS_EXIT);
            }
        } catch(Exception e) {
            // Let main TestRunner know about execution problem
            saveStackTrace(e, args[2]);
            System.exit(EXCEPTION_EXIT);
        }
        
    
private static voidsaveStackTrace(java.lang.Throwable throwable, java.lang.String fileName)
Saves a given stack trace to a given file.

        try {
            FileOutputStream fos = new FileOutputStream(fileName);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
    
            oos.writeObject(throwable);
            
            oos.flush();
            oos.close();
        } catch (IOException ex) {
            // Ignore
        }
    
protected junit.framework.TestResultstart(java.lang.String[] args)

        try {
            Test suite = TestSuite.createTest(Class.forName(args[0]), args[1]);
            return doRun(suite);
        }
        catch(Exception e) {
            throw new RuntimeException("Unable to launch test", e);
        }