Path: blob/master/test/jdk/java/net/JarURLConnection/TestDefaultBehavior.java
41149 views
/*1* Copyright (c) 2019, 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 822503726* @library /test/lib27* @summary Basic test for java.net.JarURLConnection default behavior28* @run testng/othervm TestDefaultBehavior29*/3031import java.io.IOException;32import java.net.MalformedURLException;33import java.net.JarURLConnection;34import java.net.URI;35import java.net.URL;36import java.net.URLConnection;37import java.nio.file.Files;38import java.nio.file.Path;39import java.util.jar.JarFile;40import jdk.test.lib.util.JarUtils;41import org.testng.annotations.BeforeTest;42import org.testng.annotations.Test;43import static org.testng.Assert.assertEquals;44import static org.testng.Assert.assertNotNull;4546public class TestDefaultBehavior {4748// Disable caching and create three jar files:49// 1. jar without a manifest50// 2. jar with a manifest51// 3. jar with manifest that includes an entry attribute52@BeforeTest53public void setup() throws Exception {54URLConnection.setDefaultUseCaches("jar", false);55URLConnection.setDefaultUseCaches("file", false);5657Path foo = Path.of("foo.txt");58Files.writeString(foo, "Hello there");5960Files.createDirectory(Path.of("META-INF"));61Path manifest = Path.of("META-INF/MANIFEST.MF");62Files.writeString(manifest, "Manifest-Version: 5.5\n");6364JarUtils.createJarFile(Path.of("test.jar"), Path.of("."), foo);65JarUtils.createJarFile(Path.of("testWithManifest.jar"), Path.of("."), manifest, foo);6667Files.writeString(manifest, "Manifest-Version: 7.7\n\n" + // main-section68"Name: foo.txt\nGreeting: true\n"); // individual-section69JarUtils.createJarFile(Path.of("testWithManifestAndAttr.jar"), Path.of("."), manifest, foo);70}7172@Test73public void noEntry() throws Exception {74URI fileURI = Path.of("test.jar").toUri();75URL jarFileURL = URI.create("jar:" + fileURI + "!/").toURL();76JarURLConnection jarURLConnection = new CustomJarURLConnection(jarFileURL);7778assertEquals(jarURLConnection.getAttributes(), null);79assertEquals(jarURLConnection.getCertificates(), null);80assertEquals(jarURLConnection.getEntryName(), null);81assertEquals(jarURLConnection.getJarEntry(), null);82assertNotNull(jarURLConnection.getJarFile());83assertEquals(jarURLConnection.getJarFileURL(), fileURI.toURL());84assertEquals(jarURLConnection.getMainAttributes(), null);85assertEquals(jarURLConnection.getManifest(), null);86}8788@Test89public void withEntry() throws Exception {90URI fileURI = Path.of("test.jar").toUri();91URL jarFileURL = URI.create("jar:" + fileURI + "!/foo.txt").toURL();92JarURLConnection jarURLConnection = new CustomJarURLConnection(jarFileURL);9394assertEquals(jarURLConnection.getAttributes(), null);95assertEquals(jarURLConnection.getCertificates(), null);96assertEquals(jarURLConnection.getEntryName(), "foo.txt");97assertEquals(jarURLConnection.getJarEntry().getName(), "foo.txt");98assertNotNull(jarURLConnection.getJarFile());99assertEquals(jarURLConnection.getJarFileURL(), fileURI.toURL());100assertEquals(jarURLConnection.getMainAttributes(), null);101assertEquals(jarURLConnection.getManifest(), null);102}103104@Test105public void manifestNoEntry() throws Exception {106URI fileURI = Path.of("testWithManifest.jar").toUri();107URL jarFileURL = URI.create("jar:" + fileURI + "!/").toURL();108JarURLConnection jarURLConnection = new CustomJarURLConnection(jarFileURL);109110assertEquals(jarURLConnection.getAttributes(), null);111assertEquals(jarURLConnection.getCertificates(), null);112assertEquals(jarURLConnection.getEntryName(), null);113assertEquals(jarURLConnection.getJarEntry(), null);114assertNotNull(jarURLConnection.getJarFile());115assertEquals(jarURLConnection.getJarFileURL(), fileURI.toURL());116assertEquals(jarURLConnection.getMainAttributes().getValue("Manifest-Version"), "5.5");117assertNotNull(jarURLConnection.getManifest());118}119120@Test121public void manifestWithEntry() throws Exception {122URI fileURI = Path.of("testWithManifest.jar").toUri();123URL jarFileURL = URI.create("jar:" + fileURI + "!/foo.txt").toURL();124JarURLConnection jarURLConnection = new CustomJarURLConnection(jarFileURL);125126assertEquals(jarURLConnection.getAttributes(), null);127assertEquals(jarURLConnection.getCertificates(), null);128assertEquals(jarURLConnection.getEntryName(), "foo.txt");129assertEquals(jarURLConnection.getJarEntry().getName(), "foo.txt");130assertNotNull(jarURLConnection.getJarFile());131assertEquals(jarURLConnection.getJarFileURL(), fileURI.toURL());132assertEquals(jarURLConnection.getMainAttributes().getValue("Manifest-Version"), "5.5");133assertNotNull(jarURLConnection.getManifest());134}135136@Test137public void manifestNoEntryAttr() throws Exception {138URI fileURI = Path.of("testWithManifestAndAttr.jar").toUri();139URL jarFileURL = URI.create("jar:" + fileURI + "!/").toURL();140JarURLConnection jarURLConnection = new CustomJarURLConnection(jarFileURL);141142assertEquals(jarURLConnection.getAttributes(), null);143assertEquals(jarURLConnection.getCertificates(), null);144assertEquals(jarURLConnection.getEntryName(), null);145assertEquals(jarURLConnection.getJarEntry(), null);146assertNotNull(jarURLConnection.getJarFile());147assertEquals(jarURLConnection.getJarFileURL(), fileURI.toURL());148assertEquals(jarURLConnection.getMainAttributes().getValue("Manifest-Version"), "7.7");149assertNotNull(jarURLConnection.getManifest());150}151152@Test153public void manifestWithEntryAttr() throws Exception {154URI fileURI = Path.of("testWithManifestAndAttr.jar").toUri();155URL jarFileURL = URI.create("jar:" + fileURI + "!/foo.txt").toURL();156JarURLConnection jarURLConnection = new CustomJarURLConnection(jarFileURL);157158assertEquals(jarURLConnection.getAttributes().getValue("Greeting"), "true");159assertEquals(jarURLConnection.getCertificates(), null);160assertEquals(jarURLConnection.getEntryName(), "foo.txt");161assertEquals(jarURLConnection.getJarEntry().getName(), "foo.txt");162assertNotNull(jarURLConnection.getJarFile());163assertEquals(jarURLConnection.getJarFileURL(), fileURI.toURL());164assertEquals(jarURLConnection.getMainAttributes().getValue("Manifest-Version"), "7.7");165assertNotNull(jarURLConnection.getManifest());166}167168// A minimal JarURLConnection169static class CustomJarURLConnection extends JarURLConnection {170private final URL jarFileURL;171private JarFile jarFile;172173CustomJarURLConnection(URL url) throws MalformedURLException {174super(url);175jarFileURL = url;176}177178@Override179public JarFile getJarFile() throws IOException {180if (jarFile == null)181connect();182return jarFile;183}184185@Override186public void connect() throws IOException {187jarFile = ((JarURLConnection)jarFileURL.openConnection()).getJarFile();188}189}190}191192193