KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > entity > A_Timer


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: A_Timer.java,v 1.2 2004/03/19 11:57:15 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.entity;
27
28 import org.objectweb.jonas.jtests.beans.annuaire.Personne;
29 import org.objectweb.jonas.jtests.beans.annuaire.PersonneHome;
30 import org.objectweb.jonas.jtests.util.JTestCase;
31
32 /**
33  * This is test of the TimerService.
34  * Beans used: annuaire
35  * @author Philippe Durieux (jonas team)
36  */

37 public abstract class A_Timer extends JTestCase {
38
39     String JavaDoc mynum = "638";
40     String JavaDoc myname = "Philippe Durieux";
41
42     public A_Timer(String JavaDoc name) {
43         super(name);
44     }
45
46     protected void setUp() {
47         super.setUp();
48         useBeans("annuaire", true);
49     }
50
51     /**
52      * return PersonneHome, that can be either CMP v1 or CMP v2 bean.
53      */

54     abstract public PersonneHome getHome();
55
56     /**
57      * Test simple timer
58      */

59     public void testTimer1() throws Exception JavaDoc {
60         int duration = 5;
61         Personne p = getHome().findByPrimaryKey(myname);
62         int oldval = p.getTimerCount();
63         int id = p.setTimer(duration, 0);
64         assertEquals("timer expired too quickly", oldval, p.getTimerCount());
65         sleep(1000 * (duration + 1));
66         assertEquals("timer did not expired", oldval + 1, p.getTimerCount());
67     }
68
69     /**
70      * test getTimeRemaining on a Timer
71      */

72     public void testTimeRemaining() throws Exception JavaDoc {
73         int duration1 = 5000;
74         int duration2 = 2000;
75         Personne p = getHome().findByPrimaryKey(myname);
76         int id1 = p.setTimer(duration1, 0);
77         int id2 = p.setTimer(duration2, 0);
78         long t1 = p.getTimeRemaining(id1) / 1000;
79         long t2 = p.getTimeRemaining(id2) / 1000;
80         sleep(1000);
81         assertTrue(t1 < duration1);
82         assertTrue(t2 < duration2);
83         assertTrue(t1 > duration1 - 2);
84         assertTrue(t2 > duration2 - 2);
85     }
86
87     /**
88      * Test du cancel Timer
89      */

90     public void testCancel1() throws Exception JavaDoc {
91         int duration = 2;
92         Personne p = getHome().findByPrimaryKey(myname);
93         int oldval = p.getTimerCount();
94         int id = p.setTimer(duration, 0);
95         assertEquals("timer expired too quickly", oldval, p.getTimerCount());
96         p.cancelTimer(id);
97         sleep(1000 * (duration + 1));
98         assertEquals("timer did expired", oldval, p.getTimerCount());
99     }
100
101 }
102
Popular Tags