KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > txtimer > test > PersistenceTestCase


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.txtimer.test;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.List JavaDoc;
27
28 import javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.MBeanServerInvocationHandler JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32
33 import junit.framework.TestSuite;
34
35 import org.jboss.ejb.txtimer.DatabasePersistencePolicyMBean;
36 import org.jboss.ejb.txtimer.EJBTimerService;
37 import org.jboss.ejb.txtimer.PersistencePolicy;
38 import org.jboss.ejb.txtimer.TimedObjectId;
39 import org.jboss.ejb.txtimer.TimerHandleImpl;
40 import org.jboss.mx.util.ObjectNameFactory;
41 import org.jboss.test.JBossTestCase;
42 import org.jboss.test.txtimer.interfaces.TimerEntity;
43 import org.jboss.test.txtimer.interfaces.TimerEntityHome;
44 import org.jboss.test.txtimer.interfaces.TimerSession;
45 import org.jboss.test.txtimer.interfaces.TimerSessionHome;
46
47 /**
48  * Test the Tx timer service with an Entity.
49  *
50  * @author Thomas.Diesler@jboss.org
51  * @author Dimitris.Andreadis@jboss.org
52  * @version $Revision: 38205 $
53  * @since 07-Apr-2004
54  */

55 public class PersistenceTestCase extends JBossTestCase
56 {
57    private PersistencePolicy pp;
58
59    public PersistenceTestCase(String JavaDoc name)
60    {
61       super(name);
62    }
63
64    public static TestSuite suite() throws Exception JavaDoc
65    {
66       TestSuite ts = new TestSuite();
67       ts.addTest(getDeploySetup(PersistenceTestCase.class, "ejb-txtimer.jar"));
68       return ts;
69    }
70
71    protected void setUp() throws Exception JavaDoc
72    {
73       super.setUp();
74       pp = new PersistencePolicyDelegate(getServer());
75    }
76
77    /**
78     * Test that ejbTimeout is called
79     */

80    public void testSingleEventDuration() throws Exception JavaDoc
81    {
82       // check that there are no timers persisted
83
List JavaDoc timerHandles = pp.listTimerHandles();
84       assertEquals("unexpected handle count", 0, timerHandles.size());
85
86       InitialContext JavaDoc iniCtx = getInitialContext();
87       TimerEntityHome home = (TimerEntityHome)iniCtx.lookup(TimerEntityHome.JNDI_NAME);
88       TimerEntity entity = home.create(new Integer JavaDoc(1));
89       try
90       {
91          entity.createTimer(500, 0, null);
92
93          timerHandles = pp.listTimerHandles();
94          assertEquals("unexpected handle count", 1, timerHandles.size());
95
96          sleep(1000);
97          assertEquals("unexpected call count", 1, entity.getCallCount());
98
99          timerHandles = pp.listTimerHandles();
100          assertEquals("unexpected handle count", 0, timerHandles.size());
101       }
102       finally
103       {
104          entity.remove();
105       }
106    }
107
108    /**
109     * Insert a timer for an EB and fake rstore after server startup
110     */

111    public void testRestoreToEntity() throws Exception JavaDoc
112    {
113       // check that there are no timers persisted
114
List JavaDoc timerHandles = pp.listTimerHandles();
115       assertEquals("unexpected handle count", 0, timerHandles.size());
116
117       InitialContext JavaDoc iniCtx = getInitialContext();
118       TimerEntityHome home = (TimerEntityHome)iniCtx.lookup(TimerEntityHome.JNDI_NAME);
119       TimerEntity entity = home.create(new Integer JavaDoc(1));
120       try
121       {
122          // insert a timer into the db
123
ObjectName JavaDoc oname = ObjectNameFactory.create("jboss.j2ee:jndiName=test/txtimer/TimerEntity,service=EJB");
124          TimedObjectId targetId = new TimedObjectId(oname, new Integer JavaDoc(1));
125          pp.insertTimer("pk1", targetId, new Date JavaDoc(), 0, null);
126          sleep(500);
127
128          timerHandles = pp.listTimerHandles();
129          assertEquals("unexpected handle count", 1, timerHandles.size());
130
131          // fake restore on server startup
132
// we cannot test that the PersistencePolicy is notified after startup
133
try
134          {
135             getServer().invoke(
136                   EJBTimerService.OBJECT_NAME,
137                   "restoreTimers",
138                   new Object JavaDoc[] { oname, null },
139                   new String JavaDoc[]{"javax.management.ObjectName", "java.lang.ClassLoader" }
140                   );
141          }
142          catch (Exception JavaDoc e)
143          {
144             log.warn("Could not restore ejb timers", e);
145          }
146          sleep(500);
147          assertEquals("unexpected call count", 1, entity.getCallCount());
148
149          timerHandles = pp.listTimerHandles();
150          assertEquals("unexpected handle count", 0, timerHandles.size());
151       }
152       finally
153       {
154          entity.remove();
155       }
156    }
157
158    /**
159     * Insert a timer for a SLSB and fake rstore after server startup
160     */

161    public void testRestoreToSession() throws Exception JavaDoc
162    {
163       // check that there are no timers persisted
164
List JavaDoc timerHandles = pp.listTimerHandles();
165       assertEquals("unexpected handle count", 0, timerHandles.size());
166
167       InitialContext JavaDoc iniCtx = getInitialContext();
168       TimerSessionHome home = (TimerSessionHome)iniCtx.lookup(TimerSessionHome.JNDI_NAME);
169       TimerSession session = home.create();
170       session.resetCallCount();
171       try
172       {
173          // insert a timer into the db
174
ObjectName JavaDoc oname = ObjectNameFactory.create("jboss.j2ee:jndiName=test/txtimer/TimerSession,service=EJB");
175          TimedObjectId targetId = new TimedObjectId(oname);
176          pp.insertTimer("pk1", targetId, new Date JavaDoc(), 0, null);
177          sleep(500);
178
179          timerHandles = pp.listTimerHandles();
180          assertEquals("unexpected handle count", 1, timerHandles.size());
181
182          // fake restore on server startup
183
// we cannot test that the PersistencePolicy is notified after startup
184
try
185          {
186             getServer().invoke(
187                   EJBTimerService.OBJECT_NAME,
188                   "restoreTimers",
189                   new Object JavaDoc[] { oname, null },
190                   new String JavaDoc[]{"javax.management.ObjectName", "java.lang.ClassLoader" }
191                   );
192          }
193          catch (Exception JavaDoc e)
194          {
195             log.warn("Could not restore ejb timers", e);
196          }
197          sleep(500);
198          assertEquals("unexpected call count", 1, session.getGlobalCallCount());
199
200          timerHandles = pp.listTimerHandles();
201          assertEquals("unexpected handle count", 0, timerHandles.size());
202       }
203       finally
204       {
205          session.remove();
206       }
207    }
208
209    /**
210     * Test the database roundtrip
211     */

212    public void testPersistenceEquality() throws Exception JavaDoc
213    {
214       // check that there are no timers persisted
215
List JavaDoc timerHandles = pp.listTimerHandles();
216       assertEquals("unexpected handle count", 0, timerHandles.size());
217
218       // insert a timer into the db
219
String JavaDoc timerId = "pk1";
220       ObjectName JavaDoc oname = ObjectNameFactory.create("jboss.j2ee:jndiName=test/txtimer/TimerEntity,service=EJB");
221       TimedObjectId targetId = new TimedObjectId(oname, new Integer JavaDoc(100));
222       Date JavaDoc firstEvent = new Date JavaDoc ();
223       String JavaDoc info = "info";
224       pp.insertTimer(timerId, targetId, firstEvent, 100, info);
225
226       timerHandles = pp.listTimerHandles();
227       assertEquals("unexpected handle count", 1, timerHandles.size());
228
229       TimerHandleImpl handle = (TimerHandleImpl)timerHandles.get(0);
230       assertEquals("TimerId is not equal", timerId, handle.getTimerId());
231       assertEquals("TimedObjectId is not equal", targetId, handle.getTimedObjectId());
232       assertEquals("firstEvent is not equal", firstEvent, handle.getFirstTime());
233       assertEquals("periode is not equal", 100, handle.getPeriode());
234       assertEquals("info is not equal", info, handle.getInfo());
235
236       pp.clearTimers();
237
238       timerHandles = pp.listTimerHandles();
239       assertEquals("unexpected handle count", 0, timerHandles.size());
240    }
241
242    /**
243     * Make the invokations to the MBeanConnection typesafe
244     */

245    static class PersistencePolicyDelegate implements PersistencePolicy
246    {
247       DatabasePersistencePolicyMBean proxy;
248
249       public PersistencePolicyDelegate(MBeanServerConnection JavaDoc server)
250       {
251          proxy = (DatabasePersistencePolicyMBean)
252             MBeanServerInvocationHandler.newProxyInstance(server,
253             DatabasePersistencePolicyMBean.OBJECT_NAME,
254             DatabasePersistencePolicyMBean.class, false);
255       }
256
257       public void insertTimer(String JavaDoc timerId, TimedObjectId targetId, Date JavaDoc firstEvent, long periode, Serializable JavaDoc info)
258       {
259          try
260          {
261             proxy.insertTimer(timerId, targetId, firstEvent, periode, info);
262          }
263          catch (Exception JavaDoc e)
264          {
265             throw new RuntimeException JavaDoc(e);
266          }
267       }
268
269       public void deleteTimer(String JavaDoc timerId, TimedObjectId targetId)
270       {
271          try
272          {
273             proxy.deleteTimer(timerId, targetId);
274          }
275          catch (Exception JavaDoc e)
276          {
277             throw new RuntimeException JavaDoc(e);
278          }
279       }
280
281       public void clearTimers()
282       {
283          try
284          {
285             proxy.clearTimers();
286          }
287          catch (Exception JavaDoc e)
288          {
289             throw new RuntimeException JavaDoc(e);
290          }
291       }
292
293       public void restoreTimers()
294       {
295          try
296          {
297             proxy.resetAndRestoreTimers();
298          }
299          catch (Exception JavaDoc e)
300          {
301             throw new RuntimeException JavaDoc(e);
302          }
303       }
304
305       public List JavaDoc listTimerHandles(ObjectName JavaDoc containerId, ClassLoader JavaDoc loader)
306       {
307          List JavaDoc list = null;
308          try
309          {
310             list = proxy.listTimerHandles(containerId, loader);
311          }
312          catch (Exception JavaDoc e)
313          {
314             throw new RuntimeException JavaDoc(e);
315          }
316          return list;
317       }
318       
319       public List JavaDoc listTimerHandles()
320       {
321          List JavaDoc list = null;
322          try
323          {
324             list = proxy.listTimerHandles();
325          }
326          catch (Exception JavaDoc e)
327          {
328             throw new RuntimeException JavaDoc(e);
329          }
330          return list;
331       }
332    }
333 }
334
Popular Tags