KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > time > StopWatchTest


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang.time;
17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import junit.textui.TestRunner;
22
23 /**
24  * TestCase for StopWatch.
25  *
26  * @author Stephen Colebourne
27  * @version $Id: StopWatchTest.java 161244 2005-04-14 06:16:36Z ggregory $
28  */

29 public class StopWatchTest extends TestCase {
30
31     public static void main(String JavaDoc[] args) {
32         TestRunner.run(suite());
33     }
34
35     public static Test suite() {
36         TestSuite suite = new TestSuite(StopWatchTest.class);
37         suite.setName("StopWatch Tests");
38         return suite;
39     }
40
41     public StopWatchTest(String JavaDoc s) {
42         super(s);
43     }
44
45     //-----------------------------------------------------------------------
46
public void testStopWatchSimple(){
47         StopWatch watch = new StopWatch();
48         watch.start();
49             try {Thread.sleep(550);} catch (InterruptedException JavaDoc ex) {}
50         watch.stop();
51         long time = watch.getTime();
52         assertEquals(time, watch.getTime());
53         
54         assertTrue(time >= 500);
55         assertTrue(time < 700);
56         
57         watch.reset();
58         assertEquals(0, watch.getTime());
59     }
60     
61     public void testStopWatchSimpleGet(){
62         StopWatch watch = new StopWatch();
63         assertEquals(0, watch.getTime());
64         assertEquals("0:00:00.000", watch.toString());
65         
66         watch.start();
67             try {Thread.sleep(500);} catch (InterruptedException JavaDoc ex) {}
68         assertTrue(watch.getTime() < 2000);
69     }
70     
71     public void testStopWatchSplit(){
72         StopWatch watch = new StopWatch();
73         watch.start();
74             try {Thread.sleep(550);} catch (InterruptedException JavaDoc ex) {}
75         watch.split();
76         long splitTime = watch.getSplitTime();
77             try {Thread.sleep(550);} catch (InterruptedException JavaDoc ex) {}
78         watch.unsplit();
79             try {Thread.sleep(550);} catch (InterruptedException JavaDoc ex) {}
80         watch.stop();
81         long totalTime = watch.getTime();
82         
83         assertTrue(splitTime >= 500);
84         assertTrue(splitTime < 700);
85         assertTrue(totalTime >= 1500);
86         assertTrue(totalTime < 1900);
87     }
88     
89     public void testStopWatchSuspend(){
90         StopWatch watch = new StopWatch();
91         watch.start();
92             try {Thread.sleep(550);} catch (InterruptedException JavaDoc ex) {}
93         watch.suspend();
94         long suspendTime = watch.getTime();
95             try {Thread.sleep(550);} catch (InterruptedException JavaDoc ex) {}
96         watch.resume();
97             try {Thread.sleep(550);} catch (InterruptedException JavaDoc ex) {}
98         watch.stop();
99         long totalTime = watch.getTime();
100         
101         assertTrue(suspendTime >= 500);
102         assertTrue(suspendTime < 700);
103         assertTrue(totalTime >= 1000);
104         assertTrue(totalTime < 1300);
105     }
106
107     // test bad states
108
public void testBadStates() {
109         StopWatch watch = new StopWatch();
110         try {
111             watch.stop();
112             fail("Calling stop on an unstarted StopWatch should throw an exception. ");
113         } catch(IllegalStateException JavaDoc ise) {
114             // expected
115
}
116
117         try {
118             watch.stop();
119             fail("Calling stop on an unstarted StopWatch should throw an exception. ");
120         } catch(IllegalStateException JavaDoc ise) {
121             // expected
122
}
123
124         try {
125             watch.suspend();
126             fail("Calling suspend on an unstarted StopWatch should throw an exception. ");
127         } catch(IllegalStateException JavaDoc ise) {
128             // expected
129
}
130
131         try {
132             watch.split();
133             fail("Calling split on a non-running StopWatch should throw an exception. ");
134         } catch(IllegalStateException JavaDoc ise) {
135             // expected
136
}
137
138         try {
139             watch.unsplit();
140             fail("Calling unsplit on an unsplit StopWatch should throw an exception. ");
141         } catch(IllegalStateException JavaDoc ise) {
142             // expected
143
}
144
145         try {
146             watch.resume();
147             fail("Calling resume on an unsuspended StopWatch should throw an exception. ");
148         } catch(IllegalStateException JavaDoc ise) {
149             // expected
150
}
151
152         watch.start();
153
154         try {
155             watch.start();
156             fail("Calling start on a started StopWatch should throw an exception. ");
157         } catch(IllegalStateException JavaDoc ise) {
158             // expected
159
}
160
161         try {
162             watch.unsplit();
163             fail("Calling unsplit on an unsplit StopWatch should throw an exception. ");
164         } catch(IllegalStateException JavaDoc ise) {
165             // expected
166
}
167
168         try {
169             watch.getSplitTime();
170             fail("Calling getSplitTime on an unsplit StopWatch should throw an exception. ");
171         } catch(IllegalStateException JavaDoc ise) {
172             // expected
173
}
174
175         try {
176             watch.resume();
177             fail("Calling resume on an unsuspended StopWatch should throw an exception. ");
178         } catch(IllegalStateException JavaDoc ise) {
179             // expected
180
}
181
182         watch.stop();
183
184         try {
185             watch.start();
186             fail("Calling start on a stopped StopWatch should throw an exception as it needs to be reset. ");
187         } catch(IllegalStateException JavaDoc ise) {
188             // expected
189
}
190
191     }
192
193 }
194
Popular Tags