KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > concurrent > unit > ConcurrentUnitTestCase


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.concurrent.unit;
23
24 import java.lang.Thread.UncaughtExceptionHandler;
25
26 import javax.ejb.ConcurrentAccessException JavaDoc;
27
28 import org.jboss.ejb3.test.concurrent.MyStateful;
29 import org.jboss.test.JBossTestCase;
30 import junit.framework.Test;
31
32 /**
33  * Test concurrent access on a stateful bean (EJB3 4.3.13 3rd paragraph / EJBTHREE-666)
34  *
35  * @author <a HREF="mailto:carlo.dewolf@jboss.com">Carlo de Wolf</a>
36  * @version $Revision: $
37  */

38 public class ConcurrentUnitTestCase
39    extends JBossTestCase
40 {
41    public ConcurrentUnitTestCase(String JavaDoc name)
42    {
43       super(name);
44    }
45    
46    private static class MyUncaughtExceptionHandler implements UncaughtExceptionHandler
47    {
48       private Throwable JavaDoc uncaught;
49       
50       Throwable JavaDoc getUncaughtException()
51       {
52          return uncaught;
53       }
54       
55       public void uncaughtException(Thread JavaDoc t, Throwable JavaDoc e)
56       {
57          this.uncaught = e;
58       }
59    }
60    
61    public void testConcurrentAccess() throws Exception JavaDoc
62    {
63       final MyStateful session = (MyStateful) getInitialContext().lookup("MyStatefulBean/remote");
64       
65       Runnable JavaDoc r = new Runnable JavaDoc()
66       {
67          public void run()
68          {
69             session.waitAndSee();
70          }
71       };
72
73       // do a call first (see EJBTHREE-697)
74
session.doNothing();
75       
76       // just use one 1 handler, the exception will be in there.
77
MyUncaughtExceptionHandler eh = new MyUncaughtExceptionHandler();
78       
79       Thread JavaDoc t1 = new Thread JavaDoc(r);
80       t1.setUncaughtExceptionHandler(eh);
81       Thread JavaDoc t2 = new Thread JavaDoc(r);
82       t2.setUncaughtExceptionHandler(eh);
83       
84       t1.start();
85       t2.start();
86       
87       t1.join();
88       t2.join();
89       
90       Throwable JavaDoc t = eh.getUncaughtException();
91       assertNotNull(t);
92       assertTrue("Expected a javax.ejb.ConcurrentAccessException", t instanceof ConcurrentAccessException JavaDoc);
93    }
94    
95    /**
96     * EJBTHREE-697: concurrency on proxies doesn't work
97     */

98    public void testConcurrentProxyAccess() throws Exception JavaDoc
99    {
100       final MyStateful session = (MyStateful) getInitialContext().lookup("MyStatefulBean/remote");
101       
102       Runnable JavaDoc r = new Runnable JavaDoc()
103       {
104          public void run()
105          {
106             session.waitAndSee();
107          }
108       };
109
110       // don't call proxy yet
111
//session.doNothing();
112

113       // just use one 1 handler, the exception will be in there.
114
MyUncaughtExceptionHandler eh = new MyUncaughtExceptionHandler();
115       
116       Thread JavaDoc t1 = new Thread JavaDoc(r);
117       t1.setUncaughtExceptionHandler(eh);
118       Thread JavaDoc t2 = new Thread JavaDoc(r);
119       t2.setUncaughtExceptionHandler(eh);
120       
121       t1.start();
122       t2.start();
123       
124       t1.join();
125       t2.join();
126   
127       Throwable JavaDoc t = eh.getUncaughtException();
128       assertNotNull("No exception occured during a concurrent call", t);
129       fail("never comes here");
130    }
131    
132    public static Test suite() throws Exception JavaDoc
133    {
134       return getDeploySetup(ConcurrentUnitTestCase.class, "concurrent.jar");
135    }
136
137 }
138
Popular Tags