KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > JBossTestCase


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;
23
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 import javax.management.MBeanServerConnection JavaDoc;
29 import javax.management.MalformedObjectNameException JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32
33 import org.jboss.logging.Logger;
34
35 import junit.framework.Test;
36 import junit.framework.TestSuite;
37
38 /**
39  * This is the standard base test case for jboss junit test cases. It supplies
40  * access to jboss logging, the jboss jmx server, jndi, and a method for
41  * deploying ejb packages. You may supply the name of the machine the jboss
42  * server is on with the system property jbosstest.server.name (default
43  * getInetAddress().getLocalHost().getHostName()) and the directory for
44  * deployable packages with the system property jbosstest.deploy.dir (default
45  * ../lib).
46  *
47  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
48  * @author Scott.Stark@jboss.org
49  * @version $Revision: 58499 $
50  */

51 public class JBossTestCase
52    extends AbstractTestCaseWithSetup
53 {
54
55    /**
56     * Saved exception from deployment.
57     */

58    protected static Exception JavaDoc deploymentException = null;
59
60    protected JBossTestServices delegate;
61    /** Local variable for backward compatibility */
62    protected Logger log;
63
64    // Static --------------------------------------------------------
65
/**
66     * Overriden to return JBossTestServices as the test delegate.
67     */

68    public static AbstractTestDelegate getDelegate(Class JavaDoc clazz) throws Exception JavaDoc
69    {
70       AbstractTestDelegate delegate = new JBossTestServices(clazz);
71       return delegate;
72    }
73
74    // Constructors --------------------------------------------------
75
/**
76     * Constructor for the JBossTestCase object
77     *
78     * @param name Test case name
79     */

80    public JBossTestCase(String JavaDoc name)
81    {
82       super(name);
83    }
84
85    /**
86     * Create a delegate by calling AbstractTestDelegate.getDelegate(clazz)
87     * to allow for a test specific delegate.
88     * This method then delegates to the AbstractTestDelegate.setUp method.
89     * @throws Exception
90     */

91    protected void setUp() throws Exception JavaDoc
92    {
93       super.setUp();
94       log = getLog();
95       delegate = (JBossTestServices) AbstractTestSetup.delegate;
96    }
97
98    /**
99     * This method then delegates to the AbstractTestDelegate.tearDown method.
100     * @throws Exception
101     */

102    protected void tearDown() throws Exception JavaDoc
103    {
104       if (delegate != null)
105          delegate.tearDown();
106    }
107
108    /**
109     * Overriden to restore the old behavior where the log was initialized post ctor rather
110     * than post setUp.
111     */

112    @Override JavaDoc
113    public Logger getLog()
114    {
115       Logger theLog;
116       if( delegate == null )
117       {
118          theLog = Logger.getLogger(getClass());
119       }
120       else
121       {
122          theLog = super.getLog();
123       }
124       return theLog;
125    }
126
127    public void resetDelegate()
128    {
129       try
130       {
131          delegate.reinit();
132       }
133       catch(Exception JavaDoc e)
134       {
135          log.error("Failed to init delegate", e);
136       }
137    }
138
139    // Public --------------------------------------------------------
140

141
142    /**
143     * This just checks the server is there... so you should get at least one
144     * success!
145     * Also checks if an exception occurred during deployment, and throws
146     * any such exception from here.
147     *
148     * @exception Exception Description of Exception
149     */

150    public void serverFound() throws Exception JavaDoc
151    {
152       if (deploymentException != null)
153          throw deploymentException;
154       assertTrue("Server was not found", getServer() != null);
155    }
156
157    //protected---------
158

159    /**
160     * Gets the InitialContext attribute of the JBossTestCase object
161     *
162     * @return The InitialContext value
163     * @throws Exception for any error
164     */

165    protected InitialContext JavaDoc getInitialContext() throws Exception JavaDoc
166    {
167       return delegate.getInitialContext();
168    }
169
170    /**
171     * Gets the Server attribute of the JBossTestCase object
172     *
173     * @return The Server value
174     * @throws Exception for any error
175     */

176    protected MBeanServerConnection JavaDoc getServer() throws Exception JavaDoc
177    {
178       return delegate.getServer();
179    }
180
181    /**
182     * Gets the DeployerName attribute of the JBossTestCase object
183     *
184     * @return The DeployerName value
185     * @exception MalformedObjectNameException Description of Exception
186     */

187    protected ObjectName JavaDoc getDeployerName() throws MalformedObjectNameException JavaDoc
188    {
189       return delegate.getDeployerName();
190    }
191
192
193    /**
194     * Returns the deployment directory to use. This does it's best to figure out
195     * where you are looking. If you supply a complete url, it returns it.
196     * Otherwise, it looks for jbosstest.deploy.dir or if missing ../lib. Then it
197     * tries to construct a file url or a url.
198     *
199     * @param filename name of the file/url you want
200     * @return A more or less canonical string for the url.
201     * @exception MalformedURLException Description of Exception
202     */

203    protected URL JavaDoc getDeployURL(final String JavaDoc filename) throws MalformedURLException JavaDoc
204    {
205       return delegate.getDeployURL(filename);
206    }
207
208    /**
209     * Get a URL string to a resource in the testsuite/output/resources dir.
210     * This relies on the output/resources directory being in the
211     * testcase classpath.
212     *
213     * @param resource the resource name
214     * @return the string version of the resource url
215     * @throws MalformedURLException for an invalid url
216     */

217    protected String JavaDoc getResourceURL(final String JavaDoc resource) throws MalformedURLException JavaDoc
218    {
219       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
220       URL JavaDoc resURL = loader.getResource(resource);
221       return resURL != null ? resURL.toString() : null;
222    }
223
224
225    /**
226     * invoke wraps an invoke call to the mbean server in a lot of exception
227     * unwrapping.
228     *
229     * @param name ObjectName of the mbean to be called
230     * @param method mbean method to be called
231     * @param args Object[] of arguments for the mbean method.
232     * @param sig String[] of types for the mbean methods parameters.
233     * @return Object returned by mbean method invocation.
234     * @exception Exception Description of Exception
235     */

236    protected Object JavaDoc invoke(ObjectName JavaDoc name, String JavaDoc method, Object JavaDoc[] args, String JavaDoc[] sig) throws Exception JavaDoc
237    {
238       return delegate.invoke(name, method, args, sig);
239    }
240
241    /**
242     * Deploy a package with the main deployer. The supplied name is
243     * interpreted as a url, or as a filename in jbosstest.deploy.lib or ../lib.
244     *
245     * @param name filename/url of package to deploy.
246     * @exception Exception Description of Exception
247     */

248    protected void deploy(String JavaDoc name) throws Exception JavaDoc
249    {
250       delegate.deploy(name);
251    }
252
253    /**
254     * Redeploy a package with the main deployer. The supplied name is
255     * interpreted as a url, or as a filename in jbosstest.deploy.lib or ../lib.
256     *
257     * @param name filename/url of package to deploy.
258     * @exception Exception Description of Exception
259     */

260    protected void redeploy(String JavaDoc name) throws Exception JavaDoc
261    {
262       delegate.redeploy(name);
263    }
264
265    /**
266     * Undeploy a package with the main deployer. The supplied name is
267     * interpreted as a url, or as a filename in jbosstest.deploy.lib or ../lib.
268     *
269     * @param name filename/url of package to undeploy.
270     * @exception Exception Description of Exception
271     */

272    protected void undeploy(String JavaDoc name) throws Exception JavaDoc
273    {
274       delegate.undeploy(name);
275    }
276
277    /**
278     * Get a JBossTestSetup that does login and deployment in setUp/tearDown
279     *
280     * @param test a Test
281     * @param jarNames is a comma seperated list of deployments
282     * @return the wrapping test that does the setup
283     * @throws Exception for any error
284     */

285    public static Test getDeploySetup(final Class JavaDoc clazz, final Test test, final String JavaDoc jarNames) throws Exception JavaDoc
286    {
287       JBossTestSetup wrapper = new JBossTestSetup(clazz, test)
288       {
289          protected void setUp() throws Exception JavaDoc
290          {
291             super.setUp();
292             deploymentException = null;
293             try
294             {
295                this.delegate.init();
296
297                if (this.delegate.isSecure())
298                   this.delegate.login();
299
300                if (jarNames == null) return;
301
302                // deploy the comma seperated list of jars
303
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(jarNames, ", ");
304                while (st != null && st.hasMoreTokens())
305                {
306                   String JavaDoc jarName = st.nextToken();
307                   this.redeploy(jarName);
308                   this.getLog().debug("deployed package: " + jarName);
309                }
310             }
311             catch (Exception JavaDoc ex)
312             {
313                // Throw this in testServerFound() instead.
314
deploymentException = ex;
315             }
316          }
317
318          protected void tearDown() throws Exception JavaDoc
319          {
320             if (jarNames == null) return; //Nothing to Undeploy
321

322             // undeploy the comma seperated list of jars
323
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(jarNames, ", ");
324             String JavaDoc[] depoyments = new String JavaDoc[st.countTokens()];
325             for (int i = depoyments.length - 1; i >= 0; i--)
326                depoyments[i] = st.nextToken();
327             for (int i = 0; i < depoyments.length; i++)
328             {
329                String JavaDoc jarName = depoyments[i];
330                this.undeploy(jarName);
331                this.getLog().debug("undeployed package: " + jarName);
332             }
333
334             if (this.delegate.isSecure())
335                this.delegate.logout();
336             super.tearDown();
337          }
338       };
339       return wrapper;
340    }
341
342    public static Test getDeploySetup(final Test test, final String JavaDoc jarName)
343       throws Exception JavaDoc
344    {
345       return getDeploySetup(JBossTestCase.class, test, jarName);
346    }
347    public static Test getDeploySetup(final Class JavaDoc clazz, final String JavaDoc jarName)
348       throws Exception JavaDoc
349    {
350       TestSuite suite = new TestSuite();
351       suite.addTest(new TestSuite(clazz));
352       return getDeploySetup(clazz, suite, jarName);
353    }
354
355    protected String JavaDoc getJndiURL()
356    {
357       return delegate.getJndiURL();
358    }
359
360    protected String JavaDoc getJndiInitFactory()
361    {
362       return delegate.getJndiInitFactory();
363    }
364
365    protected int getThreadCount()
366    {
367       return delegate.getThreadCount();
368    }
369
370    protected int getIterationCount()
371    {
372       return delegate.getIterationCount();
373    }
374
375    protected int getBeanCount()
376    {
377       return delegate.getBeanCount();
378    }
379
380    /**
381     * Get the JBoss server host from system property "jbosstest.server.host"
382     * This defaults to "localhost"
383     *
384     * @return the host name
385     */

386    public String JavaDoc getServerHost()
387    {
388       return delegate.getServerHost();
389    }
390
391    protected void flushAuthCache() throws Exception JavaDoc
392    {
393       flushAuthCache("other");
394    }
395
396    protected void flushAuthCache(String JavaDoc domain) throws Exception JavaDoc
397    {
398       delegate.flushAuthCache(domain);
399    }
400
401    /** Restart the connection pool associated with the DefaultDS
402     * @throws Exception on failure
403     */

404    protected void restartDBPool() throws Exception JavaDoc
405    {
406       delegate.restartDBPool();
407    }
408
409    protected void sleep(long interval) throws InterruptedException JavaDoc
410    {
411       synchronized (this)
412       {
413          wait(interval);
414       }
415    }
416 }
417
Popular Tags