1 22 package org.jboss.test.classloader.test; 23 24 import java.net.URL ; 25 import java.io.InputStream ; 26 import java.io.IOException ; 27 import javax.management.ObjectName ; 28 import javax.naming.InitialContext ; 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 44 public class ScopingUnitTestCase extends JBossTestCase 45 { 46 public ScopingUnitTestCase(String name) 47 { 48 super(name); 49 } 50 51 54 public void testSingletons() throws Exception 55 { 56 getLog().debug("+++ testSingletons"); 57 try 58 { 59 deploy("singleton1.sar"); 60 getLog().info("Deployed singleton1.sar"); 61 ObjectName testObjectName = new ObjectName ("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 state = (Integer ) getServer().getAttribute(testObjectName, "State"); 65 assertTrue("state.intValue() == ServiceMBean.STARTED", 66 state.intValue() == ServiceMBean.STARTED); 67 Object [] args = {"V1"}; 68 String [] sig = {"java.lang.String"}; 69 Boolean matches = (Boolean ) getServer().invoke(testObjectName, "checkVersion", args, sig); 70 assertTrue("checkVersion(V1) is true", matches.booleanValue()); 71 } 72 catch(Exception 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 testObjectName = new ObjectName ("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 state = (Integer ) getServer().getAttribute(testObjectName, "State"); 86 assertTrue("state.intValue() == ServiceMBean.STARTED", 87 state.intValue() == ServiceMBean.STARTED); 88 Object [] args = {"V2"}; 89 String [] sig = {"java.lang.String"}; 90 Boolean matches = (Boolean ) getServer().invoke(testObjectName, "checkVersion", args, sig); 91 assertTrue("checkVersion(V2) is true", matches.booleanValue()); 92 } 93 catch(Exception 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 110 public void testWarLog4jOverrides() throws Exception 111 { 112 getLog().debug("+++ testWarOverrides"); 113 try 114 { 115 deploy("log4j113.war"); 116 URL log4jServletURL = new URL ("http://" + getServerHost() + ":8080/log4j113/Log4jServlet/"); 117 InputStream reply = (InputStream ) log4jServletURL.getContent(); 118 getLog().debug("Accessed http://" + getServerHost() + ":8080/log4j113/Log4jServlet/"); 119 logReply(reply); 120 121 URL encServletURL = new URL ("http://" + getServerHost() + ":8080/log4j113/ENCServlet/"); 122 reply = (InputStream ) encServletURL.getContent(); 123 getLog().debug("Accessed http://" + getServerHost() + ":8080/log4j113/ENCServlet/"); 124 logReply(reply); 125 } 126 catch(Exception 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 140 public void testWarCommonsLoggingLog4jOverrides() throws Exception 141 { 142 getLog().debug("+++ testWarCommonsLoggingLog4jOverrides"); 143 try 144 { 145 deploy("common-logging.war"); 146 URL log4jServletURL = new URL ("http://" + getServerHost() + ":8080/common-logging/Log4jServlet/"); 147 InputStream reply = (InputStream ) log4jServletURL.getContent(); 148 getLog().debug("Accessed http://" + getServerHost() + ":8080/common-logging/Log4jServlet/"); 149 logReply(reply); 150 } 151 catch(Exception 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 167 public void badtestWarXmlOverrides() throws Exception 168 { 169 getLog().debug("+++ testWarOverrides"); 170 try 171 { 172 deploy("oldxerces.war"); 173 URL servletURL = new URL ("http://" + getServerHost() + ":8080/oldxerces/"); 174 InputStream reply = (InputStream ) servletURL.getContent(); 175 getLog().debug("Accessed http://" + getServerHost() + ":8080/oldxerces/"); 176 logReply(reply); 177 } 178 catch(Exception 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 191 public void testEjbOverrides() throws Exception 192 { 193 getLog().debug("+++ testEjbOverrides"); 194 try 195 { 196 deploy("log4j113-ejb.jar"); 197 InitialContext ctx = new InitialContext (); 198 StatelessSessionHome home = (StatelessSessionHome) ctx.lookup("Log4j113StatelessBean"); 199 StatelessSession bean = home.create(); 200 Throwable error = bean.checkVersion(); 201 getLog().debug("StatelessSession.checkVersion returned:", error); 202 assertTrue("checkVersion returned null", error == null); 203 } 204 catch(Exception 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 220 public void testSystemClasses() throws Exception 221 { 222 log.info("+++ Begin testSystemClasses"); 223 UnifiedLoaderRepository3 parent = new UnifiedLoaderRepository3(); 224 HeirarchicalLoaderRepository3 repository0 = new HeirarchicalLoaderRepository3(parent); 225 URL j0URL = getDeployURL("j0.jar"); 226 RepositoryClassLoader ucl0 = repository0.newClassLoader(j0URL, true); 227 228 Class c0 = ucl0.loadClass("java.sql.SQLException"); 229 StringBuffer info = new StringBuffer (); 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 c1 = ucl1.loadClass("java.sql.SQLException"); 237 info.setLength(0); 238 ClassLoaderUtils.displayClassInfo(c1, info); 239 log.info("Loaded c1: "+info); 240 241 Class c2 = ucl1.loadClass("java.sql.SQLWarning"); 242 info.setLength(0); 243 ClassLoaderUtils.displayClassInfo(c2, info); 244 log.info("Loaded c2: "+info); 245 } 246 247 252 public void testSystemClasses2() throws Exception 253 { 254 log.info("+++ Begin testSystemClasses2"); 255 UnifiedLoaderRepository3 parent = new UnifiedLoaderRepository3(); 256 HeirarchicalLoaderRepository3 repository0 = new HeirarchicalLoaderRepository3(parent); 257 URL j0URL = getDeployURL("java-sql.jar"); 258 RepositoryClassLoader ucl0 = repository0.newClassLoader(j0URL, true); 259 260 Class c0 = ucl0.loadClass("java.sql.SQLException"); 261 StringBuffer info = new StringBuffer (); 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 c1 = ucl1.loadClass("java.sql.SQLException"); 269 info.setLength(0); 270 ClassLoaderUtils.displayClassInfo(c1, info); 271 log.info("Loaded c1: "+info); 272 273 Class c2 = ucl1.loadClass("java.sql.SQLWarning"); 274 info.setLength(0); 275 ClassLoaderUtils.displayClassInfo(c2, info); 276 log.info("Loaded c2: "+info); 277 } 278 279 284 public void testSharedJNDI() throws Exception 285 { 286 getLog().debug("+++ testSharedJNDI"); 287 try 288 { 289 deploy("shared-jndi.sar"); 290 deploy("shared-jndi.war"); 291 URL servletURL = new URL ("http://" + getServerHost() + ":8080/shared-jndi/LookupServlet"); 292 InputStream reply = (InputStream ) servletURL.getContent(); 293 getLog().debug("Accessed: "+servletURL); 294 logReply(reply); 295 } 296 catch(Exception 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 reply) throws IOException 309 { 310 getLog().debug("Begin reply"); 311 byte[] tmp = new byte[256]; 312 while( reply.read(tmp) > 0 ) 313 getLog().debug(new String (tmp)); 314 reply.close(); 315 getLog().debug("End reply"); 316 } 317 } 318 | Popular Tags |