OldURLClassLoaderTest.java revision 52b939cbcc060c6bf71b65f2588e9837a86f7c5f
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 libcore.java.net; 19 20import dalvik.annotation.SideEffect; 21import java.io.File; 22import java.io.FileOutputStream; 23import java.io.IOException; 24import java.io.InputStream; 25import java.net.MalformedURLException; 26import java.net.URL; 27import java.net.URLClassLoader; 28import java.security.CodeSource; 29import java.security.Permission; 30import java.security.PermissionCollection; 31import java.security.cert.Certificate; 32import java.util.ArrayList; 33import java.util.Enumeration; 34import java.util.List; 35import java.util.jar.Manifest; 36import org.apache.harmony.security.tests.support.TestCertUtils; 37import tests.support.Support_Configuration; 38import tests.support.Support_PortManager; 39import tests.support.Support_TestWebData; 40import tests.support.Support_TestWebServer; 41import tests.support.resource.Support_Resources; 42 43public class OldURLClassLoaderTest extends junit.framework.TestCase { 44 45 URLClassLoader ucl; 46 SecurityManager sm = new SecurityManager() { 47 48 public void checkPermission(Permission perm) { 49 } 50 51 public void checkCreateClassLoader() { 52 throw new SecurityException(); 53 } 54 }; 55 56 /** 57 * @tests java.net.URLClassLoader#URLClassLoader(java.net.URL[]) 58 */ 59 public void test_Constructor$Ljava_net_URL() throws MalformedURLException { 60 URL[] u = new URL[0]; 61 ucl = new URLClassLoader(u); 62 assertTrue("Failed to set parent", ucl != null 63 && ucl.getParent() == URLClassLoader.getSystemClassLoader()); 64 65 66 URL [] urls = {new URL("http://foo.com/foo"), 67 new URL("jar:file://foo.jar!/foo.c"), 68 new URL("ftp://foo1/foo2/foo.c")}; 69 70 URLClassLoader ucl1 = new URLClassLoader(urls); 71 assertTrue(urls.length == ucl1.getURLs().length); 72 73 try { 74 Class.forName("test", false, ucl); 75 fail("Should throw ClassNotFoundException"); 76 } catch (ClassNotFoundException e) { 77 // expected 78 } 79 80 try { 81 new URLClassLoader(new URL[] { null }); 82 } catch(Exception e) { 83 fail("Unexpected exception was thrown: " + e.getMessage()); 84 } 85 } 86 87 /** 88 * @tests java.net.URLClassLoader#findResources(java.lang.String) 89 */ 90 public void test_findResourcesLjava_lang_String() throws Exception { 91 Enumeration<URL> res = null; 92 String[] resValues = { "This is a test resource file.", 93 "This is a resource from a subdir"}; 94 95 String tmp = System.getProperty("java.io.tmpdir") + "/"; 96 97 File tmpDir = new File(tmp); 98 File test1 = new File(tmp + "test0"); 99 test1.deleteOnExit(); 100 FileOutputStream out = new FileOutputStream(test1); 101 out.write(resValues[0].getBytes()); 102 out.flush(); 103 out.close(); 104 105 File subDir = new File(tmp + "subdir/"); 106 subDir.mkdir(); 107 File test2 = new File(tmp + "subdir/test0"); 108 test2.deleteOnExit(); 109 out = new FileOutputStream(test2); 110 out.write(resValues[1].getBytes()); 111 out.flush(); 112 out.close(); 113 114 URL[] urls = new URL[2]; 115 urls[0] = new URL("file://" + tmpDir.getAbsolutePath() + "/"); 116 urls[1] = new URL("file://" + subDir.getAbsolutePath() + "/"); 117 118 ucl = new URLClassLoader(urls); 119 res = ucl.findResources("test0"); 120 assertNotNull("Failed to locate resources", res); 121 122 int i = 0; 123 while (res.hasMoreElements()) { 124 StringBuffer sb = getResContent(res.nextElement()); 125 assertEquals("Returned incorrect resource/or in wrong order", 126 resValues[i++], sb.toString()); 127 } 128 assertEquals("Incorrect number of resources returned", 2, i); 129 } 130 131 public void test_addURLLjava_net_URL() throws MalformedURLException { 132 URL[] u = new URL[0]; 133 134 URL [] urls = {new URL("http://foo.com/foo"), 135 new URL("jar:file://foo.jar!/foo.c"), 136 new URL("ftp://foo1/foo2/foo.c"), null}; 137 138 TestURLClassLoader tucl = new TestURLClassLoader(u); 139 140 for(int i = 0; i < urls.length;) { 141 tucl.addURL(urls[i]); 142 i++; 143 URL [] result = tucl.getURLs(); 144 assertEquals("Result array length is incorrect: " + i, 145 i, result.length); 146 for(int j = 0; j < result.length; j++) { 147 assertEquals("Result array item is incorrect: " + j, 148 urls[j], result[j]); 149 } 150 } 151 } 152 153 public void test_getPermissions() throws MalformedURLException { 154 URL url = new URL("http://" + Support_Configuration.SpecialInetTestAddress); 155 Certificate[] chain = TestCertUtils.getCertChain(); 156 CodeSource cs = new CodeSource(url, chain); 157 TestURLClassLoader cl = new TestURLClassLoader(new URL[] {url}); 158 PermissionCollection permCol = cl.getPermissions(cs); 159 assertNotNull(permCol); 160 161 URL url1 = new URL("file://foo/foo.c"); 162 TestURLClassLoader cl1 = new TestURLClassLoader(new URL[] {url}); 163 CodeSource cs1 = new CodeSource(url1, chain); 164 PermissionCollection permCol1 = cl1.getPermissions(cs1); 165 assertNotNull(permCol1); 166 } 167 168 public void test_definePackage() throws MalformedURLException { 169 Manifest manifest = new Manifest(); 170 URL[] u = new URL[0]; 171 TestURLClassLoader tucl = new TestURLClassLoader(u); 172 173 URL [] urls = {new URL("http://foo.com/foo"), 174 new URL("jar:file://foo.jar!/foo.c"), 175 new URL("ftp://foo1/foo2/foo.c"), 176 new URL("file://new/package/name/"), 177 null}; 178 179 String packageName = "new.package.name"; 180 181 for(int i = 0; i < urls.length; i++) { 182 Package pack = tucl.definePackage(packageName + i, manifest, urls[i]); 183 assertEquals(packageName + i, pack.getName()); 184 assertNull("Implementation Title is not null", 185 pack.getImplementationTitle()); 186 assertNull("Implementation Vendor is not null", 187 pack.getImplementationVendor()); 188 assertNull("Implementation Version is not null.", 189 pack.getImplementationVersion()); 190 } 191 192 try { 193 tucl.definePackage(packageName + "0", manifest, null); 194 fail("IllegalArgumentException was not thrown."); 195 } catch(IllegalArgumentException iae) { 196 //expected 197 } 198 } 199 200 class TestURLClassLoader extends URLClassLoader { 201 public TestURLClassLoader(URL[] urls) { 202 super(urls); 203 } 204 205 public void addURL(URL url) { 206 super.addURL(url); 207 } 208 209 public Package definePackage(String name, 210 Manifest man, 211 URL url) 212 throws IllegalArgumentException { 213 return super.definePackage(name, man, url); 214 } 215 216 public Class<?> findClass(String name) 217 throws ClassNotFoundException { 218 return super.findClass(name); 219 } 220 221 protected PermissionCollection getPermissions(CodeSource codesource) { 222 return super.getPermissions(codesource); 223 } 224 } 225 226 @SideEffect("Support_TestWebServer requires isolation.") 227 public void test_findResourceLjava_lang_String() throws Exception { 228 int port = Support_PortManager.getNextPort(); 229 File tmp = File.createTempFile("test", ".txt"); 230 231 Support_TestWebServer server = new Support_TestWebServer(); 232 try { 233 234 server.initServer(port, tmp.getAbsolutePath(), "text/html"); 235 236 URL[] urls = { new URL("http://localhost:" + port + "/") }; 237 ucl = new URLClassLoader(urls); 238 URL res = ucl.findResource("test1"); 239 assertNotNull("Failed to locate resource", res); 240 241 StringBuffer sb = getResContent(res); 242 assertEquals("Returned incorrect resource", new String(Support_TestWebData.test1), 243 sb.toString()); 244 } finally { 245 server.close(); 246 } 247 } 248 249 /** 250 * Regression for Harmony-2237 251 */ 252 @SideEffect("Support_TestWebServer requires isolation.") 253 public void test_findResource_String() throws Exception { 254 File tempFile1 = File.createTempFile("textFile", ".txt"); 255 tempFile1.createNewFile(); 256 tempFile1.deleteOnExit(); 257 File tempFile2 = File.createTempFile("jarFile", ".jar"); 258 tempFile2.delete(); 259 tempFile2.deleteOnExit(); 260 261 Support_TestWebServer server = new Support_TestWebServer(); 262 int port = Support_PortManager.getNextPort(); 263 try { 264 server.initServer(port, false); 265 266 String tempPath1 = tempFile1.getParentFile().getAbsolutePath() + "/"; 267 InputStream is = getClass().getResourceAsStream( 268 "/tests/resources/hyts_patch.jar"); 269 Support_Resources.copyLocalFileto(tempFile2, is); 270 String tempPath2 = tempFile2.getAbsolutePath(); 271 String tempPath3 = "http://localhost:" + port + "/"; 272 URLClassLoader urlLoader = getURLClassLoader(tempPath1, tempPath2); 273 assertNull("Found inexistant resource", 274 urlLoader.findResource("XXX")); 275 assertNotNull("Couldn't find resource from directory", 276 urlLoader.findResource(tempFile1.getName())); 277 assertNotNull("Couldn't find resource from jar", 278 urlLoader.findResource("Blah.txt")); 279 urlLoader = getURLClassLoader(tempPath1, tempPath2, tempPath3); 280 assertNotNull("Couldn't find resource from web", 281 urlLoader.findResource("test1")); 282 assertNull("Found inexistant resource from web", 283 urlLoader.findResource("test3")); 284 } finally { 285 server.close(); 286 } 287 } 288 289 private static URLClassLoader getURLClassLoader(String... classPath) 290 throws MalformedURLException { 291 List<URL> urlList = new ArrayList<URL>(); 292 for (String path : classPath) { 293 String url; 294 File f = new File(path); 295 if (f.isDirectory()) { 296 url = "file:" + path; 297 } else if (path.startsWith("http")) { 298 url = path; 299 } else { 300 url = "jar:file:" + path + "!/"; 301 } 302 urlList.add(new URL(url)); 303 } 304 return new URLClassLoader(urlList.toArray(new URL[urlList.size()])); 305 } 306 307 private StringBuffer getResContent(URL res) throws IOException { 308 StringBuffer sb = new StringBuffer(); 309 InputStream is = res.openStream(); 310 311 int c; 312 while ((c = is.read()) != -1) { 313 sb.append((char) c); 314 } 315 is.close(); 316 return sb; 317 } 318} 319