1 18 19 package org.apache.tools.ant.taskdefs.optional.junit; 20 21 import java.io.OutputStream ; 22 import java.io.PrintWriter ; 23 import java.io.StringWriter ; 24 import java.text.NumberFormat ; 25 26 import junit.framework.AssertionFailedError; 27 import junit.framework.Test; 28 29 import org.apache.tools.ant.util.FileUtils; 30 import org.apache.tools.ant.util.StringUtils; 31 32 39 public class BriefJUnitResultFormatter implements JUnitResultFormatter { 40 41 44 private OutputStream out; 45 46 49 private PrintWriter output; 50 51 54 private StringWriter results; 55 56 59 private PrintWriter resultWriter; 60 61 64 private NumberFormat numberFormat = NumberFormat.getInstance(); 65 66 69 private String systemOutput = null; 70 71 74 private String systemError = null; 75 76 79 public BriefJUnitResultFormatter() { 80 results = new StringWriter (); 81 resultWriter = new PrintWriter (results); 82 } 83 84 88 public void setOutput(OutputStream out) { 89 this.out = out; 90 output = new PrintWriter (out); 91 } 92 93 96 97 public void setSystemOutput(String out) { 98 systemOutput = out; 99 } 100 101 104 105 public void setSystemError(String err) { 106 systemError = err; 107 } 108 109 110 114 public void startTestSuite(JUnitTest suite) { 115 if (output == null) { 116 return; } 118 StringBuffer sb = new StringBuffer ("Testsuite: "); 119 sb.append(suite.getName()); 120 sb.append(StringUtils.LINE_SEP); 121 output.write(sb.toString()); 122 output.flush(); 123 } 124 125 129 public void endTestSuite(JUnitTest suite) { 130 StringBuffer sb = new StringBuffer ("Tests run: "); 131 sb.append(suite.runCount()); 132 sb.append(", Failures: "); 133 sb.append(suite.failureCount()); 134 sb.append(", Errors: "); 135 sb.append(suite.errorCount()); 136 sb.append(", Time elapsed: "); 137 sb.append(numberFormat.format(suite.getRunTime() / 1000.0)); 138 sb.append(" sec"); 139 sb.append(StringUtils.LINE_SEP); 140 sb.append(StringUtils.LINE_SEP); 141 142 if (systemOutput != null && systemOutput.length() > 0) { 144 sb.append("------------- Standard Output ---------------") 145 .append(StringUtils.LINE_SEP) 146 .append(systemOutput) 147 .append("------------- ---------------- ---------------") 148 .append(StringUtils.LINE_SEP); 149 } 150 151 if (systemError != null && systemError.length() > 0) { 152 sb.append("------------- Standard Error -----------------") 153 .append(StringUtils.LINE_SEP) 154 .append(systemError) 155 .append("------------- ---------------- ---------------") 156 .append(StringUtils.LINE_SEP); 157 } 158 159 if (output != null) { 160 try { 161 output.write(sb.toString()); 162 resultWriter.close(); 163 output.write(results.toString()); 164 output.flush(); 165 } finally { 166 if (out != System.out && out != System.err) { 167 FileUtils.close(out); 168 } 169 } 170 } 171 } 172 173 177 public void startTest(Test test) { 178 } 179 180 184 public void endTest(Test test) { 185 } 186 187 194 public void addFailure(Test test, Throwable t) { 195 formatError("\tFAILED", test, t); 196 } 197 198 205 public void addFailure(Test test, AssertionFailedError t) { 206 addFailure(test, (Throwable ) t); 207 } 208 209 214 public void addError(Test test, Throwable error) { 215 formatError("\tCaused an ERROR", test, error); 216 } 217 218 223 protected String formatTest(Test test) { 224 if (test == null) { 225 return "Null Test: "; 226 } else { 227 return "Testcase: " + test.toString() + ":"; 228 } 229 } 230 231 237 protected synchronized void formatError(String type, Test test, 238 Throwable error) { 239 if (test != null) { 240 endTest(test); 241 } 242 243 resultWriter.println(formatTest(test) + type); 244 resultWriter.println(error.getMessage()); 245 String strace = JUnitTestRunner.getFilteredTrace(error); 246 resultWriter.println(strace); 247 resultWriter.println(); 248 } 249 } 250 | Popular Tags |