📄 junittestrunner.java
字号:
} else if (args[i].startsWith(Constants.CRASHFILE)) { crashFile = args[i].substring(Constants.CRASHFILE.length()); registerTestCase(Constants.BEFORE_FIRST_TEST); } else if (args[i].startsWith(Constants.FORMATTER)) { try { createAndStoreFormatter(args[i].substring(Constants.FORMATTER.length())); } catch (BuildException be) { System.err.println(be.getMessage()); System.exit(ERRORS); } } else if (args[i].startsWith(Constants.PROPSFILE)) { FileInputStream in = new FileInputStream(args[i] .substring(Constants.PROPSFILE.length())); props.load(in); in.close(); } else if (args[i].startsWith(Constants.SHOWOUTPUT)) { showOut = Project.toBoolean(args[i].substring(Constants.SHOWOUTPUT.length())); } else if (args[i].startsWith(Constants.LOGTESTLISTENEREVENTS)) { logTestListenerEvents = Project.toBoolean( args[i].substring(Constants.LOGTESTLISTENEREVENTS.length())); } else if (args[i].startsWith(Constants.OUTPUT_TO_FORMATTERS)) { outputToFormat = Project.toBoolean( args[i].substring(Constants.OUTPUT_TO_FORMATTERS.length())); } } // Add/overlay system properties on the properties from the Ant project Hashtable p = System.getProperties(); for (Enumeration e = p.keys(); e.hasMoreElements();) { Object key = e.nextElement(); props.put(key, p.get(key)); } int returnCode = SUCCESS; if (multipleTests) { try { java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.FileReader(args[0])); String testCaseName; int code = 0; boolean errorOccurred = false; boolean failureOccurred = false; String line = null; while ((line = reader.readLine()) != null) { StringTokenizer st = new StringTokenizer(line, ","); testCaseName = st.nextToken(); JUnitTest t = new JUnitTest(testCaseName); t.setTodir(new File(st.nextToken())); t.setOutfile(st.nextToken()); t.setProperties(props); code = launch(t, haltError, stackfilter, haltFail, showOut, outputToFormat, logTestListenerEvents); errorOccurred = (code == ERRORS); failureOccurred = (code != SUCCESS); if (errorOccurred || failureOccurred) { if ((errorOccurred && haltError) || (failureOccurred && haltFail)) { registerNonCrash(); System.exit(code); } else { if (code > returnCode) { returnCode = code; } System.out.println("TEST " + t.getName() + " FAILED"); } } } } catch (IOException e) { e.printStackTrace(); } } else { JUnitTest t = new JUnitTest(args[0]); t.setProperties(props); returnCode = launch( t, haltError, stackfilter, haltFail, showOut, outputToFormat, logTestListenerEvents); } registerNonCrash(); System.exit(returnCode); } private static Vector fromCmdLine = new Vector(); private static void transferFormatters(JUnitTestRunner runner, JUnitTest test) { runner.addFormatter(new JUnitResultFormatter() { public void startTestSuite(JUnitTest suite) throws BuildException { } public void endTestSuite(JUnitTest suite) throws BuildException { } public void setOutput(OutputStream out) { } public void setSystemOutput(String out) { } public void setSystemError(String err) { } public void addError(Test arg0, Throwable arg1) { } public void addFailure(Test arg0, AssertionFailedError arg1) { } public void endTest(Test arg0) { } public void startTest(Test arg0) { registerTestCase(JUnitVersionHelper.getTestCaseName(arg0)); } }); for (int i = 0; i < fromCmdLine.size(); i++) { FormatterElement fe = (FormatterElement) fromCmdLine.elementAt(i); if (multipleTests && fe.getUseFile()) { File destFile = new File(test.getTodir(), test.getOutfile() + fe.getExtension()); fe.setOutfile(destFile); } runner.addFormatter((JUnitResultFormatter) fe.createFormatter()); } } /** * Line format is: formatter=<classname>(,<pathname>)? */ private static void createAndStoreFormatter(String line) throws BuildException { FormatterElement fe = new FormatterElement(); int pos = line.indexOf(','); if (pos == -1) { fe.setClassname(line); fe.setUseFile(false); } else { fe.setClassname(line.substring(0, pos)); fe.setUseFile(true); if (!multipleTests) { fe.setOutfile(new File(line.substring(pos + 1))); } else { int fName = line.indexOf(IGNORED_FILE_NAME); if (fName > -1) { fe.setExtension(line .substring(fName + IGNORED_FILE_NAME.length())); } } } fromCmdLine.addElement(fe); } /** * Returns a filtered stack trace. * This is ripped out of junit.runner.BaseTestRunner. * @param t the exception to filter. * @return the filtered stack trace. */ public static String getFilteredTrace(Throwable t) { String trace = StringUtils.getStackTrace(t); return JUnitTestRunner.filterStack(trace); } /** * Filters stack frames from internal JUnit and Ant classes * @param stack the stack trace to filter. * @return the filtered stack. */ public static String filterStack(String stack) { if (!filtertrace) { return stack; } StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); StringReader sr = new StringReader(stack); BufferedReader br = new BufferedReader(sr); String line; try { while ((line = br.readLine()) != null) { if (!filterLine(line)) { pw.println(line); } } } catch (Exception e) { return stack; // return the stack unfiltered } return sw.toString(); } private static boolean filterLine(String line) { for (int i = 0; i < DEFAULT_TRACE_FILTERS.length; i++) { if (line.indexOf(DEFAULT_TRACE_FILTERS[i]) != -1) { return true; } } return false; } /** * @since Ant 1.6.2 */ private static int launch(JUnitTest t, boolean haltError, boolean stackfilter, boolean haltFail, boolean showOut, boolean outputToFormat, boolean logTestListenerEvents) { JUnitTestRunner runner = new JUnitTestRunner(t, haltError, stackfilter, haltFail, showOut, logTestListenerEvents, null); runner.forked = true; runner.outputToFormatters = outputToFormat; transferFormatters(runner, t); runner.run(); return runner.getRetCode(); } /** * @since Ant 1.7 */ private static void registerNonCrash() throws IOException { if (crashFile != null) { FileWriter out = null; try { out = new FileWriter(crashFile); out.write(Constants.TERMINATED_SUCCESSFULLY + "\n"); out.flush(); } finally { if (out != null) { out.close(); } } } } private static void registerTestCase(String testCase) { if (crashFile != null) { try { FileWriter out = null; try { out = new FileWriter(crashFile); out.write(testCase + "\n"); out.flush(); } finally { if (out != null) { out.close(); } } } catch (IOException e) { // ignored. } } } /** * Modifies a TestListener when running JUnit 4: treats AssertionFailedError * as a failure not an error. * * @since Ant 1.7 */ private TestListener wrapListener(final TestListener testListener) { return new TestListener() { public void addError(Test test, Throwable t) { if (junit4 && t instanceof AssertionFailedError) { // JUnit 4 does not distinguish between errors and failures // even in the JUnit 3 adapter. // So we need to help it a bit to retain compatibility for JUnit 3 tests. testListener.addFailure(test, (AssertionFailedError) t); } else if (junit4 && t.getClass().getName().equals("java.lang.AssertionError")) { // Not strictly necessary but probably desirable. // JUnit 4-specific test GUIs will show just "failures". // But Ant's output shows "failures" vs. "errors". // We would prefer to show "failure" for things that logically are. try { String msg = t.getMessage(); AssertionFailedError failure = msg != null ? new AssertionFailedError(msg) : new AssertionFailedError(); // To compile on pre-JDK 4 (even though this should always succeed): Method initCause = Throwable.class.getMethod( "initCause", new Class[] {Throwable.class}); initCause.invoke(failure, new Object[] {t}); testListener.addFailure(test, failure); } catch (Exception e) { // Rats. e.printStackTrace(); // should not happen testListener.addError(test, t); } } else { testListener.addError(test, t); } } public void addFailure(Test test, AssertionFailedError t) { testListener.addFailure(test, t); } public void addFailure(Test test, Throwable t) { // pre-3.4 if (t instanceof AssertionFailedError) { testListener.addFailure(test, (AssertionFailedError) t); } else { testListener.addError(test, t); } } public void endTest(Test test) { testListener.endTest(test); } public void startTest(Test test) { testListener.startTest(test); } }; } /** * Use instead of TestResult.get{Failure,Error}Count on JUnit 4, * since the adapter claims that all failures are errors. * @since Ant 1.7 */ private int[] findJUnit4FailureErrorCount(TestResult res) { int failures = 0; int errors = 0; Enumeration e = res.failures(); while (e.hasMoreElements()) { e.nextElement(); failures++; } e = res.errors(); while (e.hasMoreElements()) { Throwable t = ((TestFailure) e.nextElement()).thrownException(); if (t instanceof AssertionFailedError || t.getClass().getName().equals("java.lang.AssertionError")) { failures++; } else { errors++; } } return new int[] {failures, errors}; }} // JUnitTestRunner
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -