KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.Principal JavaDoc;
25 import java.util.Properties JavaDoc;
26
27 import javax.ejb.TimerHandle JavaDoc;
28 import javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31
32 import junit.framework.TestSuite;
33
34 import org.jboss.ejb.txtimer.TimerHandleImpl;
35 import org.jboss.test.JBossTestCase;
36 import org.jboss.test.txtimer.interfaces.TimerEntity;
37 import org.jboss.test.txtimer.interfaces.TimerEntityHome;
38 import org.jboss.test.txtimer.interfaces.TimerFacade;
39 import org.jboss.test.txtimer.interfaces.TimerFacadeHome;
40
41 /**
42  * Test the Tx timer service with an Entity.
43  *
44  * @author Thomas.Diesler@jboss.org
45  * @version $Revision: 37858 $
46  * @since 07-Apr-2004
47  */

48 public class EntityBeanTestCase extends JBossTestCase
49 {
50    public EntityBeanTestCase(String JavaDoc name)
51    {
52       super(name);
53    }
54
55    public static TestSuite suite() throws Exception JavaDoc
56    {
57       TestSuite ts = new TestSuite();
58       ts.addTest(getDeploySetup(EntityBeanTestCase.class, "ejb-txtimer.jar"));
59       return ts;
60    }
61
62    /**
63     * Test that ejbTimeout is called
64     */

65    public void testSingleEventDuration() throws Exception JavaDoc
66    {
67       InitialContext JavaDoc iniCtx = getInitialContext();
68       TimerEntityHome home = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME);
69       TimerEntity entity = home.create(new Integer JavaDoc(1));
70       try
71       {
72          entity.createTimer(500, 0, null);
73          sleep(1000);
74          assertEquals("unexpected call count", 1, entity.getCallCount());
75       }
76       finally
77       {
78          entity.remove();
79       }
80    }
81
82    /**
83     * Test that ejbTimeout is called once on each instance
84     */

85    public void testInstanceAsscociation() throws Exception JavaDoc
86    {
87       InitialContext JavaDoc iniCtx = getInitialContext();
88       TimerEntityHome home = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME);
89       TimerEntity entity1 = home.create(new Integer JavaDoc(1));
90       TimerEntity entity2 = home.create(new Integer JavaDoc(2));
91
92       try
93       {
94          entity1.createTimer(500, 1000, null);
95          entity2.createTimer(500, 1000, null);
96          sleep(1000);
97          assertEquals("unexpected call count", 1, entity1.getCallCount());
98          assertEquals("unexpected call count", 1, entity2.getCallCount());
99       }
100       finally
101       {
102          entity1.remove();
103          entity2.remove();
104       }
105    }
106
107    /**
108     * Test that ejbTimeout see the container's default principal
109     */

110    public void testEjbTimeoutCallerPrincipal() throws Exception JavaDoc
111    {
112       MBeanServerConnection JavaDoc server = getServer();
113       String JavaDoc defaultPrincipal = (String JavaDoc) server.getAttribute(new ObjectName JavaDoc("jboss.security:service=JaasSecurityManager"), "DefaultUnauthenticatedPrincipal");
114
115       InitialContext JavaDoc iniCtx = getInitialContext();
116       TimerEntityHome home = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME);
117       TimerEntity entity = home.create(new Integer JavaDoc(1));
118       try
119       {
120          entity.createTimer(500, 0, null);
121          sleep(1000);
122          assertEquals("unexpected call count", 1, entity.getCallCount());
123
124          Principal JavaDoc callerPrincipal = entity.getEjbTimeoutCaller();
125          assertEquals("unexpected principal", defaultPrincipal, callerPrincipal.getName());
126       }
127       finally
128       {
129          entity.remove();
130       }
131    }
132
133    /**
134     * The TimerHandle sould not pass through the remote interface
135     */

136    public void testReturnTimerHandle() throws Exception JavaDoc
137    {
138       InitialContext JavaDoc iniCtx = getInitialContext();
139       TimerEntityHome home = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME);
140       TimerEntity entity = home.create(new Integer JavaDoc(1));
141       try
142       {
143
144          try
145          {
146             TimerHandle JavaDoc handle = (TimerHandle JavaDoc) entity.createTimerReturnHandle(500);
147             fail("TimerHandle should not pass through the remote interface: " + handle);
148          }
149          catch (Exception JavaDoc e)
150          {
151             assertTrue("Timer list should be empty", entity.getTimers().size() == 0);
152             sleep(1000);
153             assertEquals("unexpected call count", 0, entity.getCallCount());
154          }
155
156          try
157          {
158             TimerHandle JavaDoc handle = TimerHandleImpl.parse("[[id=jboss.j2ee:jndiName=test/txtimer/TimerEntity,service=EJB,pk=1],created=10-Apr-2004 20:16:11.000,first=10-Apr-2004 20:16:11.000,periode=0]");
159             String JavaDoc retStr = entity.passTimerHandle(handle);
160             fail("TimerHandle should not pass through the remote interface: " + retStr);
161          }
162          catch (Exception JavaDoc e)
163          {
164             assertTrue("Timer list should be empty", entity.getTimers().size() == 0);
165             sleep(1000);
166             assertEquals("unexpected call count", 0, entity.getCallCount());
167          }
168       }
169       finally
170       {
171          entity.remove();
172       }
173    }
174
175    /**
176     * Create the timer and rollback the transaction, the timer create should be rolled back as well
177     */

178    public void testRollbackAfterCreate() throws Exception JavaDoc
179    {
180       InitialContext JavaDoc iniCtx = getInitialContext();
181       TimerEntityHome entityHome = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME);
182       TimerEntity entity = entityHome.create(new Integer JavaDoc(1));
183
184       TimerFacadeHome facadeHome = (TimerFacadeHome) iniCtx.lookup(TimerFacadeHome.JNDI_NAME);
185       TimerFacade facade = facadeHome.create();
186       try
187       {
188          facade.rollbackAfterCreateEntity(500);
189          assertTrue("Timer list should be empty", entity.getTimers().size() == 0);
190          sleep(1000);
191          assertEquals("unexpected call count", 0, entity.getCallCount());
192       }
193       finally
194       {
195          entity.remove();
196       }
197    }
198
199    /**
200     * Cancel the timer and rollback the transaction, the timer cancel should be rolled back as well
201     */

202    public void testRollbackAfterCancel() throws Exception JavaDoc
203    {
204       InitialContext JavaDoc iniCtx = getInitialContext();
205       TimerEntityHome entityHome = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME);
206       TimerEntity entity = entityHome.create(new Integer JavaDoc(1));
207
208       TimerFacadeHome facadeHome = (TimerFacadeHome) iniCtx.lookup(TimerFacadeHome.JNDI_NAME);
209       TimerFacade facade = facadeHome.create();
210       try
211       {
212          entity.createTimer(500, 0, null);
213          facade.rollbackAfterCancelEntity();
214          sleep(1000);
215          assertEquals("unexpected call count", 1, entity.getCallCount());
216       }
217       finally
218       {
219          entity.remove();
220       }
221    }
222
223    /**
224     * Throw a RuntimeException in ejbTimeout, the timer should retry the invocation at least once
225     */

226    public void testRetryAfterRollback() throws Exception JavaDoc
227    {
228       InitialContext JavaDoc iniCtx = getInitialContext();
229       TimerEntityHome home = (TimerEntityHome) iniCtx.lookup(TimerEntityHome.JNDI_NAME);
230       TimerEntity entity = home.create(new Integer JavaDoc(1));
231
232       try
233       {
234          Properties JavaDoc props = new Properties JavaDoc();
235          props.setProperty("rollback", "true");
236
237          entity.createTimer(500, 0, props);
238          sleep(1000);
239
240          // The timer is expected to retry the invocation to ejbTimeout at least once
241
// Note, due to bean sepuku the instance for retry should be another one as for the first attempt
242
assertEquals("unexpected call count", 1, entity.getCallCount());
243       }
244       finally
245       {
246          entity.remove();
247       }
248    }
249 }
250
Popular Tags