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.concurrent.unit;
023:
024: import java.lang.Thread.UncaughtExceptionHandler;
025:
026: import javax.ejb.ConcurrentAccessException;
027:
028: import org.jboss.ejb3.test.concurrent.MyStateful;
029: import org.jboss.test.JBossTestCase;
030: import junit.framework.Test;
031:
032: /**
033: * Test concurrent access on a stateful bean (EJB3 4.3.13 3rd paragraph / EJBTHREE-666)
034: *
035: * @author <a href="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
036: * @version $Revision: $
037: */
038: public class ConcurrentUnitTestCase extends JBossTestCase {
039: public ConcurrentUnitTestCase(String name) {
040: super (name);
041: }
042:
043: private static class MyUncaughtExceptionHandler implements
044: UncaughtExceptionHandler {
045: private Throwable uncaught;
046:
047: Throwable getUncaughtException() {
048: return uncaught;
049: }
050:
051: public void uncaughtException(Thread t, Throwable e) {
052: this .uncaught = e;
053: }
054: }
055:
056: public void testConcurrentAccess() throws Exception {
057: final MyStateful session = (MyStateful) getInitialContext()
058: .lookup("MyStatefulBean/remote");
059:
060: Runnable r = new Runnable() {
061: public void run() {
062: session.waitAndSee();
063: }
064: };
065:
066: // do a call first (see EJBTHREE-697)
067: session.doNothing();
068:
069: // just use one 1 handler, the exception will be in there.
070: MyUncaughtExceptionHandler eh = new MyUncaughtExceptionHandler();
071:
072: Thread t1 = new Thread(r);
073: t1.setUncaughtExceptionHandler(eh);
074: Thread t2 = new Thread(r);
075: t2.setUncaughtExceptionHandler(eh);
076:
077: t1.start();
078: t2.start();
079:
080: t1.join();
081: t2.join();
082:
083: Throwable t = eh.getUncaughtException();
084: assertNotNull(t);
085: assertTrue("Expected a javax.ejb.ConcurrentAccessException",
086: t instanceof ConcurrentAccessException);
087: }
088:
089: /**
090: * EJBTHREE-697: concurrency on proxies doesn't work
091: */
092: public void testConcurrentProxyAccess() throws Exception {
093: final MyStateful session = (MyStateful) getInitialContext()
094: .lookup("MyStatefulBean/remote");
095:
096: Runnable r = new Runnable() {
097: public void run() {
098: session.waitAndSee();
099: }
100: };
101:
102: // don't call proxy yet
103: //session.doNothing();
104:
105: // just use one 1 handler, the exception will be in there.
106: MyUncaughtExceptionHandler eh = new MyUncaughtExceptionHandler();
107:
108: Thread t1 = new Thread(r);
109: t1.setUncaughtExceptionHandler(eh);
110: Thread t2 = new Thread(r);
111: t2.setUncaughtExceptionHandler(eh);
112:
113: t1.start();
114: t2.start();
115:
116: t1.join();
117: t2.join();
118:
119: Throwable t = eh.getUncaughtException();
120: assertNotNull("No exception occured during a concurrent call",
121: t);
122: fail("never comes here");
123: }
124:
125: public static Test suite() throws Exception {
126: return getDeploySetup(ConcurrentUnitTestCase.class,
127: "concurrent.jar");
128: }
129:
130: }
|