1 /*
   2  * Copyright (c) 2015, 2018, 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
  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=10  MultiReleaseJarHttpProperties
  38  * @run testng/othervm -Djdk.util.jar.version=100 MultiReleaseJarHttpProperties
  39  * @run testng/othervm -Djdk.util.jar.version=8   -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarHttpProperties
  40  * @run testng/othervm -Djdk.util.jar.version=9   -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarHttpProperties
  41  * @run testng/othervm -Djdk.util.jar.version=10  -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarHttpProperties
  42  * @run testng/othervm -Djdk.util.jar.version=8   -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarHttpProperties
  43  * @run testng/othervm -Djdk.util.jar.version=9   -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarHttpProperties
  44  * @run testng/othervm -Djdk.util.jar.version=10  -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarHttpProperties
  45  * @run testng/othervm -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarHttpProperties
  46  * @run testng/othervm -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarHttpProperties
  47  */
  48 
  49 import java.io.IOException;
  50 import java.net.URL;
  51 import java.net.URLClassLoader;
  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();
  64         server.start();
  65         super.initialize();
  66     }
  67 
  68     @Override
  69     protected void initializeClassLoader() throws Exception {
  70         URL[] urls = new URL[]{
  71                 new URL("http://localhost:" + server.getPort() + "/multi-release.jar")
  72         };
  73         cldr = new URLClassLoader(urls);
  74         // load any class, Main is convenient and in the root entries
  75         rootClass = cldr.loadClass("version.Main");
  76     }
  77 
  78     @AfterClass
  79     public void close() throws IOException {
  80         // Windows requires server to stop before file is deleted
  81         if (server != null)
  82             server.stop();
  83         super.close();
  84     }
  85 
  86     /*
  87      * jdk.util.jar.enableMultiRelease=force is a no-op for URLClassLoader
  88      */
  89 
  90     @Test
  91     public void testURLClassLoader() throws Throwable {
  92         Class<?> vcls = cldr.loadClass("version.Version");
  93         invokeMethod(vcls, rtVersion);
  94     }
  95 
  96     @Test
  97     public void testGetResourceAsStream() throws Exception {
  98         String resource = rtVersion == 9 ? "/version/PackagePrivate.java" : "/version/Version.java";
  99         // use rootClass as a base for getting resources
 100         getResourceAsStream(rootClass, resource);
 101     }
 102 
 103     @Test
 104     public void testGetResource() throws Exception {
 105         String resource = rtVersion == 9 ? "/version/PackagePrivate.java" : "/version/Version.java";
 106         // use rootClass as a base for getting resources
 107         getResource(rootClass, resource);
 108     }
 109 }