1 22 package org.jboss.test.classloading; 23 24 import java.net.URL ; 25 import java.net.URLClassLoader ; 26 import java.security.ProtectionDomain ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Enumeration ; 30 31 import org.jboss.test.AbstractSystemTest; 32 import org.jboss.mx.loading.DomainClassLoaderUCLImpl; 33 import org.jboss.mx.loading.UnifiedLoaderRepositoryDCL; 34 35 41 public class UCLDomainClassLoaderTest extends AbstractSystemTest 42 { 43 static String [] SUPPORT_CLASSES = { 45 "org.jboss.test.support.jar1.simple1.Bean1", 46 "org.jboss.test.support.jar1.simple1.ISimpleBean", 47 "org.jboss.test.support.jar2.simple1.Jar1Consumer" 48 }; 49 static String [] SUPPORT_RESOURCES = { 51 "resource1.xml" 52 }; 53 54 public UCLDomainClassLoaderTest(String name) 55 { 56 super(name); 57 } 58 59 60 63 @Override 64 protected void setUp() throws Exception 65 { 66 super.setUp(); 67 ClassLoader tcl = Thread.currentThread().getContextClassLoader(); 69 for(String name : SUPPORT_CLASSES) 70 { 71 try 72 { 73 Class beanClass = tcl.loadClass(name); 74 fail("Was able to find "+name+", "+beanClass.getProtectionDomain()); 75 } 76 catch(ClassNotFoundException e) 77 { 78 getLog().debug(name+" is not on unit test classpath"); 79 } 80 } 81 for(String name : SUPPORT_RESOURCES) 82 { 83 URL resURL = tcl.getResource(name); 84 if( resURL != null ) 85 fail("Was able to find "+name+", "+resURL); 86 else 87 getLog().debug(name+" is not on unit test classpath"); 88 } 89 } 90 91 92 96 public void testSimple1Jar() 97 throws Exception 98 { 99 String beanClassName = "org.jboss.test.support.jar1.simple1.Bean1"; 100 101 UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL(); 102 URL [] cp = buildClasspath("/jar1.jar"); 103 getLog().info("Using classpath: "+Arrays.asList(cp)); 104 DomainClassLoaderUCLImpl loader = new DomainClassLoaderUCLImpl(cp, repository); 105 Class beanClass = repository.loadClass(beanClassName); 106 assertNotNull(beanClass); 107 ProtectionDomain pd = beanClass.getProtectionDomain(); 108 getLog().info(beanClass+", PD: "+pd); 109 String path = pd.getCodeSource().getLocation().getPath(); 110 assertTrue("Bean1.PD ends with jar1.jar", path.endsWith("jar1.jar")); 111 Object bean = beanClass.newInstance(); 112 assertSame(loader, bean.getClass().getClassLoader()); 113 } 114 115 121 public void testSimple2JarWithManifestCP() 122 throws Exception 123 { 124 String beanClassName = "org.jboss.test.support.jar2.Jar1Consumer"; 125 126 UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL(); 127 URL [] cp = buildClasspath("/jar2CPjar1.jar"); 128 getLog().info("Using classpath: "+Arrays.asList(cp)); 129 DomainClassLoaderUCLImpl loader = new DomainClassLoaderUCLImpl(cp, repository); 130 Class beanClass = repository.loadClass(beanClassName); 131 assertNotNull(beanClass); 132 ProtectionDomain pd = beanClass.getProtectionDomain(); 133 getLog().info(beanClass+", PD: "+pd); 134 String path = pd.getCodeSource().getLocation().getPath(); 135 assertTrue("Jar1Consumer.PD ends with jar2CPjar1.jar", path.endsWith("jar2CPjar1.jar")); 136 Object bean = beanClass.newInstance(); 137 assertSame(loader, bean.getClass().getClassLoader()); 138 139 Class beanClass2 = repository.loadClass("org.jboss.test.support.jar1.simple1.ISimpleBean"); 140 assertNotNull(beanClass2); 141 pd = beanClass2.getProtectionDomain(); 142 getLog().info(beanClass2+", PD: "+pd); 143 path = pd.getCodeSource().getLocation().getPath(); 144 assertTrue("ISimpleBean.PD ends with jar1.jar", path.endsWith("jar1.jar")); 145 assertSame(loader, beanClass2.getClassLoader()); 146 } 147 148 152 public void testMulitipleJars() 153 throws Exception 154 { 155 String beanClassName = "org.jboss.test.support.jar2.Jar1Consumer"; 156 UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL(); 157 URL [] cp = buildClasspath("/jar1.jar", "/jar2.jar"); 158 getLog().info("Using classpath: "+Arrays.asList(cp)); 159 DomainClassLoaderUCLImpl loader = new DomainClassLoaderUCLImpl(cp, repository); 160 Class beanClass = repository.loadClass(beanClassName); 161 assertNotNull(beanClass); 162 ProtectionDomain pd = beanClass.getProtectionDomain(); 163 getLog().info(beanClass+", PD: "+pd); 164 String path = pd.getCodeSource().getLocation().getPath(); 165 assertTrue("Jar1Consumer.PD ends with jar2.jar", path.endsWith("jar2.jar")); 166 Object bean = beanClass.newInstance(); 167 assertSame(loader, bean.getClass().getClassLoader()); 168 169 Class beanClass2 = repository.loadClass("org.jboss.test.support.jar1.simple1.ISimpleBean"); 170 assertNotNull(beanClass); 171 pd = beanClass2.getProtectionDomain(); 172 getLog().info(beanClass2+", PD: "+pd); 173 path = pd.getCodeSource().getLocation().getPath(); 174 assertTrue("ISimpleBean.PD ends with jar1.jar", path.endsWith("jar1.jar")); 175 assertSame(loader, beanClass2.getClassLoader()); 176 } 177 178 182 public void testMutipleClassLoaders() 183 throws Exception 184 { 185 String beanClassName = "org.jboss.test.support.jar2.Jar1Consumer"; 186 UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL(); 187 URL [] cp1 = buildClasspath("/jar1.jar"); 188 URL [] cp2 = buildClasspath("/jar2.jar"); 189 getLog().info("Using classpath: "+Arrays.asList(cp1)); 190 DomainClassLoaderUCLImpl loader1 = new DomainClassLoaderUCLImpl(cp1, repository); 191 loader1.getURLs(); 192 getLog().info("Using classpath: "+Arrays.asList(cp2)); 193 DomainClassLoaderUCLImpl loader2 = new DomainClassLoaderUCLImpl(cp2, repository); 194 Class beanClass = loader1.loadClass(beanClassName); 195 assertNotNull(beanClass); 196 ProtectionDomain pd = beanClass.getProtectionDomain(); 197 getLog().info(beanClass+", PD: "+pd); 198 String path = pd.getCodeSource().getLocation().getPath(); 199 assertTrue("Jar1Consumer.PD ends with jar2.jar", path.endsWith("jar2.jar")); 200 Object bean = beanClass.newInstance(); 201 assertSame(loader2, bean.getClass().getClassLoader()); 202 203 Class beanClass2 = repository.loadClass("org.jboss.test.support.jar1.simple1.ISimpleBean"); 204 assertNotNull(beanClass); 205 pd = beanClass2.getProtectionDomain(); 206 getLog().info(beanClass2+", PD: "+pd); 207 path = pd.getCodeSource().getLocation().getPath(); 208 assertTrue("ISimpleBean.PD ends with jar1.jar", path.endsWith("jar1.jar")); 209 assertSame(loader1, beanClass2.getClassLoader()); 210 } 211 212 216 public void testSimple1Resource() 217 throws Exception 218 { 219 UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL(); 220 URL [] cp = buildClasspath("/jar1.jar"); 221 getLog().info("Using classpath: "+Arrays.asList(cp)); 222 DomainClassLoaderUCLImpl loader = new DomainClassLoaderUCLImpl(cp, repository); 223 URL resURL = repository.getResource("resource1.xml", loader); 224 assertNotNull(resURL); 225 getLog().info(resURL); 226 String path = resURL.getPath(); 227 assertTrue("res path contains jar1.jar", path.indexOf("jar1.jar") > 0); 228 229 Enumeration <URL > resURLs = repository.findResources("resource1.xml"); 230 int count = 0; 231 while( resURLs.hasMoreElements() ) 232 { 233 getLog().info(resURLs.nextElement()); 234 count ++; 235 } 236 assertEquals("1 resource1.xml", 1, count); 237 } 238 239 245 public void testSimple2ResourcesWithManifestCP() 246 throws Exception 247 { 248 UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL(); 249 URL [] cp = buildClasspath("/jar2CPjar1.jar"); 250 getLog().info("Using classpath: "+Arrays.asList(cp)); 251 DomainClassLoaderUCLImpl loader = new DomainClassLoaderUCLImpl(cp, repository); 252 URL resURL = repository.getResource("resource1.xml", loader); 253 assertNotNull(resURL); 254 getLog().info(resURL); 255 String path = resURL.getPath(); 256 assertTrue("res path contains jar1.jar", path.indexOf("jar1.jar") > 0); 257 258 Enumeration <URL > resURLs = repository.findResources("resource1.xml"); 259 int count = 0; 260 while( resURLs.hasMoreElements() ) 261 { 262 getLog().info(resURLs.nextElement()); 263 count ++; 264 } 265 assertEquals("1 resource1.xml", 1, count); 266 } 267 268 272 public void testMulitipleJarsResource() 273 throws Exception 274 { 275 UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL(); 276 URL [] cp = buildClasspath("/jar1.jar", "/jar2.jar"); 277 getLog().info("Using classpath: "+Arrays.asList(cp)); 278 DomainClassLoaderUCLImpl loader = new DomainClassLoaderUCLImpl(cp, repository); 279 URL resURL = repository.getResource("resource1.xml", loader); 280 assertNotNull(resURL); 281 getLog().info(resURL); 282 String path = resURL.getPath(); 283 assertTrue("res path contains jar1.jar", path.indexOf("jar1.jar") > 0); 284 285 Enumeration <URL > resURLs = repository.findResources("resource1.xml"); 286 int count = 0; 287 while( resURLs.hasMoreElements() ) 288 { 289 getLog().info(resURLs.nextElement()); 290 count ++; 291 } 292 assertEquals("2 resource1.xml", 2, count); 293 } 294 295 300 public void testMutipleClassLoadersResource() 301 throws Exception 302 { 303 UnifiedLoaderRepositoryDCL repository = new UnifiedLoaderRepositoryDCL(); 304 URL [] cp1 = buildClasspath("/jar1.jar"); 305 URL [] cp2 = buildClasspath("/jar2.jar"); 306 getLog().info("Using classpath: "+Arrays.asList(cp1)); 307 DomainClassLoaderUCLImpl loader1 = new DomainClassLoaderUCLImpl(cp1, repository); 308 loader1.getURLs(); 309 getLog().info("Using classpath: "+Arrays.asList(cp2)); 310 DomainClassLoaderUCLImpl loader2 = new DomainClassLoaderUCLImpl(cp2, repository); 311 URL resURL = repository.getResource("resource1.xml", loader1); 312 assertNotNull(resURL); 313 getLog().info(resURL); 314 String path = resURL.getPath(); 315 assertTrue("res path contains jar1.jar", path.indexOf("jar1.jar") > 0); 316 resURL = repository.getResource("resource1.xml", loader2); 317 assertNotNull(resURL); 318 getLog().info(resURL); 319 path = resURL.getPath(); 320 assertTrue("res path contains jar2.jar", path.indexOf("jar2.jar") > 0); 321 322 Enumeration <URL > resURLs = repository.findResources("resource1.xml"); 323 int count = 0; 324 while( resURLs.hasMoreElements() ) 325 { 326 getLog().info(resURLs.nextElement()); 327 count ++; 328 } 329 assertEquals("2 resource1.xml", 2, count); 330 } 331 332 private URL [] buildClasspath(String ... jars) 333 { 334 ArrayList <URL > cp = new ArrayList <URL >(); 335 for(String jar : jars) 336 { 337 URL jarURL = super.getResource("/org/jboss/test/classloading/" + jar); 339 if( jarURL == null ) 340 fail("Failed to find resource for jar: "+jar); 341 cp.add(jarURL); 342 } 343 URL [] tmp = new URL [cp.size()]; 344 cp.toArray(tmp); 345 return tmp; 346 } 347 } 348 | Popular Tags |