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.util.Enumeration;
  19. import java.util.Hashtable;
  20. import java.util.Properties;
  21. import java.util.Vector;
  22. import org.apache.tools.ant.Project;
  23. /**
  24. * <p> Run a single JUnit test.
  25. *
  26. * <p> The JUnit test is actually run by {@link JUnitTestRunner}.
  27. * So read the doc comments for that class :)
  28. *
  29. * @since Ant 1.2
  30. *
  31. * @see JUnitTask
  32. * @see JUnitTestRunner
  33. */
  34. public class JUnitTest extends BaseTest implements Cloneable {
  35. /** the name of the test case */
  36. private String name = null;
  37. /** the name of the result file */
  38. private String outfile = null;
  39. // @todo this is duplicating TestResult information. Only the time is not
  40. // part of the result. So we'd better derive a new class from TestResult
  41. // and deal with it. (SB)
  42. private long runs, failures, errors;
  43. private long runTime;
  44. // Snapshot of the system properties
  45. private Properties props = null;
  46. public JUnitTest() {
  47. }
  48. public JUnitTest(String name) {
  49. this.name = name;
  50. }
  51. public JUnitTest(String name, boolean haltOnError, boolean haltOnFailure,
  52. boolean filtertrace) {
  53. this.name = name;
  54. this.haltOnError = haltOnError;
  55. this.haltOnFail = haltOnFailure;
  56. this.filtertrace = filtertrace;
  57. }
  58. /**
  59. * Set the name of the test class.
  60. */
  61. public void setName(String value) {
  62. name = value;
  63. }
  64. /**
  65. * Set the name of the output file.
  66. */
  67. public void setOutfile(String value) {
  68. outfile = value;
  69. }
  70. /**
  71. * Get the name of the test class.
  72. */
  73. public String getName() {
  74. return name;
  75. }
  76. /**
  77. * Get the name of the output file
  78. *
  79. * @return the name of the output file.
  80. */
  81. public String getOutfile() {
  82. return outfile;
  83. }
  84. public void setCounts(long runs, long failures, long errors) {
  85. this.runs = runs;
  86. this.failures = failures;
  87. this.errors = errors;
  88. }
  89. public void setRunTime(long runTime) {
  90. this.runTime = runTime;
  91. }
  92. public long runCount() {
  93. return runs;
  94. }
  95. public long failureCount() {
  96. return failures;
  97. }
  98. public long errorCount() {
  99. return errors;
  100. }
  101. public long getRunTime() {
  102. return runTime;
  103. }
  104. public Properties getProperties() {
  105. return props;
  106. }
  107. public void setProperties(Hashtable p) {
  108. props = new Properties();
  109. for (Enumeration e = p.keys(); e.hasMoreElements();) {
  110. Object key = e.nextElement();
  111. props.put(key, p.get(key));
  112. }
  113. }
  114. public boolean shouldRun(Project p) {
  115. if (ifProperty != null && p.getProperty(ifProperty) == null) {
  116. return false;
  117. } else if (unlessProperty != null
  118. && p.getProperty(unlessProperty) != null) {
  119. return false;
  120. }
  121. return true;
  122. }
  123. public FormatterElement[] getFormatters() {
  124. FormatterElement[] fes = new FormatterElement[formatters.size()];
  125. formatters.copyInto(fes);
  126. return fes;
  127. }
  128. /**
  129. * Convenient method to add formatters to a vector
  130. */
  131. void addFormattersTo(Vector v) {
  132. final int count = formatters.size();
  133. for (int i = 0; i < count; i++) {
  134. v.addElement(formatters.elementAt(i));
  135. }
  136. }
  137. /**
  138. * @since Ant 1.5
  139. */
  140. public Object clone() {
  141. try {
  142. JUnitTest t = (JUnitTest) super.clone();
  143. t.props = props == null ? null : (Properties) props.clone();
  144. t.formatters = (Vector) formatters.clone();
  145. return t;
  146. } catch (CloneNotSupportedException e) {
  147. // plain impossible
  148. return this;
  149. }
  150. }
  151. }