1. package junit.framework;
  2. import java.lang.reflect.*;
  3. /**
  4. * A test case defines the fixture to run multiple tests. To define a test case<br>
  5. * 1) implement a subclass of TestCase<br>
  6. * 2) define instance variables that store the state of the fixture<br>
  7. * 3) initialize the fixture state by overriding <code>setUp</code><br>
  8. * 4) clean-up after a test by overriding <code>tearDown</code>.<br>
  9. * Each test runs in its own fixture so there
  10. * can be no side effects among test runs.
  11. * Here is an example:
  12. * <pre>
  13. * public class MathTest extends TestCase {
  14. * protected double fValue1;
  15. * protected double fValue2;
  16. *
  17. * protected void setUp() {
  18. * fValue1= 2.0;
  19. * fValue2= 3.0;
  20. * }
  21. * }
  22. * </pre>
  23. *
  24. * For each test implement a method which interacts
  25. * with the fixture. Verify the expected results with assertions specified
  26. * by calling <code>assertTrue</code> with a boolean.
  27. * <pre>
  28. * public void testAdd() {
  29. * double result= fValue1 + fValue2;
  30. * assertTrue(result == 5.0);
  31. * }
  32. * </pre>
  33. * Once the methods are defined you can run them. The framework supports
  34. * both a static type safe and more dynamic way to run a test.
  35. * In the static way you override the runTest method and define the method to
  36. * be invoked. A convenient way to do so is with an anonymous inner class.
  37. * <pre>
  38. * TestCase test= new MathTest("add") {
  39. * public void runTest() {
  40. * testAdd();
  41. * }
  42. * };
  43. * test.run();
  44. * </pre>
  45. * The dynamic way uses reflection to implement <code>runTest</code>. It dynamically finds
  46. * and invokes a method.
  47. * In this case the name of the test case has to correspond to the test method
  48. * to be run.
  49. * <pre>
  50. * TestCase= new MathTest("testAdd");
  51. * test.run();
  52. * </pre>
  53. * The tests to be run can be collected into a TestSuite. JUnit provides
  54. * different <i>test runners</i> which can run a test suite and collect the results.
  55. * A test runner either expects a static method <code>suite</code> as the entry
  56. * point to get a test to run or it will extract the suite automatically.
  57. * <pre>
  58. * public static Test suite() {
  59. * suite.addTest(new MathTest("testAdd"));
  60. * suite.addTest(new MathTest("testDivideByZero"));
  61. * return suite;
  62. * }
  63. * </pre>
  64. * @see TestResult
  65. * @see TestSuite
  66. */
  67. public abstract class TestCase extends Assert implements Test {
  68. /**
  69. * the name of the test case
  70. */
  71. private String fName;
  72. /**
  73. * No-arg constructor to enable serialization. This method
  74. * is not intended to be used by mere mortals without calling setName().
  75. */
  76. public TestCase() {
  77. fName= null;
  78. }
  79. /**
  80. * Constructs a test case with the given name.
  81. */
  82. public TestCase(String name) {
  83. fName= name;
  84. }
  85. /**
  86. * Counts the number of test cases executed by run(TestResult result).
  87. */
  88. public int countTestCases() {
  89. return 1;
  90. }
  91. /**
  92. * Creates a default TestResult object
  93. *
  94. * @see TestResult
  95. */
  96. protected TestResult createResult() {
  97. return new TestResult();
  98. }
  99. /**
  100. * A convenience method to run this test, collecting the results with a
  101. * default TestResult object.
  102. *
  103. * @see TestResult
  104. */
  105. public TestResult run() {
  106. TestResult result= createResult();
  107. run(result);
  108. return result;
  109. }
  110. /**
  111. * Runs the test case and collects the results in TestResult.
  112. */
  113. public void run(TestResult result) {
  114. result.run(this);
  115. }
  116. /**
  117. * Runs the bare test sequence.
  118. * @exception Throwable if any exception is thrown
  119. */
  120. public void runBare() throws Throwable {
  121. setUp();
  122. try {
  123. runTest();
  124. }
  125. finally {
  126. tearDown();
  127. }
  128. }
  129. /**
  130. * Override to run the test and assert its state.
  131. * @exception Throwable if any exception is thrown
  132. */
  133. protected void runTest() throws Throwable {
  134. assertNotNull(fName);
  135. Method runMethod= null;
  136. try {
  137. // use getMethod to get all public inherited
  138. // methods. getDeclaredMethods returns all
  139. // methods of this class but excludes the
  140. // inherited ones.
  141. runMethod= getClass().getMethod(fName, null);
  142. } catch (NoSuchMethodException e) {
  143. fail("Method \""+fName+"\" not found");
  144. }
  145. if (!Modifier.isPublic(runMethod.getModifiers())) {
  146. fail("Method \""+fName+"\" should be public");
  147. }
  148. try {
  149. runMethod.invoke(this, new Class[0]);
  150. }
  151. catch (InvocationTargetException e) {
  152. e.fillInStackTrace();
  153. throw e.getTargetException();
  154. }
  155. catch (IllegalAccessException e) {
  156. e.fillInStackTrace();
  157. throw e;
  158. }
  159. }
  160. /**
  161. * Sets up the fixture, for example, open a network connection.
  162. * This method is called before a test is executed.
  163. */
  164. protected void setUp() throws Exception {
  165. }
  166. /**
  167. * Tears down the fixture, for example, close a network connection.
  168. * This method is called after a test is executed.
  169. */
  170. protected void tearDown() throws Exception {
  171. }
  172. /**
  173. * Returns a string representation of the test case
  174. */
  175. public String toString() {
  176. return getName() + "(" + getClass().getName() + ")";
  177. }
  178. /**
  179. * Gets the name of a TestCase
  180. * @return returns a String
  181. */
  182. public String getName() {
  183. return fName;
  184. }
  185. /**
  186. * Sets the name of a TestCase
  187. * @param name The name to set
  188. */
  189. public void setName(String name) {
  190. fName= name;
  191. }
  192. }