1 17 18 package org.apache.geronimo.kernel.classloader; 19 20 import java.net.URL ; 21 import java.net.MalformedURLException ; 22 import java.util.Enumeration ; 23 import java.util.zip.ZipEntry ; 24 import java.util.jar.JarOutputStream ; 25 import java.util.jar.Manifest ; 26 import java.util.jar.Attributes ; 27 import java.io.InputStream ; 28 import java.io.IOException ; 29 import java.io.File ; 30 import java.io.FileOutputStream ; 31 import java.io.FileNotFoundException ; 32 33 import org.apache.geronimo.testsupport.TestSupport; 34 35 38 public class UrlResourceFinderTest extends TestSupport { 39 private File jarFile; 40 private Manifest manifest; 41 private Attributes resourceAttributes; 42 private File alternateJarFile; 43 private File testResource; 44 45 51 public void testResourceEnumeration() throws Exception { 52 URL jar1 = new File (BASEDIR, "src/test/data/resourceFinderTest/jar1/").toURL(); 53 URL jar2 = new File (BASEDIR, "src/test/data/resourceFinderTest/jar2/").toURL(); 54 UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL []{jar1, jar2}); 55 56 Enumeration enumeration = resourceFinder.findResources("resource"); 57 58 assertTrue(enumeration.hasMoreElements()); 60 assertTrue(enumeration.hasMoreElements()); 61 URL resource1 = (URL ) enumeration.nextElement(); 62 assertNotNull(resource1); 63 assertEquals("resource1", toString(resource1.openStream())); 64 65 assertTrue(enumeration.hasMoreElements()); 67 assertTrue(enumeration.hasMoreElements()); 68 URL resource2 = (URL ) enumeration.nextElement(); 69 assertNotNull(resource2); 70 assertEquals("resource2", toString(resource2.openStream())); 71 assertFalse(enumeration.hasMoreElements()); 72 } 73 74 public void testDirectoryResource() throws Exception { 75 URL jar = new File (BASEDIR, "src/test/data/resourceFinderTest/jar1/").toURL(); 76 UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL []{jar}); 77 78 ResourceHandle resource = resourceFinder.getResource("resource"); 79 assertNotNull(resource); 80 81 assertEquals("resource1", new String (resource.getBytes())); 83 84 assertEquals("resource1", toString(resource.getInputStream())); 86 87 URL url = resource.getUrl(); 89 assertEquals("resource1", toString(url.openStream())); 90 91 URL copyUrl = new URL (url.toExternalForm()); 93 assertEquals("resource1", toString(copyUrl.openStream())); 94 95 URL directUrl = resourceFinder.findResource("resource"); 97 assertEquals("resource1", toString(directUrl.openStream())); 98 assertEquals("resource1", toString(new URL (directUrl.toExternalForm()).openStream())); 99 100 assertEquals("resource1".length(), resource.getContentLength()); 102 103 assertEquals("resource", resource.getName()); 105 106 assertNull(resource.getAttributes()); 108 109 assertNull(resource.getManifest()); 111 } 112 113 public void testJarResource() throws Exception { 114 URL jar = jarFile.toURL(); 115 UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL []{jar}); 116 117 ResourceHandle resource = resourceFinder.getResource("resource"); 118 assertNotNull(resource); 119 120 assertEquals("resource3", new String (resource.getBytes())); 122 123 assertEquals("resource3", toString(resource.getInputStream())); 125 126 URL url = resource.getUrl(); 128 assertEquals("resource3", toString(url.openStream())); 129 130 URL copyUrl = new URL (url.toExternalForm()); 132 assertEquals("resource3", toString(copyUrl.openStream())); 133 134 URL directUrl = resourceFinder.findResource("resource"); 136 assertEquals("resource3", toString(directUrl.openStream())); 137 assertEquals("resource3", toString(new URL (directUrl.toExternalForm()).openStream())); 138 139 assertEquals("resource3".length(), resource.getContentLength()); 141 142 assertEquals("resource", resource.getName()); 144 145 assertEquals(resourceAttributes, resource.getAttributes()); 147 148 assertEquals(manifest, resource.getManifest()); 150 } 151 152 public void testAddURL() throws Exception { 153 URL jar1 = new File (BASEDIR, "src/test/data/resourceFinderTest/jar1/").toURL(); 154 UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL []{jar1}); 155 156 Enumeration enumeration = resourceFinder.findResources("resource"); 157 158 assertTrue(enumeration.hasMoreElements()); 160 assertTrue(enumeration.hasMoreElements()); 161 URL resource1 = (URL ) enumeration.nextElement(); 162 assertNotNull(resource1); 163 assertEquals("resource1", toString(resource1.openStream())); 164 assertFalse(enumeration.hasMoreElements()); 165 166 URL jar2 = new File (BASEDIR, "src/test/data/resourceFinderTest/jar2/").toURL(); 168 resourceFinder.addUrl(jar2); 169 170 ResourceHandle resource = resourceFinder.getResource("resource"); 172 assertNotNull(resource); 173 assertEquals("resource1", new String (resource.getBytes())); 174 175 resource1 = resourceFinder.findResource("resource"); 177 assertEquals("resource1", toString(resource1.openStream())); 178 179 enumeration = resourceFinder.findResources("resource"); 181 182 assertTrue(enumeration.hasMoreElements()); 184 assertTrue(enumeration.hasMoreElements()); 185 resource1 = (URL ) enumeration.nextElement(); 186 assertNotNull(resource1); 187 assertEquals("resource1", toString(resource1.openStream())); 188 assertTrue(enumeration.hasMoreElements()); 189 190 assertTrue(enumeration.hasMoreElements()); 192 assertTrue(enumeration.hasMoreElements()); 193 URL resource2 = (URL ) enumeration.nextElement(); 194 assertNotNull(resource2); 195 assertEquals("resource2", toString(resource2.openStream())); 196 assertFalse(enumeration.hasMoreElements()); 197 } 198 199 public void testConcurrentAddURL() throws Exception { 200 URL jar1 = new File (BASEDIR, "src/test/data/resourceFinderTest/jar1/").toURL(); 201 URL jar2 = new File (BASEDIR, "src/test/data/resourceFinderTest/jar2/").toURL(); 202 UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL []{jar1, jar2}); 203 204 Enumeration enumeration = resourceFinder.findResources("resource"); 205 206 assertTrue(enumeration.hasMoreElements()); 208 assertTrue(enumeration.hasMoreElements()); 209 URL resource1 = (URL ) enumeration.nextElement(); 210 assertNotNull(resource1); 211 assertEquals("resource1", toString(resource1.openStream())); 212 assertTrue(enumeration.hasMoreElements()); 213 214 URL newJar = jarFile.toURL(); 218 resourceFinder.addUrl(newJar); 219 220 ResourceHandle jar3Resouce = resourceFinder.getResource("jar3"); 223 assertNotNull(jar3Resouce); 224 assertEquals("jar3", new String (jar3Resouce.getBytes())); 225 226 URL jar3Url = resourceFinder.findResource("jar3"); 228 assertEquals("jar3", toString(jar3Url.openStream())); 229 230 234 assertTrue(enumeration.hasMoreElements()); 236 assertTrue(enumeration.hasMoreElements()); 237 URL resource2 = (URL ) enumeration.nextElement(); 238 assertNotNull(resource2); 239 assertEquals("resource2", toString(resource2.openStream())); 240 assertFalse(enumeration.hasMoreElements()); 241 } 242 243 public void testDirectoryDestroy() throws Exception { 244 URL jar = new File (BASEDIR, "src/test/data/resourceFinderTest/jar1/").toURL(); 245 UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL []{jar}); 246 assertDestroyed(resourceFinder, "resource1", null); 247 } 248 249 public void testJarDestroy() throws Exception { 250 URL jar = jarFile.toURL(); 251 UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL []{jar}); 252 assertDestroyed(resourceFinder, "resource3", manifest); 253 } 254 255 public void testUrlCopy() throws Exception { 256 URL jar = jarFile.toURL(); 257 UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL []{jar}); 258 259 URL resource = resourceFinder.findResource("resource"); 261 assertNotNull(resource); 262 assertEquals("resource3", toString(resource.openStream())); 263 264 URL stringCopy = new URL (resource.toExternalForm()); 266 assertEquals("resource3", toString(stringCopy.openStream())); 267 268 URL handlerCopy = new URL (resource, resource.toExternalForm()); 270 assertEquals("resource3", toString(handlerCopy.openStream())); 271 272 URL other = new URL (resource, "jar3"); 274 assertEquals("jar3", toString(other.openStream())); 275 } 276 277 public void testUrlAccess() throws Exception { 278 URL jar = jarFile.toURL(); 279 UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL []{jar}); 280 281 URL geronimoUrl = resourceFinder.findResource("resource"); 283 assertNotNull(geronimoUrl); 284 assertEquals("resource3", toString(geronimoUrl.openStream())); 285 286 URL systemUrl = new URL (geronimoUrl.toExternalForm()); 288 assertEquals("resource3", toString(systemUrl.openStream())); 289 290 assertEquals("jar3", toString(new URL (systemUrl, "jar3").openStream())); 292 assertEquals("jar3", toString(new URL (geronimoUrl, "jar3").openStream())); 293 294 String mainEntry = "jar:" + jarFile.toURL().toExternalForm() + "!/jar3"; 296 assertEquals("jar3", toString(new URL (systemUrl, mainEntry).openStream())); 297 assertEquals("jar3", toString(new URL (geronimoUrl, mainEntry).openStream())); 298 299 try { 301 new URL (systemUrl, "unknown").openStream(); 302 fail("Expected a FileNotFoundException"); 303 } catch (FileNotFoundException expected) { 304 } 305 try { 306 new URL (geronimoUrl, "unknown").openStream(); 307 fail("Expected a FileNotFoundException"); 308 } catch (FileNotFoundException expected) { 309 } 310 311 String alternateEntry = "jar:" + alternateJarFile.toURL().toExternalForm() + "!/jar4"; 313 assertEquals("jar4", toString(new URL (systemUrl, alternateEntry).openStream())); 314 assertEquals("jar4", toString(new URL (geronimoUrl, alternateEntry).openStream())); 315 316 String alternateUnknownEntry = "jar:" + alternateJarFile.toURL().toExternalForm() + "!/unknown"; 318 try { 319 new URL (systemUrl, alternateUnknownEntry).openStream(); 320 fail("Expected a FileNotFoundException"); 321 } catch (FileNotFoundException expected) { 322 } 323 try { 324 new URL (geronimoUrl, alternateUnknownEntry).openStream(); 325 fail("Expected a FileNotFoundException"); 326 } catch (FileNotFoundException expected) { 327 } 328 329 assertEquals("testResource", toString(new URL (systemUrl, testResource.toURL().toExternalForm()).openStream())); 331 assertEquals("testResource", toString(new URL (geronimoUrl, testResource.toURL().toExternalForm()).openStream())); 332 333 String badEntry = "jar:" + alternateJarFile.toURL().toExternalForm(); 335 try { 336 new URL (systemUrl, badEntry).openStream(); 337 fail("Expected a FileNotFoundException"); 338 } catch (MalformedURLException expected) { 339 } 340 try { 341 new URL (geronimoUrl, badEntry).openStream(); 342 fail("Expected a FileNotFoundException"); 343 } catch (MalformedURLException expected) { 344 } 345 346 badEntry = "jar:" + alternateJarFile.toURL().toExternalForm() + "!/foo.jar!/bar"; 348 try { 349 new URL (systemUrl, badEntry).openStream(); 350 fail("Expected a FileNotFoundException"); 351 } catch (FileNotFoundException expected) { 352 } 353 try { 354 new URL (geronimoUrl, badEntry).openStream(); 355 fail("Expected a FileNotFoundException"); 356 } catch (FileNotFoundException expected) { 357 } 358 } 359 360 public void assertDestroyed(UrlResourceFinder resourceFinder, String resourceValue, Manifest expectedManifest) throws Exception { 361 ResourceHandle resource = resourceFinder.getResource("resource"); 362 assertNotNull(resource); 363 assertEquals(resourceValue, new String (resource.getBytes())); 364 365 URL url = resource.getUrl(); 367 assertEquals(resourceValue, toString(url.openStream())); 368 369 URL copyUrl = new URL (url.toExternalForm()); 371 assertEquals(resourceValue, toString(copyUrl.openStream())); 372 373 URL directUrl = resourceFinder.findResource("resource"); 375 assertEquals(resourceValue, toString(directUrl.openStream())); 376 URL directUrlCopy = new URL (directUrl.toExternalForm()); 377 assertEquals(resourceValue, toString(directUrlCopy.openStream())); 378 379 resourceFinder.destroy(); 381 382 assertNull(resourceFinder.getResource("resource")); 384 385 assertNull(resourceFinder.findResource("resource")); 387 388 assertFalse(resourceFinder.findResources("resource").hasMoreElements()); 390 391 try { 393 assertEquals(resourceValue, toString(url.openStream())); 394 } catch (IllegalStateException expected) { 395 } catch (IOException expected) { 396 } 397 try { 398 assertEquals(resourceValue, toString(directUrl.openStream())); 399 } catch (IllegalStateException expected) { 400 } catch (IOException expected) { 401 } 402 403 assertEquals(resourceValue, toString(copyUrl.openStream())); 405 assertEquals(resourceValue, toString(directUrlCopy.openStream())); 406 407 assertEquals("resource", resource.getName()); 409 try { 410 if (expectedManifest != null) { 411 assertEquals(expectedManifest.getAttributes("resource"), resource.getAttributes()); 412 } 413 } catch (IllegalStateException expected) { 414 } 415 try { 416 assertEquals(expectedManifest, resource.getManifest()); 417 } catch (IllegalStateException expected) { 418 } 419 try { 420 assertEquals(resourceValue, toString(resource.getUrl().openStream())); 421 } catch (IllegalStateException expected) { 422 } 423 try { 424 assertEquals(resourceValue, toString(resource.getInputStream())); 425 } catch (IllegalStateException expected) { 426 } catch (IOException expected) { 427 } 428 try { 429 assertEquals(resourceValue, new String (resource.getBytes())); 430 } catch (IllegalStateException expected) { 431 } catch (IOException expected) { 432 } 433 } 434 435 protected void setUp() throws Exception { 436 super.setUp(); 437 438 manifest = new Manifest (); 442 Attributes mainAttributes = manifest.getMainAttributes(); 443 mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); 444 mainAttributes.putValue("food", "nacho"); 445 resourceAttributes = new Attributes (); 446 resourceAttributes.putValue("drink", "margarita"); 447 manifest.getEntries().put("resource", resourceAttributes); 448 449 File targetDir = new File (BASEDIR, "target"); 450 jarFile = new File (targetDir, "resourceFinderTest.jar"); 451 JarOutputStream jarOutputStream = new JarOutputStream (new FileOutputStream (jarFile), manifest); 452 jarOutputStream.putNextEntry(new ZipEntry ("resource")); 453 jarOutputStream.write("resource3".getBytes()); 454 jarOutputStream.putNextEntry(new ZipEntry ("jar3")); 455 jarOutputStream.write("jar3".getBytes()); 456 IoUtil.close(jarOutputStream); 457 458 alternateJarFile = new File (targetDir, "alternate.jar"); 459 log.debug(alternateJarFile.getAbsolutePath()); 460 jarOutputStream = new JarOutputStream (new FileOutputStream (alternateJarFile), manifest); 461 jarOutputStream.putNextEntry(new ZipEntry ("resource")); 462 jarOutputStream.write("resource4".getBytes()); 463 jarOutputStream.putNextEntry(new ZipEntry ("jar4")); 464 jarOutputStream.write("jar4".getBytes()); 465 IoUtil.close(jarOutputStream); 466 467 testResource = new File (targetDir, "testResource"); 468 FileOutputStream fileOutputStream = new FileOutputStream (testResource); 469 fileOutputStream.write("testResource".getBytes()); 470 IoUtil.close(fileOutputStream); 471 } 472 473 protected void tearDown() throws Exception { 474 jarFile.delete(); 475 super.tearDown(); 476 } 477 478 private static String toString(InputStream in) throws IOException { 479 try { 480 byte[] bytes = IoUtil.getBytes(in); 481 String string = new String (bytes); 482 return string; 483 } finally { 484 IoUtil.close(in); 485 } 486 } 487 } 488 | Popular Tags |