FileDocCategorySizeDatePackage
BaseTestRunner.javaAPI DocAndroid 1.5 API9802Wed May 06 22:42:02 BST 2009junit.runner

BaseTestRunner

public abstract class BaseTestRunner extends Object implements TestListener
Base class for all test runners. This class was born live on stage in Sardinia during XP2000.

Fields Summary
public static final String
SUITE_METHODNAME
private static Properties
fPreferences
static int
fgMaxMessageLength
static boolean
fgFilterStack
boolean
fLoading
Constructors Summary
Methods Summary
public synchronized voidaddError(Test test, java.lang.Throwable t)

        testFailed(TestRunListener.STATUS_ERROR, test, t);
    
public synchronized voidaddFailure(Test test, AssertionFailedError t)

        testFailed(TestRunListener.STATUS_FAILURE, test, t);
    
protected voidclearStatus()
Clears the status message.

 // Belongs in the GUI TestRunner class
    
public java.lang.StringelapsedTimeAsString(long runTime)
Returns the formatted string of the elapsed time.

        return NumberFormat.getInstance().format((double)runTime/1000);
    
public synchronized voidendTest(Test test)

        testEnded(test.toString());
    
public java.lang.StringextractClassName(java.lang.String className)
Extract the class name from a String

        if(className.startsWith("Default package for"))
            return className.substring(className.lastIndexOf(".")+1);
        return className;
    
static booleanfilterLine(java.lang.String line)

        String[] patterns= new String[] {
            "junit.framework.TestCase",
            "junit.framework.TestResult",
            "junit.framework.TestSuite",
            "junit.framework.Assert.", // don't filter AssertionFailure
            "junit.swingui.TestRunner",
            "junit.awtui.TestRunner",
            "junit.textui.TestRunner",
            "java.lang.reflect.Method.invoke("
        };
        for (int i= 0; i < patterns.length; i++) {
            if (line.indexOf(patterns[i]) > 0)
                return true;
        }
        return false;
    
public static java.lang.StringgetFilteredTrace(java.lang.Throwable t)
Returns a filtered stack trace

        StringWriter stringWriter= new StringWriter();
        PrintWriter writer= new PrintWriter(stringWriter);
        t.printStackTrace(writer);
        StringBuffer buffer= stringWriter.getBuffer();
        String trace= buffer.toString();
        return BaseTestRunner.getFilteredTrace(trace);
    
public static java.lang.StringgetFilteredTrace(java.lang.String stack)
Filters stack frames from internal JUnit classes

        if (showStackRaw())
            return stack;

        StringWriter sw= new StringWriter();
        PrintWriter pw= new PrintWriter(sw);
        StringReader sr= new StringReader(stack);
        // BEGIN android-changed
        // Use a sensible default buffer size
        BufferedReader br= new BufferedReader(sr, 1000);
        // END android-changed

        String line;
        try {
            while ((line= br.readLine()) != null) {
                if (!filterLine(line))
                    pw.println(line);
            }
        } catch (Exception IOException) {
            return stack; // return the stack unfiltered
        }
        return sw.toString();
    
public TestSuiteLoadergetLoader()
Returns the loader to be used.

        if (useReloadingTestSuiteLoader())
            return new ReloadingTestSuiteLoader();
        return new StandardTestSuiteLoader();
    
public static java.lang.StringgetPreference(java.lang.String key)

         return getPreferences().getProperty(key);
     
public static intgetPreference(java.lang.String key, int dflt)

         String value= getPreference(key);
         int intValue= dflt;
         if (value == null)
             return intValue;
         try {
             intValue= Integer.parseInt(value);
          } catch (NumberFormatException ne) {
         }
         return intValue;
     
protected static java.util.PropertiesgetPreferences()

        if (fPreferences == null) {
            fPreferences= new Properties();
             fPreferences.put("loading", "true");
             fPreferences.put("filterstack", "true");
              readPreferences();
        }
        return fPreferences;
    
private static java.io.FilegetPreferencesFile()

         String home= System.getProperty("user.home");
         return new File(home, "junit.properties");
     
public TestgetTest(java.lang.String suiteClassName)
Returns the Test corresponding to the given suite. This is a template method, subclasses override runFailed(), clearStatus().

        if (suiteClassName.length() <= 0) {
            clearStatus();
            return null;
        }
        Class testClass= null;
        try {
            testClass= loadSuiteClass(suiteClassName);
        } catch (ClassNotFoundException e) {
            String clazz= e.getMessage();
            if (clazz == null)
                clazz= suiteClassName;
            runFailed("Class not found \""+clazz+"\"");
            return null;
        } catch(Exception e) {
            runFailed("Error: "+e.toString());
            return null;
        }
        Method suiteMethod= null;
        try {
            suiteMethod= testClass.getMethod(SUITE_METHODNAME, new Class[0]);
         } catch(Exception e) {
             // try to extract a test suite automatically
            clearStatus();
            return new TestSuite(testClass);
        }
        if (! Modifier.isStatic(suiteMethod.getModifiers())) {
            runFailed("Suite() method must be static");
            return null;
        }
        Test test= null;
        try {
            test= (Test)suiteMethod.invoke(null); // static method
            if (test == null)
                return test;
        }
        catch (InvocationTargetException e) {
            runFailed("Failed to invoke suite():" + e.getTargetException().toString());
            return null;
        }
        catch (IllegalAccessException e) {
            runFailed("Failed to invoke suite():" + e.toString());
            return null;
        }

        clearStatus();
        return test;
    
public static booleaninVAJava()

        try {
            Class.forName("com.ibm.uvm.tools.DebugSupport");
        }
        catch (Exception e) {
            return false;
        }
        return true;
    
protected java.lang.ClassloadSuiteClass(java.lang.String suiteClassName)
Returns the loaded Class for a suite name.

        return getLoader().load(suiteClassName);
    
protected java.lang.StringprocessArguments(java.lang.String[] args)
Processes the command line arguments and returns the name of the suite class to run or null

        String suiteName= null;
        for (int i= 0; i < args.length; i++) {
            if (args[i].equals("-noloading")) {
                setLoading(false);
            } else if (args[i].equals("-nofilterstack")) {
                fgFilterStack= false;
            } else if (args[i].equals("-c")) {
                if (args.length > i+1)
                    suiteName= extractClassName(args[i+1]);
                else
                    System.out.println("Missing Test class name");
                i++;
            } else {
                suiteName= args[i];
            }
        }
        return suiteName;
    
private static voidreadPreferences()

         InputStream is= null;
         try {
             is= new FileInputStream(getPreferencesFile());
             setPreferences(new Properties(getPreferences()));
            getPreferences().load(is);
        } catch (IOException e) {
            try {
                if (is != null)
                    is.close();
            } catch (IOException e1) {
            }
        }
     
protected abstract voidrunFailed(java.lang.String message)
Override to define how to handle a failed loading of a test suite.

public static voidsavePreferences()

        FileOutputStream fos= new FileOutputStream(getPreferencesFile());
        try {
            getPreferences().store(fos, "");
        } finally {
            fos.close();
        }
    
public voidsetLoading(boolean enable)
Sets the loading behaviour of the test runner

        fLoading= enable;
    
public voidsetPreference(java.lang.String key, java.lang.String value)

        getPreferences().setProperty(key, value);
    
protected static voidsetPreferences(java.util.Properties preferences)

        fPreferences= preferences;
    
protected static booleanshowStackRaw()

        return !getPreference("filterstack").equals("true") || fgFilterStack == false;
    
public synchronized voidstartTest(Test test)


    /*
    * Implementation of TestListener
    */
         
        testStarted(test.toString());
    
public abstract voidtestEnded(java.lang.String testName)

public abstract voidtestFailed(int status, Test test, java.lang.Throwable t)

public abstract voidtestStarted(java.lang.String testName)

public static java.lang.Stringtruncate(java.lang.String s)
Truncates a String to the maximum length.

        if (fgMaxMessageLength != -1 && s.length() > fgMaxMessageLength)
            s= s.substring(0, fgMaxMessageLength)+"...";
        return s;
    
protected booleanuseReloadingTestSuiteLoader()

        return getPreference("loading").equals("true") && !inVAJava() && fLoading;