diff options
Diffstat (limited to 'test-runner')
-rw-r--r-- | test-runner/Android.mk | 1 | ||||
-rw-r--r-- | test-runner/src/android/test/mock/MockPackageManager.java | 2 | ||||
-rw-r--r-- | test-runner/src/junit/runner/BaseTestRunner.java | 342 | ||||
-rw-r--r-- | test-runner/src/junit/runner/StandardTestSuiteLoader.java | 20 | ||||
-rw-r--r-- | test-runner/src/junit/runner/TestRunListener.java | 20 | ||||
-rw-r--r-- | test-runner/src/junit/runner/TestSuiteLoader.java | 9 | ||||
-rw-r--r-- | test-runner/src/junit/runner/Version.java | 20 | ||||
-rw-r--r-- | test-runner/src/junit/textui/ResultPrinter.java | 142 | ||||
-rw-r--r-- | test-runner/src/junit/textui/TestRunner.java | 203 |
9 files changed, 2 insertions, 757 deletions
diff --git a/test-runner/Android.mk b/test-runner/Android.mk index 6d81b71..0d9e4f1 100644 --- a/test-runner/Android.mk +++ b/test-runner/Android.mk @@ -21,6 +21,7 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := $(call all-java-files-under, src) LOCAL_JAVA_LIBRARIES := core core-junit framework +LOCAL_STATIC_JAVA_LIBRARIES := junit-runner LOCAL_MODULE:= android.test.runner diff --git a/test-runner/src/android/test/mock/MockPackageManager.java b/test-runner/src/android/test/mock/MockPackageManager.java index 86689f3..0399b3b 100644 --- a/test-runner/src/android/test/mock/MockPackageManager.java +++ b/test-runner/src/android/test/mock/MockPackageManager.java @@ -545,7 +545,7 @@ public class MockPackageManager extends PackageManager { * @hide */ @Override - public void updateUserName(int id, String name) { + public void setUserName(int id, String name) { throw new UnsupportedOperationException(); } diff --git a/test-runner/src/junit/runner/BaseTestRunner.java b/test-runner/src/junit/runner/BaseTestRunner.java deleted file mode 100644 index 8cfd7fa..0000000 --- a/test-runner/src/junit/runner/BaseTestRunner.java +++ /dev/null @@ -1,342 +0,0 @@ -package junit.runner; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.PrintWriter; -import java.io.StringReader; -import java.io.StringWriter; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.text.NumberFormat; -import java.util.Properties; - -import junit.framework.AssertionFailedError; -import junit.framework.Test; -import junit.framework.TestListener; -import junit.framework.TestSuite; - -/** - * Base class for all test runners. - * This class was born live on stage in Sardinia during XP2000. - */ -public abstract class BaseTestRunner implements TestListener { - public static final String SUITE_METHODNAME= "suite"; - - private static Properties fPreferences; - static int fgMaxMessageLength= 500; - static boolean fgFilterStack= true; - boolean fLoading= true; - - /* - * Implementation of TestListener - */ - public synchronized void startTest(Test test) { - testStarted(test.toString()); - } - - protected static void setPreferences(Properties preferences) { - fPreferences= preferences; - } - - protected static Properties getPreferences() { - if (fPreferences == null) { - fPreferences= new Properties(); - fPreferences.put("loading", "true"); - fPreferences.put("filterstack", "true"); - readPreferences(); - } - return fPreferences; - } - - public static void savePreferences() throws IOException { - FileOutputStream fos= new FileOutputStream(getPreferencesFile()); - try { - getPreferences().store(fos, ""); - } finally { - fos.close(); - } - } - - // android-changed remove 'static' qualifier for API compatibility - public void setPreference(String key, String value) { - getPreferences().put(key, value); - } - - public synchronized void endTest(Test test) { - testEnded(test.toString()); - } - - public synchronized void addError(final Test test, final Throwable t) { - testFailed(TestRunListener.STATUS_ERROR, test, t); - } - - public synchronized void addFailure(final Test test, final AssertionFailedError t) { - testFailed(TestRunListener.STATUS_FAILURE, test, t); - } - - // TestRunListener implementation - - public abstract void testStarted(String testName); - - public abstract void testEnded(String testName); - - public abstract void testFailed(int status, Test test, Throwable t); - - /** - * Returns the Test corresponding to the given suite. This is - * a template method, subclasses override runFailed(), clearStatus(). - */ - public Test getTest(String suiteClassName) { - 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, (Object[])new Class[0]); // 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; - } - - /** - * Returns the formatted string of the elapsed time. - */ - public String elapsedTimeAsString(long runTime) { - return NumberFormat.getInstance().format((double)runTime/1000); - } - - /** - * Processes the command line arguments and - * returns the name of the suite class to run or null - */ - protected String processArguments(String[] args) { - 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; - } - - /** - * Sets the loading behaviour of the test runner - */ - public void setLoading(boolean enable) { - fLoading= enable; - } - /** - * Extract the class name from a String in VA/Java style - */ - public String extractClassName(String className) { - if(className.startsWith("Default package for")) - return className.substring(className.lastIndexOf(".")+1); - return className; - } - - /** - * Truncates a String to the maximum length. - */ - public static String truncate(String s) { - if (fgMaxMessageLength != -1 && s.length() > fgMaxMessageLength) - s= s.substring(0, fgMaxMessageLength)+"..."; - return s; - } - - /** - * Override to define how to handle a failed loading of - * a test suite. - */ - protected abstract void runFailed(String message); - - // BEGIN android-changed - add back getLoader() for API compatibility - /** - * Returns the loader to be used. - * - * @deprecated not present in JUnit4.10 - */ - public TestSuiteLoader getLoader() { - if (useReloadingTestSuiteLoader()) - return new ReloadingTestSuiteLoader(); - return new StandardTestSuiteLoader(); - } - // END android-changed - - /** - * Returns the loaded Class for a suite name. - */ - protected Class<?> loadSuiteClass(String suiteClassName) throws ClassNotFoundException { - return Class.forName(suiteClassName); - } - - /** - * Clears the status message. - */ - protected void clearStatus() { // Belongs in the GUI TestRunner class - } - - protected boolean useReloadingTestSuiteLoader() { - return getPreference("loading").equals("true") && fLoading; - } - - private static File getPreferencesFile() { - String home= System.getProperty("user.home"); - return new File(home, "junit.properties"); - } - - private static void readPreferences() { - 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) { - } - } - } - - public static String getPreference(String key) { - return getPreferences().getProperty(key); - } - - public static int getPreference(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; - } - - /** - * Returns a filtered stack trace - */ - public static String getFilteredTrace(Throwable t) { - StringWriter stringWriter= new StringWriter(); - PrintWriter writer= new PrintWriter(stringWriter); - t.printStackTrace(writer); - StringBuffer buffer= stringWriter.getBuffer(); - String trace= buffer.toString(); - return BaseTestRunner.getFilteredTrace(trace); - } - - // BEGIN android-changed - add back this method for API compatibility - /** @deprecated not present in JUnit4.10 */ - public static boolean inVAJava() { - return false; - } - // END android-changed - - /** - * Filters stack frames from internal JUnit classes - */ - public static String getFilteredTrace(String stack) { - 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(); - } - - protected static boolean showStackRaw() { - return !getPreference("filterstack").equals("true") || fgFilterStack == false; - } - - static boolean filterLine(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; - } - - static { - fgMaxMessageLength= getPreference("maxmessage", fgMaxMessageLength); - } - -} diff --git a/test-runner/src/junit/runner/StandardTestSuiteLoader.java b/test-runner/src/junit/runner/StandardTestSuiteLoader.java deleted file mode 100644 index 381e684..0000000 --- a/test-runner/src/junit/runner/StandardTestSuiteLoader.java +++ /dev/null @@ -1,20 +0,0 @@ -package junit.runner; - -/** - * The standard test suite loader. It can only load the same class once. - * {@hide} - Not needed for 1.0 SDK - */ -public class StandardTestSuiteLoader implements TestSuiteLoader { - /** - * Uses the system class loader to load the test class - */ - public Class load(String suiteClassName) throws ClassNotFoundException { - return Class.forName(suiteClassName); - } - /** - * Uses the system class loader to load the test class - */ - public Class reload(Class aClass) throws ClassNotFoundException { - return aClass; - } -} diff --git a/test-runner/src/junit/runner/TestRunListener.java b/test-runner/src/junit/runner/TestRunListener.java deleted file mode 100644 index 0410f0c..0000000 --- a/test-runner/src/junit/runner/TestRunListener.java +++ /dev/null @@ -1,20 +0,0 @@ -package junit.runner; -/** - * A listener interface for observing the - * execution of a test run. Unlike TestListener, - * this interface using only primitive objects, - * making it suitable for remote test execution. - * {@hide} - Not needed for 1.0 SDK - */ -public interface TestRunListener { - /* test status constants*/ - public static final int STATUS_ERROR= 1; - public static final int STATUS_FAILURE= 2; - - public void testRunStarted(String testSuiteName, int testCount); - public void testRunEnded(long elapsedTime); - public void testRunStopped(long elapsedTime); - public void testStarted(String testName); - public void testEnded(String testName); - public void testFailed(int status, String testName, String trace); -} diff --git a/test-runner/src/junit/runner/TestSuiteLoader.java b/test-runner/src/junit/runner/TestSuiteLoader.java deleted file mode 100644 index 581ea23..0000000 --- a/test-runner/src/junit/runner/TestSuiteLoader.java +++ /dev/null @@ -1,9 +0,0 @@ -package junit.runner; - -/** - * An interface to define how a test suite should be loaded. - */ -public interface TestSuiteLoader { - abstract public Class load(String suiteClassName) throws ClassNotFoundException; - abstract public Class reload(Class aClass) throws ClassNotFoundException; -} diff --git a/test-runner/src/junit/runner/Version.java b/test-runner/src/junit/runner/Version.java deleted file mode 100644 index 4a6dc85..0000000 --- a/test-runner/src/junit/runner/Version.java +++ /dev/null @@ -1,20 +0,0 @@ -package junit.runner; - -/** - * This class defines the current version of JUnit - */ -public class Version { - private Version() { - // don't instantiate - } - - public static String id() { - return "4.10"; - } - - // android-changed - /** @hide - not needed for public API */ - public static void main(String[] args) { - System.out.println(id()); - } -} diff --git a/test-runner/src/junit/textui/ResultPrinter.java b/test-runner/src/junit/textui/ResultPrinter.java deleted file mode 100644 index 4b26558..0000000 --- a/test-runner/src/junit/textui/ResultPrinter.java +++ /dev/null @@ -1,142 +0,0 @@ - -package junit.textui; - -import java.io.PrintStream; -// The following line was removed for compatibility with Android libraries. -//import java.text.NumberFormat; -import java.util.Enumeration; - -import junit.framework.AssertionFailedError; -import junit.framework.Test; -import junit.framework.TestFailure; -import junit.framework.TestListener; -import junit.framework.TestResult; -import junit.runner.BaseTestRunner; - -public class ResultPrinter implements TestListener { - PrintStream fWriter; - int fColumn= 0; - - public ResultPrinter(PrintStream writer) { - fWriter= writer; - } - - /* API for use by textui.TestRunner - */ - - synchronized void print(TestResult result, long runTime) { - printHeader(runTime); - printErrors(result); - printFailures(result); - printFooter(result); - } - - void printWaitPrompt() { - getWriter().println(); - getWriter().println("<RETURN> to continue"); - } - - /* Internal methods - */ - - protected void printHeader(long runTime) { - getWriter().println(); - getWriter().println("Time: "+elapsedTimeAsString(runTime)); - } - - protected void printErrors(TestResult result) { - printDefects(result.errors(), result.errorCount(), "error"); - } - - protected void printFailures(TestResult result) { - printDefects(result.failures(), result.failureCount(), "failure"); - } - - protected void printDefects(Enumeration<TestFailure> booBoos, int count, String type) { - if (count == 0) return; - if (count == 1) - getWriter().println("There was " + count + " " + type + ":"); - else - getWriter().println("There were " + count + " " + type + "s:"); - for (int i= 1; booBoos.hasMoreElements(); i++) { - printDefect(booBoos.nextElement(), i); - } - } - - public void printDefect(TestFailure booBoo, int count) { // only public for testing purposes - printDefectHeader(booBoo, count); - printDefectTrace(booBoo); - } - - protected void printDefectHeader(TestFailure booBoo, int count) { - // I feel like making this a println, then adding a line giving the throwable a chance to print something - // before we get to the stack trace. - getWriter().print(count + ") " + booBoo.failedTest()); - } - - protected void printDefectTrace(TestFailure booBoo) { - getWriter().print(BaseTestRunner.getFilteredTrace(booBoo.trace())); - } - - protected void printFooter(TestResult result) { - if (result.wasSuccessful()) { - getWriter().println(); - getWriter().print("OK"); - getWriter().println (" (" + result.runCount() + " test" + (result.runCount() == 1 ? "": "s") + ")"); - - } else { - getWriter().println(); - getWriter().println("FAILURES!!!"); - getWriter().println("Tests run: "+result.runCount()+ - ", Failures: "+result.failureCount()+ - ", Errors: "+result.errorCount()); - } - getWriter().println(); - } - - - /** - * Returns the formatted string of the elapsed time. - * Duplicated from BaseTestRunner. Fix it. - */ - protected String elapsedTimeAsString(long runTime) { - // The following line was altered for compatibility with - // Android libraries. - return Double.toString((double)runTime/1000); - } - - public PrintStream getWriter() { - return fWriter; - } - /** - * @see junit.framework.TestListener#addError(Test, Throwable) - */ - public void addError(Test test, Throwable t) { - getWriter().print("E"); - } - - /** - * @see junit.framework.TestListener#addFailure(Test, AssertionFailedError) - */ - public void addFailure(Test test, AssertionFailedError t) { - getWriter().print("F"); - } - - /** - * @see junit.framework.TestListener#endTest(Test) - */ - public void endTest(Test test) { - } - - /** - * @see junit.framework.TestListener#startTest(Test) - */ - public void startTest(Test test) { - getWriter().print("."); - if (fColumn++ >= 40) { - getWriter().println(); - fColumn= 0; - } - } - -} diff --git a/test-runner/src/junit/textui/TestRunner.java b/test-runner/src/junit/textui/TestRunner.java deleted file mode 100644 index e955e0e..0000000 --- a/test-runner/src/junit/textui/TestRunner.java +++ /dev/null @@ -1,203 +0,0 @@ -package junit.textui; - - -import java.io.PrintStream; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestResult; -import junit.framework.TestSuite; -import junit.runner.BaseTestRunner; -import junit.runner.Version; - -/** - * A command line based tool to run tests. - * <pre> - * java junit.textui.TestRunner [-wait] TestCaseClass - * </pre> - * - * <p>TestRunner expects the name of a TestCase class as argument. - * If this class defines a static <code>suite</code> method it - * will be invoked and the returned test is run. Otherwise all - * the methods starting with "test" having no arguments are run. - * <p> - * When the wait command line argument is given TestRunner - * waits until the users types RETURN. - * <p> - * TestRunner prints a trace as the tests are executed followed by a - * summary at the end. - */ -public class TestRunner extends BaseTestRunner { - private ResultPrinter fPrinter; - - public static final int SUCCESS_EXIT= 0; - public static final int FAILURE_EXIT= 1; - public static final int EXCEPTION_EXIT= 2; - - /** - * Constructs a TestRunner. - */ - public TestRunner() { - this(System.out); - } - - /** - * Constructs a TestRunner using the given stream for all the output - */ - public TestRunner(PrintStream writer) { - this(new ResultPrinter(writer)); - } - - /** - * Constructs a TestRunner using the given ResultPrinter all the output - */ - public TestRunner(ResultPrinter printer) { - fPrinter= printer; - } - - /** - * Runs a suite extracted from a TestCase subclass. - */ - static public void run(Class<? extends TestCase> testClass) { - run(new TestSuite(testClass)); - } - - /** - * Runs a single test and collects its results. - * This method can be used to start a test run - * from your program. - * <pre> - * public static void main (String[] args) { - * test.textui.TestRunner.run(suite()); - * } - * </pre> - */ - static public TestResult run(Test test) { - TestRunner runner= new TestRunner(); - return runner.doRun(test); - } - - /** - * Runs a single test and waits until the user - * types RETURN. - */ - static public void runAndWait(Test suite) { - TestRunner aTestRunner= new TestRunner(); - aTestRunner.doRun(suite, true); - } - - @Override - public void testFailed(int status, Test test, Throwable t) { - } - - @Override - public void testStarted(String testName) { - } - - @Override - public void testEnded(String testName) { - } - - /** - * Creates the TestResult to be used for the test run. - */ - protected TestResult createTestResult() { - return new TestResult(); - } - - public TestResult doRun(Test test) { - return doRun(test, false); - } - - public TestResult doRun(Test suite, boolean wait) { - TestResult result= createTestResult(); - result.addListener(fPrinter); - long startTime= System.currentTimeMillis(); - suite.run(result); - long endTime= System.currentTimeMillis(); - long runTime= endTime-startTime; - fPrinter.print(result, runTime); - - pause(wait); - return result; - } - - protected void pause(boolean wait) { - if (!wait) return; - fPrinter.printWaitPrompt(); - try { - System.in.read(); - } - catch(Exception e) { - } - } - - public static void main(String args[]) { - TestRunner aTestRunner= new TestRunner(); - try { - TestResult r= aTestRunner.start(args); - if (!r.wasSuccessful()) - System.exit(FAILURE_EXIT); - System.exit(SUCCESS_EXIT); - } catch(Exception e) { - System.err.println(e.getMessage()); - System.exit(EXCEPTION_EXIT); - } - } - - /** - * Starts a test run. Analyzes the command line arguments - * and runs the given test suite. - */ - public TestResult start(String args[]) throws Exception { - String testCase= ""; - String method= ""; - boolean wait= false; - - for (int i= 0; i < args.length; i++) { - if (args[i].equals("-wait")) - wait= true; - else if (args[i].equals("-c")) - testCase= extractClassName(args[++i]); - else if (args[i].equals("-m")) { - String arg= args[++i]; - int lastIndex= arg.lastIndexOf('.'); - testCase= arg.substring(0, lastIndex); - method= arg.substring(lastIndex + 1); - } else if (args[i].equals("-v")) - System.err.println("JUnit " + Version.id() + " by Kent Beck and Erich Gamma"); - else - testCase= args[i]; - } - - if (testCase.equals("")) - throw new Exception("Usage: TestRunner [-wait] testCaseName, where name is the name of the TestCase class"); - - try { - if (!method.equals("")) - return runSingleMethod(testCase, method, wait); - Test suite= getTest(testCase); - return doRun(suite, wait); - } catch (Exception e) { - throw new Exception("Could not create and run test suite: " + e); - } - } - - protected TestResult runSingleMethod(String testCase, String method, boolean wait) throws Exception { - Class<? extends TestCase> testClass= loadSuiteClass(testCase).asSubclass(TestCase.class); - Test test= TestSuite.createTest(testClass, method); - return doRun(test, wait); - } - - @Override - protected void runFailed(String message) { - System.err.println(message); - System.exit(FAILURE_EXIT); - } - - public void setPrinter(ResultPrinter printer) { - fPrinter= printer; - } - - -} |