001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.classloader.test;
023:
024: import java.net.URL;
025: import java.io.InputStream;
026: import java.io.IOException;
027: import javax.management.ObjectName;
028: import javax.naming.InitialContext;
029:
030: import org.jboss.test.JBossTestCase;
031: import org.jboss.test.classloader.scoping.override.ejb.log4j113.StatelessSession;
032: import org.jboss.test.classloader.scoping.override.ejb.log4j113.StatelessSessionHome;
033: import org.jboss.system.ServiceMBean;
034: import org.jboss.mx.loading.HeirarchicalLoaderRepository3;
035: import org.jboss.mx.loading.RepositoryClassLoader;
036: import org.jboss.mx.loading.UnifiedLoaderRepository3;
037: import org.jboss.mx.loading.ClassLoaderUtils;
038:
039: /** Unit tests for class and resource scoping
040: *
041: * @author Scott.Stark@jboss.org
042: * @version $Revision: 60485 $
043: */
044: public class ScopingUnitTestCase extends JBossTestCase {
045: public ScopingUnitTestCase(String name) {
046: super (name);
047: }
048:
049: /** Test the scoping of singleton classes in two independent service
050: * deployments
051: */
052: public void testSingletons() throws Exception {
053: getLog().debug("+++ testSingletons");
054: try {
055: deploy("singleton1.sar");
056: getLog().info("Deployed singleton1.sar");
057: ObjectName testObjectName = new ObjectName(
058: "jboss.test:service=TestService,version=V1");
059: boolean isRegistered = getServer().isRegistered(
060: testObjectName);
061: assertTrue(
062: "jboss.test:loader=singleton.sar,version=V1 isRegistered",
063: isRegistered);
064: Integer state = (Integer) getServer().getAttribute(
065: testObjectName, "State");
066: assertTrue("state.intValue() == ServiceMBean.STARTED",
067: state.intValue() == ServiceMBean.STARTED);
068: Object[] args = { "V1" };
069: String[] sig = { "java.lang.String" };
070: Boolean matches = (Boolean) getServer().invoke(
071: testObjectName, "checkVersion", args, sig);
072: assertTrue("checkVersion(V1) is true", matches
073: .booleanValue());
074: } catch (Exception e) {
075: getLog().info("Failed to validate singleton1.sar", e);
076: throw e;
077: }
078:
079: try {
080: deploy("singleton2.sar");
081: getLog().info("Deployed singleton2.sar");
082: ObjectName testObjectName = new ObjectName(
083: "jboss.test:service=TestService,version=V2");
084: boolean isRegistered = getServer().isRegistered(
085: testObjectName);
086: assertTrue(
087: "jboss.test:loader=singleton.sar,version=V2 isRegistered",
088: isRegistered);
089: Integer state = (Integer) getServer().getAttribute(
090: testObjectName, "State");
091: assertTrue("state.intValue() == ServiceMBean.STARTED",
092: state.intValue() == ServiceMBean.STARTED);
093: Object[] args = { "V2" };
094: String[] sig = { "java.lang.String" };
095: Boolean matches = (Boolean) getServer().invoke(
096: testObjectName, "checkVersion", args, sig);
097: assertTrue("checkVersion(V2) is true", matches
098: .booleanValue());
099: } catch (Exception e) {
100: getLog().info("Failed to validate singleton2.sar", e);
101: throw e;
102: } finally {
103: undeploy("singleton1.sar");
104: getLog().info("Undeployed singleton1.sar");
105: undeploy("singleton2.sar");
106: getLog().info("Undeployed singleton2.sar");
107: }
108: }
109:
110: /** Test the ability to override the server classes with war local versions
111: * of log4j classes
112: */
113: public void testWarLog4jOverrides() throws Exception {
114: getLog().debug("+++ testWarOverrides");
115: try {
116: deploy("log4j113.war");
117: URL log4jServletURL = new URL("http://" + getServerHost()
118: + ":8080/log4j113/Log4jServlet/");
119: InputStream reply = (InputStream) log4jServletURL
120: .getContent();
121: getLog().debug(
122: "Accessed http://" + getServerHost()
123: + ":8080/log4j113/Log4jServlet/");
124: logReply(reply);
125:
126: URL encServletURL = new URL("http://" + getServerHost()
127: + ":8080/log4j113/ENCServlet/");
128: reply = (InputStream) encServletURL.getContent();
129: getLog().debug(
130: "Accessed http://" + getServerHost()
131: + ":8080/log4j113/ENCServlet/");
132: logReply(reply);
133: } catch (Exception e) {
134: getLog().info(
135: "Failed to access Log4jServlet in log4j113.war", e);
136: throw e;
137: } finally {
138: undeploy("log4j113.war");
139: }
140: }
141:
142: /** Test the ability to override the server classes with war local versions
143: * of log4j classes when using commons-logging.
144: */
145: public void testWarCommonsLoggingLog4jOverrides() throws Exception {
146: getLog().debug("+++ testWarCommonsLoggingLog4jOverrides");
147: try {
148: deploy("common-logging.war");
149: URL log4jServletURL = new URL("http://" + getServerHost()
150: + ":8080/common-logging/Log4jServlet/");
151: InputStream reply = (InputStream) log4jServletURL
152: .getContent();
153: getLog().debug(
154: "Accessed http://" + getServerHost()
155: + ":8080/common-logging/Log4jServlet/");
156: logReply(reply);
157: } catch (Exception e) {
158: getLog()
159: .info(
160: "Failed to access Log4jServlet in common-logging.war",
161: e);
162: throw e;
163: } finally {
164: undeploy("common-logging.war");
165: }
166: }
167:
168: /** Test the ability to override the server classes with war local versions
169: * of log4j classes when using commons-logging. This version includes the
170: * current commons-logging.jar in the war WEB-INF/lib.
171: */
172: public void testWarCommonsLoggingWithCLJarLog4jOverrides()
173: throws Exception {
174: getLog().debug(
175: "+++ testWarCommonsLoggingWithCLJarLog4jOverrides");
176: try {
177: deploy("cl11-withjar.war");
178: URL log4jServletURL = new URL(
179: "http://"
180: + getServerHost()
181: + ":8080/cl11-withjar/Log4jServlet/?log-name=cl11-withjar.log");
182: InputStream reply = (InputStream) log4jServletURL
183: .getContent();
184: getLog().debug(
185: "Accessed http://" + getServerHost()
186: + ":8080/cl11-withjar/Log4jServlet/");
187: logReply(reply);
188: } catch (Exception e) {
189: getLog()
190: .info(
191: "Failed to access Log4jServlet in cl11-withjar.war",
192: e);
193: throw e;
194: } finally {
195: undeploy("cl11-withjar.war");
196: }
197: }
198:
199: /** Test the ability to override the server classes with war local versions
200: * of xml parser classes.
201: * This test is invalid as of jdk1.4+ due to the bundling of the xerces
202: * parser with the jdk
203: */
204: public void badtestWarXmlOverrides() throws Exception {
205: getLog().debug("+++ testWarOverrides");
206: try {
207: deploy("oldxerces.war");
208: URL servletURL = new URL("http://" + getServerHost()
209: + ":8080/oldxerces/");
210: InputStream reply = (InputStream) servletURL.getContent();
211: getLog().debug(
212: "Accessed http://" + getServerHost()
213: + ":8080/oldxerces/");
214: logReply(reply);
215: } catch (Exception e) {
216: getLog().info("Failed to access oldxerces.war", e);
217: throw e;
218: } finally {
219: undeploy("oldxerces.war");
220: }
221: }
222:
223: /** Test the ability to override the server classes with ejb local versions
224: */
225: public void testEjbOverrides() throws Exception {
226: getLog().debug("+++ testEjbOverrides");
227: try {
228: deploy("log4j113-ejb.jar");
229: InitialContext ctx = new InitialContext();
230: StatelessSessionHome home = (StatelessSessionHome) ctx
231: .lookup("Log4j113StatelessBean");
232: StatelessSession bean = home.create();
233: Throwable error = bean.checkVersion();
234: getLog().debug("StatelessSession.checkVersion returned:",
235: error);
236: assertTrue("checkVersion returned null", error == null);
237: } catch (Exception e) {
238: getLog()
239: .info(
240: "Failed to access Log4j113StatelessBean in log4j113-ejb.jar",
241: e);
242: throw e;
243: } finally {
244: undeploy("log4j113-ejb.jar");
245: }
246: }
247:
248: /** Tests for accessing java system classes from scoped
249: * repositories.
250: *
251: * @throws Exception
252: */
253: public void testSystemClasses() throws Exception {
254: log.info("+++ Begin testSystemClasses");
255: UnifiedLoaderRepository3 parent = new UnifiedLoaderRepository3();
256: HeirarchicalLoaderRepository3 repository0 = new HeirarchicalLoaderRepository3(
257: parent);
258: URL j0URL = getDeployURL("j0.jar");
259: RepositoryClassLoader ucl0 = repository0.newClassLoader(j0URL,
260: true);
261:
262: Class c0 = ucl0.loadClass("java.sql.SQLException");
263: StringBuffer info = new StringBuffer();
264: ClassLoaderUtils.displayClassInfo(c0, info);
265: log.info("Loaded c0: " + info);
266:
267: HeirarchicalLoaderRepository3 repository1 = new HeirarchicalLoaderRepository3(
268: parent);
269: repository1.setUseParentFirst(false);
270: RepositoryClassLoader ucl1 = repository1.newClassLoader(j0URL,
271: true);
272: Class c1 = ucl1.loadClass("java.sql.SQLException");
273: info.setLength(0);
274: ClassLoaderUtils.displayClassInfo(c1, info);
275: log.info("Loaded c1: " + info);
276:
277: Class c2 = ucl1.loadClass("java.sql.SQLWarning");
278: info.setLength(0);
279: ClassLoaderUtils.displayClassInfo(c2, info);
280: log.info("Loaded c2: " + info);
281: }
282:
283: /** Tests for accessing java system classes from scoped
284: * repositories that have system class packages.
285: *
286: * @throws Exception
287: */
288: public void testSystemClasses2() throws Exception {
289: log.info("+++ Begin testSystemClasses2");
290: UnifiedLoaderRepository3 parent = new UnifiedLoaderRepository3();
291: HeirarchicalLoaderRepository3 repository0 = new HeirarchicalLoaderRepository3(
292: parent);
293: URL j0URL = getDeployURL("java-sql.jar");
294: RepositoryClassLoader ucl0 = repository0.newClassLoader(j0URL,
295: true);
296:
297: Class c0 = ucl0.loadClass("java.sql.SQLException");
298: StringBuffer info = new StringBuffer();
299: ClassLoaderUtils.displayClassInfo(c0, info);
300: log.info("Loaded c0: " + info);
301:
302: HeirarchicalLoaderRepository3 repository1 = new HeirarchicalLoaderRepository3(
303: parent);
304: repository1.setUseParentFirst(false);
305: RepositoryClassLoader ucl1 = repository1.newClassLoader(j0URL,
306: true);
307: Class c1 = ucl1.loadClass("java.sql.SQLException");
308: info.setLength(0);
309: ClassLoaderUtils.displayClassInfo(c1, info);
310: log.info("Loaded c1: " + info);
311:
312: Class c2 = ucl1.loadClass("java.sql.SQLWarning");
313: info.setLength(0);
314: ClassLoaderUtils.displayClassInfo(c2, info);
315: log.info("Loaded c2: " + info);
316: }
317:
318: /** Test the interaction through jndi of a service which binds a custom
319: * object into jndi and a servlet which looks up the custom object when
320: * the service and servlet have different class loader scopes that both
321: * have the custom object.
322: */
323: public void testSharedJNDI() throws Exception {
324: getLog().debug("+++ testSharedJNDI");
325: try {
326: deploy("shared-jndi.sar");
327: deploy("shared-jndi.war");
328: URL servletURL = new URL("http://" + getServerHost()
329: + ":8080/shared-jndi/LookupServlet");
330: InputStream reply = (InputStream) servletURL.getContent();
331: getLog().debug("Accessed: " + servletURL);
332: logReply(reply);
333: } catch (Exception e) {
334: getLog().info("Failed to access LookupServlet", e);
335: throw e;
336: } finally {
337: undeploy("shared-jndi.war");
338: undeploy("shared-jndi.sar");
339: }
340: }
341:
342: private void logReply(InputStream reply) throws IOException {
343: getLog().debug("Begin reply");
344: byte[] tmp = new byte[256];
345: while (reply.read(tmp) > 0)
346: getLog().debug(new String(tmp));
347: reply.close();
348: getLog().debug("End reply");
349: }
350: }
|