KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > util > StopWatchTest


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
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 /**
24  * Tests for the StopWatch class.
25  * Note that this test relies heavily on time and should not be run at the same time
26  * as a huge workload, if that is the case then some of the tests will fail.
27  *
28  * @author redsolo
29  */

30 public class StopWatchTest extends TestCase {
31     /**
32  * Tests to start the StopWatch and then stop it.
33  * @throws InterruptedException thrown if a <code>Thread.sleep()</code> fail.
34  */

35     public void testStop() throws InterruptedException JavaDoc {
36         StopWatch watch = new StopWatch();
37         StopWatch watch2 = new StopWatch();
38         watch2.stop();
39
40         Thread.sleep(50);
41
42         long difference = watch.getTiming() - watch2.getTiming();
43
44         if (Math.abs(difference) < 40) {
45             fail("Stopwatch wasnt stopped.");
46         }
47     }
48
49     /**
50  * Tests to restarts the StopWatch and then stop it.
51  * @throws InterruptedException thrown if a <code>Thread.sleep()</code> fail.
52  */

53     public void testStart() throws InterruptedException JavaDoc {
54         StopWatch watch = new StopWatch();
55         StopWatch watch2 = new StopWatch();
56         watch.stop();
57         watch2.stop();
58         watch2.start();
59
60         Thread.sleep(50);
61
62         long difference = watch.getTiming() - watch2.getTiming();
63
64         if (Math.abs(difference) < 40) {
65             fail("Stopwatch wasnt restarted correctly.");
66         }
67     }
68
69     /**
70  * Test to start the timer and stop it, and see that it returns a valid time.
71  * @throws InterruptedException thrown if the sleep was interrupted.
72  */

73     public void testTiming() throws InterruptedException JavaDoc {
74         StopWatch watch = new StopWatch();
75
76         Thread.sleep(50);
77         watch.stop();
78
79         if (watch.getTiming() < 50) {
80             fail("Stopwatch returned too small value. expected < 50 but was <" +
81                 watch.getTiming() + ">");
82         }
83     }
84 }
85
Popular Tags