01: /**
02: * EasyBeans
03: * Copyright (C) 2006 Bull S.A.S.
04: * Contact: easybeans@ow2.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * 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 1970 2007-10-16 11:49:25Z benoitf $
23: * --------------------------------------------------------------------------
24: */package org.ow2.easybeans.tests.common.resources;
25:
26: import javax.annotation.Resource;
27: import javax.ejb.Timer;
28: import javax.ejb.TimerService;
29: import javax.naming.Context;
30: import javax.naming.InitialContext;
31:
32: /**
33: * TimerService Tester.
34: * @author Eduardo Studzinski Estima de Castro
35: * @author Gisele Pinheiro Souza
36: *
37: */
38: public class TimerServiceTester {
39:
40: /**
41: * Timer Interval Duration.
42: */
43: public static final int TIMER_DURATION = 500000;
44: /**
45: * Timer Service.
46: */
47: @Resource
48: private TimerService tServ;
49:
50: /**
51: * Default Constructor.
52: */
53: public TimerServiceTester() {
54: }
55:
56: /**
57: * Tests a timerService access.
58: * @throws Exception if a problem occurs.
59: */
60: protected void access00() throws Exception {
61: checkInstance(tServ);
62: }
63:
64: /**
65: * Checks if a timer service reference is working well.
66: * @param ts reference
67: * @throws Exception if a problem occurs
68: */
69: public static void checkInstance(final TimerService ts)
70: throws Exception {
71: //Creates a timer
72: Timer t = ts.createTimer(TIMER_DURATION, "Timer Created");
73: //Verifies a timer method
74: t.getTimeRemaining();
75:
76: //Verifies if the new timer was added to the timers list
77: if (ts.getTimers().size() < 1) {
78: throw new IllegalStateException(
79: "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 {
90: Context ctx = new InitialContext();
91:
92: TimerService ts = (TimerService) ctx
93: .lookup("java:comp/TimerService");
94: if (ts == null) {
95: throw new Exception(
96: "TimerService reference obtained using JNDI API is null.");
97: }
98: }
99: }
|