1 /*
   2  * Copyright (c) 2015, 2019, 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 8194070
  27  * @summary Test the System properties for JarFile that support multi-release jar files
  28  * @library /lib/testlibrary/java/util/jar /test/lib
  29  * @modules jdk.jartool
  30  *          jdk.compiler
  31  *          jdk.httpserver
  32  * @build Compiler JarBuilder CreateMultiReleaseTestJars SimpleHttpServer
  33  * @run testng MultiReleaseJarHttpProperties
  34  * @run testng/othervm -Djdk.util.jar.version=0   MultiReleaseJarHttpProperties
  35  * @run testng/othervm -Djdk.util.jar.version=8   MultiReleaseJarHttpProperties
  36  * @run testng/othervm -Djdk.util.jar.version=9   MultiReleaseJarHttpProperties
  37  * @run testng/othervm -Djdk.util.jar.version=100 MultiReleaseJarHttpProperties
  38  * @run testng/othervm -Djdk.util.jar.version=8   -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarHttpProperties
  39  * @run testng/othervm -Djdk.util.jar.version=9   -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarHttpProperties
  40  * @run testng/othervm -Djdk.util.jar.version=8   -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarHttpProperties
  41  * @run testng/othervm -Djdk.util.jar.version=9   -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarHttpProperties
  42  * @run testng/othervm -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarHttpProperties
  43  * @run testng/othervm -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarHttpProperties
  44  */
  45 
  46 import java.io.IOException;
  47 import java.net.InetAddress;
  48 import java.net.URL;
  49 import java.net.URLClassLoader;
  50 
  51 import jdk.test.lib.net.URIBuilder;
  52 
  53 import org.testng.Assert;
  54 import org.testng.annotations.AfterClass;
  55 import org.testng.annotations.BeforeClass;
  56 import org.testng.annotations.Test;
  57 
  58 public class MultiReleaseJarHttpProperties extends MultiReleaseJarProperties {
  59     private SimpleHttpServer server;
  60 
  61     @BeforeClass
  62     public void initialize() throws Exception {
  63         server = new SimpleHttpServer(InetAddress.getLoopbackAddress());
  64         server.start();
  65         super.initialize();
  66     }
  67 
  68     @Override
  69     protected void initializeClassLoader() throws Exception {
  70         URL[] urls = new URL[]{
  71                 URIBuilder.newBuilder().scheme("http").port(server.getPort()).loopback()
  72                         .path("/multi-release.jar").toURL(),
  73         };
  74         cldr = new URLClassLoader(urls);
  75         // load any class, Main is convenient and in the root entries
  76         rootClass = cldr.loadClass("version.Main");
  77     }
  78 
  79     @AfterClass
  80     public void close() throws IOException {
  81         // Windows requires server to stop before file is deleted
  82         if (server != null)
  83             server.stop();
  84         super.close();
  85     }
  86 
  87     /*
  88      * jdk.util.jar.enableMultiRelease=force is a no-op for URLClassLoader
  89      */
  90 
  91     @Test
  92     public void testURLClassLoader() throws Throwable {
  93         Class<?> vcls = cldr.loadClass("version.Version");
  94         invokeMethod(vcls, rtVersion);
  95     }
  96 
  97     @Test
  98     public void testGetResourceAsStream() throws Exception {
  99         String resource = rtVersion == 9 ? "/version/PackagePrivate.java" : "/version/Version.java";
 100         // use rootClass as a base for getting resources
 101         getResourceAsStream(rootClass, resource);
 102     }
 103 
 104     @Test
 105     public void testGetResource() throws Exception {
 106         String resource = rtVersion == 9 ? "/version/PackagePrivate.java" : "/version/Version.java";
 107         // use rootClass as a base for getting resources
 108         getResource(rootClass, resource);
 109     }
 110 }