1 2 3 package org.quilt.reports; 4 5 import org.apache.tools.ant.taskdefs.optional.junit.*; 6 import org.apache.tools.ant.BuildException; 7 8 import java.io.OutputStream ; 9 import java.io.StringWriter ; 10 import java.io.PrintWriter ; 11 import java.io.IOException ; 12 import java.text.NumberFormat ; 13 import java.util.Hashtable ; 14 15 import junit.framework.AssertionFailedError; 16 import junit.framework.Test; 17 18 import org.quilt.runner.Runner; 19 import org.quilt.framework.*; 20 21 22 23 public class PlainFormatter extends BaseFormatter { 24 25 private Hashtable testStarts = new Hashtable (); 26 private Hashtable failed = new Hashtable (); 27 28 29 public PlainFormatter() { 30 results = new StringWriter (); 31 resultWriter = new PrintWriter (results); 32 } 33 34 36 public void endTestSuite(QuiltTest qt) throws BuildException { 37 StringBuffer sb = new StringBuffer ("Testsuite: " + qt.getName() 38 + "\nTests run: " + qt.runCount() 39 + ", Failures: " + qt.failureCount() 40 + ", Errors: " + qt.errorCount() 41 + ", Time elapsed: " + numberFormat.format( 42 qt.getRunTime() / 1000.0) 43 + " sec\n"); 44 45 if (systemOutput != null && systemOutput.length() > 0) { 46 sb.append( 47 "------------- Standard Output ----------------\n" 48 + systemOutput 49 + "------------- ---------------- ---------------\n"); 50 } 51 52 if (systemError != null && systemError.length() > 0) { 53 sb.append( 54 "------------- Standard Error -----------------\n" 55 + systemError 56 + "------------- ---------------- ---------------\n"); 57 } 58 59 sb.append("\n"); 60 61 if (out != null) { 62 try { 63 out.write(sb.toString().getBytes()); 64 resultWriter.close(); 65 out.write(results.toString().getBytes()); 66 out.flush(); 67 } catch (IOException ioex) { 68 throw new BuildException("Unable to write output", ioex); 69 } finally { 70 if (out != System.out && out != System.err) { 71 try { 72 out.close(); 73 } catch (IOException e) {} 74 } 75 } 76 } 77 } 78 79 public void startTest(Test t) { 81 testStarts.put(t, new Long (System.currentTimeMillis())); 82 failed.put(t, Boolean.FALSE); 83 } 84 85 public void endTest(Test test) { 86 synchronized (resultWriter) { 87 resultWriter.print("Testcase: " + getTestName(test)); 89 90 if (Boolean.TRUE.equals(failed.get(test))) { 92 return; 93 } 94 Long t0 = (Long ) testStarts.get(test); 95 double seconds = 0; 96 if (t0 != null) { 98 seconds = 99 (System.currentTimeMillis() - t0.longValue()) / 1000.0; 100 } 101 102 resultWriter.println(" took " 103 + numberFormat.format(seconds) + " sec"); 104 } 105 } 106 public void addFailure(Test test, Throwable t) { 107 formatError("\tFAILED", test, t); 108 } 109 public void addFailure(Test test, AssertionFailedError t) { 110 addFailure(test, (Throwable ) t); 111 } 112 public void addError(Test test, Throwable t) { 113 formatError("\tCaused an ERROR", test, t); 114 } 115 private void formatError(String type, Test test, Throwable t) { 117 synchronized (resultWriter) { 118 if (test != null) { 119 endTest(test); 120 failed.put(test, Boolean.TRUE); 121 } 122 123 resultWriter.println(type); 124 resultWriter.println(t.getMessage()); 125 String strace = runner.getFilteredTrace(t, filtertrace); 126 resultWriter.print(strace); 127 resultWriter.println(""); 128 } 129 } 130 } 131 | Popular Tags |