KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > resources > TimerServiceTester


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@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: TimerServiceTester.java 434 2006-05-09 12:48:37Z studzine $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.resources;
26
27 import javax.annotation.Resource;
28 import javax.ejb.Timer JavaDoc;
29 import javax.ejb.TimerService JavaDoc;
30 import javax.naming.Context JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32
33
34 /**
35  * TimerService Tester.
36  * @author Eduardo Studzinski Estima de Castro
37  * @author Gisele Pinheiro Souza
38  *
39  */

40 public class TimerServiceTester {
41
42     /**
43      * Timer Interval Duration.
44      */

45     public static final int TIMER_DURATION = 500000;
46     /**
47      * Timer Service.
48      */

49     @Resource
50     private TimerService JavaDoc tServ;
51
52     /**
53      * Default Constructor.
54      */

55     public TimerServiceTester(){
56     }
57
58     /**
59      * Tests a timerService access.
60      * @throws Exception if a problem occurs.
61      */

62     protected void access00() throws Exception JavaDoc{
63         checkInstance(tServ);
64     }
65
66     /**
67      * Checks if a timer service reference is working well.
68      * @param ts reference
69      * @throws Exception if a problem occurs
70      */

71     public static void checkInstance(final TimerService JavaDoc ts) throws Exception JavaDoc{
72         //Creates a timer
73
Timer JavaDoc t = ts.createTimer(TIMER_DURATION, "Timer Created");
74         //Verifies a timer method
75
t.getTimeRemaining();
76
77         //Verifies if the new timer was added to the timers list
78
if(ts.getTimers().size() < 1){
79             throw new IllegalStateException JavaDoc("The Timers Collection must have at least one timer. It was created in this method.");
80         }
81         //Cancels the timer
82
t.cancel();
83     }
84
85     /**
86      * Checks if a timer service reference can be obtained using the JNDI API.
87      * @throws Exception if a problem occurs
88      */

89     public static void checkJNDI() throws Exception JavaDoc{
90         Context JavaDoc ctx = new InitialContext JavaDoc();
91
92         TimerService JavaDoc ts = (TimerService JavaDoc) ctx.lookup("java:comp/TimerService");
93         if (ts == null){
94             throw new Exception JavaDoc("TimerService reference obtained using JNDI API is null.");
95         }
96     }
97 }
98
Popular Tags