001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.test.asynchronous.unit;
023:
024: import java.lang.reflect.InvocationTargetException;
025: import java.util.Collection;
026: import javax.ejb.EJBAccessException;
027: import javax.management.MBeanServerConnection;
028: import javax.management.ObjectName;
029: import javax.transaction.RollbackException;
030: import javax.transaction.UserTransaction;
031: import org.jboss.aspects.asynch.AsynchProvider;
032: import org.jboss.aspects.asynch.Future;
033: import org.jboss.ejb3.Ejb3Registry;
034: import org.jboss.ejb3.JBossProxy;
035: import org.jboss.ejb3.asynchronous.Asynch;
036: import org.jboss.ejb3.test.asynchronous.SecuredStatelessRemote;
037: import org.jboss.ejb3.test.asynchronous.ServiceRemote;
038: import org.jboss.ejb3.test.asynchronous.StatefulClusteredRemote;
039: import org.jboss.ejb3.test.asynchronous.StatefulRemote;
040: import org.jboss.ejb3.test.asynchronous.StatelessClusteredRemote;
041: import org.jboss.ejb3.test.asynchronous.StatelessRemote;
042: import org.jboss.ejb3.test.asynchronous.TxSessionRemote;
043: import org.jboss.logging.Logger;
044: import org.jboss.security.SecurityAssociation;
045: import org.jboss.security.SimplePrincipal;
046: import org.jboss.test.JBossTestCase;
047: import junit.framework.Test;
048:
049: /**
050: * @author <a href="mailto:kabir.khan@jboss.org">Kabir Khan</a>
051: * @version $Revision: 57207 $
052: */
053: public class AsynchronousTestCase extends JBossTestCase {
054: private static final Logger log = Logger
055: .getLogger(AsynchronousTestCase.class);
056:
057: static boolean deployed = false;
058: static int test = 0;
059:
060: public AsynchronousTestCase(String name) {
061: super (name);
062: }
063:
064: public void testSLRemoteAsynchronous() throws Exception {
065: StatelessRemote tester = (StatelessRemote) getInitialContext()
066: .lookup("StatelessBean/remote");
067: assertEquals("Wrong return for stateless remote", 11, tester
068: .method(11));
069:
070: StatelessRemote asynchTester = (StatelessRemote) Asynch
071: .getAsynchronousProxy(tester);
072: assertEquals("Wrong return value for stateless remote", 0,
073: asynchTester.method(12));
074: Future future = Asynch.getFutureResult(asynchTester);
075: int ret = (Integer) future.get();
076: assertEquals("Wrong async return value for stateless remote",
077: ret, 12);
078: }
079:
080: public void testSLClusteredAsynchronous() throws Exception {
081: StatelessClusteredRemote tester = (StatelessClusteredRemote) getInitialContext()
082: .lookup("StatelessClusteredBean/remote");
083: assertEquals("Wrong return for stateless clustered", 21, tester
084: .method(21));
085:
086: StatelessClusteredRemote asynchTester = (StatelessClusteredRemote) ((JBossProxy) tester)
087: .getAsynchronousProxy();
088: assertEquals("Wrong return value for stateless clustered", 0,
089: asynchTester.method(22));
090: AsynchProvider ap = (AsynchProvider) asynchTester;
091: Future future = ap.getFuture();
092: int ret = (Integer) future.get();
093: assertEquals(
094: "Wrong async return value for stateless clustered",
095: ret, 22);
096: }
097:
098: public void testSLLocalAsynchrounous() throws Exception {
099: MBeanServerConnection server = getServer();
100: ObjectName testerName = new ObjectName(
101: "jboss.ejb3:service=Tester,test=asynchronous");
102: Object[] params = {};
103: String[] sig = {};
104: server.invoke(testerName, "testSLLocalAsynchronous", params,
105: sig);
106: }
107:
108: public void testSFRemoteAsynchronous() throws Exception {
109: StatefulRemote tester = (StatefulRemote) getInitialContext()
110: .lookup("StatefulBean/remote");
111: assertEquals("Wrong return for stateful remote", 31, tester
112: .method(31));
113:
114: StatefulRemote asynchTester = (StatefulRemote) ((JBossProxy) tester)
115: .getAsynchronousProxy();
116: assertEquals("Wrong return value for stateful remote", 0,
117: asynchTester.method(32));
118: AsynchProvider ap = (AsynchProvider) asynchTester;
119: Future future = ap.getFuture();
120: int ret = (Integer) future.get();
121: assertEquals("Wrong async return value for stateful remote",
122: ret, 32);
123: }
124:
125: public void testSFClusteredAsynchronous() throws Exception {
126: StatefulClusteredRemote tester = (StatefulClusteredRemote) getInitialContext()
127: .lookup("StatefulClusteredBean/remote");
128: assertEquals("Wrong return for stateful clustered", 41, tester
129: .method(41));
130:
131: StatefulClusteredRemote asynchTester = (StatefulClusteredRemote) ((JBossProxy) tester)
132: .getAsynchronousProxy();
133: assertEquals("Wrong return value for stateful clustered", 0,
134: asynchTester.method(42));
135: AsynchProvider ap = (AsynchProvider) asynchTester;
136: Future future = ap.getFuture();
137: int ret = (Integer) future.get();
138: assertEquals("Wrong async return value for stateful clustered",
139: ret, 42);
140: }
141:
142: public void testSFLocalAsynchrounous() throws Exception {
143: MBeanServerConnection server = getServer();
144: ObjectName testerName = new ObjectName(
145: "jboss.ejb3:service=Tester,test=asynchronous");
146: Object[] params = {};
147: String[] sig = {};
148: server.invoke(testerName, "testSFLocalAsynchronous", params,
149: sig);
150: }
151:
152: public void testServiceRemoteAsynchronous() throws Exception {
153: ServiceRemote tester = (ServiceRemote) getInitialContext()
154: .lookup("ServiceBean/remote");
155: assertEquals("Wrong return for service remote", 51, tester
156: .method(51));
157:
158: ServiceRemote asynchTester = (ServiceRemote) ((JBossProxy) tester)
159: .getAsynchronousProxy();
160: assertEquals("Wrong return value for service remote", 0,
161: asynchTester.method(52));
162: AsynchProvider ap = (AsynchProvider) asynchTester;
163: Future future = ap.getFuture();
164: int ret = (Integer) future.get();
165: assertEquals("Wrong async return value for service remote",
166: ret, 52);
167: }
168:
169: public void testServiceLocalAsynchrounous() throws Exception {
170: MBeanServerConnection server = getServer();
171: ObjectName testerName = new ObjectName(
172: "jboss.ejb3:service=Tester,test=asynchronous");
173: Object[] params = {};
174: String[] sig = {};
175: server.invoke(testerName, "testServiceLocalAsynchronous",
176: params, sig);
177: }
178:
179: public void testAsynchSecurity() throws Exception {
180:
181: SecuredStatelessRemote tester = (SecuredStatelessRemote) getInitialContext()
182: .lookup("SecuredStatelessBean/remote");
183: SecuredStatelessRemote asynchTester = (SecuredStatelessRemote) ((JBossProxy) tester)
184: .getAsynchronousProxy();
185: AsynchProvider ap = (AsynchProvider) asynchTester;
186:
187: SecurityAssociation
188: .setPrincipal(new SimplePrincipal("rolefail"));
189: SecurityAssociation.setCredential("password".toCharArray());
190:
191: asynchTester.method(61);
192: Object ret = getReturnOrException(ap);
193: assertTrue("SecurityException not thrown: " + ret,
194: ret instanceof EJBAccessException);
195:
196: asynchTester.uncheckedMethod(62);
197: ret = getReturnOrException(ap);
198: assertEquals("Wrong value", 62, ret);
199:
200: asynchTester.excludedMethod(63);
201: ret = getReturnOrException(ap);
202: assertTrue("SecurityException not thrown: " + ret,
203: ret instanceof EJBAccessException);
204:
205: SecurityAssociation
206: .setPrincipal(new SimplePrincipal("somebody"));
207: SecurityAssociation.setCredential("password".toCharArray());
208:
209: asynchTester.method(64);
210: ret = getReturnOrException(ap);
211: assertEquals("Wrong return for authorised method", 64, ret);
212:
213: SecurityAssociation.setPrincipal(new SimplePrincipal(
214: "nosuchuser"));
215: SecurityAssociation.setCredential("password".toCharArray());
216:
217: asynchTester.method(65);
218: ret = getReturnOrException(ap);
219: assertTrue("SecurityException not thrown: " + ret,
220: ret instanceof EJBAccessException);
221: }
222:
223: public void testRemoteAsynchTransaction() throws Exception {
224: TxSessionRemote tester = (TxSessionRemote) getInitialContext()
225: .lookup("TxSessionBean/remote");
226: TxSessionRemote asynchTester = (TxSessionRemote) ((JBossProxy) tester)
227: .getAsynchronousProxy();
228: AsynchProvider ap = (AsynchProvider) asynchTester;
229: UserTransaction tx = (UserTransaction) getInitialContext()
230: .lookup("UserTransaction");
231:
232: //Add some entries in different threads and commit
233: tx.begin();
234: tester.createFruit("apple", false);
235: tester.createFruit("pear", false);
236: tester.createFruit("tomato", false);
237:
238: asynchTester.createVeg("Potato", false);
239: waitForProvider(ap);
240: asynchTester.createVeg("Turnip", false);
241: waitForProvider(ap);
242: asynchTester.createVeg("Carrot", false);
243: waitForProvider(ap);
244: tx.commit();
245:
246: tx.begin();
247: Collection entries = tester.getEntries();
248: tx.commit();
249: assertEquals("Wrong number of entries", 6, entries.size());
250:
251: //Cleanup synchronously
252: tx.begin();
253: tester.cleanAll();
254: tx.commit();
255:
256: tx.begin();
257: entries = tester.getEntries();
258: tx.commit();
259: assertEquals("Wrong number of entries", 0, entries.size());
260:
261: //Add some entries in different threads and rollback
262: tx.begin();
263: tester.createFruit("apple", false);
264: tester.createFruit("pear", false);
265: tester.createFruit("tomato", false);
266:
267: asynchTester.createVeg("Potato", false);
268: waitForProvider(ap);
269: asynchTester.createVeg("Turnip", false);
270: waitForProvider(ap);
271: asynchTester.createVeg("Carrot", false);
272: waitForProvider(ap);
273: tx.rollback();
274:
275: tx.begin();
276: entries = tester.getEntries();
277: tx.commit();
278:
279: assertEquals("Wrong number of entries", 0, entries.size());
280:
281: //Add some entries in different threads and rollback from within Tx
282: tx.begin();
283: tester.createFruit("apple", false);
284: tester.createFruit("pear", false);
285: tester.createFruit("tomato", true);
286:
287: asynchTester.createVeg("Potato", false);
288: waitForProvider(ap);
289: asynchTester.createVeg("Turnip", false);
290: waitForProvider(ap);
291: asynchTester.createVeg("Carrot", true);
292: waitForProvider(ap);
293:
294: boolean rollbackException = false;
295: try {
296: tx.commit();
297: } catch (RollbackException e) {
298: rollbackException = true;
299: }
300:
301: assertTrue("RollbackException not picked up", rollbackException);
302:
303: tx.begin();
304: entries = tester.getEntries();
305: tx.commit();
306: assertEquals("Wrong number of entries", 0, entries.size());
307: }
308:
309: public void testLocalAsynchTransaction() throws Exception {
310: MBeanServerConnection server = getServer();
311: ObjectName testerName = new ObjectName(
312: "jboss.ejb3:service=Tester,test=asynchronous");
313: Object[] params = {};
314: String[] sig = {};
315: server.invoke(testerName, "testLocalAsynchTransaction", params,
316: sig);
317:
318: }
319:
320: private Object getReturnOrException(AsynchProvider provider)
321: throws Exception {
322: try {
323: Future future = provider.getFuture();
324:
325: waitForFuture(future);
326: return future.get();
327: } catch (InvocationTargetException e) {
328: return e.getCause();
329: }
330: }
331:
332: private void waitForProvider(AsynchProvider provider)
333: throws InterruptedException {
334: Future future = provider.getFuture();
335: waitForFuture(future);
336: }
337:
338: private void waitForFuture(Future future)
339: throws InterruptedException {
340: while (!future.isDone()) {
341: Thread.sleep(100);
342: }
343: }
344:
345: public static Test suite() throws Exception {
346: return getDeploySetup(AsynchronousTestCase.class,
347: "asynchronous-test.sar, asynchronous-test.jar");
348: }
349:
350: }
|