1 18 19 package org.apache.tools.ant.taskdefs.optional.junit; 20 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.text.NumberFormat ; 24 import junit.framework.AssertionFailedError; 25 import junit.framework.Test; 26 import org.apache.tools.ant.BuildException; 27 28 32 33 public class SummaryJUnitResultFormatter 34 implements JUnitResultFormatter, JUnitTaskMirror.SummaryJUnitResultFormatterMirror { 35 36 39 private NumberFormat nf = NumberFormat.getInstance(); 40 43 private OutputStream out; 44 45 private boolean withOutAndErr = false; 46 private String systemOutput = null; 47 private String systemError = null; 48 49 52 public SummaryJUnitResultFormatter() { 53 } 54 58 public void startTestSuite(JUnitTest suite) { 59 String newLine = System.getProperty("line.separator"); 60 StringBuffer sb = new StringBuffer ("Running "); 61 sb.append(suite.getName()); 62 sb.append(newLine); 63 64 try { 65 out.write(sb.toString().getBytes()); 66 out.flush(); 67 } catch (IOException ioex) { 68 throw new BuildException("Unable to write summary output", ioex); 69 } 70 } 71 75 public void startTest(Test t) { 76 } 77 81 public void endTest(Test test) { 82 } 83 88 public void addFailure(Test test, Throwable t) { 89 } 90 97 public void addFailure(Test test, AssertionFailedError t) { 98 addFailure(test, (Throwable ) t); 99 } 100 105 public void addError(Test test, Throwable t) { 106 } 107 108 109 public void setOutput(OutputStream out) { 110 this.out = out; 111 } 112 113 114 public void setSystemOutput(String out) { 115 systemOutput = out; 116 } 117 118 119 public void setSystemError(String err) { 120 systemError = err; 121 } 122 123 128 public void setWithOutAndErr(boolean value) { 129 withOutAndErr = value; 130 } 131 132 137 public void endTestSuite(JUnitTest suite) throws BuildException { 138 String newLine = System.getProperty("line.separator"); 139 StringBuffer sb = new StringBuffer ("Tests run: "); 140 sb.append(suite.runCount()); 141 sb.append(", Failures: "); 142 sb.append(suite.failureCount()); 143 sb.append(", Errors: "); 144 sb.append(suite.errorCount()); 145 sb.append(", Time elapsed: "); 146 sb.append(nf.format(suite.getRunTime() / 1000.0)); 147 sb.append(" sec"); 148 sb.append(newLine); 149 150 if (withOutAndErr) { 151 if (systemOutput != null && systemOutput.length() > 0) { 152 sb.append("Output:").append(newLine).append(systemOutput) 153 .append(newLine); 154 } 155 156 if (systemError != null && systemError.length() > 0) { 157 sb.append("Error: ").append(newLine).append(systemError) 158 .append(newLine); 159 } 160 } 161 162 try { 163 out.write(sb.toString().getBytes()); 164 out.flush(); 165 } catch (IOException ioex) { 166 throw new BuildException("Unable to write summary output", ioex); 167 } finally { 168 if (out != System.out && out != System.err) { 169 try { 170 out.close(); 171 } catch (IOException e) { 172 } 174 } 175 } 176 } 177 } 178 | Popular Tags |