KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > clients > timer > 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.10 2005/07/12 08:46:10 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.clients.timer;
27
28 import javax.ejb.TimerHandle JavaDoc;
29 import org.objectweb.jonas.jtests.beans.transacted.Simple;
30 import org.objectweb.jonas.jtests.util.JTestCase;
31
32 /**
33  * This is test of the TimerService.
34  * tests here are common to entity and session beans.
35  * beans used : transacted
36  * @author Philippe Durieux (jonas team)
37  */

38 public abstract class A_Timer extends JTestCase {
39
40     /**
41      * constructor
42      * @param name name of the test suite.
43      */

44     public A_Timer(String JavaDoc name) {
45         super(name);
46     }
47
48     /**
49      * Sets up the fixture, here load the beans if not loaded yet.
50      * This method is called before a test is executed.
51      */

52     protected void setUp() {
53         super.setUp();
54         useBeans("transacted", true);
55     }
56
57     /**
58      * Get an instance of the bean.
59      * This method depends on the home used to get it.
60      * For entity bean, the arg is used to get a particular instance.
61      * For session beans, we get any session bean from the pool.
62      */

63     public abstract Simple getSimple(int i) throws Exception JavaDoc;
64
65     // --------------------------------------------------------------------
66
// test cases
67
// --------------------------------------------------------------------
68

69     /**
70      * Test single-event timer
71      */

72     public void testTimer1() throws Exception JavaDoc {
73         int duration = 5;
74         Simple s = getSimple(10);
75         try {
76             int oldval = s.getTimerCount();
77             int id = s.setTimer(duration, 0);
78             sleep(2000);
79             assertEquals("timer expired too quickly", oldval, s.getTimerCount());
80             sleep(4000);
81             assertEquals("timer did not expired", oldval + 1, s.getTimerCount());
82         } finally {
83             s.remove();
84         }
85     }
86
87     /**
88      * Test single-event timer Handle
89      */

90     public void testTimerHandle1() throws Exception JavaDoc {
91         int duration = 5;
92         Simple s = getSimple(12);
93         try {
94             int oldval = s.getTimerCount();
95             int id = s.setTimerGetHandle(duration, 0);
96             sleep(2000);
97             assertEquals("timer expired too quickly", oldval, s.getTimerCount());
98             sleep(4000);
99             assertEquals("timer did not expired", oldval + 1, s.getTimerCount());
100         } finally {
101             s.remove();
102         }
103     }
104
105     /**
106      * Test periodic timer Handle
107      */

108     public void testPeriodicTimerHandle1() throws Exception JavaDoc {
109         int duration = 5;
110         int period = 200;
111         Simple s = getSimple(12);
112         try {
113             int oldval = s.getTimerCount();
114             int id = s.setTimerGetHandle(duration, period);
115             sleep(2000);
116             assertEquals("timer expired too quickly", oldval, s.getTimerCount());
117             sleep(4000);
118             assertEquals("timer did not expired", oldval + 1, s.getTimerCount());
119         } finally {
120             s.remove();
121         }
122     }
123
124     /**
125      * test getTimeRemaining on a Timer
126      */

127     public void testTimeRemaining() throws Exception JavaDoc {
128         int duration1 = 5000;
129         int duration2 = 2000;
130         Simple s = getSimple(11);
131         try {
132             int id1 = s.setTimer(duration1, 0);
133             int id2 = s.setTimer(duration2, 0);
134             long t1 = s.getTimeRemaining(id1) / 1000;
135             long t2 = s.getTimeRemaining(id2) / 1000;
136             sleep(1000);
137             assertTrue(t1 < duration1);
138             assertTrue(t2 < duration2);
139             assertTrue(t1 > duration1 - 2);
140             assertTrue(t2 > duration2 - 2);
141         } finally {
142             s.remove();
143         }
144     }
145
146     /**
147      * Test du cancel Timer
148      */

149     public void testCancel1() throws Exception JavaDoc {
150         int duration = 2;
151         Simple s = getSimple(12);
152         try {
153             int oldval = s.getTimerCount();
154             int id = s.setTimer(duration, 0);
155             assertEquals("timer expired too quickly", oldval, s.getTimerCount());
156             s.cancelTimer(id);
157             sleep(1000 * (duration + 1));
158             assertEquals("timer did expired", oldval, s.getTimerCount());
159         } finally {
160             s.remove();
161         }
162     }
163
164     /**
165      * Test of getTimers
166      */

167     public void testGetTimers() throws Exception JavaDoc {
168         Simple s = getSimple(20);
169         int [] ids = new int[10];
170         int before = s.getTimerNumber();
171         try {
172             for (int i = 0; i < 10; i++) {
173                 ids[i] = s.setTimer(i + 1, 100);
174             }
175             sleep(1000);
176             assertEquals("Bad number of timers", 10, s.getTimerNumber() - before);
177             for (int i = 0; i < 10; i++) {
178                 s.cancelTimer(ids[i]);
179             }
180         } finally {
181             s.remove();
182         }
183     }
184     
185     /**
186      * Test getTimers with cancel outside tx
187      * The cancelled timer should not be returned
188      */

189     public void testGetTimerCancelled() throws Exception JavaDoc {
190         Simple s = getSimple(20);
191         int [] ids = new int[10];
192         int before = s.getTimerNumber();
193         try {
194             for (int i = 0; i < 10; i++) {
195                 ids[i] = s.setTimer(i + 1, 100);
196             }
197             sleep(1000);
198             s.cancelTimer(ids[0]);
199             assertEquals("Bad number of timers", 9, s.getTimerNumber() - before);
200             for (int i = 1; i < 10; i++) {
201                 s.cancelTimer(ids[i]);
202             }
203         } finally {
204             s.remove();
205         }
206     }
207
208     /**
209      * Test getTimers with cancel in the transaction.
210      * The cancelled timer should not be returned
211      */

212     public void testGetTimerCancelledInTx() throws Exception JavaDoc {
213         Simple s = getSimple(20);
214         int [] ids = new int[10];
215         int before = s.getTimerNumber();
216         // start transaction
217
utx.begin();
218         try {
219             for (int i = 0; i < 10; i++) {
220                 ids[i] = s.setTimer(i + 1, 100);
221             }
222             sleep(1000);
223             s.cancelTimer(ids[0]);
224             assertEquals("Bad number of timers", 9, s.getTimerNumber() - before);
225             for (int i = 1; i < 10; i++) {
226                 s.cancelTimer(ids[i]);
227             }
228         } finally {
229             utx.commit();
230             s.remove();
231         }
232     }
233
234     /**
235      * Test getHandle on one-shot timer
236      */

237     public void testGetHandle1() throws Exception JavaDoc {
238         int duration = 2;
239         Simple s = getSimple(30);
240         try {
241             int id = s.setTimer(duration, 0);
242             TimerHandle JavaDoc th = s.getTimerHandle(id);
243             assertTrue(th != null);
244         } finally {
245             s.remove();
246         }
247     }
248
249     /**
250      * Test getHandle on one-shot timer that has expired
251      */

252     public void testGetExpiredHandle1() throws Exception JavaDoc {
253         int duration = 1;
254         Simple s = getSimple(30);
255         try {
256             int id = s.setTimer(duration, 0);
257             sleep(3000);
258             TimerHandle JavaDoc th = s.getTimerHandle(id);
259             assertTrue(th == null);
260         } finally {
261             s.remove();
262         }
263     }
264 }
265
Popular Tags