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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package tests.api.java.net;
20 import dalvik.annotation.BrokenTest;
21 import dalvik.annotation.SideEffect;
22 import dalvik.annotation.TestLevel;
23 import dalvik.annotation.TestTargetClass;
24 import dalvik.annotation.TestTargetNew;
25 import dalvik.annotation.TestTargets;
27 import org.apache.harmony.security.tests.support.TestCertUtils;
29 import tests.support.Support_Configuration;
30 import tests.support.Support_PortManager;
31 import tests.support.Support_TestWebData;
32 import tests.support.Support_TestWebServer;
33 import tests.support.resource.Support_Resources;
36 import java.io.FileOutputStream;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.net.MalformedURLException;
41 import java.net.URLClassLoader;
42 import java.net.URLStreamHandler;
43 import java.net.URLStreamHandlerFactory;
44 import java.security.CodeSource;
45 import java.security.Permission;
46 import java.security.PermissionCollection;
47 import java.security.cert.Certificate;
48 import java.util.ArrayList;
49 import java.util.Enumeration;
50 import java.util.List;
51 import java.util.Vector;
52 import java.util.jar.Manifest;
55 value = URLClassLoader.class,
58 level = TestLevel.NOT_NECESSARY,
59 notes = "findClass uses defineClass which is not implemented",
61 args = {java.lang.String.class}
65 public class URLClassLoaderTest extends junit.framework.TestCase {
67 class BogusClassLoader extends ClassLoader {
68 public URL getResource(String res) {
70 return new URL("http://test/BogusClassLoader");
71 } catch (MalformedURLException e) {
77 public class URLClassLoaderExt extends URLClassLoader {
79 public URLClassLoaderExt(URL[] urls) {
83 public Class<?> findClass(String cl) throws ClassNotFoundException {
84 return super.findClass(cl);
89 SecurityManager sm = new SecurityManager() {
91 public void checkPermission(Permission perm) {
94 public void checkCreateClassLoader() {
95 throw new SecurityException();
100 * @tests java.net.URLClassLoader#URLClassLoader(java.net.URL[])
103 level = TestLevel.COMPLETE,
105 method = "URLClassLoader",
106 args = {java.net.URL[].class}
108 public void test_Constructor$Ljava_net_URL() throws MalformedURLException {
109 URL[] u = new URL[0];
110 ucl = new URLClassLoader(u);
111 assertTrue("Failed to set parent", ucl != null
112 && ucl.getParent() == URLClassLoader.getSystemClassLoader());
115 URL [] urls = {new URL("http://foo.com/foo"),
116 new URL("jar:file://foo.jar!/foo.c"),
117 new URL("ftp://foo1/foo2/foo.c")};
119 URLClassLoader ucl1 = new URLClassLoader(urls);
120 assertTrue(urls.length == ucl1.getURLs().length);
123 Class.forName("test", false, ucl);
124 fail("Should throw ClassNotFoundException");
125 } catch (ClassNotFoundException e) {
129 SecurityManager oldSm = System.getSecurityManager();
130 System.setSecurityManager(sm);
132 new URLClassLoader(u);
133 fail("SecurityException should be thrown.");
134 } catch (SecurityException e) {
137 System.setSecurityManager(oldSm);
141 new URLClassLoader(new URL[] { null });
142 } catch(Exception e) {
143 fail("Unexpected exception was thrown: " + e.getMessage());
148 * @tests java.net.URLClassLoader#URLClassLoader(java.net.URL[],
149 * java.lang.ClassLoader)
152 level = TestLevel.COMPLETE,
154 method = "URLClassLoader",
155 args = {java.net.URL[].class, java.lang.ClassLoader.class}
157 public void test_Constructor$Ljava_net_URLLjava_lang_ClassLoader() {
158 ClassLoader cl = new BogusClassLoader();
159 URL[] u = new URL[0];
160 ucl = new URLClassLoader(u, cl);
161 URL res = ucl.getResource("J");
163 assertEquals("Failed to set parent", "/BogusClassLoader", res.getFile());
165 SecurityManager oldSm = System.getSecurityManager();
166 System.setSecurityManager(sm);
168 new URLClassLoader(u, cl);
169 fail("SecurityException should be thrown.");
170 } catch (SecurityException e) {
173 System.setSecurityManager(oldSm);
178 * @tests java.net.URLClassLoader#findResources(java.lang.String)
181 level = TestLevel.COMPLETE,
182 notes = "IOException checking missing. "
183 + "A test case that loads a resource from a webserver is missing.",
184 method = "findResources",
185 args = {java.lang.String.class}
187 @SideEffect("Support_TestWebServer requires isolation.")
188 public void test_findResourcesLjava_lang_String() throws Exception {
189 Enumeration<URL> res = null;
190 String[] resValues = { "This is a test resource file.",
191 "This is a resource from a subdir"};
193 String tmp = System.getProperty("java.io.tmpdir") + "/";
195 File tmpDir = new File(tmp);
196 File test1 = new File(tmp + "test0");
197 test1.deleteOnExit();
198 FileOutputStream out = new FileOutputStream(test1);
199 out.write(resValues[0].getBytes());
203 File subDir = new File(tmp + "subdir/");
205 File test2 = new File(tmp + "subdir/test0");
206 test2.deleteOnExit();
207 out = new FileOutputStream(test2);
208 out.write(resValues[1].getBytes());
212 URL[] urls = new URL[2];
213 urls[0] = new URL("file://" + tmpDir.getAbsolutePath() + "/");
214 urls[1] = new URL("file://" + subDir.getAbsolutePath() + "/");
216 ucl = new URLClassLoader(urls);
217 res = ucl.findResources("test0");
218 assertNotNull("Failed to locate resources", res);
221 while (res.hasMoreElements()) {
222 StringBuffer sb = getResContent(res.nextElement());
223 assertEquals("Returned incorrect resource/or in wrong order",
224 resValues[i++], sb.toString());
226 assertEquals("Incorrect number of resources returned", 2, i);
230 * @tests java.net.URLClassLoader#getURLs()
233 level = TestLevel.COMPLETE,
238 public void test_getURLs() throws MalformedURLException {
239 URL[] urls = new URL[4];
240 urls[0] = new URL("http://" + Support_Configuration.HomeAddress);
241 urls[1] = new URL("http://" + Support_Configuration.TestResources + "/");
242 urls[2] = new URL("ftp://" + Support_Configuration.TestResources + "/");
243 urls[3] = new URL("jar:file:c://" + Support_Configuration.TestResources
245 ucl = new URLClassLoader(urls);
246 URL[] ucUrls = ucl.getURLs();
247 for (int i = 0; i < urls.length; i++) {
248 assertEquals("Returned incorrect URL[]", urls[i], ucUrls[i]);
253 * @tests java.net.URLClassLoader#newInstance(java.net.URL[])
256 level = TestLevel.COMPLETE,
258 method = "newInstance",
259 args = {java.net.URL[].class}
261 @BrokenTest("web address used from support doesn't work anymore")
262 public void test_newInstance$Ljava_net_URL() throws MalformedURLException,
263 ClassNotFoundException {
264 // Verify that loaded class' have correct permissions
267 URL[] urls = new URL[1];
268 urls[0] = new URL(Support_Resources.getResourceURL("/UCL/UCL.jar"));
269 ucl = URLClassLoader.newInstance(urls);
270 cl = ucl.loadClass("ucl.ResClass");
272 res = cl.getClassLoader().getResource("XX.class");
273 assertNotNull("Failed to load class", cl);
275 "Loaded class unable to access resource from same codeSource",
279 urls[0] = new URL("jar:"
280 + Support_Resources.getResourceURL("/UCL/UCL.jar!/"));
281 ucl = URLClassLoader.newInstance(urls);
282 cl = ucl.loadClass("ucl.ResClass");
283 assertNotNull("Failed to load class from explicit jar URL", cl);
287 * @tests java.net.URLClassLoader#newInstance(java.net.URL[],
288 * java.lang.ClassLoader)
291 level = TestLevel.COMPLETE,
293 method = "newInstance",
294 args = {java.net.URL[].class, java.lang.ClassLoader.class}
296 public void test_newInstance$Ljava_net_URLLjava_lang_ClassLoader() {
297 ClassLoader cl = new BogusClassLoader();
298 URL[] u = new URL[0];
299 ucl = URLClassLoader.newInstance(u, cl);
300 URL res = ucl.getResource("J");
302 assertEquals("Failed to set parent", "/BogusClassLoader", res.getFile());
306 * @tests java.net.URLClassLoader#URLClassLoader(java.net.URL[],
307 * java.lang.ClassLoader, java.net.URLStreamHandlerFactory)
310 level = TestLevel.COMPLETE,
312 method = "URLClassLoader",
313 args = {java.net.URL[].class, java.lang.ClassLoader.class,
314 java.net.URLStreamHandlerFactory.class}
316 public void test_Constructor$Ljava_net_URLLjava_lang_ClassLoaderLjava_net_URLStreamHandlerFactory() {
317 class TestFactory implements URLStreamHandlerFactory {
318 public URLStreamHandler createURLStreamHandler(String protocol) {
322 ClassLoader cl = new BogusClassLoader();
323 URL[] u = new URL[0];
324 ucl = new URLClassLoader(u, cl, new TestFactory());
325 URL res = ucl.getResource("J");
327 assertEquals("Failed to set parent", "/BogusClassLoader", res.getFile());
329 SecurityManager oldSm = System.getSecurityManager();
330 System.setSecurityManager(sm);
332 new URLClassLoader(u, cl, new TestFactory());
333 fail("SecurityException should be thrown.");
334 } catch (SecurityException e) {
337 System.setSecurityManager(oldSm);
342 level = TestLevel.COMPLETE,
347 public void test_addURLLjava_net_URL() throws MalformedURLException {
348 URL[] u = new URL[0];
350 URL [] urls = {new URL("http://foo.com/foo"),
351 new URL("jar:file://foo.jar!/foo.c"),
352 new URL("ftp://foo1/foo2/foo.c"), null};
354 TestURLClassLoader tucl = new TestURLClassLoader(u);
356 for(int i = 0; i < urls.length;) {
357 tucl.addURL(urls[i]);
359 URL [] result = tucl.getURLs();
360 assertEquals("Result array length is incorrect: " + i,
362 for(int j = 0; j < result.length; j++) {
363 assertEquals("Result array item is incorrect: " + j,
370 level = TestLevel.SUFFICIENT,
372 method = "getPermissions",
373 args = { CodeSource.class }
375 public void test_getPermissions() throws MalformedURLException {
376 URL url = new URL("http://" + Support_Configuration.SpecialInetTestAddress);
377 Certificate[] chain = TestCertUtils.getCertChain();
378 CodeSource cs = new CodeSource(url, chain);
379 TestURLClassLoader cl = new TestURLClassLoader(new URL[] {url});
380 PermissionCollection permCol = cl.getPermissions(cs);
381 assertNotNull(permCol);
383 URL url1 = new URL("file://foo/foo.c");
384 TestURLClassLoader cl1 = new TestURLClassLoader(new URL[] {url});
385 CodeSource cs1 = new CodeSource(url1, chain);
386 PermissionCollection permCol1 = cl1.getPermissions(cs1);
387 assertNotNull(permCol1);
391 level = TestLevel.COMPLETE,
393 method = "definePackage",
394 args = { java.lang.String.class, java.util.jar.Manifest.class,
397 public void test_definePackage() throws MalformedURLException {
398 Manifest manifest = new Manifest();
399 URL[] u = new URL[0];
400 TestURLClassLoader tucl = new TestURLClassLoader(u);
402 URL [] urls = {new URL("http://foo.com/foo"),
403 new URL("jar:file://foo.jar!/foo.c"),
404 new URL("ftp://foo1/foo2/foo.c"),
405 new URL("file://new/package/name/"),
408 String packageName = "new.package.name";
410 for(int i = 0; i < urls.length; i++) {
411 Package pack = tucl.definePackage(packageName + i, manifest, urls[i]);
412 assertEquals(packageName + i, pack.getName());
413 assertNull("Implementation Title is not null",
414 pack.getImplementationTitle());
415 assertNull("Implementation Vendor is not null",
416 pack.getImplementationVendor());
417 assertNull("Implementation Version is not null.",
418 pack.getImplementationVersion());
422 tucl.definePackage(packageName + "0", manifest, null);
423 fail("IllegalArgumentException was not thrown.");
424 } catch(IllegalArgumentException iae) {
429 class TestURLClassLoader extends URLClassLoader {
430 public TestURLClassLoader(URL[] urls) {
434 public void addURL(URL url) {
438 public Package definePackage(String name,
441 throws IllegalArgumentException {
442 return super.definePackage(name, man, url);
445 public Class<?> findClass(String name)
446 throws ClassNotFoundException {
447 return super.findClass(name);
450 protected PermissionCollection getPermissions(CodeSource codesource) {
451 return super.getPermissions(codesource);
456 * @tests java.net.URLClassLoader#findResource(java.lang.String)
459 level = TestLevel.PARTIAL_COMPLETE,
461 method = "findResource",
462 args = {java.lang.String.class}
464 @SideEffect("Support_TestWebServer requires isolation.")
465 public void test_findResourceLjava_lang_String() throws Exception {
466 int port = Support_PortManager.getNextPort();
467 File tmp = File.createTempFile("test", ".txt");
469 Support_TestWebServer server = new Support_TestWebServer();
472 server.initServer(port, tmp.getAbsolutePath(), "text/html");
474 URL[] urls = { new URL("http://localhost:" + port + "/") };
475 ucl = new URLClassLoader(urls);
476 URL res = ucl.findResource("test1");
477 assertNotNull("Failed to locate resource", res);
479 StringBuffer sb = getResContent(res);
480 assertEquals("Returned incorrect resource", new String(Support_TestWebData.test1),
489 level = TestLevel.PARTIAL,
490 notes = "Checks getResource, indirectly checks findResource",
491 clazz = ClassLoader.class,
492 method = "getResource",
493 args = {java.lang.String.class}
496 level = TestLevel.PARTIAL_COMPLETE,
497 notes = "Checks getResource, indirectly checks findResource",
498 method = "findResource",
499 args = {java.lang.String.class}
502 public void testFindResource_H3461() throws Exception {
503 File tmpDir = new File(System.getProperty("java.io.tmpdir"));
504 File dir = new File(tmpDir, "encode#me");
506 URLClassLoader loader;
513 dirUrl = dir.toURI().toURL();
514 loader = new URLClassLoader( new URL[] { dirUrl });
516 f = File.createTempFile("temp", ".dat", dir);
518 f2 = File.createTempFile("bad#name#", ".dat", dir);
521 assertNotNull("Unable to load resource from path with problematic name",
522 loader.getResource(f.getName()));
523 assertEquals("URL was not correctly encoded",
525 loader.getResource(f2.getName()));
529 * @tests java.net.URLClassLoader#getResource(java.lang.String)
532 level = TestLevel.COMPLETE,
534 method = "getResource",
535 args = {java.lang.String.class}
537 public void test_getResourceLjava_lang_String()
538 throws MalformedURLException {
539 URL url1 = new URL("file:///");
540 URLClassLoader loader = new URLClassLoader(new URL[] { url1 });
541 long start = System.currentTimeMillis();
542 // try without the leading /
543 URL result = loader.getResource("dir1/file1");
544 long end = System.currentTimeMillis();
545 long time = end - start;
550 start = System.currentTimeMillis();
551 // try with the leading forward slash
552 result = loader.getResource("/dir1/file1");
553 end = System.currentTimeMillis();
554 long uncTime = end - start;
555 assertTrue("too long. UNC path formed? UNC time: " + uncTime
556 + " regular time: " + time, uncTime <= (time * 4));
560 * Regression for Harmony-2237
563 level = TestLevel.PARTIAL_COMPLETE,
564 notes = "Regression test",
565 method = "findResource",
566 args = {java.lang.String.class}
568 @SideEffect("Support_TestWebServer requires isolation.")
569 public void test_findResource_String() throws Exception {
570 File tempFile1 = File.createTempFile("textFile", ".txt");
571 tempFile1.createNewFile();
572 tempFile1.deleteOnExit();
573 File tempFile2 = File.createTempFile("jarFile", ".jar");
575 tempFile2.deleteOnExit();
577 Support_TestWebServer server = new Support_TestWebServer();
578 int port = Support_PortManager.getNextPort();
580 server.initServer(port, false);
582 String tempPath1 = tempFile1.getParentFile().getAbsolutePath() + "/";
583 InputStream is = getClass().getResourceAsStream(
584 "/tests/resources/hyts_patch.jar");
585 Support_Resources.copyLocalFileto(tempFile2, is);
586 String tempPath2 = tempFile2.getAbsolutePath();
587 String tempPath3 = "http://localhost:" + port + "/";
588 URLClassLoader urlLoader = getURLClassLoader(tempPath1, tempPath2);
589 assertNull("Found inexistant resource",
590 urlLoader.findResource("XXX")); //$NON-NLS-1$
591 assertNotNull("Couldn't find resource from directory",
592 urlLoader.findResource(tempFile1.getName())); //$NON-NLS-1$
593 assertNotNull("Couldn't find resource from jar",
594 urlLoader.findResource("Blah.txt")); //$NON-NLS-1$
595 urlLoader = getURLClassLoader(tempPath1, tempPath2, tempPath3);
596 assertNotNull("Couldn't find resource from web",
597 urlLoader.findResource("test1")); //$NON-NLS-1$
598 assertNull("Found inexistant resource from web",
599 urlLoader.findResource("test3")); //$NON-NLS-1$
605 private static URLClassLoader getURLClassLoader(String... classPath)
606 throws MalformedURLException {
607 List<URL> urlList = new ArrayList<URL>();
608 for (String path : classPath) {
610 File f = new File(path);
611 if (f.isDirectory()) {
612 url = "file:" + path;
613 } else if (path.startsWith("http")) {
616 url = "jar:file:" + path + "!/";
618 urlList.add(new URL(url));
620 return new URLClassLoader(urlList.toArray(new URL[urlList.size()]));
623 private StringBuffer getResContent(URL res) throws IOException {
624 StringBuffer sb = new StringBuffer();
625 InputStream is = res.openStream();
628 while ((c = is.read()) != -1) {