1. /*
  2. * Copyright 2001-2002,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 junit.framework.AssertionFailedError;
  24. import junit.framework.Test;
  25. import org.apache.tools.ant.BuildException;
  26. /**
  27. * Prints plain text output of the test to a specified Writer.
  28. * Inspired by the PlainJUnitResultFormatter.
  29. *
  30. *
  31. * @see FormatterElement
  32. * @see PlainJUnitResultFormatter
  33. */
  34. public class BriefJUnitResultFormatter implements JUnitResultFormatter {
  35. /**
  36. * Where to write the log to.
  37. */
  38. private OutputStream out;
  39. /**
  40. * Used for writing the results.
  41. */
  42. private PrintWriter output;
  43. /**
  44. * Used as part of formatting the results.
  45. */
  46. private StringWriter results;
  47. /**
  48. * Used for writing formatted results to.
  49. */
  50. private PrintWriter resultWriter;
  51. /**
  52. * Formatter for timings.
  53. */
  54. private NumberFormat numberFormat = NumberFormat.getInstance();
  55. /**
  56. * Output suite has written to System.out
  57. */
  58. private String systemOutput = null;
  59. /**
  60. * Output suite has written to System.err
  61. */
  62. private String systemError = null;
  63. public BriefJUnitResultFormatter() {
  64. results = new StringWriter();
  65. resultWriter = new PrintWriter(results);
  66. }
  67. /**
  68. * Sets the stream the formatter is supposed to write its results to.
  69. */
  70. public void setOutput(OutputStream out) {
  71. this.out = out;
  72. output = new PrintWriter(out);
  73. }
  74. public void setSystemOutput(String out) {
  75. systemOutput = out;
  76. }
  77. public void setSystemError(String err) {
  78. systemError = err;
  79. }
  80. /**
  81. * The whole testsuite started.
  82. */
  83. public void startTestSuite(JUnitTest suite) throws BuildException {
  84. }
  85. /**
  86. * The whole testsuite ended.
  87. */
  88. public void endTestSuite(JUnitTest suite) throws BuildException {
  89. String newLine = System.getProperty("line.separator");
  90. StringBuffer sb = new StringBuffer("Testsuite: ");
  91. sb.append(suite.getName());
  92. sb.append(newLine);
  93. sb.append("Tests run: ");
  94. sb.append(suite.runCount());
  95. sb.append(", Failures: ");
  96. sb.append(suite.failureCount());
  97. sb.append(", Errors: ");
  98. sb.append(suite.errorCount());
  99. sb.append(", Time elapsed: ");
  100. sb.append(numberFormat.format(suite.getRunTime() / 1000.0));
  101. sb.append(" sec");
  102. sb.append(newLine);
  103. sb.append(newLine);
  104. // append the err and output streams to the log
  105. if (systemOutput != null && systemOutput.length() > 0) {
  106. sb.append("------------- Standard Output ---------------")
  107. .append(newLine)
  108. .append(systemOutput)
  109. .append("------------- ---------------- ---------------")
  110. .append(newLine);
  111. }
  112. if (systemError != null && systemError.length() > 0) {
  113. sb.append("------------- Standard Error -----------------")
  114. .append(newLine)
  115. .append(systemError)
  116. .append("------------- ---------------- ---------------")
  117. .append(newLine);
  118. }
  119. if (output != null) {
  120. try {
  121. output.write(sb.toString());
  122. resultWriter.close();
  123. output.write(results.toString());
  124. output.flush();
  125. } finally {
  126. if (out != System.out && out != System.err) {
  127. try {
  128. out.close();
  129. } catch (IOException e) {
  130. // ignore
  131. }
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * A test started.
  138. */
  139. public void startTest(Test test) {
  140. }
  141. /**
  142. * A test ended.
  143. */
  144. public void endTest(Test test) {
  145. }
  146. /**
  147. * Interface TestListener for JUnit <= 3.4.
  148. *
  149. * <p>A Test failed.
  150. */
  151. public void addFailure(Test test, Throwable t) {
  152. formatError("\tFAILED", test, t);
  153. }
  154. /**
  155. * Interface TestListener for JUnit > 3.4.
  156. *
  157. * <p>A Test failed.
  158. */
  159. public void addFailure(Test test, AssertionFailedError t) {
  160. addFailure(test, (Throwable) t);
  161. }
  162. /**
  163. * A test caused an error.
  164. */
  165. public void addError(Test test, Throwable error) {
  166. formatError("\tCaused an ERROR", test, error);
  167. }
  168. /**
  169. * Format the test for printing..
  170. */
  171. protected String formatTest(Test test) {
  172. if (test == null) {
  173. return "Null Test: ";
  174. } else {
  175. return "Testcase: " + test.toString() + ":";
  176. }
  177. }
  178. /**
  179. * Format an error and print it.
  180. */
  181. protected synchronized void formatError(String type, Test test,
  182. Throwable error) {
  183. if (test != null) {
  184. endTest(test);
  185. }
  186. resultWriter.println(formatTest(test) + type);
  187. resultWriter.println(error.getMessage());
  188. String strace = JUnitTestRunner.getFilteredTrace(error);
  189. resultWriter.println(strace);
  190. resultWriter.println();
  191. }
  192. }