jdk/test/sun/net/www/protocol/jar/MultiReleaseJarURLConnection.java
changeset 36129 332b49163fc9
child 36236 eec5770c5b01
equal deleted inserted replaced
36128:e17994ab030a 36129:332b49163fc9
       
     1 /*
       
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 8132734
       
    27  * @summary Test that URL connections to multi-release jars can be runtime versioned
       
    28  * @library /lib/testlibrary/java/util/jar
       
    29  * @build Compiler JarBuilder CreateMultiReleaseTestJars
       
    30  * @run testng MultiReleaseJarURLConnection
       
    31  */
       
    32 
       
    33 import java.io.IOException;
       
    34 import java.io.InputStream;
       
    35 import java.net.JarURLConnection;
       
    36 import java.net.URL;
       
    37 import java.net.URLConnection;
       
    38 import java.nio.file.Files;
       
    39 import java.nio.file.Paths;
       
    40 import java.util.jar.JarFile;
       
    41 
       
    42 import org.testng.Assert;
       
    43 import org.testng.annotations.AfterClass;
       
    44 import org.testng.annotations.BeforeClass;
       
    45 import org.testng.annotations.Test;
       
    46 
       
    47 public class MultiReleaseJarURLConnection {
       
    48     String userdir = System.getProperty("user.dir",".");
       
    49     String urlFile = "jar:file:" + userdir + "/multi-release.jar!/";
       
    50     String urlEntry = urlFile + "version/Version.java";
       
    51 
       
    52     @BeforeClass
       
    53     public void initialize() throws Exception {
       
    54         CreateMultiReleaseTestJars creator = new CreateMultiReleaseTestJars();
       
    55         creator.compileEntries();
       
    56         creator.buildMultiReleaseJar();
       
    57     }
       
    58 
       
    59     @AfterClass
       
    60     public void close() throws IOException {
       
    61         Files.delete(Paths.get(userdir, "multi-release.jar"));
       
    62     }
       
    63 
       
    64     @Test
       
    65     public void testRuntimeVersioning() throws Exception {
       
    66         Assert.assertTrue(readAndCompare(new URL(urlEntry), "return 8"));
       
    67         // #runtime is "magic"
       
    68         Assert.assertTrue(readAndCompare(new URL(urlEntry + "#runtime"), "return 9"));
       
    69         // #fragment or any other fragment is not magic
       
    70         Assert.assertTrue(readAndCompare(new URL(urlEntry + "#fragment"), "return 8"));
       
    71         // cached entities not affected
       
    72         Assert.assertTrue(readAndCompare(new URL(urlEntry), "return 8"));
       
    73     }
       
    74 
       
    75     @Test
       
    76     public void testCachedJars() throws Exception {
       
    77         URL rootUrl = new URL(urlFile);
       
    78         JarURLConnection juc = (JarURLConnection)rootUrl.openConnection();
       
    79         JarFile rootJar = juc.getJarFile();
       
    80         JarFile.Release root = rootJar.getVersion();
       
    81 
       
    82         URL runtimeUrl = new URL(urlFile + "#runtime");
       
    83         juc = (JarURLConnection)runtimeUrl.openConnection();
       
    84         JarFile runtimeJar = juc.getJarFile();
       
    85         JarFile.Release runtime = runtimeJar.getVersion();
       
    86         Assert.assertNotEquals(root, runtime);
       
    87 
       
    88         juc = (JarURLConnection)rootUrl.openConnection();
       
    89         JarFile jar = juc.getJarFile();
       
    90         Assert.assertEquals(jar.getVersion(), root);
       
    91         Assert.assertEquals(jar, rootJar);
       
    92 
       
    93         juc = (JarURLConnection)runtimeUrl.openConnection();
       
    94         jar = juc.getJarFile();
       
    95         Assert.assertEquals(jar.getVersion(), runtime);
       
    96         Assert.assertEquals(jar, runtimeJar);
       
    97 
       
    98         rootJar.close();
       
    99         runtimeJar.close();
       
   100         jar.close(); // probably not needed
       
   101     }
       
   102 
       
   103     private boolean readAndCompare(URL url, String match) throws Exception {
       
   104         boolean result;
       
   105         // necessary to do it this way, instead of openStream(), so we can
       
   106         // close underlying JarFile, otherwise windows can't delete the file
       
   107         URLConnection conn = url.openConnection();
       
   108         try (InputStream is = conn.getInputStream()) {
       
   109             byte[] bytes = is.readAllBytes();
       
   110             result = (new String(bytes)).contains(match);
       
   111         }
       
   112         if (conn instanceof JarURLConnection) {
       
   113             ((JarURLConnection)conn).getJarFile().close();
       
   114         }
       
   115         return result;
       
   116     }
       
   117 }