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.nio.file.Files;
  38 import java.nio.file.Paths;
  39 
  40 import org.testng.Assert;
  41 import org.testng.annotations.AfterClass;
  42 import org.testng.annotations.BeforeClass;
  43 import org.testng.annotations.Test;
  44 
  45 public class MultiReleaseJarURLConnection {
  46     String userdir = System.getProperty("user.dir",".");
  47     String urlString = "jar:file:" + userdir + "/multi-release.jar!/version/Version.java";
  48 
  49     @BeforeClass
  50     public void initialize() throws Exception {
  51         CreateMultiReleaseTestJars creator = new CreateMultiReleaseTestJars();
  52         creator.compileEntries();
  53         creator.buildMultiReleaseJar();
  54     }
  55 
  56     @AfterClass
  57     public void close() throws IOException {
  58         Files.delete(Paths.get(userdir, "multi-release.jar"));
  59     }
  60 
  61     @Test
  62     public void testRuntimeVersioning() throws Exception {
  63         Assert.assertTrue(readAndCompare(new URL(urlString), "return 8"));
  64         Assert.assertTrue(readAndCompare(new URL(urlString + "#runtime"), "return 9"));
  65         Assert.assertTrue(readAndCompare(new URL(urlString), "return 8"));
  66     }
  67 
  68     private boolean readAndCompare(URL url, String match) throws Exception {
  69         boolean result;
  70         // necessary to do it this way, instead of openStream(), so we can
  71         // close underlying JarFile, otherwise windows can't delete the file
  72         JarURLConnection conn = (JarURLConnection)url.openConnection();
  73         try (InputStream is = conn.getInputStream()) {
  74             byte[] bytes = is.readAllBytes();
  75             result = (new String(bytes)).contains(match);
  76         }
  77         conn.getJarFile().close();
  78         return result;
  79     }
  80 }