Path: blob/master/test/jdk/sun/net/www/protocol/jar/B6449504.java
41159 views
/*1* Copyright (c) 2006, 2017, 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* @test25* @bug 644950426* @run main/othervm B6449504 caching27* @run main/othervm B6449504 no_caching28* @summary REGRESSION: ZipException throws when try to read a XML file29*/3031import java.io.File;32import java.io.IOException;33import java.io.InputStream;34import java.net.JarURLConnection;35import java.net.URL;36import java.nio.file.Files;37import java.nio.file.Paths;38import java.nio.file.StandardCopyOption;3940public class B6449504 {4142public static void main (String[] args) throws Exception {43setup();44boolean caching = args[0].equals("caching");45String dirname = System.getProperty("test.classes");46File f = new File(dirname);47dirname = f.toURI().toString();4849String u = "jar:" + dirname + "/bar.jar";50URL url = new URL(u + "!/DoesNotExist.txt");51System.out.println("url = " + url);52JarURLConnection j1 = (JarURLConnection)url.openConnection();5354URL url2 = new URL(u + "!/test.txt");55System.out.println("url2 = " + url2);56JarURLConnection j2 = (JarURLConnection)url2.openConnection();5758j1.setUseCaches(caching);59j2.setUseCaches(caching);6061/* connecting to j2 opens the jar file but does not read it */6263j2.connect();6465try {66/* attempt to read a non-existing entry in the jar file67* shows the bug, where the jar file is closed after the68* attempt fails.69*/70InputStream is = j1.getInputStream();71} catch (IOException e) {72System.out.println("Got expected exception from j1 ");73}7475/* If bug present, this will fail because we think the jar76* is ready to be read, after the connect() above, but we77* get a ZipException because it has been closed78*/79InputStream is = j2.getInputStream();80readAndClose(is);81System.out.println("OK");82}8384static void readAndClose(InputStream is) throws IOException {85while (is.read() != -1) ;86is.close();87}8889static void setup() throws IOException {90Files.copy(Paths.get(System.getProperty("test.src"), "bar.jar"),91Paths.get(System.getProperty("test.classes"), "bar.jar"),92StandardCopyOption.REPLACE_EXISTING);93}94}959697