View Javadoc

1   // Copyright (C) 2005, 2006 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Unit tests for {@link StopWatchImplementation}.
33   *
34   * @author Philip Aston
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 }