⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 junittestrunner.java

📁 ant源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                } catch (NoSuchMethodException e) {                    // no appropriate suite method found. We don't report any                    // error here since it might be perfectly normal.                }                if (suiteMethod != null) {                    // if there is a suite method available, then try                    // to extract the suite from it. If there is an error                    // here it will be caught below and reported.                    suite = (Test) suiteMethod.invoke(null, new Class[0]);                } else {                    Class junit4TestAdapterClass = null;                    // Check for JDK 5 first. Will *not* help on JDK 1.4                    // if only junit-4.0.jar in CP because in that case                    // linkage of whole task will already have failed! But                    // will help if CP has junit-3.8.2.jar:junit-4.0.jar.                    // In that case first C.fN will fail with CNFE and we                    // will avoid UnsupportedClassVersionError.                    try {                        Class.forName("java.lang.annotation.Annotation");                        if (loader == null) {                            junit4TestAdapterClass =                                Class.forName("junit.framework.JUnit4TestAdapter");                        } else {                            junit4TestAdapterClass =                                Class.forName("junit.framework.JUnit4TestAdapter",                                              true, loader);                        }                    } catch (ClassNotFoundException e) {                        // OK, fall back to JUnit 3.                    }                    junit4 = junit4TestAdapterClass != null;                    if (junit4) {                        // Let's use it!                        suite =                            (Test) junit4TestAdapterClass                            .getConstructor(new Class[] {Class.class}).                            newInstance(new Object[] {testClass});                    } else {                        // Use JUnit 3.                        // try to extract a test suite automatically this                        // will generate warnings if the class is no                        // suitable Test                        suite = new TestSuite(testClass);                    }                }            } catch (Throwable e) {                retCode = ERRORS;                exception = e;            }            long start = System.currentTimeMillis();            fireStartTestSuite();            startTestSuiteSuccess = true;            if (exception != null) { // had an exception constructing suite                for (int i = 0; i < formatters.size(); i++) {                    ((TestListener) formatters.elementAt(i))                        .addError(null, exception);                }                junitTest.setCounts(1, 0, 1);                junitTest.setRunTime(0);            } else {                try {                    logTestListenerEvent("tests to run: " + suite.countTestCases());                    suite.run(res);                } finally {                    if (junit4) {                        int[] cnts = findJUnit4FailureErrorCount(res);                        junitTest.setCounts(res.runCount(), cnts[0], cnts[1]);                    } else {                        junitTest.setCounts(res.runCount(), res.failureCount(),                                res.errorCount());                    }                    junitTest.setRunTime(System.currentTimeMillis() - start);                }            }        } finally {            if (perm != null) {                perm.restoreSecurityManager();            }            if (savedOut != null) {                System.setOut(savedOut);            }            if (savedErr != null) {                System.setErr(savedErr);            }            systemError.close();            systemError = null;            systemOut.close();            systemOut = null;            if (startTestSuiteSuccess) {                sendOutAndErr(new String(outStrm.toByteArray()),                              new String(errStrm.toByteArray()));            }        }        fireEndTestSuite();        if (retCode != SUCCESS || res.errorCount() != 0) {            retCode = ERRORS;        } else if (res.failureCount() != 0) {            retCode = FAILURES;        }    }    /**     * Returns what System.exit() would return in the standalone version.     *     * @return 2 if errors occurred, 1 if tests failed else 0.     */    public int getRetCode() {        return retCode;    }    /**     * Interface TestListener.     *     * <p>A new Test is started.     * @param t the test.     */    public void startTest(Test t) {        String testName = JUnitVersionHelper.getTestCaseName(t);        logTestListenerEvent("startTest(" + testName + ")");    }    /**     * Interface TestListener.     *     * <p>A Test is finished.     * @param test the test.     */    public void endTest(Test test) {        String testName = JUnitVersionHelper.getTestCaseName(test);        logTestListenerEvent("endTest(" + testName + ")");    }    private void logTestListenerEvent(String msg) {        PrintStream out = savedOut != null ? savedOut : System.out;        if (logTestListenerEvents) {            out.flush();            out.println(JUnitTask.TESTLISTENER_PREFIX + msg);            out.flush();        }    }    /**     * Interface TestListener for JUnit &lt;= 3.4.     *     * <p>A Test failed.     * @param test the test.     * @param t    the exception thrown by the test.     */    public void addFailure(Test test, Throwable t) {        String testName = JUnitVersionHelper.getTestCaseName(test);        logTestListenerEvent("addFailure(" + testName + ", " + t.getMessage() + ")");        if (haltOnFailure) {            res.stop();        }    }    /**     * Interface TestListener for JUnit &gt; 3.4.     *     * <p>A Test failed.     * @param test the test.     * @param t    the assertion thrown by the test.     */    public void addFailure(Test test, AssertionFailedError t) {        addFailure(test, (Throwable) t);    }    /**     * Interface TestListener.     *     * <p>An error occurred while running the test.     * @param test the test.     * @param t    the error thrown by the test.     */    public void addError(Test test, Throwable t) {        String testName = JUnitVersionHelper.getTestCaseName(test);        logTestListenerEvent("addError(" + testName + ", " + t.getMessage() + ")");        if (haltOnError) {            res.stop();        }    }    /**     * Permissions for the test run.     * @since Ant 1.6     * @param permissions the permissions to use.     */    public void setPermissions(Permissions permissions) {        perm = permissions;    }    /**     * Handle a string destined for standard output.     * @param output the string to output     */    public void handleOutput(String output) {        if (!logTestListenerEvents && output.startsWith(JUnitTask.TESTLISTENER_PREFIX)) {            // ignore        } else if (systemOut != null) {            systemOut.print(output);        }    }    /**     * Handle input.     * @param buffer not used.     * @param offset not used.     * @param length not used.     * @return -1 always.     * @throws IOException never.     * @see org.apache.tools.ant.Task#handleInput(byte[], int, int)     *     * @since Ant 1.6     */    public int handleInput(byte[] buffer, int offset, int length)        throws IOException {        return -1;    }    /** {@inheritDoc}. */    public void handleErrorOutput(String output) {        if (systemError != null) {            systemError.print(output);        }    }    /** {@inheritDoc}. */    public void handleFlush(String output) {        if (systemOut != null) {            systemOut.print(output);        }    }    /** {@inheritDoc}. */    public void handleErrorFlush(String output) {        if (systemError != null) {            systemError.print(output);        }    }    private void sendOutAndErr(String out, String err) {        for (int i = 0; i < formatters.size(); i++) {            JUnitResultFormatter formatter =                ((JUnitResultFormatter) formatters.elementAt(i));            formatter.setSystemOutput(out);            formatter.setSystemError(err);        }    }    private void fireStartTestSuite() {        for (int i = 0; i < formatters.size(); i++) {            ((JUnitResultFormatter) formatters.elementAt(i))                .startTestSuite(junitTest);        }    }    private void fireEndTestSuite() {        for (int i = 0; i < formatters.size(); i++) {            ((JUnitResultFormatter) formatters.elementAt(i))                .endTestSuite(junitTest);        }    }    /**     * Add a formatter.     * @param f the formatter to add.     */    public void addFormatter(JUnitResultFormatter f) {        formatters.addElement(f);    }    /** {@inheritDoc}. */    public void addFormatter(JUnitTaskMirror.JUnitResultFormatterMirror f) {        formatters.addElement((JUnitResultFormatter) f);    }    /**     * Entry point for standalone (forked) mode.     *     * Parameters: testcaseclassname plus parameters in the format     * key=value, none of which is required.     *     * <table cols="4" border="1">     * <tr><th>key</th><th>description</th><th>default value</th></tr>     *     * <tr><td>haltOnError</td><td>halt test on     * errors?</td><td>false</td></tr>     *     * <tr><td>haltOnFailure</td><td>halt test on     * failures?</td><td>false</td></tr>     *     * <tr><td>formatter</td><td>A JUnitResultFormatter given as     * classname,filename. If filename is ommitted, System.out is     * assumed.</td><td>none</td></tr>     *     * <tr><td>showoutput</td><td>send output to System.err/.out as     * well as to the formatters?</td><td>false</td></tr>     *     * <tr><td>logtestlistenerevents</td><td>log TestListener events to     * System.out.</td><td>false</td></tr>     *     * </table>     * @param args the command line arguments.     * @throws IOException on error.     */    public static void main(String[] args) throws IOException {        boolean haltError = false;        boolean haltFail = false;        boolean stackfilter = true;        Properties props = new Properties();        boolean showOut = false;        boolean outputToFormat = true;        boolean logTestListenerEvents = false;        if (args.length == 0) {            System.err.println("required argument TestClassName missing");            System.exit(ERRORS);        }        if (args[0].startsWith(Constants.TESTSFILE)) {            multipleTests = true;            args[0] = args[0].substring(Constants.TESTSFILE.length());        }        for (int i = 1; i < args.length; i++) {            if (args[i].startsWith(Constants.HALT_ON_ERROR)) {                haltError = Project.toBoolean(args[i].substring(Constants.HALT_ON_ERROR.length()));            } else if (args[i].startsWith(Constants.HALT_ON_FAILURE)) {                haltFail = Project.toBoolean(args[i].substring(Constants.HALT_ON_FAILURE.length()));            } else if (args[i].startsWith(Constants.FILTERTRACE)) {                stackfilter = Project.toBoolean(args[i].substring(Constants.FILTERTRACE.length()));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -