📄 briefunitresultformatter.java
字号:
package org.momeunit.ant.formatter;import java.io.IOException;import java.io.OutputStream;import java.util.Hashtable;import org.apache.tools.ant.BuildException;import org.momeunit.ant.core.Utility;import org.momeunit.ant.event.TestEvent;/** * Predefined <code>brief</code> formatter. Generates report of plain text. It * resembles functionality of it's <code><junit></code> analog and * prints only detailed information for test cases that failed. * * @author Sergio Morozov * @version 1.1.2 */public class BriefUnitResultFormatter extends UnitResultFormatter{ /** * Number of tests run. */ private int testsRun = 0; private Hashtable testsStartTime = new Hashtable(); /** * Time of executing tests. */ private double suiteTime = 0.0; /** * Number of tests that failed. */ private int failedTests = 0; /** * Number of errors that threw an error. */ private int errorTests = 0; /** * Where to write the log to. */ private StringBuffer curSB = new StringBuffer(); /* * (non-Javadoc) * * @see UnitResultFormatter#startTestSuite(java.lang.String) */ public void startTestSuite(String name) { super.startTestSuite(name); this.curSB = new StringBuffer(); } /* * (non-Javadoc) * * @see org.momeunit.ant.formatter.UnitResultFormatter#endTestSuite() */ public void endTestSuite() throws BuildException { OutputStream out = this.getOutput(); if (out != null) { StringBuffer sumSB = new StringBuffer("Testsuite: "); sumSB.append(this.getTestSuiteName()).append(Utility.NEWLINE); sumSB.append("Tests run: ").append(this.testsRun); sumSB.append(", Failures: ").append(this.failedTests); sumSB.append(", Errors: ").append(this.errorTests); sumSB.append(", Time elapsed: ").append( Utility.formatTime(this.suiteTime)).append(" sec"); sumSB.append(Utility.NEWLINE); if (getSystemOutput() != null && getSystemOutput().length() > 0) { sumSB.append("------------- Standard Output ---------------").append( Utility.NEWLINE).append(getSystemOutput()).append( "------------- ---------------- ---------------").append( Utility.NEWLINE); } if (getSystemError() != null && getSystemError().length() > 0) { sumSB.append("------------- Standard Error -----------------").append( Utility.NEWLINE).append(getSystemError()).append( "------------- ---------------- ---------------").append( Utility.NEWLINE); } sumSB.append(Utility.NEWLINE); try { out.write(sumSB.toString().getBytes(getEncoding())); out.write(curSB.toString().getBytes(getEncoding())); out.flush(); } catch (IOException ioex) { throw new BuildException("Unable to write output", ioex); } finally { if (out != System.out && out != System.err) { try { out.close(); } catch (IOException e) {} } } } } /** * Generates description text of error or failure occcured based on given * type and test event. * * @param type * type of error. * @param event * test event. */ private void formatError(String type, TestEvent event) { curSB.append(event.getTestName()).append(' ').append(type).append( Utility.NEWLINE); curSB.append(event.getMsg()).append(Utility.NEWLINE); String stackTrace = event.getStackTrace(); if (this.isFilterTrace()) stackTrace = filterStackTrace(stackTrace); curSB.append(stackTrace).append(Utility.NEWLINE).append(Utility.NEWLINE); } /* * (non-Javadoc) * * @see TestEventListener#addError(TestEvent) */ public void addError(TestEvent event) { errorTests++; formatError("\tCaused an ERROR", event); this.endTest(event); } /* * (non-Javadoc) * * @see TestEventListener#addFailure(TestEvent) */ public void addFailure(TestEvent event) { failedTests++; formatError("\tFAILED", event); this.endTest(event); } /* * (non-Javadoc) * * @see TestEventListener#endTest(TestEvent) */ public void endTest(TestEvent event) { Long startTime = (Long) this.testsStartTime.get(event.getTestName()); if (startTime != null) { this.suiteTime += (event.getTimestamp() - startTime.longValue()) / 1000.0; this.testsStartTime.remove(event.getTestName()); } } /* * (non-Javadoc) * * @see TestEventListener#startTest(TestEvent) */ public void startTest(TestEvent event) { this.testsRun++; this.testsStartTime .put(event.getTestName(), new Long(event.getTimestamp())); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -