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