KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > asynchronous > unit > AsynchronousTestCase


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.ejb3.test.asynchronous.unit;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.util.Collection JavaDoc;
26 import javax.ejb.EJBAccessException JavaDoc;
27 import javax.management.MBeanServerConnection JavaDoc;
28 import javax.management.ObjectName JavaDoc;
29 import javax.transaction.RollbackException JavaDoc;
30 import javax.transaction.UserTransaction JavaDoc;
31 import org.jboss.aspects.asynch.AsynchProvider;
32 import org.jboss.aspects.asynch.Future;
33 import org.jboss.ejb3.Ejb3Registry;
34 import org.jboss.ejb3.JBossProxy;
35 import org.jboss.ejb3.asynchronous.Asynch;
36 import org.jboss.ejb3.test.asynchronous.SecuredStatelessRemote;
37 import org.jboss.ejb3.test.asynchronous.ServiceRemote;
38 import org.jboss.ejb3.test.asynchronous.StatefulClusteredRemote;
39 import org.jboss.ejb3.test.asynchronous.StatefulRemote;
40 import org.jboss.ejb3.test.asynchronous.StatelessClusteredRemote;
41 import org.jboss.ejb3.test.asynchronous.StatelessRemote;
42 import org.jboss.ejb3.test.asynchronous.TxSessionRemote;
43 import org.jboss.logging.Logger;
44 import org.jboss.security.SecurityAssociation;
45 import org.jboss.security.SimplePrincipal;
46 import org.jboss.test.JBossTestCase;
47 import junit.framework.Test;
48
49 /**
50  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
51  * @version $Revision: 43264 $
52  */

53 public class AsynchronousTestCase extends JBossTestCase
54 {
55    private static final Logger log = Logger.getLogger(AsynchronousTestCase.class);
56
57    static boolean deployed = false;
58    static int test = 0;
59
60    public AsynchronousTestCase(String JavaDoc name)
61    {
62       super(name);
63    }
64
65    public void testSLRemoteAsynchronous() throws Exception JavaDoc
66    {
67       StatelessRemote tester =
68             (StatelessRemote) getInitialContext().lookup("StatelessBean/remote");
69       assertEquals("Wrong return for stateless remote", 11, tester.method(11));
70
71       StatelessRemote asynchTester = (StatelessRemote)Asynch.getAsynchronousProxy(tester);
72       assertEquals("Wrong return value for stateless remote", 0, asynchTester.method(12));
73       Future future = Asynch.getFutureResult(asynchTester);
74       int ret = (Integer JavaDoc) future.get();
75       assertEquals("Wrong async return value for stateless remote", ret, 12);
76    }
77
78    public void testSLClusteredAsynchronous() throws Exception JavaDoc
79    {
80       StatelessClusteredRemote tester =
81             (StatelessClusteredRemote) getInitialContext().lookup("StatelessClusteredBean/remote");
82       assertEquals("Wrong return for stateless clustered", 21, tester.method(21));
83
84       StatelessClusteredRemote asynchTester = (StatelessClusteredRemote)((JBossProxy)tester).getAsynchronousProxy();
85       assertEquals("Wrong return value for stateless clustered", 0, asynchTester.method(22));
86       AsynchProvider ap = (AsynchProvider) asynchTester;
87       Future future = ap.getFuture();
88       int ret = (Integer JavaDoc) future.get();
89       assertEquals("Wrong async return value for stateless clustered", ret, 22);
90    }
91
92    public void testSLLocalAsynchrounous() throws Exception JavaDoc
93    {
94       MBeanServerConnection JavaDoc server = getServer();
95       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.ejb3:service=Tester,test=asynchronous");
96       Object JavaDoc[] params = {};
97       String JavaDoc[] sig = {};
98       server.invoke(testerName, "testSLLocalAsynchronous", params, sig);
99    }
100
101    public void testSFRemoteAsynchronous() throws Exception JavaDoc
102    {
103       StatefulRemote tester =
104             (StatefulRemote) getInitialContext().lookup("StatefulBean/remote");
105       assertEquals("Wrong return for stateful remote", 31, tester.method(31));
106
107       StatefulRemote asynchTester = (StatefulRemote)((JBossProxy)tester).getAsynchronousProxy();
108       assertEquals("Wrong return value for stateful remote", 0, asynchTester.method(32));
109       AsynchProvider ap = (AsynchProvider) asynchTester;
110       Future future = ap.getFuture();
111       int ret = (Integer JavaDoc) future.get();
112       assertEquals("Wrong async return value for stateful remote", ret, 32);
113    }
114
115    public void testSFClusteredAsynchronous() throws Exception JavaDoc
116    {
117       StatefulClusteredRemote tester =
118             (StatefulClusteredRemote) getInitialContext().lookup("StatefulClusteredBean/remote");
119       assertEquals("Wrong return for stateful clustered", 41, tester.method(41));
120
121       StatefulClusteredRemote asynchTester = (StatefulClusteredRemote)((JBossProxy)tester).getAsynchronousProxy();
122       assertEquals("Wrong return value for stateful clustered", 0, asynchTester.method(42));
123       AsynchProvider ap = (AsynchProvider) asynchTester;
124       Future future = ap.getFuture();
125       int ret = (Integer JavaDoc) future.get();
126       assertEquals("Wrong async return value for stateful clustered", ret, 42);
127    }
128
129    public void testSFLocalAsynchrounous() throws Exception JavaDoc
130    {
131       MBeanServerConnection JavaDoc server = getServer();
132       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.ejb3:service=Tester,test=asynchronous");
133       Object JavaDoc[] params = {};
134       String JavaDoc[] sig = {};
135       server.invoke(testerName, "testSFLocalAsynchronous", params, sig);
136    }
137
138    public void testServiceRemoteAsynchronous() throws Exception JavaDoc
139    {
140       ServiceRemote tester =
141             (ServiceRemote) getInitialContext().lookup("ServiceBean/remote");
142       assertEquals("Wrong return for service remote", 51, tester.method(51));
143
144       ServiceRemote asynchTester = (ServiceRemote)((JBossProxy)tester).getAsynchronousProxy();
145       assertEquals("Wrong return value for service remote", 0, asynchTester.method(52));
146       AsynchProvider ap = (AsynchProvider) asynchTester;
147       Future future = ap.getFuture();
148       int ret = (Integer JavaDoc) future.get();
149       assertEquals("Wrong async return value for service remote", ret, 52);
150    }
151
152    public void testServiceLocalAsynchrounous() throws Exception JavaDoc
153    {
154       MBeanServerConnection JavaDoc server = getServer();
155       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.ejb3:service=Tester,test=asynchronous");
156       Object JavaDoc[] params = {};
157       String JavaDoc[] sig = {};
158       server.invoke(testerName, "testServiceLocalAsynchronous", params, sig);
159    }
160
161    public void testAsynchSecurity() throws Exception JavaDoc
162    {
163
164       SecuredStatelessRemote tester =
165             (SecuredStatelessRemote) getInitialContext().lookup("SecuredStatelessBean/remote");
166       SecuredStatelessRemote asynchTester = (SecuredStatelessRemote)((JBossProxy)tester).getAsynchronousProxy();
167       AsynchProvider ap = (AsynchProvider)asynchTester;
168
169       SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail"));
170       SecurityAssociation.setCredential("password".toCharArray());
171
172       asynchTester.method(61);
173       Object JavaDoc ret = getReturnOrException(ap);
174       assertTrue("SecurityException not thrown: " + ret, ret instanceof EJBAccessException JavaDoc);
175
176       asynchTester.uncheckedMethod(62);
177       ret = getReturnOrException(ap);
178       assertEquals("Wrong value", 62, ret);
179
180       asynchTester.excludedMethod(63);
181       ret = getReturnOrException(ap);
182       assertTrue("SecurityException not thrown: " + ret, ret instanceof EJBAccessException JavaDoc);
183
184       SecurityAssociation.setPrincipal(new SimplePrincipal("somebody"));
185       SecurityAssociation.setCredential("password".toCharArray());
186
187       asynchTester.method(64);
188       ret = getReturnOrException(ap);
189       assertEquals("Wrong return for authorised method", 64, ret);
190
191       SecurityAssociation.setPrincipal(new SimplePrincipal("nosuchuser"));
192       SecurityAssociation.setCredential("password".toCharArray());
193
194       asynchTester.method(65);
195       ret = getReturnOrException(ap);
196       assertTrue("SecurityException not thrown: " + ret, ret instanceof EJBAccessException JavaDoc);
197    }
198
199    public void testRemoteAsynchTransaction() throws Exception JavaDoc
200    {
201       TxSessionRemote tester = (TxSessionRemote) getInitialContext().lookup("TxSessionBean/remote");
202       TxSessionRemote asynchTester = (TxSessionRemote)((JBossProxy)tester).getAsynchronousProxy();
203       AsynchProvider ap = (AsynchProvider) asynchTester;
204       UserTransaction JavaDoc tx = (UserTransaction JavaDoc)getInitialContext().lookup("UserTransaction");
205
206       //Add some entries in different threads and commit
207
tx.begin();
208       tester.createFruit("apple", false);
209       tester.createFruit("pear", false);
210       tester.createFruit("tomato", false);
211
212       asynchTester.createVeg("Potato", false);
213       waitForProvider(ap);
214       asynchTester.createVeg("Turnip", false);
215       waitForProvider(ap);
216       asynchTester.createVeg("Carrot", false);
217       waitForProvider(ap);
218       tx.commit();
219
220       tx.begin();
221       Collection JavaDoc entries = tester.getEntries();
222       tx.commit();
223       assertEquals("Wrong number of entries", 6, entries.size());
224
225       //Cleanup synchronously
226
tx.begin();
227       tester.cleanAll();
228       tx.commit();
229
230       tx.begin();
231       entries = tester.getEntries();
232       tx.commit();
233       assertEquals("Wrong number of entries", 0, entries.size());
234
235       //Add some entries in different threads and rollback
236
tx.begin();
237       tester.createFruit("apple", false);
238       tester.createFruit("pear", false);
239       tester.createFruit("tomato", false);
240
241       asynchTester.createVeg("Potato", false);
242       waitForProvider(ap);
243       asynchTester.createVeg("Turnip", false);
244       waitForProvider(ap);
245       asynchTester.createVeg("Carrot", false);
246       waitForProvider(ap);
247       tx.rollback();
248
249       tx.begin();
250       entries = tester.getEntries();
251       tx.commit();
252
253       assertEquals("Wrong number of entries", 0, entries.size());
254
255       //Add some entries in different threads and rollback from within Tx
256
tx.begin();
257       tester.createFruit("apple", false);
258       tester.createFruit("pear", false);
259       tester.createFruit("tomato", true);
260
261       asynchTester.createVeg("Potato", false);
262       waitForProvider(ap);
263       asynchTester.createVeg("Turnip", false);
264       waitForProvider(ap);
265       asynchTester.createVeg("Carrot", true);
266       waitForProvider(ap);
267
268       boolean rollbackException = false;
269       try
270       {
271          tx.commit();
272       }
273       catch(RollbackException JavaDoc e)
274       {
275          rollbackException = true;
276       }
277
278       assertTrue("RollbackException not picked up", rollbackException);
279
280       tx.begin();
281       entries = tester.getEntries();
282       tx.commit();
283       assertEquals("Wrong number of entries", 0, entries.size());
284    }
285
286    public void testLocalAsynchTransaction() throws Exception JavaDoc
287    {
288       MBeanServerConnection JavaDoc server = getServer();
289       ObjectName JavaDoc testerName = new ObjectName JavaDoc("jboss.ejb3:service=Tester,test=asynchronous");
290       Object JavaDoc[] params = {};
291       String JavaDoc[] sig = {};
292       server.invoke(testerName, "testLocalAsynchTransaction", params, sig);
293
294    }
295
296    private Object JavaDoc getReturnOrException(AsynchProvider provider)throws Exception JavaDoc
297    {
298       try
299       {
300          Future future = provider.getFuture();
301
302          waitForFuture(future);
303          return future.get();
304       }
305       catch(InvocationTargetException JavaDoc e)
306       {
307          return e.getCause();
308       }
309    }
310
311    private void waitForProvider(AsynchProvider provider) throws InterruptedException JavaDoc
312    {
313       Future future = provider.getFuture();
314       waitForFuture(future);
315    }
316
317    private void waitForFuture(Future future) throws InterruptedException JavaDoc
318    {
319       while (!future.isDone())
320       {
321          Thread.sleep(100);
322       }
323    }
324
325    public static Test suite() throws Exception JavaDoc
326    {
327       return getDeploySetup(AsynchronousTestCase.class, "asynchronous-test.sar, asynchronous-test.jar");
328    }
329
330
331 }
332
Popular Tags