KickJava   Java API By Example, From Geeks To Geeks.

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


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
25 import java.util.Properties JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.rmi.PortableRemoteObject JavaDoc;
28
29 import junit.framework.AssertionFailedError;
30 import junit.framework.TestCase;
31 import junit.framework.TestResult;
32
33 /**
34  * An ejb test case is an extension to test case where the test is executed
35  * in the ejb server's virtual machine.
36  *
37  * Two new methods setUpEJB and tearDownEJB have been added. These methods
38  * work just like setUp and tearDown except they run in a sepperate transaction.
39  * The execution order is as follows:
40  * <pre>
41  * 1. setUpEJB (TX 1)
42  * 2. run (TX 2)
43  * 2.1. runBare
44  * 2.1.1 setUp
45  * 2.1.2 <your test method>
46  * 2.1.3 tearDown
47  * 3. ejbTearDown (TX 2)
48  * </pre>
49  *
50  * For an ejb test case to run successfully, the following must be setup:
51  * <pre>
52  * 1. The ejb test case class must be availabe to the client vm.
53  * 2. The ejb test case class must be availabe to the EJBTestRunner bean
54  * on the server.
55  * 3. The EJBTestRunnerHome must be bound to "ejb/EJBTestRunner" in the
56  * jndi context obtained from new InitialContext();
57  * 4. The EJBTestRunner bean must be configured as specified in the
58  * EJBTestRunner javadoc.
59  * </pre>
60  *
61  * @see EJBTestRunner
62  * @see junit.framework.TestCase
63  *
64  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
65  * @author Scott.Stark@jboss.org
66  * @version $Revision: 37406 $
67  */

68 public class EJBTestCase extends TestCase
69 {
70    private boolean serverSide = false;
71    protected Properties JavaDoc props;
72
73    /**
74     * Constructs a test case that will run the method with the specified name.
75     * @param methodName the name of the method that will executed when this
76     * test is run
77     */

78    public EJBTestCase(String JavaDoc methodName)
79    {
80       super(methodName);
81    }
82
83    /**
84     * Sets the flag that is used to determine if the class
85     * is running on the server side.
86     * @param serverSide boolean flag that this class uses to determine
87     * if it's running on the server side.
88     */

89    public void setServerSide(boolean serverSide)
90    {
91       this.serverSide = serverSide;
92    }
93
94    /**
95     * Is this class running on the server side?
96     * @return true if this class is running on the server side
97     */

98    public boolean isServerSide()
99    {
100       return serverSide;
101    }
102
103    /** Allow EJBTestCase subclasses to override the EJBRunnerHome JNDI name
104     * @return The JNDI name of the EJBRunnerHome home interface binding. The
105     * default is "ejb/EJBTestRunner"
106     */

107    public String JavaDoc getEJBRunnerJndiName()
108    {
109       return "ejb/EJBTestRunner";
110    }
111
112    /**
113     * @return the properties associated with the test case
114     */

115    public Properties JavaDoc getProps()
116    {
117       return props;
118    }
119    /**
120     * @param props the properties associated with the test case
121     */

122    public void setProps(Properties JavaDoc props)
123    {
124       this.props = props;
125    }
126
127    public void run(TestResult result)
128    {
129       ClassLoader JavaDoc oldClassLoader = null;
130       try
131       {
132          // If we are on the server side, set the thread context class loader
133
// to the class loader that loaded this class. This fixes problems
134
// with the current implementation of the JUnit gui test runners class
135
// reloading logic. The gui relods the test classes with each run but
136
// does not set the context class loader so calls to Class.forName load
137
// the class in the wrong class loader.
138
if (!isServerSide())
139          {
140             oldClassLoader = Thread.currentThread().getContextClassLoader();
141             Thread.currentThread().setContextClassLoader(
142                getClass().getClassLoader());
143          }
144
145          super.run(result);
146       }
147       finally
148       {
149          // be a good citizen, reset the context loader
150
if (oldClassLoader != null)
151          {
152             Thread.currentThread().setContextClassLoader(oldClassLoader);
153          }
154       }
155    }
156
157    public void runBare() throws Throwable JavaDoc
158    {
159       if (!isServerSide())
160       {
161          // We're not on the server side yet, invoke the test on the serverside.
162
EJBTestRunner testRunner = null;
163          try
164          {
165             testRunner = getEJBTestRunner();
166             if( props != null )
167                testRunner.run(getClass().getName(), getName(), props);
168             else
169                testRunner.run(getClass().getName(), getName());
170          }
171          catch (RemoteTestException e)
172          {
173             // if the remote test exception is from an assertion error
174
// rethrow it with a sub class of AssertionFailedError so it is
175
// picked up as a failure and not an error.
176
// The server has to throw sub classes of Error because that is the
177
// allowable scope of application exceptions. So
178
// AssertionFailedError which is an instance of error has to be
179
// wrapped in an exception.
180
Throwable JavaDoc remote = e.getRemoteThrowable();
181             if (remote instanceof AssertionFailedError)
182             {
183                throw new RemoteAssertionFailedError(
184                   (AssertionFailedError) remote, e.getRemoteStackTrace());
185             }
186             throw e;
187          }
188          finally
189          {
190             // be a good citizen, drop my ref to the session bean.
191
if (testRunner != null)
192             {
193                testRunner.remove();
194             }
195          }
196       }
197       else
198       {
199          // We're on the server side so, invoke the test the usual way.
200
super.runBare();
201       }
202    }
203
204    /** Sets up the ejb test case. This method is called before
205     * each test is executed and is run in a private transaction.
206     * @param props the properties passed in from the client
207     * @throws Exception if a problem occures
208     */

209    public void setUpEJB(Properties JavaDoc props) throws Exception JavaDoc
210    {
211       this.props = props;
212    }
213
214    /** Tears down the ejb test case. This method is called after
215     * each test is executed and is run in a private transaction.
216     * @param props the properties passed in from the client
217     * @throws Exception if a problem occures
218     */

219    public void tearDownEJB(Properties JavaDoc props) throws Exception JavaDoc
220    {
221    }
222
223    /**
224     * Looks up the ejb test runner home in JNDI (at "ejb/EJBTestRunner")
225     * and creates a new runner.
226     * @throws Exception if any problem happens
227     */

228    private EJBTestRunner getEJBTestRunner() throws Exception JavaDoc
229    {
230       InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
231
232       // Get a reference from this to the Bean's Home interface
233
String JavaDoc name = getEJBRunnerJndiName();
234       Object JavaDoc ref = jndiContext.lookup(name);
235       EJBTestRunnerHome runnerHome = (EJBTestRunnerHome)
236          PortableRemoteObject.narrow(ref, EJBTestRunnerHome.class);
237
238       // create the test runner
239
return runnerHome.create();
240    }
241 }
242
Popular Tags