Path: blob/master/test/jdk/sun/misc/URLClassPath/JarLoaderTest.java
41149 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Portions Copyright (c) 2013 IBM Corporation25*/2627/* @test28* @bug 718337329* @summary URLClassLoader fails to close handles to Jar files opened during30* getResource()31*/3233import java.io.*;34import java.net.*;35import java.util.zip.*;3637public class JarLoaderTest {38public static void main(String[] args) throws Exception {39// Create a JAR file40File f = new File("urlcl" + 1 + ".jar");41ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(f));4243// add a file44zos.putNextEntry(new ZipEntry("TestResource"));45byte[] b = "This is a test resource".getBytes();46zos.write(b, 0, b.length);47zos.close();4849// Load the file using cl.getResource()50URLClassLoader cl = new URLClassLoader(new URL[] { new URL("jar:" +51f.toURI().toURL() + "!/")}, null);52cl.getResource("TestResource");5354// Close the class loader - this should free up all of its Closeables,55// including the JAR file56cl.close();5758// Try to delete the JAR file59f.delete();6061// Check to see if the file was deleted62if (f.exists()) {63System.out.println(64"Test FAILED: Closeables failed to close handle to jar file");65// Delete the jar using a workaround66for (URL u : cl.getURLs()) {67if (u.getProtocol().equals("jar")) {68((JarURLConnection)u.openConnection()).getJarFile().close();69}70f.delete();71}72throw new RuntimeException("File could not be deleted");73} else {74System.out.println("Test PASSED");75}76}77}787980