URLClassLoaderTest.java revision 3b5fe2883109da9470a8baf20e6700aba0d78193
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 (RuntimeException e) {
508            e.printStackTrace();
509            // expected
510        }
511
512        try {
513            Class.forName("Main4", true, ucl);
514            fail("ClassNotFoundException should be thrown");
515        } catch (ClassNotFoundException e) {
516            // Expected
517        }
518
519        Support_Resources.copyFile(resources, "JarIndex", "hyts_22-new.jar");
520        urls[0] = new URL("file:/" + resPath + "/JarIndex/hyts_22-new.jar");
521        ucl = URLClassLoader.newInstance(urls, null);
522        assertNotNull("Cannot find resource", ucl.findResource("cpack/"));
523        Support_Resources.copyFile(resources, "JarIndex", "hyts_11.jar");
524        urls[0] = new URL("file:/" + resPath + "/JarIndex/hyts_31.jar");
525        ucl = URLClassLoader.newInstance(urls, null);
526
527        try {
528            Class.forName("cpack.Mock", true, ucl);
529            fail("ClassNotFoundException should be thrown");
530        } catch (ClassNotFoundException e) {
531            // Expected
532        }
533
534        // testing circular reference
535        Support_Resources.copyFile(resources, "JarIndex", "hyts_41.jar");
536        Support_Resources.copyFile(resources, "JarIndex", "hyts_42.jar");
537        urls[0] = new URL("file:/" + resPath + "/JarIndex/hyts_41.jar");
538        ucl = URLClassLoader.newInstance(urls, null);
539        en = ucl.findResources("bpack/");
540        resourcesFound = resourcesFound
541                && ((URL) en.nextElement()).equals(new URL("jar:file:/"
542                        + resPath.replace('\\', '/')
543                        + "/JarIndex/hyts_42.jar!/bpack/"));
544        assertTrue("Resources not found (2)", resourcesFound);
545        assertFalse("No more resources expected", en.hasMoreElements());
546
547        // Regression test for HARMONY-2357.
548        try {
549            URLClassLoaderExt cl = new URLClassLoaderExt(new URL[557]);
550            cl.findClass("0");
551            fail("NullPointerException should be thrown");
552        } catch (NullPointerException npe) {
553            // Expected
554        }
555
556        // Regression test for HARMONY-2871.
557        URLClassLoader cl = new URLClassLoader(new URL[] { new URL("file:/foo.jar") });
558
559        try {
560            Class.forName("foo.Foo", false, cl);
561        } catch (Exception ex) {
562            // Don't care
563        }
564
565        try {
566            Class.forName("foo.Foo", false, cl);
567            fail("NullPointerException should be thrown");
568        } catch (ClassNotFoundException cnfe) {
569            // Expected
570        }
571    }
572
573    /**
574     * @tests java.net.URLClassLoader#findResource(java.lang.String)
575     */
576    @TestTargetNew(
577        level = TestLevel.COMPLETE,
578        notes = "",
579        method = "findResource",
580        args = {java.lang.String.class}
581    )
582    @BrokenTest("web address used from support doesn't work anymore")
583    public void test_findResourceLjava_lang_String()
584            throws MalformedURLException {
585        URL res = null;
586
587        URL[] urls = new URL[2];
588        urls[0] = new URL("http://" + Support_Configuration.HomeAddress);
589        urls[1] = new URL(Support_Resources.getResourceURL("/"));
590        ucl = new URLClassLoader(urls);
591        res = ucl.findResource("RESOURCE.TXT");
592        assertNotNull("Failed to locate resource", res);
593
594        StringBuffer sb = new StringBuffer();
595        try {
596            java.io.InputStream is = res.openStream();
597
598            int c;
599            while ((c = is.read()) != -1) {
600                sb.append((char) c);
601            }
602            is.close();
603        } catch (IOException e) {
604        }
605        assertTrue("Returned incorrect resource", !sb.toString().equals(
606                "This is a test resource file"));
607    }
608
609    @TestTargets({
610        @TestTargetNew(
611            level = TestLevel.COMPLETE,
612            notes = "Checks getResource, indirectly checks findResource",
613            method = "getResource",
614            args = {java.lang.String.class}
615        ),
616        @TestTargetNew(
617            level = TestLevel.COMPLETE,
618            notes = "Checks getResource, indirectly checks findResource",
619            method = "findResource",
620            args = {java.lang.String.class}
621        )
622    })
623    public void testFindResource_H3461() throws Exception {
624        File tmpDir = new File(System.getProperty("java.io.tmpdir"));
625        File dir = new File(tmpDir, "encode#me");
626        File f, f2;
627        URLClassLoader loader;
628        URL dirUrl;
629
630        if (!dir.exists()) {
631            dir.mkdir();
632        }
633        dir.deleteOnExit();
634        dirUrl = dir.toURI().toURL();
635        loader = new URLClassLoader( new URL[] { dirUrl });
636
637        f = File.createTempFile("temp", ".dat", dir);
638        f.deleteOnExit();
639        f2 = File.createTempFile("bad#name#", ".dat", dir);
640        f2.deleteOnExit();
641
642        assertNotNull("Unable to load resource from path with problematic name",
643            loader.getResource(f.getName()));
644        assertEquals("URL was not correctly encoded",
645            f2.toURI().toURL(),
646            loader.getResource(f2.getName()));
647    }
648
649    /**
650     * @tests java.net.URLClassLoader#getResource(java.lang.String)
651     */
652    @TestTargetNew(
653        level = TestLevel.COMPLETE,
654        notes = "",
655        method = "getResource",
656        args = {java.lang.String.class}
657    )
658    public void test_getResourceLjava_lang_String()
659            throws MalformedURLException {
660        URL url1 = new URL("file:///");
661        URLClassLoader loader = new URLClassLoader(new URL[] { url1 }, null);
662        long start = System.currentTimeMillis();
663        // try without the leading /
664        URL result = loader.getResource("dir1/file1");
665        long end = System.currentTimeMillis();
666        long time = end - start;
667        if (time < 100) {
668            time = 100;
669        }
670
671        start = System.currentTimeMillis();
672        // try with the leading forward slash
673        result = loader.getResource("/dir1/file1");
674        end = System.currentTimeMillis();
675        long uncTime = end - start;
676        assertTrue("too long. UNC path formed? UNC time: " + uncTime
677                + " regular time: " + time, uncTime <= (time * 4));
678    }
679
680    /**
681     * Regression for Harmony-2237
682     */
683    @TestTargetNew(
684        level = TestLevel.PARTIAL,
685        notes = "Regression test",
686        method = "findResource",
687        args = {java.lang.String.class}
688    )
689    public void test_getResource() throws Exception {
690        URLClassLoader urlLoader = getURLClassLoader();
691        assertNull(urlLoader.findResource("XXX")); //$NON-NLS-1$
692    }
693
694    private static URLClassLoader getURLClassLoader() {
695        String classPath = System.getProperty("java.class.path");
696        StringTokenizer tok = new StringTokenizer(classPath, File.pathSeparator);
697        Vector<URL> urlVec = new Vector<URL>();
698        String resPackage = Support_Resources.RESOURCE_PACKAGE;
699        try {
700            while (tok.hasMoreTokens()) {
701                String path = tok.nextToken();
702                String url;
703                if (new File(path).isDirectory())
704                    url = "file:" + path + resPackage + "subfolder/";
705                else
706                    url = "jar:file:" + path + "!" + resPackage + "subfolder/";
707                urlVec.addElement(new URL(url));
708            }
709        } catch (MalformedURLException e) {
710            // do nothing
711        }
712        URL[] urls = new URL[urlVec.size()];
713        for (int i = 0; i < urlVec.size(); i++) {
714            urls[i] = urlVec.elementAt(i);
715        }
716        URLClassLoader loader = new URLClassLoader(urls, null);
717        return loader;
718    }
719}
720