KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > kernel > classloader > UrlResourceFinderTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.kernel.classloader;
19
20 import java.net.URL JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.zip.ZipEntry JavaDoc;
24 import java.util.jar.JarOutputStream JavaDoc;
25 import java.util.jar.Manifest JavaDoc;
26 import java.util.jar.Attributes JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.File JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31 import java.io.FileNotFoundException JavaDoc;
32
33 import org.apache.geronimo.testsupport.TestSupport;
34
35 /**
36  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
37  */

38 public class UrlResourceFinderTest extends TestSupport {
39     private File JavaDoc jarFile;
40     private Manifest JavaDoc manifest;
41     private Attributes JavaDoc resourceAttributes;
42     private File JavaDoc alternateJarFile;
43     private File JavaDoc testResource;
44
45     /**
46      * There are 2 "jars" with a "resource" inside. Make sure the enumeration has exactly 2 elements and
47      * that hasMoreElements() doesn't advance the iterator.
48      *
49      * @throws Exception
50      */

51     public void testResourceEnumeration() throws Exception JavaDoc {
52         URL JavaDoc jar1 = new File JavaDoc(BASEDIR, "src/test/data/resourceFinderTest/jar1/").toURL();
53         URL JavaDoc jar2 = new File JavaDoc(BASEDIR, "src/test/data/resourceFinderTest/jar2/").toURL();
54         UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL JavaDoc[]{jar1, jar2});
55
56         Enumeration JavaDoc enumeration = resourceFinder.findResources("resource");
57
58         // resource1
59
assertTrue(enumeration.hasMoreElements());
60         assertTrue(enumeration.hasMoreElements());
61         URL JavaDoc resource1 = (URL JavaDoc) enumeration.nextElement();
62         assertNotNull(resource1);
63         assertEquals("resource1", toString(resource1.openStream()));
64
65         // resource2
66
assertTrue(enumeration.hasMoreElements());
67         assertTrue(enumeration.hasMoreElements());
68         URL JavaDoc resource2 = (URL JavaDoc) enumeration.nextElement();
69         assertNotNull(resource2);
70         assertEquals("resource2", toString(resource2.openStream()));
71         assertFalse(enumeration.hasMoreElements());
72     }
73
74     public void testDirectoryResource() throws Exception JavaDoc {
75         URL JavaDoc jar = new File JavaDoc(BASEDIR, "src/test/data/resourceFinderTest/jar1/").toURL();
76         UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL JavaDoc[]{jar});
77
78         ResourceHandle resource = resourceFinder.getResource("resource");
79         assertNotNull(resource);
80
81         // handle.getBytes()
82
assertEquals("resource1", new String JavaDoc(resource.getBytes()));
83
84         // handle.getInputStream()
85
assertEquals("resource1", toString(resource.getInputStream()));
86
87         // handle.getUrl()
88
URL JavaDoc url = resource.getUrl();
89         assertEquals("resource1", toString(url.openStream()));
90
91         // copy the url and verify we can still get the data
92
URL JavaDoc copyUrl = new URL JavaDoc(url.toExternalForm());
93         assertEquals("resource1", toString(copyUrl.openStream()));
94
95         // resourceFinder.findResource
96
URL JavaDoc directUrl = resourceFinder.findResource("resource");
97         assertEquals("resource1", toString(directUrl.openStream()));
98         assertEquals("resource1", toString(new URL JavaDoc(directUrl.toExternalForm()).openStream()));
99
100         // handle.getContentLength()
101
assertEquals("resource1".length(), resource.getContentLength());
102
103         // handle.getName()
104
assertEquals("resource", resource.getName());
105
106         // handle.getAttributes()
107
assertNull(resource.getAttributes());
108
109         // handle.getManifest()
110
assertNull(resource.getManifest());
111     }
112
113     public void testJarResource() throws Exception JavaDoc {
114         URL JavaDoc jar = jarFile.toURL();
115         UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL JavaDoc[]{jar});
116
117         ResourceHandle resource = resourceFinder.getResource("resource");
118         assertNotNull(resource);
119
120         // handle.getBytes()
121
assertEquals("resource3", new String JavaDoc(resource.getBytes()));
122
123         // handle.getInputStream()
124
assertEquals("resource3", toString(resource.getInputStream()));
125
126         // handle.getUrl()
127
URL JavaDoc url = resource.getUrl();
128         assertEquals("resource3", toString(url.openStream()));
129
130         // copy the url and verify we can still get the data
131
URL JavaDoc copyUrl = new URL JavaDoc(url.toExternalForm());
132         assertEquals("resource3", toString(copyUrl.openStream()));
133
134         // resourceFinder.findResource
135
URL JavaDoc directUrl = resourceFinder.findResource("resource");
136         assertEquals("resource3", toString(directUrl.openStream()));
137         assertEquals("resource3", toString(new URL JavaDoc(directUrl.toExternalForm()).openStream()));
138
139         // handle.getContentLength()
140
assertEquals("resource3".length(), resource.getContentLength());
141
142         // handle.getName()
143
assertEquals("resource", resource.getName());
144
145         // handle.getAttributes()
146
assertEquals(resourceAttributes, resource.getAttributes());
147
148         // handle.getManifest()
149
assertEquals(manifest, resource.getManifest());
150     }
151
152     public void testAddURL() throws Exception JavaDoc {
153         URL JavaDoc jar1 = new File JavaDoc(BASEDIR, "src/test/data/resourceFinderTest/jar1/").toURL();
154         UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL JavaDoc[]{jar1});
155
156         Enumeration JavaDoc enumeration = resourceFinder.findResources("resource");
157
158         // resource1
159
assertTrue(enumeration.hasMoreElements());
160         assertTrue(enumeration.hasMoreElements());
161         URL JavaDoc resource1 = (URL JavaDoc) enumeration.nextElement();
162         assertNotNull(resource1);
163         assertEquals("resource1", toString(resource1.openStream()));
164         assertFalse(enumeration.hasMoreElements());
165
166         // addUrl
167
URL JavaDoc jar2 = new File JavaDoc(BASEDIR, "src/test/data/resourceFinderTest/jar2/").toURL();
168         resourceFinder.addUrl(jar2);
169
170         // getResource should find the first jar only
171
ResourceHandle resource = resourceFinder.getResource("resource");
172         assertNotNull(resource);
173         assertEquals("resource1", new String JavaDoc(resource.getBytes()));
174
175         // findResource should find the first jar only
176
resource1 = resourceFinder.findResource("resource");
177         assertEquals("resource1", toString(resource1.openStream()));
178
179         // findResouces should see both jars
180
enumeration = resourceFinder.findResources("resource");
181
182         // resource1
183
assertTrue(enumeration.hasMoreElements());
184         assertTrue(enumeration.hasMoreElements());
185         resource1 = (URL JavaDoc) enumeration.nextElement();
186         assertNotNull(resource1);
187         assertEquals("resource1", toString(resource1.openStream()));
188         assertTrue(enumeration.hasMoreElements());
189
190         // resource2
191
assertTrue(enumeration.hasMoreElements());
192         assertTrue(enumeration.hasMoreElements());
193         URL JavaDoc resource2 = (URL JavaDoc) enumeration.nextElement();
194         assertNotNull(resource2);
195         assertEquals("resource2", toString(resource2.openStream()));
196         assertFalse(enumeration.hasMoreElements());
197     }
198
199     public void testConcurrentAddURL() throws Exception JavaDoc {
200         URL JavaDoc jar1 = new File JavaDoc(BASEDIR, "src/test/data/resourceFinderTest/jar1/").toURL();
201         URL JavaDoc jar2 = new File JavaDoc(BASEDIR, "src/test/data/resourceFinderTest/jar2/").toURL();
202         UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL JavaDoc[]{jar1, jar2});
203
204         Enumeration JavaDoc enumeration = resourceFinder.findResources("resource");
205
206         // resource1
207
assertTrue(enumeration.hasMoreElements());
208         assertTrue(enumeration.hasMoreElements());
209         URL JavaDoc resource1 = (URL JavaDoc) enumeration.nextElement();
210         assertNotNull(resource1);
211         assertEquals("resource1", toString(resource1.openStream()));
212         assertTrue(enumeration.hasMoreElements());
213
214         //
215
// addURL
216
//
217
URL JavaDoc newJar = jarFile.toURL();
218         resourceFinder.addUrl(newJar);
219
220         // new resources should be available
221
// getResource should find the first jar only
222
ResourceHandle jar3Resouce = resourceFinder.getResource("jar3");
223         assertNotNull(jar3Resouce);
224         assertEquals("jar3", new String JavaDoc(jar3Resouce.getBytes()));
225
226         // findResource should find the first jar only
227
URL JavaDoc jar3Url = resourceFinder.findResource("jar3");
228         assertEquals("jar3", toString(jar3Url.openStream()));
229
230         //
231
// enumeration from above should still be valid, but only see the resources available at the time it was created
232
//
233

234         // resource2
235
assertTrue(enumeration.hasMoreElements());
236         assertTrue(enumeration.hasMoreElements());
237         URL JavaDoc resource2 = (URL JavaDoc) enumeration.nextElement();
238         assertNotNull(resource2);
239         assertEquals("resource2", toString(resource2.openStream()));
240         assertFalse(enumeration.hasMoreElements());
241     }
242
243     public void testDirectoryDestroy() throws Exception JavaDoc {
244         URL JavaDoc jar = new File JavaDoc(BASEDIR, "src/test/data/resourceFinderTest/jar1/").toURL();
245         UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL JavaDoc[]{jar});
246         assertDestroyed(resourceFinder, "resource1", null);
247     }
248
249     public void testJarDestroy() throws Exception JavaDoc {
250         URL JavaDoc jar = jarFile.toURL();
251         UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL JavaDoc[]{jar});
252         assertDestroyed(resourceFinder, "resource3", manifest);
253     }
254
255     public void testUrlCopy() throws Exception JavaDoc {
256         URL JavaDoc jar = jarFile.toURL();
257         UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL JavaDoc[]{jar});
258
259         // get the resource
260
URL JavaDoc resource = resourceFinder.findResource("resource");
261         assertNotNull(resource);
262         assertEquals("resource3", toString(resource.openStream()));
263
264         // copy resource with string
265
URL JavaDoc stringCopy = new URL JavaDoc(resource.toExternalForm());
266         assertEquals("resource3", toString(stringCopy.openStream()));
267
268         // copy resource perserving the url handler
269
URL JavaDoc handlerCopy = new URL JavaDoc(resource, resource.toExternalForm());
270         assertEquals("resource3", toString(handlerCopy.openStream()));
271
272         // access the other resource using the original url as a starting point
273
URL JavaDoc other = new URL JavaDoc(resource, "jar3");
274         assertEquals("jar3", toString(other.openStream()));
275     }
276
277     public void testUrlAccess() throws Exception JavaDoc {
278         URL JavaDoc jar = jarFile.toURL();
279         UrlResourceFinder resourceFinder = new UrlResourceFinder(new URL JavaDoc[]{jar});
280
281         // get geronimo url from the resource finder
282
URL JavaDoc geronimoUrl = resourceFinder.findResource("resource");
283         assertNotNull(geronimoUrl);
284         assertEquals("resource3", toString(geronimoUrl.openStream()));
285
286         // get a system url by copying the url by string
287
URL JavaDoc systemUrl = new URL JavaDoc(geronimoUrl.toExternalForm());
288         assertEquals("resource3", toString(systemUrl.openStream()));
289
290         // verify both can see the jar3 file withing the jar file
291
assertEquals("jar3", toString(new URL JavaDoc(systemUrl, "jar3").openStream()));
292         assertEquals("jar3", toString(new URL JavaDoc(geronimoUrl, "jar3").openStream()));
293
294         // verify both can see the jar3 file withing the jar file using a full url spec
295
String JavaDoc mainEntry = "jar:" + jarFile.toURL().toExternalForm() + "!/jar3";
296         assertEquals("jar3", toString(new URL JavaDoc(systemUrl, mainEntry).openStream()));
297         assertEquals("jar3", toString(new URL JavaDoc(geronimoUrl, mainEntry).openStream()));
298
299         // verify both throw a FileNotFoundExcetion for an unknown file
300
try {
301             new URL JavaDoc(systemUrl, "unknown").openStream();
302             fail("Expected a FileNotFoundException");
303         } catch (FileNotFoundException JavaDoc expected) {
304         }
305         try {
306             new URL JavaDoc(geronimoUrl, "unknown").openStream();
307             fail("Expected a FileNotFoundException");
308         } catch (FileNotFoundException JavaDoc expected) {
309         }
310
311         // verify both can see the alternate jar
312
String JavaDoc alternateEntry = "jar:" + alternateJarFile.toURL().toExternalForm() + "!/jar4";
313         assertEquals("jar4", toString(new URL JavaDoc(systemUrl, alternateEntry).openStream()));
314         assertEquals("jar4", toString(new URL JavaDoc(geronimoUrl, alternateEntry).openStream()));
315
316         // verify both throw a FileNotFoundExcetion for an unknown entry in the alternate file
317
String JavaDoc alternateUnknownEntry = "jar:" + alternateJarFile.toURL().toExternalForm() + "!/unknown";
318         try {
319             new URL JavaDoc(systemUrl, alternateUnknownEntry).openStream();
320             fail("Expected a FileNotFoundException");
321         } catch (FileNotFoundException JavaDoc expected) {
322         }
323         try {
324             new URL JavaDoc(geronimoUrl, alternateUnknownEntry).openStream();
325             fail("Expected a FileNotFoundException");
326         } catch (FileNotFoundException JavaDoc expected) {
327         }
328
329         // verify both work an excepton for a non-jar entry
330
assertEquals("testResource", toString(new URL JavaDoc(systemUrl, testResource.toURL().toExternalForm()).openStream()));
331         assertEquals("testResource", toString(new URL JavaDoc(geronimoUrl, testResource.toURL().toExternalForm()).openStream()));
332
333         // verify both fail for a spec without a !/
334
String JavaDoc badEntry = "jar:" + alternateJarFile.toURL().toExternalForm();
335         try {
336             new URL JavaDoc(systemUrl, badEntry).openStream();
337             fail("Expected a FileNotFoundException");
338         } catch (MalformedURLException JavaDoc expected) {
339         }
340         try {
341             new URL JavaDoc(geronimoUrl, badEntry).openStream();
342             fail("Expected a FileNotFoundException");
343         } catch (MalformedURLException JavaDoc expected) {
344         }
345
346         // verify both throw FileNotFoundException for a nested jar file
347
badEntry = "jar:" + alternateJarFile.toURL().toExternalForm() + "!/foo.jar!/bar";
348         try {
349             new URL JavaDoc(systemUrl, badEntry).openStream();
350             fail("Expected a FileNotFoundException");
351         } catch (FileNotFoundException JavaDoc expected) {
352         }
353         try {
354             new URL JavaDoc(geronimoUrl, badEntry).openStream();
355             fail("Expected a FileNotFoundException");
356         } catch (FileNotFoundException JavaDoc expected) {
357         }
358     }
359
360     public void assertDestroyed(UrlResourceFinder resourceFinder, String JavaDoc resourceValue, Manifest JavaDoc expectedManifest) throws Exception JavaDoc {
361         ResourceHandle resource = resourceFinder.getResource("resource");
362         assertNotNull(resource);
363         assertEquals(resourceValue, new String JavaDoc(resource.getBytes()));
364
365         // handle.getUrl()
366
URL JavaDoc url = resource.getUrl();
367         assertEquals(resourceValue, toString(url.openStream()));
368
369         // copy the url and verify we can still get the data
370
URL JavaDoc copyUrl = new URL JavaDoc(url.toExternalForm());
371         assertEquals(resourceValue, toString(copyUrl.openStream()));
372
373         // resourceFinder.findResource
374
URL JavaDoc directUrl = resourceFinder.findResource("resource");
375         assertEquals(resourceValue, toString(directUrl.openStream()));
376         URL JavaDoc directUrlCopy = new URL JavaDoc(directUrl.toExternalForm());
377         assertEquals(resourceValue, toString(directUrlCopy.openStream()));
378
379         // destroy
380
resourceFinder.destroy();
381
382         // getResource always returns null
383
assertNull(resourceFinder.getResource("resource"));
384
385         // findResource always returns null
386
assertNull(resourceFinder.findResource("resource"));
387
388         // findResources always returns an empty enumeration
389
assertFalse(resourceFinder.findResources("resource").hasMoreElements());
390
391         // existing url may not work
392
try {
393             assertEquals(resourceValue, toString(url.openStream()));
394         } catch (IllegalStateException JavaDoc expected) {
395         } catch (IOException JavaDoc expected) {
396         }
397         try {
398             assertEquals(resourceValue, toString(directUrl.openStream()));
399         } catch (IllegalStateException JavaDoc expected) {
400         } catch (IOException JavaDoc expected) {
401         }
402
403         // the copied urls will work since they are proviced by the vm
404
assertEquals(resourceValue, toString(copyUrl.openStream()));
405         assertEquals(resourceValue, toString(directUrlCopy.openStream()));
406
407         // existing resource handle may not work since the location was closed
408
assertEquals("resource", resource.getName());
409         try {
410             if (expectedManifest != null) {
411                 assertEquals(expectedManifest.getAttributes("resource"), resource.getAttributes());
412             }
413         } catch (IllegalStateException JavaDoc expected) {
414         }
415         try {
416             assertEquals(expectedManifest, resource.getManifest());
417         } catch (IllegalStateException JavaDoc expected) {
418         }
419         try {
420             assertEquals(resourceValue, toString(resource.getUrl().openStream()));
421         } catch (IllegalStateException JavaDoc expected) {
422         }
423         try {
424             assertEquals(resourceValue, toString(resource.getInputStream()));
425         } catch (IllegalStateException JavaDoc expected) {
426         } catch (IOException JavaDoc expected) {
427         }
428         try {
429             assertEquals(resourceValue, new String JavaDoc(resource.getBytes()));
430         } catch (IllegalStateException JavaDoc expected) {
431         } catch (IOException JavaDoc expected) {
432         }
433     }
434
435     protected void setUp() throws Exception JavaDoc {
436         super.setUp();
437
438         //
439
// Build a simple Jar file to test with
440
//
441
manifest = new Manifest JavaDoc();
442         Attributes JavaDoc mainAttributes = manifest.getMainAttributes();
443         mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
444         mainAttributes.putValue("food", "nacho");
445         resourceAttributes = new Attributes JavaDoc();
446         resourceAttributes.putValue("drink", "margarita");
447         manifest.getEntries().put("resource", resourceAttributes);
448
449         File JavaDoc targetDir = new File JavaDoc(BASEDIR, "target");
450         jarFile = new File JavaDoc(targetDir, "resourceFinderTest.jar");
451         JarOutputStream JavaDoc jarOutputStream = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(jarFile), manifest);
452         jarOutputStream.putNextEntry(new ZipEntry JavaDoc("resource"));
453         jarOutputStream.write("resource3".getBytes());
454         jarOutputStream.putNextEntry(new ZipEntry JavaDoc("jar3"));
455         jarOutputStream.write("jar3".getBytes());
456         IoUtil.close(jarOutputStream);
457
458         alternateJarFile = new File JavaDoc(targetDir, "alternate.jar");
459         log.debug(alternateJarFile.getAbsolutePath());
460         jarOutputStream = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(alternateJarFile), manifest);
461         jarOutputStream.putNextEntry(new ZipEntry JavaDoc("resource"));
462         jarOutputStream.write("resource4".getBytes());
463         jarOutputStream.putNextEntry(new ZipEntry JavaDoc("jar4"));
464         jarOutputStream.write("jar4".getBytes());
465         IoUtil.close(jarOutputStream);
466
467         testResource = new File JavaDoc(targetDir, "testResource");
468         FileOutputStream JavaDoc fileOutputStream = new FileOutputStream JavaDoc(testResource);
469         fileOutputStream.write("testResource".getBytes());
470         IoUtil.close(fileOutputStream);
471     }
472
473     protected void tearDown() throws Exception JavaDoc {
474         jarFile.delete();
475         super.tearDown();
476     }
477
478     private static String JavaDoc toString(InputStream JavaDoc in) throws IOException JavaDoc {
479         try {
480             byte[] bytes = IoUtil.getBytes(in);
481             String JavaDoc string = new String JavaDoc(bytes);
482             return string;
483         } finally {
484             IoUtil.close(in);
485         }
486     }
487 }
488
Popular Tags