📄 testerrorindicator.java
字号:
package org.momeunit.ant.formatter;import java.util.Hashtable;import org.apache.tools.ant.Task;import org.momeunit.ant.core.Utility;import org.momeunit.ant.event.TestEvent;/** * Indicates if an error or failure occurred during tests run. Processes results * of tests run. Contains methods for querying whether an error or failure * occurred {@link #hasErrors()} and {@link #hasFailures()}; accessing last * error message {@link #getErrorMessage()}; accessing last failure message * {@link #getFailureMessage()}. Contains methods {@link #setSummary(boolean)} * and {@link #setWithOutAndErr(boolean)} to specify whether to print summary * (with system error and output produced). * * @author Sergio Morozov * @version 1.1.2 */public class TestErrorIndicator extends UnitResultFormatter{ private String errorMsg = null; private String failureMsg = null; private int errors = 0; private int failures = 0; private int testsRun = 0; private Hashtable testsStartTime = new Hashtable(); private double suiteTime = 0.0; private boolean withOutAndErr = false; private boolean summary = false; private Task owner = null; /** * Instantiates TestErrorIndicator with owning task and given printsummary * functionality. * * @param owner * owning task. * @param summary * whether to print short summary. * @param withOutAndErr * whether include system output and system error produced by * emulator and tests run. * @since 1.1 */ public TestErrorIndicator(Task owner, boolean summary, boolean withOutAndErr) { super(); setOwner(owner); setWithOutAndErr(withOutAndErr); setSummary(summary); } /** * Instantiates TestErrorIndicator with owning task and given printsummary * functionality. Didn't include system output and system error produced by * emulator and tests run. * * @param owner * owning task. * @param summary * whether to print short summary. * @since 1.1 */ public TestErrorIndicator(Task owner, boolean summary) { this(owner, summary, false); } /** * Instantiates TestErrorIndicator with owning task. * * @param owner * owning task. * @since 1.1 */ public TestErrorIndicator(Task owner) { this(owner, false, false); } /** * Instantiates TestErrorIndicator. * * @since 1.1 */ public TestErrorIndicator() { this(null, false, false); } /** * Sets whether to print short summary. * * @param summary * boolean that indicates whether to print short summary. * @since 1.1 */ public void setSummary(boolean summary) { this.summary = summary; } /** * Sets owning task. * * @param owner * owning task. * @since 1.1 */ public void setOwner(Task owner) { this.owner = owner; } /** * Sets whether include system output and system error produced by emulator * and tests run. * * @param withOutAndErr * boolean that indicates whether include system output and system * error produced by emulator and tests run. * @since 1.1 */ public void setWithOutAndErr(boolean withOutAndErr) { this.withOutAndErr = withOutAndErr; } /* * (non-Javadoc) * * @see TestEventListener#addError(TestEvent) */ public void addError(TestEvent event) { this.errors++; this.errorMsg = event.getTestName() + " caused an error: " + event.getMsg(); this.endTest(event); } /* * (non-Javadoc) * * @see TestEventListener#addFailure(TestEvent) */ public void addFailure(TestEvent event) { this.failures++; this.failureMsg = event.getTestName() + " failed : " + event.getMsg(); 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())); } /** * Returns last error message. * * @return the last error message. * @since 1.1 */ public String getErrorMessage() { return this.errorMsg; } /** * Returns last failure message. * * @return the last failure message. * @since 1.1 */ public String getFailureMessage() { return this.failureMsg; } /** * Whether some test threw an error. * * @return <code>true</code> if some test threw an error, <code>false</code> * otherwise. * @since 1.1 */ public boolean hasErrors() { return this.errors > 0; } /** * Whether some test failed. * * @return <code>true</code> if some test failed, <code>false</code> * otherwise. * @since 1.1 */ public boolean hasFailures() { return this.failures > 0; } /* * (non-Javadoc) * * @see org.momeunit.ant.formatter.UnitResultFormatter#endTestSuite() */ public void endTestSuite() { if (this.summary) { StringBuffer sb = new StringBuffer("Tests run: ").append(this.testsRun); sb.append(", Failures: ").append(this.failures); sb.append(", Errors: ").append(this.errors); sb.append(", Time elapsed: ").append(Utility.formatTime(this.suiteTime)) .append(" sec"); sb.append(Utility.NEWLINE); if (this.withOutAndErr) { if (getSystemOutput() != null && getSystemOutput().length() > 0) sb .append("Output:").append(Utility.NEWLINE) .append(getSystemOutput()).append(Utility.NEWLINE); if (getSystemError() != null && getSystemError().length() > 0) sb .append("Error: ").append(Utility.NEWLINE).append(getSystemError()) .append(Utility.NEWLINE); } Utility.log(this.owner, sb.toString()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -