KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > util > ejb > EJBTestRunnerBean


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.util.ejb;
23
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.util.Properties JavaDoc;
26 import javax.ejb.EJBException JavaDoc;
27 import javax.ejb.SessionBean JavaDoc;
28 import javax.ejb.SessionContext JavaDoc;
29 import javax.naming.Binding JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32 import javax.naming.NamingEnumeration JavaDoc;
33 import javax.transaction.Status JavaDoc;
34 import javax.transaction.SystemException JavaDoc;
35
36 /**
37  * Implementation of the ejb test runner.
38  *
39  * @see EJBTestRunner
40  *
41  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
42  * @author Scott.Stark@jboss.org
43  * @version $Revision: 37406 $
44  */

45 public class EJBTestRunnerBean implements SessionBean JavaDoc
46 {
47    transient private SessionContext JavaDoc ctx;
48    private String JavaDoc runnerJndiName;
49
50    /** Run the specified test method on the given class name using a Properties
51     * map built from all java:comp/env entries.
52     *
53     * @param className the name of the test class
54     * @param methodName the name of the test method
55     * @throws RemoteTestException If any throwable is thrown during
56     * execution of the method, it is wrapped with a RemoteTestException and
57     * rethrown.
58     */

59    public void run(String JavaDoc className, String JavaDoc methodName)
60       throws RemoteTestException
61    {
62       Properties JavaDoc props = new Properties JavaDoc();
63       try
64       {
65          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
66          NamingEnumeration JavaDoc bindings = ctx.listBindings("java:comp/env");
67          while( bindings.hasMore() )
68          {
69             Binding JavaDoc binding = (Binding JavaDoc) bindings.next();
70             String JavaDoc name = binding.getName();
71             String JavaDoc value = binding.getObject().toString();
72             props.setProperty(name, value);
73          }
74       }
75       catch(NamingException JavaDoc e)
76       {
77          throw new RemoteTestException(e);
78       }
79       run(className, methodName, props);
80    }
81
82    /** Run the specified test method on the given class name
83     *
84     * @param className the name of the test class
85     * @param methodName the name of the test method
86     * @param props
87     * @throws RemoteTestException If any throwable is thrown during
88     * execution of the method, it is wrapped with a RemoteTestException and
89     * rethrown.
90     */

91    public void run(String JavaDoc className, String JavaDoc methodName, Properties JavaDoc props)
92       throws RemoteTestException
93    {
94       EJBTestCase testCase = getTestInstance(className, methodName);
95
96       setUpEJB(testCase, props);
97
98       RemoteTestException exception = null;
99       try
100       {
101          runTestCase(testCase);
102       }
103       catch (RemoteTestException e)
104       {
105          exception = e;
106       }
107       finally
108       {
109          try
110          {
111             tearDownEJB(testCase, props);
112          }
113          catch (RemoteTestException e)
114          {
115             // favor the run exception if one was thrown
116
if (exception != null)
117             {
118                exception = e;
119             }
120          }
121          if (exception != null)
122          {
123             throw exception;
124          }
125       }
126    }
127
128    /**
129     * Runs the setUpEJB method on the specified test case
130     * @param testCase the actual test case that will be run
131     * @throws RemoteTestException If any throwable is thrown during execution
132     * of the method, it is wrapped with a RemoteTestException and rethrown.
133     */

134    private void setUpEJB(EJBTestCase testCase, Properties JavaDoc props)
135       throws RemoteTestException
136    {
137       try
138       {
139          ctx.getUserTransaction().begin();
140          try
141          {
142             testCase.setUpEJB(props);
143          }
144          catch (Throwable JavaDoc e)
145          {
146             throw new RemoteTestException(e);
147          }
148          if (ctx.getUserTransaction().getStatus() == Status.STATUS_ACTIVE)
149          {
150             ctx.getUserTransaction().commit();
151          }
152       }
153       catch (Throwable JavaDoc e)
154       {
155          try
156          {
157             ctx.getUserTransaction().rollback();
158          }
159          catch (SystemException JavaDoc unused)
160          {
161             // eat the exception we are exceptioning out anyway
162
}
163          if (e instanceof RemoteTestException)
164          {
165             throw (RemoteTestException) e;
166          }
167          throw new RemoteTestException(e);
168       }
169    }
170
171    /**
172     * Runs the test method on the specified test case
173     * @param testCase the actual test case that will be run
174     * @throws RemoteTestException If any throwable is thrown during execution
175     * of the method, it is wrapped with a RemoteTestException and rethrown.
176     */

177    private void runTestCase(EJBTestCase testCase) throws RemoteTestException
178    {
179       try
180       {
181          ctx.getUserTransaction().begin();
182          try
183          {
184             testCase.runBare();
185          }
186          catch (Throwable JavaDoc e)
187          {
188             throw new RemoteTestException(e);
189          }
190          if (ctx.getUserTransaction().getStatus() == Status.STATUS_ACTIVE)
191          {
192             ctx.getUserTransaction().commit();
193          }
194       }
195       catch (Throwable JavaDoc e)
196       {
197          try
198          {
199             ctx.getUserTransaction().rollback();
200          }
201          catch (SystemException JavaDoc unused)
202          {
203             // eat the exception we are exceptioning out anyway
204
}
205          if (e instanceof RemoteTestException)
206          {
207             throw (RemoteTestException) e;
208          }
209          throw new RemoteTestException(e);
210       }
211    }
212
213    /**
214     * Runs the tearDownEJB method on the specified test case
215     * @param testCase the actual test case that will be run
216     * @throws RemoteTestException If any throwable is thrown during execution
217     * of the method, it is wrapped with a RemoteTestException and rethrown.
218     */

219    private void tearDownEJB(EJBTestCase testCase, Properties JavaDoc props)
220       throws RemoteTestException
221    {
222
223       try
224       {
225          ctx.getUserTransaction().begin();
226          try
227          {
228             testCase.tearDownEJB(props);
229          }
230          catch (Throwable JavaDoc e)
231          {
232             throw new RemoteTestException(e);
233          }
234          if (ctx.getUserTransaction().getStatus() == Status.STATUS_ACTIVE)
235          {
236             ctx.getUserTransaction().commit();
237          }
238       }
239       catch (Throwable JavaDoc e)
240       {
241          try
242          {
243             ctx.getUserTransaction().rollback();
244          }
245          catch (SystemException JavaDoc unused)
246          {
247             // eat the exception we are exceptioning out anyway
248
}
249          if (e instanceof RemoteTestException)
250          {
251             throw (RemoteTestException) e;
252          }
253          throw new RemoteTestException(e);
254       }
255    }
256
257    /**
258     * Gets a instance of the test class with the specified class name and
259     * initialized to execute the specified method.
260     *
261     * @param className the name of the test class
262     * @param methodName the name of the test method
263     * @return a new instance of the test class with the specified class name and
264     * initialized to execute the specified method.
265     */

266    private EJBTestCase getTestInstance(String JavaDoc className, String JavaDoc methodName)
267    {
268       Class JavaDoc testClass = null;
269       try
270       {
271          ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
272          testClass = loader.loadClass(className);
273       }
274       catch (ClassNotFoundException JavaDoc e)
275       {
276          throw new EJBException JavaDoc("Test class not found : " + className);
277       }
278
279       Constructor JavaDoc constructor = null;
280       try
281       {
282          constructor = testClass.getConstructor(new Class JavaDoc[]{String JavaDoc.class});
283       }
284       catch (Exception JavaDoc e)
285       {
286          throw new EJBException JavaDoc("Test class does not have a constructor " +
287             "which has a single String argument.", e);
288       }
289
290       try
291       {
292          EJBTestCase testCase =
293             (EJBTestCase) constructor.newInstance(new Object JavaDoc[]{methodName});
294          testCase.setServerSide(true);
295          return testCase;
296       }
297       catch (Exception JavaDoc e)
298       {
299          throw new EJBException JavaDoc("Cannot instantiate test class: " +
300             testClass.getName(), e);
301       }
302    }
303
304    public void ejbCreate()
305    {
306    }
307
308    public void ejbRemove()
309    {
310    }
311
312    public void ejbActivate()
313    {
314    }
315
316    public void ejbPassivate()
317    {
318    }
319
320    public void setSessionContext(SessionContext JavaDoc ctx)
321    {
322       this.ctx = ctx;
323    }
324 }
325
Popular Tags