KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > classloader > test > ScopingUnitTestCase


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.classloader.test;
23
24 import java.net.URL JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29
30 import org.jboss.test.JBossTestCase;
31 import org.jboss.test.classloader.scoping.override.ejb.log4j113.StatelessSession;
32 import org.jboss.test.classloader.scoping.override.ejb.log4j113.StatelessSessionHome;
33 import org.jboss.system.ServiceMBean;
34 import org.jboss.mx.loading.HeirarchicalLoaderRepository3;
35 import org.jboss.mx.loading.RepositoryClassLoader;
36 import org.jboss.mx.loading.UnifiedLoaderRepository3;
37 import org.jboss.mx.loading.ClassLoaderUtils;
38
39 /** Unit tests for class and resource scoping
40  *
41  * @author Scott.Stark@jboss.org
42  * @version $Revision: 44451 $
43  */

44 public class ScopingUnitTestCase extends JBossTestCase
45 {
46    public ScopingUnitTestCase(String JavaDoc name)
47    {
48       super(name);
49    }
50
51    /** Test the scoping of singleton classes in two independent service
52     * deployments
53     */

54    public void testSingletons() throws Exception JavaDoc
55    {
56       getLog().debug("+++ testSingletons");
57       try
58       {
59          deploy("singleton1.sar");
60          getLog().info("Deployed singleton1.sar");
61          ObjectName JavaDoc testObjectName = new ObjectName JavaDoc("jboss.test:service=TestService,version=V1");
62          boolean isRegistered = getServer().isRegistered(testObjectName);
63          assertTrue("jboss.test:loader=singleton.sar,version=V1 isRegistered", isRegistered);
64          Integer JavaDoc state = (Integer JavaDoc) getServer().getAttribute(testObjectName, "State");
65          assertTrue("state.intValue() == ServiceMBean.STARTED",
66                state.intValue() == ServiceMBean.STARTED);
67          Object JavaDoc[] args = {"V1"};
68          String JavaDoc[] sig = {"java.lang.String"};
69          Boolean JavaDoc matches = (Boolean JavaDoc) getServer().invoke(testObjectName, "checkVersion", args, sig);
70          assertTrue("checkVersion(V1) is true", matches.booleanValue());
71       }
72       catch(Exception JavaDoc e)
73       {
74          getLog().info("Failed to validate singleton1.sar", e);
75          throw e;
76       }
77
78       try
79       {
80          deploy("singleton2.sar");
81          getLog().info("Deployed singleton2.sar");
82          ObjectName JavaDoc testObjectName = new ObjectName JavaDoc("jboss.test:service=TestService,version=V2");
83          boolean isRegistered = getServer().isRegistered(testObjectName);
84          assertTrue("jboss.test:loader=singleton.sar,version=V2 isRegistered", isRegistered);
85          Integer JavaDoc state = (Integer JavaDoc) getServer().getAttribute(testObjectName, "State");
86          assertTrue("state.intValue() == ServiceMBean.STARTED",
87                state.intValue() == ServiceMBean.STARTED);
88          Object JavaDoc[] args = {"V2"};
89          String JavaDoc[] sig = {"java.lang.String"};
90          Boolean JavaDoc matches = (Boolean JavaDoc) getServer().invoke(testObjectName, "checkVersion", args, sig);
91          assertTrue("checkVersion(V2) is true", matches.booleanValue());
92       }
93       catch(Exception JavaDoc e)
94       {
95          getLog().info("Failed to validate singleton2.sar", e);
96          throw e;
97       }
98       finally
99       {
100          undeploy("singleton1.sar");
101          getLog().info("Undeployed singleton1.sar");
102          undeploy("singleton2.sar");
103          getLog().info("Undeployed singleton2.sar");
104       }
105    }
106
107    /** Test the ability to override the server classes with war local versions
108     * of log4j classes
109     */

110    public void testWarLog4jOverrides() throws Exception JavaDoc
111    {
112       getLog().debug("+++ testWarOverrides");
113       try
114       {
115          deploy("log4j113.war");
116          URL JavaDoc log4jServletURL = new URL JavaDoc("http://" + getServerHost() + ":8080/log4j113/Log4jServlet/");
117          InputStream JavaDoc reply = (InputStream JavaDoc) log4jServletURL.getContent();
118          getLog().debug("Accessed http://" + getServerHost() + ":8080/log4j113/Log4jServlet/");
119          logReply(reply);
120
121          URL JavaDoc encServletURL = new URL JavaDoc("http://" + getServerHost() + ":8080/log4j113/ENCServlet/");
122          reply = (InputStream JavaDoc) encServletURL.getContent();
123          getLog().debug("Accessed http://" + getServerHost() + ":8080/log4j113/ENCServlet/");
124          logReply(reply);
125       }
126       catch(Exception JavaDoc e)
127       {
128          getLog().info("Failed to access Log4jServlet in log4j113.war", e);
129          throw e;
130       }
131       finally
132       {
133          undeploy("log4j113.war");
134       }
135    }
136
137    /** Test the ability to override the server classes with war local versions
138     * of log4j classes when using commons-logging.
139     */

140    public void testWarCommonsLoggingLog4jOverrides() throws Exception JavaDoc
141    {
142       getLog().debug("+++ testWarCommonsLoggingLog4jOverrides");
143       try
144       {
145          deploy("common-logging.war");
146          URL JavaDoc log4jServletURL = new URL JavaDoc("http://" + getServerHost() + ":8080/common-logging/Log4jServlet/");
147          InputStream JavaDoc reply = (InputStream JavaDoc) log4jServletURL.getContent();
148          getLog().debug("Accessed http://" + getServerHost() + ":8080/common-logging/Log4jServlet/");
149          logReply(reply);
150       }
151       catch(Exception JavaDoc e)
152       {
153          getLog().info("Failed to access Log4jServlet in common-logging.war", e);
154          throw e;
155       }
156       finally
157       {
158          undeploy("common-logging.war");
159       }
160    }
161
162    /** Test the ability to override the server classes with war local versions
163     * of xml parser classes.
164     * This test is invalid as of jdk1.4+ due to the bundling of the xerces
165     * parser with the jdk
166     */

167    public void badtestWarXmlOverrides() throws Exception JavaDoc
168    {
169       getLog().debug("+++ testWarOverrides");
170       try
171       {
172          deploy("oldxerces.war");
173          URL JavaDoc servletURL = new URL JavaDoc("http://" + getServerHost() + ":8080/oldxerces/");
174          InputStream JavaDoc reply = (InputStream JavaDoc) servletURL.getContent();
175          getLog().debug("Accessed http://" + getServerHost() + ":8080/oldxerces/");
176          logReply(reply);
177       }
178       catch(Exception JavaDoc e)
179       {
180          getLog().info("Failed to access oldxerces.war", e);
181          throw e;
182       }
183       finally
184       {
185          undeploy("oldxerces.war");
186       }
187    }
188
189    /** Test the ability to override the server classes with ejb local versions
190     */

191    public void testEjbOverrides() throws Exception JavaDoc
192    {
193       getLog().debug("+++ testEjbOverrides");
194       try
195       {
196          deploy("log4j113-ejb.jar");
197          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
198          StatelessSessionHome home = (StatelessSessionHome) ctx.lookup("Log4j113StatelessBean");
199          StatelessSession bean = home.create();
200          Throwable JavaDoc error = bean.checkVersion();
201          getLog().debug("StatelessSession.checkVersion returned:", error);
202          assertTrue("checkVersion returned null", error == null);
203       }
204       catch(Exception JavaDoc e)
205       {
206          getLog().info("Failed to access Log4j113StatelessBean in log4j113-ejb.jar", e);
207          throw e;
208       }
209       finally
210       {
211          undeploy("log4j113-ejb.jar");
212       }
213    }
214
215    /** Tests for accessing java system classes from scoped
216     * repositories.
217     *
218     * @throws Exception
219     */

220    public void testSystemClasses() throws Exception JavaDoc
221    {
222       log.info("+++ Begin testSystemClasses");
223       UnifiedLoaderRepository3 parent = new UnifiedLoaderRepository3();
224       HeirarchicalLoaderRepository3 repository0 = new HeirarchicalLoaderRepository3(parent);
225       URL JavaDoc j0URL = getDeployURL("j0.jar");
226       RepositoryClassLoader ucl0 = repository0.newClassLoader(j0URL, true);
227
228       Class JavaDoc c0 = ucl0.loadClass("java.sql.SQLException");
229       StringBuffer JavaDoc info = new StringBuffer JavaDoc();
230       ClassLoaderUtils.displayClassInfo(c0, info);
231       log.info("Loaded c0: "+info);
232
233       HeirarchicalLoaderRepository3 repository1 = new HeirarchicalLoaderRepository3(parent);
234       repository1.setUseParentFirst(false);
235       RepositoryClassLoader ucl1 = repository1.newClassLoader(j0URL, true);
236       Class JavaDoc c1 = ucl1.loadClass("java.sql.SQLException");
237       info.setLength(0);
238       ClassLoaderUtils.displayClassInfo(c1, info);
239       log.info("Loaded c1: "+info);
240
241       Class JavaDoc c2 = ucl1.loadClass("java.sql.SQLWarning");
242       info.setLength(0);
243       ClassLoaderUtils.displayClassInfo(c2, info);
244       log.info("Loaded c2: "+info);
245    }
246
247    /** Tests for accessing java system classes from scoped
248     * repositories that have system class packages.
249     *
250     * @throws Exception
251     */

252    public void testSystemClasses2() throws Exception JavaDoc
253    {
254       log.info("+++ Begin testSystemClasses2");
255       UnifiedLoaderRepository3 parent = new UnifiedLoaderRepository3();
256       HeirarchicalLoaderRepository3 repository0 = new HeirarchicalLoaderRepository3(parent);
257       URL JavaDoc j0URL = getDeployURL("java-sql.jar");
258       RepositoryClassLoader ucl0 = repository0.newClassLoader(j0URL, true);
259
260       Class JavaDoc c0 = ucl0.loadClass("java.sql.SQLException");
261       StringBuffer JavaDoc info = new StringBuffer JavaDoc();
262       ClassLoaderUtils.displayClassInfo(c0, info);
263       log.info("Loaded c0: "+info);
264
265       HeirarchicalLoaderRepository3 repository1 = new HeirarchicalLoaderRepository3(parent);
266       repository1.setUseParentFirst(false);
267       RepositoryClassLoader ucl1 = repository1.newClassLoader(j0URL, true);
268       Class JavaDoc c1 = ucl1.loadClass("java.sql.SQLException");
269       info.setLength(0);
270       ClassLoaderUtils.displayClassInfo(c1, info);
271       log.info("Loaded c1: "+info);
272
273       Class JavaDoc c2 = ucl1.loadClass("java.sql.SQLWarning");
274       info.setLength(0);
275       ClassLoaderUtils.displayClassInfo(c2, info);
276       log.info("Loaded c2: "+info);
277    }
278
279    /** Test the interaction through jndi of a service which binds a custom
280     * object into jndi and a servlet which looks up the custom object when
281     * the service and servlet have different class loader scopes that both
282     * have the custom object.
283     */

284    public void testSharedJNDI() throws Exception JavaDoc
285    {
286       getLog().debug("+++ testSharedJNDI");
287       try
288       {
289          deploy("shared-jndi.sar");
290          deploy("shared-jndi.war");
291          URL JavaDoc servletURL = new URL JavaDoc("http://" + getServerHost() + ":8080/shared-jndi/LookupServlet");
292          InputStream JavaDoc reply = (InputStream JavaDoc) servletURL.getContent();
293          getLog().debug("Accessed: "+servletURL);
294          logReply(reply);
295       }
296       catch(Exception JavaDoc e)
297       {
298          getLog().info("Failed to access LookupServlet", e);
299          throw e;
300       }
301       finally
302       {
303          undeploy("shared-jndi.war");
304          undeploy("shared-jndi.sar");
305       }
306    }
307
308    private void logReply(InputStream JavaDoc reply) throws IOException JavaDoc
309    {
310       getLog().debug("Begin reply");
311       byte[] tmp = new byte[256];
312       while( reply.read(tmp) > 0 )
313          getLog().debug(new String JavaDoc(tmp));
314       reply.close();
315       getLog().debug("End reply");
316    }
317 }
318
Popular Tags