001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.lang.time;
018:
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import junit.textui.TestRunner;
023:
024: /**
025: * TestCase for StopWatch.
026: *
027: * @author Stephen Colebourne
028: * @version $Id: StopWatchTest.java 504351 2007-02-06 22:49:50Z bayard $
029: */
030: public class StopWatchTest extends TestCase {
031:
032: public static void main(String[] args) {
033: TestRunner.run(suite());
034: }
035:
036: public static Test suite() {
037: TestSuite suite = new TestSuite(StopWatchTest.class);
038: suite.setName("StopWatch Tests");
039: return suite;
040: }
041:
042: public StopWatchTest(String s) {
043: super (s);
044: }
045:
046: //-----------------------------------------------------------------------
047: public void testStopWatchSimple() {
048: StopWatch watch = new StopWatch();
049: watch.start();
050: try {
051: Thread.sleep(550);
052: } catch (InterruptedException ex) {
053: }
054: watch.stop();
055: long time = watch.getTime();
056: assertEquals(time, watch.getTime());
057:
058: assertTrue(time >= 500);
059: assertTrue(time < 700);
060:
061: watch.reset();
062: assertEquals(0, watch.getTime());
063: }
064:
065: public void testStopWatchSimpleGet() {
066: StopWatch watch = new StopWatch();
067: assertEquals(0, watch.getTime());
068: assertEquals("0:00:00.000", watch.toString());
069:
070: watch.start();
071: try {
072: Thread.sleep(500);
073: } catch (InterruptedException ex) {
074: }
075: assertTrue(watch.getTime() < 2000);
076: }
077:
078: public void testStopWatchSplit() {
079: StopWatch watch = new StopWatch();
080: watch.start();
081: try {
082: Thread.sleep(550);
083: } catch (InterruptedException ex) {
084: }
085: watch.split();
086: long splitTime = watch.getSplitTime();
087: String splitStr = watch.toSplitString();
088: try {
089: Thread.sleep(550);
090: } catch (InterruptedException ex) {
091: }
092: watch.unsplit();
093: try {
094: Thread.sleep(550);
095: } catch (InterruptedException ex) {
096: }
097: watch.stop();
098: long totalTime = watch.getTime();
099:
100: assertEquals("Formatted split string not the correct length",
101: splitStr.length(), 11);
102: assertTrue(splitTime >= 500);
103: assertTrue(splitTime < 700);
104: assertTrue(totalTime >= 1500);
105: assertTrue(totalTime < 1900);
106: }
107:
108: public void testStopWatchSuspend() {
109: StopWatch watch = new StopWatch();
110: watch.start();
111: try {
112: Thread.sleep(550);
113: } catch (InterruptedException ex) {
114: }
115: watch.suspend();
116: long suspendTime = watch.getTime();
117: try {
118: Thread.sleep(550);
119: } catch (InterruptedException ex) {
120: }
121: watch.resume();
122: try {
123: Thread.sleep(550);
124: } catch (InterruptedException ex) {
125: }
126: watch.stop();
127: long totalTime = watch.getTime();
128:
129: assertTrue(suspendTime >= 500);
130: assertTrue(suspendTime < 700);
131: assertTrue(totalTime >= 1000);
132: assertTrue(totalTime < 1300);
133: }
134:
135: public void testLang315() {
136: StopWatch watch = new StopWatch();
137: watch.start();
138: try {
139: Thread.sleep(200);
140: } catch (InterruptedException ex) {
141: }
142: watch.suspend();
143: long suspendTime = watch.getTime();
144: try {
145: Thread.sleep(200);
146: } catch (InterruptedException ex) {
147: }
148: watch.stop();
149: long totalTime = watch.getTime();
150: assertTrue(suspendTime == totalTime);
151: }
152:
153: // test bad states
154: public void testBadStates() {
155: StopWatch watch = new StopWatch();
156: try {
157: watch.stop();
158: fail("Calling stop on an unstarted StopWatch should throw an exception. ");
159: } catch (IllegalStateException ise) {
160: // expected
161: }
162:
163: try {
164: watch.stop();
165: fail("Calling stop on an unstarted StopWatch should throw an exception. ");
166: } catch (IllegalStateException ise) {
167: // expected
168: }
169:
170: try {
171: watch.suspend();
172: fail("Calling suspend on an unstarted StopWatch should throw an exception. ");
173: } catch (IllegalStateException ise) {
174: // expected
175: }
176:
177: try {
178: watch.split();
179: fail("Calling split on a non-running StopWatch should throw an exception. ");
180: } catch (IllegalStateException ise) {
181: // expected
182: }
183:
184: try {
185: watch.unsplit();
186: fail("Calling unsplit on an unsplit StopWatch should throw an exception. ");
187: } catch (IllegalStateException ise) {
188: // expected
189: }
190:
191: try {
192: watch.resume();
193: fail("Calling resume on an unsuspended StopWatch should throw an exception. ");
194: } catch (IllegalStateException ise) {
195: // expected
196: }
197:
198: watch.start();
199:
200: try {
201: watch.start();
202: fail("Calling start on a started StopWatch should throw an exception. ");
203: } catch (IllegalStateException ise) {
204: // expected
205: }
206:
207: try {
208: watch.unsplit();
209: fail("Calling unsplit on an unsplit StopWatch should throw an exception. ");
210: } catch (IllegalStateException ise) {
211: // expected
212: }
213:
214: try {
215: watch.getSplitTime();
216: fail("Calling getSplitTime on an unsplit StopWatch should throw an exception. ");
217: } catch (IllegalStateException ise) {
218: // expected
219: }
220:
221: try {
222: watch.resume();
223: fail("Calling resume on an unsuspended StopWatch should throw an exception. ");
224: } catch (IllegalStateException ise) {
225: // expected
226: }
227:
228: watch.stop();
229:
230: try {
231: watch.start();
232: fail("Calling start on a stopped StopWatch should throw an exception as it needs to be reset. ");
233: } catch (IllegalStateException ise) {
234: // expected
235: }
236:
237: }
238:
239: }
|