URLClassLoaderTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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
18package tests.api.java.net;
19
20import dalvik.annotation.BrokenTest;
21import dalvik.annotation.TestTargetClass;
22import dalvik.annotation.TestTargets;
23import dalvik.annotation.TestLevel;
24import dalvik.annotation.TestTargetNew;
25
26import java.io.File;
27import java.io.FilePermission;
28import java.io.IOException;
29import java.io.InputStream;
30import java.net.DatagramSocket;
31import java.net.MalformedURLException;
32import java.net.SocketException;
33import java.net.URI;
34import java.net.URL;
35import java.net.URLClassLoader;
36import java.net.URLStreamHandler;
37import java.net.URLStreamHandlerFactory;
38import java.security.CodeSource;
39import java.security.Permission;
40import java.security.PermissionCollection;
41import java.security.Permissions;
42import java.security.cert.Certificate;
43import java.util.Enumeration;
44import java.util.NoSuchElementException;
45import java.util.StringTokenizer;
46import java.util.Vector;
47import java.util.jar.Manifest;
48
49import org.apache.harmony.luni.util.InvalidJarIndexException;
50import org.apache.harmony.security.tests.support.TestCertUtils;
51
52import tests.support.Support_Configuration;
53import tests.support.resource.Support_Resources;
54
55@TestTargetClass(URLClassLoader.class)
56public class URLClassLoaderTest extends junit.framework.TestCase {
57
58    class BogusClassLoader extends ClassLoader {
59        public URL getResource(String res) {
60            try {
61                return new URL("http://test/BogusClassLoader");
62            } catch (MalformedURLException e) {
63                return null;
64            }
65        }
66    }
67
68    public class URLClassLoaderExt extends URLClassLoader {
69
70        public URLClassLoaderExt(URL[] urls) {
71            super(urls);
72        }
73
74        public Class<?> findClass(String cl) throws ClassNotFoundException {
75            return super.findClass(cl);
76        }
77    }
78
79    URLClassLoader ucl;
80    SecurityManager sm = new SecurityManager() {
81
82        public void checkPermission(Permission perm) {
83        }
84
85        public void checkCreateClassLoader() {
86            throw new SecurityException();
87        }
88    };
89
90    /**
91     * @tests java.net.URLClassLoader#URLClassLoader(java.net.URL[])
92     */
93    @TestTargetNew(
94        level = TestLevel.COMPLETE,
95        notes = "",
96        method = "URLClassLoader",
97        args = {java.net.URL[].class}
98    )
99    public void test_Constructor$Ljava_net_URL() throws MalformedURLException {
100        URL[] u = new URL[0];
101        ucl = new URLClassLoader(u);
102        assertTrue("Failed to set parent", ucl != null
103                && ucl.getParent() == URLClassLoader.getSystemClassLoader());
104
105
106        URL [] urls = {new URL("http://foo.com/foo"),
107                       new URL("jar:file://foo.jar!/foo.c"),
108                       new URL("ftp://foo1/foo2/foo.c")};
109
110        URLClassLoader ucl1 = new URLClassLoader(urls);
111        assertTrue(urls.length == ucl1.getURLs().length);
112
113        try {
114            Class.forName("test", false, ucl);
115            fail("Should throw ClassNotFoundException");
116        } catch (ClassNotFoundException e) {
117            // expected
118        }
119
120        SecurityManager oldSm = System.getSecurityManager();
121        System.setSecurityManager(sm);
122        try {
123            new URLClassLoader(u);
124            fail("SecurityException should be thrown.");
125        } catch (SecurityException e) {
126            // expected
127        } finally {
128            System.setSecurityManager(oldSm);
129        }
130
131        try {
132            new URLClassLoader(new URL[] { null });
133        } catch(Exception e) {
134            fail("Unexpected exception was thrown: " + e.getMessage());
135        }
136    }
137
138    /**
139     * @tests java.net.URLClassLoader#URLClassLoader(java.net.URL[],
140     *        java.lang.ClassLoader)
141     */
142    @TestTargetNew(
143        level = TestLevel.COMPLETE,
144        notes = "",
145        method = "URLClassLoader",
146        args = {java.net.URL[].class, java.lang.ClassLoader.class}
147    )
148    public void test_Constructor$Ljava_net_URLLjava_lang_ClassLoader() {
149        ClassLoader cl = new BogusClassLoader();
150        URL[] u = new URL[0];
151        ucl = new URLClassLoader(u, cl);
152        URL res = ucl.getResource("J");
153        assertNotNull(res);
154        assertEquals("Failed to set parent", "/BogusClassLoader", res.getFile());
155
156        SecurityManager oldSm = System.getSecurityManager();
157        System.setSecurityManager(sm);
158        try {
159            new URLClassLoader(u, cl);
160            fail("SecurityException should be thrown.");
161        } catch (SecurityException e) {
162            // expected
163        } finally {
164            System.setSecurityManager(oldSm);
165        }
166    }
167
168    /**
169     * @tests java.net.URLClassLoader#findResources(java.lang.String)
170     */
171    @TestTargetNew(
172        level = TestLevel.SUFFICIENT,
173        notes = "IOException checking missed.",
174        method = "findResources",
175        args = {java.lang.String.class}
176    )
177    @BrokenTest("web address used from support doesn't work anymore")
178    public void test_findResourcesLjava_lang_String() throws IOException {
179        Enumeration res = null;
180        String[] resValues = { "This is a test resource file.",
181                "This is a resource from a subdir" };
182
183        URL[] urls = new URL[2];
184        urls[0] = new URL(Support_Resources.getResourceURL("/"));
185        urls[1] = new URL(Support_Resources.getResourceURL("/subdir1/"));
186        ucl = new URLClassLoader(urls);
187        res = ucl.findResources("RESOURCE.TXT");
188        assertNotNull("Failed to locate resources", res);
189
190        int i = 0;
191        while (res.hasMoreElements()) {
192            StringBuffer sb = new StringBuffer();
193            InputStream is = ((URL) res.nextElement()).openStream();
194            int c;
195            while ((c = is.read()) != -1) {
196                sb.append((char) c);
197            }
198            assertEquals("Returned incorrect resource/or in wrong order",
199                    resValues[i++], sb.toString());
200        }
201        assertTrue("Incorrect number of resources returned: " + i, i == 2);
202    }
203
204    /**
205     * @tests java.net.URLClassLoader#getURLs()
206     */
207    @TestTargetNew(
208        level = TestLevel.COMPLETE,
209        notes = "",
210        method = "getURLs",
211        args = {}
212    )
213    public void test_getURLs() throws MalformedURLException {
214        URL[] urls = new URL[4];
215        urls[0] = new URL("http://" + Support_Configuration.HomeAddress);
216        urls[1] = new URL("http://" + Support_Configuration.TestResources + "/");
217        urls[2] = new URL("ftp://" + Support_Configuration.TestResources + "/");
218        urls[3] = new URL("jar:file:c://" + Support_Configuration.TestResources
219                + "!/");
220        ucl = new URLClassLoader(urls);
221        URL[] ucUrls = ucl.getURLs();
222        for (int i = 0; i < urls.length; i++) {
223            assertEquals("Returned incorrect URL[]", urls[i], ucUrls[i]);
224        }
225    }
226
227    /**
228     * @tests java.net.URLClassLoader#newInstance(java.net.URL[])
229     */
230    @TestTargetNew(
231        level = TestLevel.COMPLETE,
232        notes = "",
233        method = "newInstance",
234        args = {java.net.URL[].class}
235    )
236    @BrokenTest("web address used from support doesn't work anymore")
237    public void test_newInstance$Ljava_net_URL() throws MalformedURLException,
238            ClassNotFoundException {
239        // Verify that loaded class' have correct permissions
240        Class cl = null;
241        URL res = null;
242        URL[] urls = new URL[1];
243        urls[0] = new URL(Support_Resources.getResourceURL("/UCL/UCL.jar"));
244        ucl = URLClassLoader.newInstance(urls);
245        cl = ucl.loadClass("ucl.ResClass");
246
247        res = cl.getClassLoader().getResource("XX.class");
248        assertNotNull("Failed to load class", cl);
249        assertNotNull(
250                "Loaded class unable to access resource from same codeSource",
251                res);
252        cl = null;
253
254        urls[0] = new URL("jar:"
255                + Support_Resources.getResourceURL("/UCL/UCL.jar!/"));
256        ucl = URLClassLoader.newInstance(urls);
257        cl = ucl.loadClass("ucl.ResClass");
258        assertNotNull("Failed to load class from explicit jar URL", cl);
259    }
260
261    /**
262     * @tests java.net.URLClassLoader#newInstance(java.net.URL[],
263     *        java.lang.ClassLoader)
264     */
265    @TestTargetNew(
266        level = TestLevel.COMPLETE,
267        notes = "",
268        method = "newInstance",
269        args = {java.net.URL[].class, java.lang.ClassLoader.class}
270    )
271    public void test_newInstance$Ljava_net_URLLjava_lang_ClassLoader() {
272        ClassLoader cl = new BogusClassLoader();
273        URL[] u = new URL[0];
274        ucl = URLClassLoader.newInstance(u, cl);
275        URL res = ucl.getResource("J");
276        assertNotNull(res);
277        assertEquals("Failed to set parent", "/BogusClassLoader", res.getFile());
278    }
279
280    /**
281     * @tests java.net.URLClassLoader#URLClassLoader(java.net.URL[],
282     *        java.lang.ClassLoader, java.net.URLStreamHandlerFactory)
283     */
284    @TestTargetNew(
285        level = TestLevel.COMPLETE,
286        notes = "",
287        method = "URLClassLoader",
288        args = {java.net.URL[].class, java.lang.ClassLoader.class,
289                java.net.URLStreamHandlerFactory.class}
290    )
291    public void test_Constructor$Ljava_net_URLLjava_lang_ClassLoaderLjava_net_URLStreamHandlerFactory() {
292        class TestFactory implements URLStreamHandlerFactory {
293            public URLStreamHandler createURLStreamHandler(String protocol) {
294                return null;
295            }
296        }
297        ClassLoader cl = new BogusClassLoader();
298        URL[] u = new URL[0];
299        ucl = new URLClassLoader(u, cl, new TestFactory());
300        URL res = ucl.getResource("J");
301        assertNotNull(res);
302        assertEquals("Failed to set parent", "/BogusClassLoader", res.getFile());
303
304        SecurityManager oldSm = System.getSecurityManager();
305        System.setSecurityManager(sm);
306        try {
307            new URLClassLoader(u, cl, new TestFactory());
308            fail("SecurityException should be thrown.");
309        } catch (SecurityException e) {
310            // expected
311        } finally {
312            System.setSecurityManager(oldSm);
313        }
314    }
315
316    @TestTargetNew(
317        level = TestLevel.COMPLETE,
318        notes = "",
319        method = "addURL",
320        args = { URL.class }
321    )
322    public void test_addURLLjava_net_URL() throws MalformedURLException {
323        URL[] u = new URL[0];
324
325        URL [] urls = {new URL("http://foo.com/foo"),
326                       new URL("jar:file://foo.jar!/foo.c"),
327                       new URL("ftp://foo1/foo2/foo.c"), null};
328
329        TestURLClassLoader tucl = new TestURLClassLoader(u);
330
331        for(int i = 0; i < urls.length;) {
332            tucl.addURL(urls[i]);
333            i++;
334            URL [] result = tucl.getURLs();
335            assertEquals("Result array length is incorrect: " + i,
336                                                            i, result.length);
337            for(int j = 0; j < result.length; j++) {
338                assertEquals("Result array item is incorrect: " + j,
339                                                            urls[j], result[j]);
340            }
341        }
342    }
343
344    @TestTargetNew(
345        level = TestLevel.SUFFICIENT,
346        notes = "",
347        method = "getPermissions",
348        args = { CodeSource.class }
349    )
350    public void test_getPermissions() throws MalformedURLException {
351        URL url = new URL("http://" + Support_Configuration.SpecialInetTestAddress);
352        Certificate[] chain = TestCertUtils.getCertChain();
353        CodeSource cs = new CodeSource(url, chain);
354        TestURLClassLoader cl = new TestURLClassLoader(new URL[] {url});
355        PermissionCollection permCol = cl.getPermissions(cs);
356        assertNotNull(permCol);
357
358        URL url1 = new URL("file://foo/foo.c");
359        TestURLClassLoader cl1 = new TestURLClassLoader(new URL[] {url});
360        CodeSource cs1 = new CodeSource(url1, chain);
361        PermissionCollection permCol1 = cl1.getPermissions(cs1);
362        assertNotNull(permCol1);
363    }
364
365    @TestTargetNew(
366        level = TestLevel.COMPLETE,
367        notes = "",
368        method = "definePackage",
369        args = { java.lang.String.class, java.util.jar.Manifest.class,
370                 java.net.URL.class }
371    )
372    public void test_definePackage() throws MalformedURLException {
373        Manifest manifest = new Manifest();
374        URL[] u = new URL[0];
375        TestURLClassLoader tucl = new TestURLClassLoader(u);
376
377        URL [] urls = {new URL("http://foo.com/foo"),
378                new URL("jar:file://foo.jar!/foo.c"),
379                new URL("ftp://foo1/foo2/foo.c"),
380                new URL("file://new/package/name/"),
381                null};
382
383        String packageName = "new.package.name";
384
385        for(int i = 0; i < urls.length; i++) {
386            Package pack = tucl.definePackage(packageName + i, manifest, urls[i]);
387            assertEquals(packageName + i, pack.getName());
388            assertNull("Implementation Title is not null",
389                    pack.getImplementationTitle());
390            assertNull("Implementation Vendor is not null",
391                    pack.getImplementationVendor());
392            assertNull("Implementation Version is not null.",
393                    pack.getImplementationVersion());
394        }
395
396        try {
397            tucl.definePackage(packageName + "0", manifest, null);
398            fail("IllegalArgumentException was not thrown.");
399        } catch(IllegalArgumentException iae) {
400            //expected
401        }
402    }
403
404    class TestURLClassLoader extends URLClassLoader {
405        public TestURLClassLoader(URL[] urls) {
406            super(urls);
407        }
408
409        public void addURL(URL url) {
410            super.addURL(url);
411        }
412
413        public Package definePackage(String name,
414                                     Manifest man,
415                                     URL url)
416                                     throws IllegalArgumentException {
417            return super.definePackage(name, man, url);
418        }
419
420        public Class<?> findClass(String name)
421                                        throws ClassNotFoundException {
422            return super.findClass(name);
423        }
424
425        protected PermissionCollection getPermissions(CodeSource codesource) {
426            return super.getPermissions(codesource);
427        }
428    }
429
430    /**
431     * @throws ClassNotFoundException
432     * @throws IOException
433     * @tests java.net.URLClassLoader#findClass(java.lang.String)
434     */
435    @TestTargetNew(
436        level = TestLevel.COMPLETE,
437        notes = "",
438        method = "findClass",
439        args = {java.lang.String.class}
440    )
441    @BrokenTest("")
442    public void test_findClassLjava_lang_String()
443            throws ClassNotFoundException, IOException {
444        File resources = Support_Resources.createTempFolder();
445        String resPath = resources.toString();
446        if (resPath.charAt(0) == '/' || resPath.charAt(0) == '\\') {
447            resPath = resPath.substring(1);
448        }
449
450        java.net.URL[] urls = new java.net.URL[1];
451        java.net.URLClassLoader ucl = null;
452        boolean classFound;
453        boolean exception;
454        boolean goodException;
455        Enumeration en;
456        boolean resourcesFound;
457        Support_Resources.copyFile(resources, "JarIndex", "hyts_11.jar");
458        Support_Resources.copyFile(resources, "JarIndex", "hyts_12.jar");
459        Support_Resources.copyFile(resources, "JarIndex", "hyts_13.jar");
460        Support_Resources.copyFile(resources, "JarIndex", "hyts_14.jar");
461        urls[0] = new URL("file:/" + resPath + "/JarIndex/hyts_11.jar");
462        ucl = URLClassLoader.newInstance(urls, null);
463        URL resURL = ucl.findResource("Test.txt");
464        URL reference = new URL("jar:file:/" + resPath.replace('\\', '/')
465                + "/JarIndex/hyts_14.jar!/Test.txt");
466        assertTrue("Resource not found: " + resURL + " ref: " + reference,
467                resURL.equals(reference));
468
469        Class c = Class.forName("cpack.CNothing", true, ucl);
470        assertNotNull(c);
471
472        Support_Resources.copyFile(resources, "JarIndex", "hyts_21.jar");
473        Support_Resources.copyFile(resources, "JarIndex", "hyts_22.jar");
474        Support_Resources.copyFile(resources, "JarIndex", "hyts_23.jar");
475        urls[0] = new URL("file:/" + resPath + "/JarIndex/hyts_21.jar");
476        ucl = URLClassLoader.newInstance(urls, null);
477        en = ucl.findResources("bpack/");
478
479        try {
480            resourcesFound = true;
481            URL url1 = (URL) en.nextElement();
482            URL url2 = (URL) en.nextElement();
483            System.out.println(url1);
484            System.out.println(url2);
485            resourcesFound = resourcesFound
486                    && url1.equals(new URL("jar:file:/"
487                            + resPath.replace('\\', '/')
488                            + "/JarIndex/hyts_22.jar!/bpack/"));
489            resourcesFound = resourcesFound
490                    && url2.equals(new URL("jar:file:/"
491                            + resPath.replace('\\', '/')
492                            + "/JarIndex/hyts_23.jar!/bpack/"));
493            if (en.hasMoreElements()) {
494                resourcesFound = false;
495            }
496        } catch (NoSuchElementException e) {
497            resourcesFound = false;
498        }
499        assertTrue("Resources not found (1)", resourcesFound);
500
501        Class c2 = Class.forName("bpack.Homer", true, ucl);
502        assertNotNull(c2);
503
504        try {
505            Class.forName("bpack.Bart", true, ucl);
506            fail("InvalidJarIndexException should be thrown");
507        } catch (InvalidJarIndexException e) {
508            // expected
509        }
510
511        try {
512            Class.forName("Main4", true, ucl);
513            fail("ClassNotFoundException should be thrown");
514        } catch (ClassNotFoundException e) {
515            // Expected
516        }
517
518        Support_Resources.copyFile(resources, "JarIndex", "hyts_22-new.jar");
519        urls[0] = new URL("file:/" + resPath + "/JarIndex/hyts_22-new.jar");
520        ucl = URLClassLoader.newInstance(urls, null);
521        assertNotNull("Cannot find resource", ucl.findResource("cpack/"));
522        Support_Resources.copyFile(resources, "JarIndex", "hyts_11.jar");
523        urls[0] = new URL("file:/" + resPath + "/JarIndex/hyts_31.jar");
524        ucl = URLClassLoader.newInstance(urls, null);
525
526        try {
527            Class.forName("cpack.Mock", true, ucl);
528            fail("ClassNotFoundException should be thrown");
529        } catch (ClassNotFoundException e) {
530            // Expected
531        }
532
533        // testing circular reference
534        Support_Resources.copyFile(resources, "JarIndex", "hyts_41.jar");
535        Support_Resources.copyFile(resources, "JarIndex", "hyts_42.jar");
536        urls[0] = new URL("file:/" + resPath + "/JarIndex/hyts_41.jar");
537        ucl = URLClassLoader.newInstance(urls, null);
538        en = ucl.findResources("bpack/");
539        resourcesFound = resourcesFound
540                && ((URL) en.nextElement()).equals(new URL("jar:file:/"
541                        + resPath.replace('\\', '/')
542                        + "/JarIndex/hyts_42.jar!/bpack/"));
543        assertTrue("Resources not found (2)", resourcesFound);
544        assertFalse("No more resources expected", en.hasMoreElements());
545
546        // Regression test for HARMONY-2357.
547        try {
548            URLClassLoaderExt cl = new URLClassLoaderExt(new URL[557]);
549            cl.findClass("0");
550            fail("NullPointerException should be thrown");
551        } catch (NullPointerException npe) {
552            // Expected
553        }
554
555        // Regression test for HARMONY-2871.
556        URLClassLoader cl = new URLClassLoader(new URL[] { new URL("file:/foo.jar") });
557
558        try {
559            Class.forName("foo.Foo", false, cl);
560        } catch (Exception ex) {
561            // Don't care
562        }
563
564        try {
565            Class.forName("foo.Foo", false, cl);
566            fail("NullPointerException should be thrown");
567        } catch (ClassNotFoundException cnfe) {
568            // Expected
569        }
570    }
571
572    /**
573     * @tests java.net.URLClassLoader#findResource(java.lang.String)
574     */
575    @TestTargetNew(
576        level = TestLevel.COMPLETE,
577        notes = "",
578        method = "findResource",
579        args = {java.lang.String.class}
580    )
581    @BrokenTest("web address used from support doesn't work anymore")
582    public void test_findResourceLjava_lang_String()
583            throws MalformedURLException {
584        URL res = null;
585
586        URL[] urls = new URL[2];
587        urls[0] = new URL("http://" + Support_Configuration.HomeAddress);
588        urls[1] = new URL(Support_Resources.getResourceURL("/"));
589        ucl = new URLClassLoader(urls);
590        res = ucl.findResource("RESOURCE.TXT");
591        assertNotNull("Failed to locate resource", res);
592
593        StringBuffer sb = new StringBuffer();
594        try {
595            java.io.InputStream is = res.openStream();
596
597            int c;
598            while ((c = is.read()) != -1) {
599                sb.append((char) c);
600            }
601            is.close();
602        } catch (IOException e) {
603        }
604        assertTrue("Returned incorrect resource", !sb.toString().equals(
605                "This is a test resource file"));
606    }
607
608    @TestTargets({
609        @TestTargetNew(
610            level = TestLevel.COMPLETE,
611            notes = "Checks getResource, indirectly checks findResource",
612            method = "getResource",
613            args = {java.lang.String.class}
614        ),
615        @TestTargetNew(
616            level = TestLevel.COMPLETE,
617            notes = "Checks getResource, indirectly checks findResource",
618            method = "findResource",
619            args = {java.lang.String.class}
620        )
621    })
622    public void testFindResource_H3461() throws Exception {
623        File tmpDir = new File(System.getProperty("java.io.tmpdir"));
624        File dir = new File(tmpDir, "encode#me");
625        File f, f2;
626        URLClassLoader loader;
627        URL dirUrl;
628
629        if (!dir.exists()) {
630            dir.mkdir();
631        }
632        dir.deleteOnExit();
633        dirUrl = dir.toURI().toURL();
634        loader = new URLClassLoader( new URL[] { dirUrl });
635
636        f = File.createTempFile("temp", ".dat", dir);
637        f.deleteOnExit();
638        f2 = File.createTempFile("bad#name#", ".dat", dir);
639        f2.deleteOnExit();
640
641        assertNotNull("Unable to load resource from path with problematic name",
642            loader.getResource(f.getName()));
643        assertEquals("URL was not correctly encoded",
644            f2.toURI().toURL(),
645            loader.getResource(f2.getName()));
646    }
647
648    /**
649     * @tests java.net.URLClassLoader#getResource(java.lang.String)
650     */
651    @TestTargetNew(
652        level = TestLevel.COMPLETE,
653        notes = "",
654        method = "getResource",
655        args = {java.lang.String.class}
656    )
657    public void test_getResourceLjava_lang_String()
658            throws MalformedURLException {
659        URL url1 = new URL("file:///");
660        URLClassLoader loader = new URLClassLoader(new URL[] { url1 }, null);
661        long start = System.currentTimeMillis();
662        // try without the leading /
663        URL result = loader.getResource("dir1/file1");
664        long end = System.currentTimeMillis();
665        long time = end - start;
666        if (time < 100) {
667            time = 100;
668        }
669
670        start = System.currentTimeMillis();
671        // try with the leading forward slash
672        result = loader.getResource("/dir1/file1");
673        end = System.currentTimeMillis();
674        long uncTime = end - start;
675        assertTrue("too long. UNC path formed? UNC time: " + uncTime
676                + " regular time: " + time, uncTime <= (time * 4));
677    }
678
679    /**
680     * Regression for Harmony-2237
681     */
682    @TestTargetNew(
683        level = TestLevel.PARTIAL,
684        notes = "Regression test",
685        method = "findResource",
686        args = {java.lang.String.class}
687    )
688    public void test_getResource() throws Exception {
689        URLClassLoader urlLoader = getURLClassLoader();
690        assertNull(urlLoader.findResource("XXX")); //$NON-NLS-1$
691    }
692
693    private static URLClassLoader getURLClassLoader() {
694        String classPath = System.getProperty("java.class.path");
695        StringTokenizer tok = new StringTokenizer(classPath, File.pathSeparator);
696        Vector<URL> urlVec = new Vector<URL>();
697        String resPackage = Support_Resources.RESOURCE_PACKAGE;
698        try {
699            while (tok.hasMoreTokens()) {
700                String path = tok.nextToken();
701                String url;
702                if (new File(path).isDirectory())
703                    url = "file:" + path + resPackage + "subfolder/";
704                else
705                    url = "jar:file:" + path + "!" + resPackage + "subfolder/";
706                urlVec.addElement(new URL(url));
707            }
708        } catch (MalformedURLException e) {
709            // do nothing
710        }
711        URL[] urls = new URL[urlVec.size()];
712        for (int i = 0; i < urlVec.size(); i++) {
713            urls[i] = urlVec.elementAt(i);
714        }
715        URLClassLoader loader = new URLClassLoader(urls, null);
716        return loader;
717    }
718}
719