1
2
3
4
5
6
7 package com.diasparsoftware.gsbase.test;
8
9 import junit.framework.TestCase;
10
11 import com.diasparsoftware.gsbase.Stopwatch;
12
13 /***
14 * If you decide to implement @link{Stopwatch} then you should
15 * pass these tests.
16 *
17 * @version $Revision: 1.1 $
18 * @author <a href="mailto:jbr@diasparsoftware.com">J. B. Rainsberger</a>
19 */
20 public abstract class StopwatchTestCase extends TestCase {
21 protected Stopwatch stopwatch;
22
23 /***
24 * Providing the stopwatch implementation is a subclass
25 * responsibility.
26 */
27 protected void setUp() throws Exception {
28 fail("Override me and invoke setStopwatch()");
29 }
30
31 protected void setStopwatch(Stopwatch stopwatch) {
32 this.stopwatch = stopwatch;
33 }
34
35 public void testLastTimeWithoutStarting() {
36 assertEquals(0, stopwatch.getLastTime());
37 }
38
39 public void testReset() {
40 stopwatch.reset();
41 assertEquals(0, stopwatch.getLastTime());
42 }
43
44 public void testLastTimeWithoutStopping() {
45 stopwatch.start();
46 assertEquals(0, stopwatch.getLastTime());
47 }
48
49 /***
50 * The expected behavior when reading the last time depends
51 * on the implementation, so it is a subclass responsibility.
52 * Usually you will start the stopwatch, pause for some time,
53 * stop the stopwatch then verify the reading somehow.
54 *
55 * @throws Exception Just in case the underlying implementation
56 * uses an API that throws an Exception
57 */
58 public abstract void testLastTime() throws Exception;
59
60 public void testResetAfterUse() throws Exception {
61 stopwatch.start();
62 Thread.sleep(200);
63 stopwatch.stop();
64 stopwatch.reset();
65 assertEquals(0, stopwatch.getLastTime());
66 }
67 }