View Javadoc

1   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2   // This file is copied from J2MEUnit and changed to use cldcunit.runner.TestCaseHelper.
3   //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4   package junit.framework;
5   
6   import cldcunit.runner.InstrumentedTestCaseHelper;
7   import cldcunit.runner.TestCaseHelper;
8   
9   /**
10   * A test case defines the fixture to run multiple tests. To define a test case<br>
11   * 1) implement a subclass of TestCase<br>
12   * 2) define instance variables that store the state of the fixture<br>
13   * 3) initialize the fixture state by overriding <code>setUp</code><br>
14   * 4) clean-up after a test by overriding <code>tearDown</code>.<br>
15   * Each test runs in its own fixture so there can be no side effects among test runs. Here is an example:
16   * 
17   * <pre>
18   * public class MathTest extends TestCase {
19   * 	protected double fValue1;
20   * 
21   * 	protected double fValue2;
22   * 
23   * 	protected void setUp() {
24   * 		fValue1 = 2.0;
25   * 		fValue2 = 3.0;
26   * 	}
27   * }
28   * </pre>
29   * 
30   * For each test implement a method which interacts with the fixture. Verify the expected results with assertions
31   * specified by calling <code>assertTrue</code> with a boolean.
32   * 
33   * <pre>
34   * public void testAdd() {
35   * 	double result = fValue1 + fValue2;
36   * 	assertTrue(result == 5.0);
37   * }
38   * </pre>
39   * 
40   * Once the methods are defined you can run them. The framework supports both a static type safe and more dynamic way to
41   * run a test. In the static way you override the runTest method and define the method to be invoked. A convenient way
42   * to do so is with an anonymous inner class.
43   * 
44   * <pre>
45   * TestCase test = new MathTest(&quot;add&quot;) {
46   * 	public void runTest() {
47   * 		testAdd();
48   * 	}
49   * };
50   * test.run();
51   * </pre>
52   * 
53   * The dynamic way uses reflection to implement <code>runTest</code>. It dynamically finds and invokes a method. In this
54   * case the name of the test case has to correspond to the test method to be run.
55   * 
56   * <pre>
57   * TestCase = new MathTest(&quot;testAdd&quot;);
58   * test.run();
59   * </pre>
60   * 
61   * The tests to be run can be collected into a TestSuite. JUnit provides different <i>test runners</i> which can run a
62   * test suite and collect the results. A test runner either expects a static method <code>suite</code> as the entry
63   * point to get a test to run or it will extract the suite automatically.
64   * 
65   * <pre>
66   * public static Test suite() {
67   * 	suite.addTest(new MathTest(&quot;testAdd&quot;));
68   * 	suite.addTest(new MathTest(&quot;testDivideByZero&quot;));
69   * 	return suite;
70   * }
71   * </pre>
72   * 
73   * @see TestResult
74   * @see TestSuite
75   */
76  
77  public abstract class TestCase extends Assert implements Test {
78  	/**
79  	 * the name of the test case
80  	 */
81  	private TestMethod ftestMethod;
82  
83  	private TestMethod[] decleredTestMethods;
84  
85  	private TestCaseHelper helper;
86  
87  	/**
88  	 * No-arg constructor to enable serialization. This method is not intended to be used by mere mortals without
89  	 * calling setName().
90  	 */
91  	public TestCase() {
92  		ftestMethod = null;
93  		helper = getHelper();
94  		decleredTestMethods = helper.createAllTestMethods();
95  	}
96  
97  	public TestCase(String name) {
98  		helper = getHelper();
99  		TestMethod testMethod = helper.createTestMethod(name);
100 		ftestMethod = testMethod;
101 		decleredTestMethods = new TestMethod[] { testMethod };
102 	}
103 
104 	/**
105 	 * Constructs a test case with the given name.
106 	 */
107 	public TestCase(TestMethod testMethod) {
108 		ftestMethod = testMethod;
109 	}
110 
111 	/**
112 	 * Counts the number of test cases executed by run(TestResult result).
113 	 */
114 	public int countTestCases() {
115 		return 1;
116 	}
117 
118 	/**
119 	 * Creates a default TestResult object
120 	 * 
121 	 * @see TestResult
122 	 */
123 	protected TestResult createResult() {
124 		return new TestResult();
125 	}
126 
127 	/**
128 	 * A convenience method to run this test, collecting the results with a default TestResult object.
129 	 * 
130 	 * @see TestResult
131 	 */
132 	public TestResult run() {
133 		TestResult result = createResult();
134 		run(result);
135 		return result;
136 	}
137 
138 	/**
139 	 * Runs the test case and collects the results in TestResult.
140 	 */
141 	public void run(TestResult result) {
142 		result.run(this);
143 	}
144 
145 	/**
146 	 * Runs the bare test sequence.
147 	 * 
148 	 * @exception Throwable
149 	 *                if any exception is thrown
150 	 */
151 	public void runBare() throws Throwable {
152 		setUp();
153 		try {
154 			runTest();
155 		} finally {
156 			tearDown();
157 		}
158 	}
159 
160 	/**
161 	 * Override to run the test and assert its state.
162 	 * 
163 	 * @exception Throwable
164 	 *                if any exception is thrown
165 	 */
166 	protected void runTest() throws Throwable {
167 		assertNotNull(ftestMethod);
168 		ftestMethod.run(this);
169 		System.out.println("." + ftestMethod);
170 	}
171 
172 	/**
173 	 * Sets up the fixture, for example, open a network connection. This method is called before a test is executed.
174 	 */
175 	protected void setUp() throws Exception {
176 	}
177 
178 	/**
179 	 * Tears down the fixture, for example, close a network connection. This method is called after a test is executed.
180 	 */
181 	protected void tearDown() throws Exception {
182 	}
183 
184 	/**
185 	 * Returns a string representation of the test case
186 	 */
187 	public String toString() {
188 		return getTestMethod() + "(" + getClass().getName() + ")";
189 	}
190 
191 	/**
192 	 * Gets the TestMethod
193 	 * 
194 	 * @return returns a TestMethod
195 	 */
196 	public TestMethod getTestMethod() {
197 		return ftestMethod;
198 	}
199 
200 	/**
201 	 * Sets the name of a TestCase
202 	 * 
203 	 * @param name
204 	 *            The name to set
205 	 */
206 	public void setTestMethod(TestMethod testMethod) {
207 		ftestMethod = testMethod;
208 	}
209 
210 	public String getName() {
211 		return getTestMethod().getName();
212 	}
213 	
214 	 public void setName(String name) {
215         ftestMethod = getHelper().createTestMethod(name);
216     }
217 
218 	public TestCaseHelper getHelper() {
219 		if (helper == null) {
220 			String javaVersion = System.getProperty("java.specification.version");
221 			try {
222 				// System.out.println("javaVersion=" + javaVersion);
223 				if ((javaVersion != null)
224 						&& ((javaVersion.equals("1.6") || javaVersion.equals("1.5") || (javaVersion.equals("1.4"))))) {
225 					helper = (TestCaseHelper) Class.forName("cldcunit.runner.ReflectionTestCaseHelper").newInstance();
226 				} else {
227 					helper = new InstrumentedTestCaseHelper();
228 				}
229 				helper.setTestCase(this);
230 			} catch (Throwable e) {
231 				// System.out.println("Can't create ReflectionTestCaseHelper " + e.toString());
232 				helper = new InstrumentedTestCaseHelper();
233 				helper.setTestCase(this);
234 			}
235 		}
236 		return helper;
237 	}
238 
239 	public TestMethod[] getTestMethods() {
240 		return decleredTestMethods;
241 	}
242 
243 }