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.io.PrintWriter ; 24 import java.io.StringWriter ; 25 import java.text.NumberFormat ; 26 import java.util.Hashtable ; 27 28 import junit.framework.AssertionFailedError; 29 import junit.framework.Test; 30 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.util.FileUtils; 33 import org.apache.tools.ant.util.StringUtils; 34 35 36 40 41 public class PlainJUnitResultFormatter implements JUnitResultFormatter { 42 43 46 private NumberFormat nf = NumberFormat.getInstance(); 47 50 private Hashtable testStarts = new Hashtable (); 51 54 private OutputStream out; 55 58 private StringWriter inner; 59 62 private PrintWriter wri; 63 66 private Hashtable failed = new Hashtable (); 67 68 private String systemOutput = null; 69 private String systemError = null; 70 71 72 public PlainJUnitResultFormatter() { 73 inner = new StringWriter (); 74 wri = new PrintWriter (inner); 75 } 76 77 78 public void setOutput(OutputStream out) { 79 this.out = out; 80 } 81 82 83 public void setSystemOutput(String out) { 84 systemOutput = out; 85 } 86 87 88 public void setSystemError(String err) { 89 systemError = err; 90 } 91 92 97 public void startTestSuite(JUnitTest suite) throws BuildException { 98 if (out == null) { 99 return; } 101 StringBuffer sb = new StringBuffer ("Testsuite: "); 102 sb.append(suite.getName()); 103 sb.append(StringUtils.LINE_SEP); 104 try { 105 out.write(sb.toString().getBytes()); 106 out.flush(); 107 } catch (IOException ex) { 108 throw new BuildException("Unable to write output", ex); 109 } 110 } 111 112 117 public void endTestSuite(JUnitTest suite) throws BuildException { 118 StringBuffer sb = new StringBuffer ("Tests run: "); 119 sb.append(suite.runCount()); 120 sb.append(", Failures: "); 121 sb.append(suite.failureCount()); 122 sb.append(", Errors: "); 123 sb.append(suite.errorCount()); 124 sb.append(", Time elapsed: "); 125 sb.append(nf.format(suite.getRunTime() / 1000.0)); 126 sb.append(" sec"); 127 sb.append(StringUtils.LINE_SEP); 128 129 if (systemOutput != null && systemOutput.length() > 0) { 131 sb.append("------------- Standard Output ---------------") 132 .append(StringUtils.LINE_SEP) 133 .append(systemOutput) 134 .append("------------- ---------------- ---------------") 135 .append(StringUtils.LINE_SEP); 136 } 137 138 if (systemError != null && systemError.length() > 0) { 139 sb.append("------------- Standard Error -----------------") 140 .append(StringUtils.LINE_SEP) 141 .append(systemError) 142 .append("------------- ---------------- ---------------") 143 .append(StringUtils.LINE_SEP); 144 } 145 146 sb.append(StringUtils.LINE_SEP); 147 148 if (out != null) { 149 try { 150 out.write(sb.toString().getBytes()); 151 wri.close(); 152 out.write(inner.toString().getBytes()); 153 out.flush(); 154 } catch (IOException ioex) { 155 throw new BuildException("Unable to write output", ioex); 156 } finally { 157 if (out != System.out && out != System.err) { 158 FileUtils.close(out); 159 } 160 } 161 } 162 } 163 164 170 public void startTest(Test t) { 171 testStarts.put(t, new Long (System.currentTimeMillis())); 172 failed.put(t, Boolean.FALSE); 173 } 174 175 181 public void endTest(Test test) { 182 if (Boolean.TRUE.equals(failed.get(test))) { 183 return; 184 } 185 synchronized (wri) { 186 wri.print("Testcase: " 187 + JUnitVersionHelper.getTestCaseName(test)); 188 Long l = (Long ) testStarts.get(test); 189 double seconds = 0; 190 if (l != null) { 192 seconds = 193 (System.currentTimeMillis() - l.longValue()) / 1000.0; 194 } 195 196 wri.println(" took " + nf.format(seconds) + " sec"); 197 } 198 } 199 200 207 public void addFailure(Test test, Throwable t) { 208 formatError("\tFAILED", test, t); 209 } 210 211 218 public void addFailure(Test test, AssertionFailedError t) { 219 addFailure(test, (Throwable ) t); 220 } 221 222 229 public void addError(Test test, Throwable t) { 230 formatError("\tCaused an ERROR", test, t); 231 } 232 233 private void formatError(String type, Test test, Throwable t) { 234 synchronized (wri) { 235 if (test != null) { 236 endTest(test); 237 failed.put(test, Boolean.TRUE); 238 } 239 240 wri.println(type); 241 wri.println(t.getMessage()); 242 String strace = JUnitTestRunner.getFilteredTrace(t); 243 wri.print(strace); 244 wri.println(""); 245 } 246 } 247 248 } | Popular Tags |