KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.ejb.Timer JavaDoc;
25 import javax.ejb.TimerService JavaDoc;
26 import javax.transaction.TransactionManager JavaDoc;
27
28 import org.jboss.tm.TransactionManagerLocator;
29
30 /**
31  * @author Thomas.Diesler@jboss.org
32  * @author Dimitris.Andreadis@jboss.org
33  * @version $Revision: 43815 $
34  */

35 public class TransactionalTimerTestCase extends TimerTestBase
36 {
37    protected static TransactionManager JavaDoc txManager = TransactionManagerLocator.getInstance().locate();
38
39    public TransactionalTimerTestCase(String JavaDoc name)
40    {
41       super(name);
42    }
43
44    public void testRollbackBeforeExpire() throws Exception JavaDoc
45    {
46       txManager.begin();
47
48       TimedMockObject to = new TimedMockObject();
49       TimerService JavaDoc service = createTimerService(to);
50
51       service.createTimer(500, null);
52       assertEquals("Expected one txtimer", 1, service.getTimers().size());
53       txManager.rollback();
54       sleep(1000);
55       assertEquals("TimedObject called", 0, to.getCallCount());
56       assertEquals("Expected no txtimer", 0, service.getTimers().size());
57    }
58
59    public void testRollbackAfterCreate() throws Exception JavaDoc
60    {
61       txManager.begin();
62
63       TimedMockObject to = new TimedMockObject();
64       TimerService JavaDoc service = createTimerService(to);
65       service.createTimer(500, null);
66       txManager.rollback();
67       assertEquals("Expected no txtimer", 0, service.getTimers().size());
68    }
69
70    public void testRollbackCancel() throws Exception JavaDoc
71    {
72       TimedMockObject to = new TimedMockObject();
73       TimerService JavaDoc service = createTimerService(to);
74       Timer JavaDoc timer = service.createTimer(500, null);
75
76       txManager.begin();
77       timer.cancel();
78       txManager.rollback();
79
80       assertEquals("Expected one txtimer", 1, service.getTimers().size());
81       sleep(1000);
82       assertEquals("TimedObject not called", 1, to.getCallCount());
83    }
84    
85    public void testExpireBeforeCommit() throws Exception JavaDoc
86    {
87       TimedMockObject to = new TimedMockObject();
88       TimerService JavaDoc service = createTimerService(to);
89       
90       txManager.begin();
91       Timer JavaDoc timer = service.createTimer(500, null);
92       sleep(1000);
93       assertEquals("TimedObject called", 0, to.getCallCount());
94       txManager.commit();
95       // On WinXp, immediately after commit the timer will be scheduled
96
// with a time in the past, due to sleep(1000) and so it will immediately timeout
97
// On Linux, it seems the timer thread has not run at this point, so sleep again!
98
sleep(1000);
99       assertEquals("TimedObject called", 1, to.getCallCount());
100       assertEquals("Expected no txtimer", 0, service.getTimers().size());
101    }
102 }
103
Popular Tags