1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.engine.process;
23
24 import net.grinder.engine.process.StopWatch.StopWatchNotRunningException;
25 import net.grinder.engine.process.StopWatch.StopWatchRunningException;
26 import net.grinder.util.TimeAuthority;
27 import net.grinder.util.TimeAuthorityStubFactory;
28 import junit.framework.TestCase;
29
30
31
32
33
34
35
36 public class TestStopWatchImplementation extends TestCase {
37
38 public void testStopWatch() throws Exception {
39 final TimeAuthorityStubFactory timeAuthorityStubFactory =
40 new TimeAuthorityStubFactory();
41 final TimeAuthority timeAuthority =
42 timeAuthorityStubFactory.getStub();
43 timeAuthorityStubFactory.nextTime(2000);
44
45 final StopWatch stopWatch = new StopWatchImplementation(timeAuthority);
46
47 try {
48 stopWatch.stop();
49 fail("Expected StopWatchNotRunningException");
50 }
51 catch (StopWatchNotRunningException e) {
52 }
53
54 timeAuthorityStubFactory.assertNoMoreCalls();
55
56 stopWatch.start();
57
58 timeAuthorityStubFactory.assertSuccess("getTimeInMilliseconds");
59 timeAuthorityStubFactory.assertNoMoreCalls();
60
61 try {
62 stopWatch.start();
63 fail("Expected StopWatchRunningException");
64 }
65 catch (StopWatchRunningException e) {
66 }
67
68 try {
69 stopWatch.reset();
70 fail("Expected StopWatchRunningException");
71 }
72 catch (StopWatchRunningException e) {
73 }
74
75 try {
76 stopWatch.getTime();
77 fail("Expected StopWatchRunningException");
78 }
79 catch (StopWatchRunningException e) {
80 }
81
82 timeAuthorityStubFactory.assertNoMoreCalls();
83
84 final StopWatch stopWatch2 = new StopWatchImplementation(timeAuthority);
85
86 try {
87 stopWatch2.add(stopWatch);
88 fail("Expected StopWatchRunningException");
89 }
90 catch (StopWatchRunningException e) {
91 }
92
93 timeAuthorityStubFactory.assertNoMoreCalls();
94 timeAuthorityStubFactory.nextTime(3000);
95
96 stopWatch.stop();
97
98 assertEquals(1000, stopWatch.getTime());
99
100 timeAuthorityStubFactory.assertSuccess("getTimeInMilliseconds");
101 timeAuthorityStubFactory.assertNoMoreCalls();
102
103 stopWatch.reset();
104
105 assertEquals(0, stopWatch.getTime());
106
107 stopWatch.start();
108 stopWatch.stop();
109 assertEquals(0, stopWatch.getTime());
110
111 stopWatch.start();
112 timeAuthorityStubFactory.nextTime(5000);
113 stopWatch.stop();
114 assertEquals(2000, stopWatch.getTime());
115 }
116 }