1. /*
  2. * Copyright 2000-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.junit;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.io.PrintWriter;
  21. import java.io.StringWriter;
  22. import java.text.NumberFormat;
  23. import java.util.Hashtable;
  24. import junit.framework.AssertionFailedError;
  25. import junit.framework.Test;
  26. import org.apache.tools.ant.BuildException;
  27. /**
  28. * Prints plain text output of the test to a specified Writer.
  29. *
  30. */
  31. public class PlainJUnitResultFormatter implements JUnitResultFormatter {
  32. /**
  33. * Formatter for timings.
  34. */
  35. private NumberFormat nf = NumberFormat.getInstance();
  36. /**
  37. * Timing helper.
  38. */
  39. private Hashtable testStarts = new Hashtable();
  40. /**
  41. * Where to write the log to.
  42. */
  43. private OutputStream out;
  44. /**
  45. * Helper to store intermediate output.
  46. */
  47. private StringWriter inner;
  48. /**
  49. * Convenience layer on top of {@link #inner inner}.
  50. */
  51. private PrintWriter wri;
  52. /**
  53. * Suppress endTest if testcase failed.
  54. */
  55. private Hashtable failed = new Hashtable();
  56. private String systemOutput = null;
  57. private String systemError = null;
  58. public PlainJUnitResultFormatter() {
  59. inner = new StringWriter();
  60. wri = new PrintWriter(inner);
  61. }
  62. public void setOutput(OutputStream out) {
  63. this.out = out;
  64. }
  65. public void setSystemOutput(String out) {
  66. systemOutput = out;
  67. }
  68. public void setSystemError(String err) {
  69. systemError = err;
  70. }
  71. /**
  72. * Empty.
  73. */
  74. public void startTestSuite(JUnitTest suite) {
  75. }
  76. /**
  77. * The whole testsuite ended.
  78. */
  79. public void endTestSuite(JUnitTest suite) throws BuildException {
  80. String newLine = System.getProperty("line.separator");
  81. StringBuffer sb = new StringBuffer("Testsuite: ");
  82. sb.append(suite.getName());
  83. sb.append(newLine);
  84. sb.append("Tests run: ");
  85. sb.append(suite.runCount());
  86. sb.append(", Failures: ");
  87. sb.append(suite.failureCount());
  88. sb.append(", Errors: ");
  89. sb.append(suite.errorCount());
  90. sb.append(", Time elapsed: ");
  91. sb.append(nf.format(suite.getRunTime() / 1000.0));
  92. sb.append(" sec");
  93. sb.append(newLine);
  94. // append the err and output streams to the log
  95. if (systemOutput != null && systemOutput.length() > 0) {
  96. sb.append("------------- Standard Output ---------------")
  97. .append(newLine)
  98. .append(systemOutput)
  99. .append("------------- ---------------- ---------------")
  100. .append(newLine);
  101. }
  102. if (systemError != null && systemError.length() > 0) {
  103. sb.append("------------- Standard Error -----------------")
  104. .append(newLine)
  105. .append(systemError)
  106. .append("------------- ---------------- ---------------")
  107. .append(newLine);
  108. }
  109. sb.append(newLine);
  110. if (out != null) {
  111. try {
  112. out.write(sb.toString().getBytes());
  113. wri.close();
  114. out.write(inner.toString().getBytes());
  115. out.flush();
  116. } catch (IOException ioex) {
  117. throw new BuildException("Unable to write output", ioex);
  118. } finally {
  119. if (out != System.out && out != System.err) {
  120. try {
  121. out.close();
  122. } catch (IOException e) {
  123. // ignore
  124. }
  125. }
  126. }
  127. }
  128. }
  129. /**
  130. * Interface TestListener.
  131. *
  132. * <p>A new Test is started.
  133. */
  134. public void startTest(Test t) {
  135. testStarts.put(t, new Long(System.currentTimeMillis()));
  136. failed.put(t, Boolean.FALSE);
  137. }
  138. /**
  139. * Interface TestListener.
  140. *
  141. * <p>A Test is finished.
  142. */
  143. public void endTest(Test test) {
  144. if (Boolean.TRUE.equals(failed.get(test))) {
  145. return;
  146. }
  147. synchronized (wri) {
  148. wri.print("Testcase: "
  149. + JUnitVersionHelper.getTestCaseName(test));
  150. Long l = (Long) testStarts.get(test);
  151. double seconds = 0;
  152. // can be null if an error occurred in setUp
  153. if (l != null) {
  154. seconds =
  155. (System.currentTimeMillis() - l.longValue()) / 1000.0;
  156. }
  157. wri.println(" took " + nf.format(seconds) + " sec");
  158. }
  159. }
  160. /**
  161. * Interface TestListener for JUnit <= 3.4.
  162. *
  163. * <p>A Test failed.
  164. */
  165. public void addFailure(Test test, Throwable t) {
  166. formatError("\tFAILED", test, t);
  167. }
  168. /**
  169. * Interface TestListener for JUnit > 3.4.
  170. *
  171. * <p>A Test failed.
  172. */
  173. public void addFailure(Test test, AssertionFailedError t) {
  174. addFailure(test, (Throwable) t);
  175. }
  176. /**
  177. * Interface TestListener.
  178. *
  179. * <p>An error occurred while running the test.
  180. */
  181. public void addError(Test test, Throwable t) {
  182. formatError("\tCaused an ERROR", test, t);
  183. }
  184. private void formatError(String type, Test test, Throwable t) {
  185. synchronized (wri) {
  186. if (test != null) {
  187. endTest(test);
  188. failed.put(test, Boolean.TRUE);
  189. }
  190. wri.println(type);
  191. wri.println(t.getMessage());
  192. String strace = JUnitTestRunner.getFilteredTrace(t);
  193. wri.print(strace);
  194. wri.println("");
  195. }
  196. }
  197. } // PlainJUnitResultFormatter