1. /*
  2. * Copyright 2000-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.text.NumberFormat;
  21. import junit.framework.AssertionFailedError;
  22. import junit.framework.Test;
  23. import org.apache.tools.ant.BuildException;
  24. /**
  25. * Prints short summary output of the test to Ant's logging system.
  26. *
  27. */
  28. public class SummaryJUnitResultFormatter implements JUnitResultFormatter {
  29. /**
  30. * Formatter for timings.
  31. */
  32. private NumberFormat nf = NumberFormat.getInstance();
  33. /**
  34. * OutputStream to write to.
  35. */
  36. private OutputStream out;
  37. private boolean withOutAndErr = false;
  38. private String systemOutput = null;
  39. private String systemError = null;
  40. /**
  41. * Empty
  42. */
  43. public SummaryJUnitResultFormatter() {
  44. }
  45. /**
  46. * Empty
  47. */
  48. public void startTestSuite(JUnitTest suite) {
  49. }
  50. /**
  51. * Empty
  52. */
  53. public void startTest(Test t) {
  54. }
  55. /**
  56. * Empty
  57. */
  58. public void endTest(Test test) {
  59. }
  60. /**
  61. * Empty
  62. */
  63. public void addFailure(Test test, Throwable t) {
  64. }
  65. /**
  66. * Interface TestListener for JUnit > 3.4.
  67. *
  68. * <p>A Test failed.
  69. */
  70. public void addFailure(Test test, AssertionFailedError t) {
  71. addFailure(test, (Throwable) t);
  72. }
  73. /**
  74. * Empty
  75. */
  76. public void addError(Test test, Throwable t) {
  77. }
  78. public void setOutput(OutputStream out) {
  79. this.out = out;
  80. }
  81. public void setSystemOutput(String out) {
  82. systemOutput = out;
  83. }
  84. public void setSystemError(String err) {
  85. systemError = err;
  86. }
  87. /**
  88. * Should the output to System.out and System.err be written to
  89. * the summary.
  90. */
  91. public void setWithOutAndErr(boolean value) {
  92. withOutAndErr = value;
  93. }
  94. /**
  95. * The whole testsuite ended.
  96. */
  97. public void endTestSuite(JUnitTest suite) throws BuildException {
  98. String newLine = System.getProperty("line.separator");
  99. StringBuffer sb = new StringBuffer("Tests run: ");
  100. sb.append(suite.runCount());
  101. sb.append(", Failures: ");
  102. sb.append(suite.failureCount());
  103. sb.append(", Errors: ");
  104. sb.append(suite.errorCount());
  105. sb.append(", Time elapsed: ");
  106. sb.append(nf.format(suite.getRunTime() / 1000.0));
  107. sb.append(" sec");
  108. sb.append(newLine);
  109. if (withOutAndErr) {
  110. if (systemOutput != null && systemOutput.length() > 0) {
  111. sb.append("Output:").append(newLine).append(systemOutput)
  112. .append(newLine);
  113. }
  114. if (systemError != null && systemError.length() > 0) {
  115. sb.append("Error: ").append(newLine).append(systemError)
  116. .append(newLine);
  117. }
  118. }
  119. try {
  120. out.write(sb.toString().getBytes());
  121. out.flush();
  122. } catch (IOException ioex) {
  123. throw new BuildException("Unable to write summary output", ioex);
  124. } finally {
  125. if (out != System.out && out != System.err) {
  126. try {
  127. out.close();
  128. } catch (IOException e) {
  129. // ignore
  130. }
  131. }
  132. }
  133. }
  134. }