01: //The contents of this file are subject to the Mozilla Public License Version 1.1
02: //(the "License"); you may not use this file except in compliance with the
03: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
04: //
05: //Software distributed under the License is distributed on an "AS IS" basis,
06: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
07: //for the specific language governing rights and
08: //limitations under the License.
09: //
10: //The Original Code is "The Columba Project"
11: //
12: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14: //
15: //All Rights Reserved.
16: package org.columba.core.util;
17:
18: import junit.framework.TestCase;
19:
20: import org.columba.core.base.StopWatch;
21:
22: /**
23: * Tests for the StopWatch class.
24: * Note that this test relies heavily on time and should not be run at the same time
25: * as a huge workload, if that is the case then some of the tests will fail.
26: *
27: * @author redsolo
28: */
29: public class StopWatchTest extends TestCase {
30: /**
31: * Tests to start the StopWatch and then stop it.
32: * @throws InterruptedException thrown if a <code>Thread.sleep()</code> fail.
33: */
34: public void testStop() throws InterruptedException {
35: StopWatch watch = new StopWatch();
36: StopWatch watch2 = new StopWatch();
37: watch2.stop();
38:
39: Thread.sleep(50);
40:
41: long difference = watch.getTiming() - watch2.getTiming();
42:
43: if (Math.abs(difference) < 40) {
44: fail("Stopwatch wasnt stopped.");
45: }
46: }
47:
48: /**
49: * Tests to restarts the StopWatch and then stop it.
50: * @throws InterruptedException thrown if a <code>Thread.sleep()</code> fail.
51: */
52: public void testStart() throws InterruptedException {
53: StopWatch watch = new StopWatch();
54: StopWatch watch2 = new StopWatch();
55: watch.stop();
56: watch2.stop();
57: watch2.start();
58:
59: Thread.sleep(50);
60:
61: long difference = watch.getTiming() - watch2.getTiming();
62:
63: if (Math.abs(difference) < 40) {
64: fail("Stopwatch wasnt restarted correctly.");
65: }
66: }
67:
68: /**
69: * Test to start the timer and stop it, and see that it returns a valid time.
70: * @throws InterruptedException thrown if the sleep was interrupted.
71: */
72: public void testTiming() throws InterruptedException {
73: StopWatch watch = new StopWatch();
74:
75: Thread.sleep(50);
76: watch.stop();
77:
78: if (watch.getTiming() < 50) {
79: fail("Stopwatch returned too small value. expected < 50 but was <"
80: + watch.getTiming() + ">");
81: }
82: }
83: }
|