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

CoreTestRunner

public class CoreTestRunner extends TestRunner
A special TestRunner implementation that is able to filter out annotated tests and handles our known failures properly (expects them to fail). Handy when running the Core Libraries tests on Android, the bare-metal Dalvik VM, or the RI.

Fields Summary
private static boolean
IS_DALVIK
Reflects our environment.
private static final int
DEFAULT_FLAGS_DALVIK
Defines the default flags for running on Dalvik.
private static final int
DEFAULT_FLAGS_NON_DALVIK
Defines the default flags for running on an RI.
private int
fFlags
Holds the flags specified by the user on the command line.
private int
fTimeout
Holds the timeout value specified by the user on the command line.
private int
fStep
Constructors Summary
public CoreTestRunner()
Creates a new instance of our CoreTestRunner.

    
                
      
        super();
    
Methods Summary
protected junit.textui.ResultPrintercreatePrinter()

        return new CoreTestPrinter(System.out, fFlags);
    
private junit.framework.TestcreateTest(java.lang.String testCase)
Tries to create a Test instance from a given string. The string might either specify a class only or a class plus a method name, separated by a "#".

        int p = testCase.indexOf("#");
        if (p != -1) {
            String testName = testCase.substring(p + 1);
            testCase = testCase.substring(0, p);

            return TestSuite.createTest(Class.forName(testCase), testName);
        } else {
            return getTest(testCase);
        }
        
    
protected junit.framework.TestResultcreateTestResult()

        return new CoreTestResult(fFlags, fTimeout);
    
public junit.framework.TestResultdoRun(junit.framework.Test suite, boolean wait)

        setPrinter(createPrinter());
        
        /*
         * Make sure the original suite is unreachable after we have
         * created the new one, so GC can dispose terminated tests.
         */
        suite = new CoreTestSuite(suite, fFlags, fStep, null);
        
        return super.doRun(suite, wait);
    
public static voidmain(java.lang.String[] args)
Provides our main entry point.

        Logger.global.setLevel(Level.OFF);
        
        System.out.println(
                "--------------------------------------------------");
        System.out.println("Android Core Libraries Test Suite");
        System.out.println("Version 1.0");
        System.out.println(
                "Copyright (c) 2009 The Android Open Source Project");
        System.out.println("");
        
        CoreTestRunner testRunner = new CoreTestRunner();
        try {
            TestResult r = testRunner.start(args);
            
            System.out.println(
            "--------------------------------------------------");
            
            if (!r.wasSuccessful()) {
                System.exit(FAILURE_EXIT);
            } else {
                System.exit(SUCCESS_EXIT);
            }
        } catch(Exception e) {
            System.err.println(e.getMessage());
            System.exit(EXCEPTION_EXIT);
        }
        
    
private voidshowHelp()
Prints a help screen on the console.

        System.out.println("Usage: run-core-tests {<param>} <test>");
        System.out.println();
        System.out.println("Where <test> is a class name, optionally followed");
        System.out.println("by \"#\" and a method name, and <param> is one of");
        System.out.println("the following:");
        System.out.println();
        System.out.println("    --include-all");
        System.out.println("    --exclude-all");
        System.out.println("    --include-android-only");
        System.out.println("    --exclude-android-only");
        System.out.println("    --include-broken-tests");
        System.out.println("    --exclude-broken-tests");
        System.out.println("    --include-known-failures");
        System.out.println("    --exclude-known-failures");
        System.out.println("    --include-normal-tests");
        System.out.println("    --exclude-normal-tests");
        System.out.println("    --include-side-effects");
        System.out.println("    --exclude-side-effects");
        System.out.println();
        System.out.println("    --known-failures-must-fail");
        System.out.println("    --known-failures-must-pass");
        System.out.println("    --timeout <seconds>");
        // System.out.println("    --find-side-effect <test>");
        System.out.println("    --isolate-all");
        System.out.println("    --isolate-none");
        System.out.println("    --verbose");
        System.out.println("    --help");
        System.out.println();
        System.out.println("Default parameters are:");
        System.out.println();
        
        if (IS_DALVIK) {
            System.out.println("    --include-android-only");
            System.out.println("    --exclude-broken-tests");
            System.out.println("    --include-known-failures");
            System.out.println("    --include-normal-tests");
            System.out.println("    --include-side-effects");
            System.out.println("    --known-failures-must-fail");
        } else {
            System.out.println("    --exclude-android-only");
            System.out.println("    --exclude-broken-tests");
            System.out.println("    --include-known-failures");
            System.out.println("    --include-normal-tests");
            System.out.println("    --include-side-effects");
            System.out.println("    --known-failures-must-pass");
        }
        
        System.out.println();
    
protected junit.framework.TestResultstart(java.lang.String[] args)

        String testName = null;
        // String victimName = null;
        
        boolean wait = false;
        
        if (IS_DALVIK) {
            fFlags = DEFAULT_FLAGS_DALVIK;
        } else {
            fFlags = DEFAULT_FLAGS_NON_DALVIK;
        }
        
        for (int i= 0; i < args.length; i++) {
            if (args[i].startsWith("--")) {
                if (args[i].equals("--wait")) {
                    wait = true;
                } else if (args[i].equals("--include-all")) {
                    fFlags = fFlags | CoreTestSuite.RUN_ALL_TESTS;
                } else if (args[i].equals("--exclude-all")) {
                    fFlags = fFlags & ~CoreTestSuite.RUN_ALL_TESTS;
                } else if (args[i].equals("--include-android-only")) {
                    fFlags = fFlags | CoreTestSuite.RUN_ANDROID_ONLY;
                } else if (args[i].equals("--exclude-android-only")) {
                    fFlags = fFlags & ~CoreTestSuite.RUN_ANDROID_ONLY;
                } else if (args[i].equals("--include-broken-tests")) {
                    fFlags = fFlags | CoreTestSuite.RUN_BROKEN_TESTS;
                } else if (args[i].equals("--exclude-broken-tests")) {
                    fFlags = fFlags & ~CoreTestSuite.RUN_BROKEN_TESTS;
                } else if (args[i].equals("--include-known-failures")) {
                    fFlags = fFlags | CoreTestSuite.RUN_KNOWN_FAILURES;
                } else if (args[i].equals("--exclude-known-failures")) {
                    fFlags = fFlags & ~CoreTestSuite.RUN_KNOWN_FAILURES;
                } else if (args[i].equals("--include-normal-tests")) {
                    fFlags = fFlags | CoreTestSuite.RUN_NORMAL_TESTS;
                } else if (args[i].equals("--exclude-normal-tests")) {
                    fFlags = fFlags & ~CoreTestSuite.RUN_NORMAL_TESTS;
                } else if (args[i].equals("--include-side-effects")) {
                    fFlags = fFlags | CoreTestSuite.RUN_SIDE_EFFECTS;
                } else if (args[i].equals("--exclude-side-effects")) {
                    fFlags = fFlags & ~CoreTestSuite.RUN_SIDE_EFFECTS;
                } else if (args[i].equals("--known-failures-must-fail")) {
                    fFlags = fFlags | CoreTestSuite.INVERT_KNOWN_FAILURES;
                } else if (args[i].equals("--known-failures-must-pass")) {
                    fFlags = fFlags & ~CoreTestSuite.INVERT_KNOWN_FAILURES;
                } else if (args[i].equals("--timeout")) {
                    fTimeout = Integer.parseInt(args[++i]);
                } else if (args[i].equals("--reverse")) {
                    fFlags = fFlags | CoreTestSuite.REVERSE;
                } else if (args[i].equals("--step")) {
                    fStep = Integer.parseInt(args[++i]);
                } else if (args[i].equals("--isolate-all")) {
                    fFlags = (fFlags | CoreTestSuite.ISOLATE_ALL) & 
                                   ~CoreTestSuite.ISOLATE_NONE;
                } else if (args[i].equals("--isolate-none")) {
                    fFlags = (fFlags | CoreTestSuite.ISOLATE_NONE) & 
                                   ~CoreTestSuite.ISOLATE_ALL;
                } else if (args[i].equals("--verbose")) {
                    fFlags = fFlags | CoreTestSuite.VERBOSE;
                // } else if (args[i].equals("--find-side-effect")) {
                //    victimName = args[++i];
                } else if (args[i].equals("--dry-run")) {
                    fFlags = fFlags | CoreTestSuite.DRY_RUN;
                } else if (args[i].equals("--help")) {
                    showHelp();
                    System.exit(1);
                } else {
                    System.err.println("Unknown argument " + args[i] + 
                            ", try --help");
                    System.exit(1);
                }
            } else {
                testName = args[i];
            }
        }
        
        if (IS_DALVIK) {
            System.out.println("Using Dalvik VM version " + 
                    System.getProperty("java.vm.version"));
        } else {
            System.out.println("Using Java VM version " + 
                    System.getProperty("java.version"));
        }
        System.out.println();

        try {
            return doRun(createTest(testName), wait);
        }
        catch(Exception e) {
            e.printStackTrace();
            throw new Exception("Could not create and run test suite: " + e);
        }