KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jegg > timer > TimerTest


1 /*
2  * Copyright (c) 2004, Bruce Lowery
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * - Neither the name of JEGG nor the names of its contributors may be used
14  * to endorse or promote products derived from this software without
15  * specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */

29 package jegg.timer;
30
31 import jegg.impl.TestBase;
32
33 public class TimerTest extends TestBase
34 {
35     static
36     {
37         setTestClass(TimerTest.class);
38     }
39     
40     private TestTimeoutListener tl = null;
41     
42     class TestTimeoutListener implements TimeoutListener
43     {
44         public int timeouts = 0;
45         public long last_timeout = 0;
46         public void timeout(Timer t)
47         {
48             timeouts++;
49             last_timeout = System.currentTimeMillis();
50         }
51     }
52     
53     /**
54      * @param n
55      */

56     public TimerTest(String JavaDoc n)
57     {
58         super(n);
59     }
60
61     /* (non-Javadoc)
62      * @see egg.TestBase#setup()
63      */

64     protected void setup()
65     {
66         tl = new TestTimeoutListener();
67     }
68
69     /* (non-Javadoc)
70      * @see egg.TestBase#teardown()
71      */

72     protected void teardown()
73     {
74         // EMPTY
75
}
76
77     public void test1()
78     {
79         assertEquals("test timer not cleared", 0, tl.timeouts);
80         long before = System.currentTimeMillis();
81         Timer t = Timer.createSingleShotTimer(tl,1000);
82         assertNotNull("Failed to create timer",t);
83         try{Thread.sleep(2000);}catch(Throwable JavaDoc e){/**/}
84         long interval = tl.last_timeout - before;
85         assertEquals("test timer not called-back", 1, tl.timeouts);
86         assertTrue("Timer fired too early: "+interval+" msec", 950 < interval);
87     }
88 }
89
Popular Tags