jdk/test/java/util/jar/JarFile/MultiReleaseJarHttpProperties.java
changeset 36129 332b49163fc9
child 37785 daf6dcc73e9d
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 the System properties for JarFile that support multi-release jar files
       
    28  * @library /lib/testlibrary/java/util/jar
       
    29  * @build Compiler JarBuilder CreateMultiReleaseTestJars
       
    30  * @run testng MultiReleaseJarHttpProperties
       
    31  * @run testng/othervm -Djdk.util.jar.version=0 MultiReleaseJarHttpProperties
       
    32  * @run testng/othervm -Djdk.util.jar.version=8 MultiReleaseJarHttpProperties
       
    33  * @run testng/othervm -Djdk.util.jar.version=9 MultiReleaseJarHttpProperties
       
    34  * @run testng/othervm -Djdk.util.jar.version=10 MultiReleaseJarHttpProperties
       
    35  * @run testng/othervm -Djdk.util.jar.version=100 MultiReleaseJarHttpProperties
       
    36  * @run testng/othervm -Djdk.util.jar.version=8 -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarHttpProperties
       
    37  * @run testng/othervm -Djdk.util.jar.version=9 -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarHttpProperties
       
    38  * @run testng/othervm -Djdk.util.jar.version=10 -Djdk.util.jar.enableMultiRelease=false MultiReleaseJarHttpProperties
       
    39  * @run testng/othervm -Djdk.util.jar.version=8 -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarHttpProperties
       
    40  * @run testng/othervm -Djdk.util.jar.version=9 -Djdk.util.jar.enableMultiRelease=force MultiReleaseJarHttpProperties
       
    41  * @run testng/othervm -Djdk.util.jar.version=10 -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 com.sun.net.httpserver.*;
       
    47 
       
    48 import java.io.IOException;
       
    49 import java.io.InputStream;
       
    50 import java.io.OutputStream;
       
    51 import java.net.InetSocketAddress;
       
    52 import java.net.URL;
       
    53 import java.net.URLClassLoader;
       
    54 import java.nio.file.Files;
       
    55 import java.nio.file.Path;
       
    56 import java.nio.file.Paths;
       
    57 
       
    58 import org.testng.Assert;
       
    59 import org.testng.annotations.AfterClass;
       
    60 import org.testng.annotations.BeforeClass;
       
    61 import org.testng.annotations.Test;
       
    62 
       
    63 public class MultiReleaseJarHttpProperties extends MultiReleaseJarProperties {
       
    64     private SimpleHttpServer server;
       
    65 
       
    66     @BeforeClass
       
    67     public void initialize() throws Exception {
       
    68         server = new SimpleHttpServer();
       
    69         server.start();
       
    70         super.initialize();
       
    71     }
       
    72 
       
    73     @Override
       
    74     protected void initializeClassLoader() throws Exception {
       
    75         URL[] urls = new URL[]{
       
    76                 new URL("http://localhost:" + server.getPort() + "/multi-release-jar")
       
    77         };
       
    78         cldr = new URLClassLoader(urls);
       
    79         // load any class, Main is convenient and in the root entries
       
    80         rootClass = cldr.loadClass("version.Main");
       
    81     }
       
    82 
       
    83     @AfterClass
       
    84     public void close() throws IOException {
       
    85         // Windows requires server to stop before file is deleted
       
    86         if (server != null)
       
    87             server.stop();
       
    88         super.close();
       
    89     }
       
    90 
       
    91     /*
       
    92      * jdk.util.jar.enableMultiRelease=force is a no-op for URLClassLoader
       
    93      */
       
    94 
       
    95     @Test
       
    96     public void testURLClassLoader() throws Throwable {
       
    97         Class<?> vcls = cldr.loadClass("version.Version");
       
    98         invokeMethod(vcls, rtVersion);
       
    99     }
       
   100 
       
   101     @Test
       
   102     public void testGetResourceAsStream() throws Exception {
       
   103         String resource = rtVersion == 9 ? "/version/PackagePrivate.java" : "/version/Version.java";
       
   104         // use rootClass as a base for getting resources
       
   105         getResourceAsStream(rootClass, resource);
       
   106     }
       
   107 
       
   108     @Test
       
   109     public void testGetResource() throws Exception {
       
   110         String resource = rtVersion == 9 ? "/version/PackagePrivate.java" : "/version/Version.java";
       
   111         // use rootClass as a base for getting resources
       
   112         getResource(rootClass, resource);
       
   113     }
       
   114 }
       
   115 
       
   116 /**
       
   117  * Extremely simple server that only performs one task.  The server listens for
       
   118  * requests on the ephemeral port.  If it sees a request that begins with
       
   119  * "/multi-release-jar", it consumes the request and returns a stream of bytes
       
   120  * representing the jar file multi-release.jar found in "userdir".
       
   121  */
       
   122 class SimpleHttpServer {
       
   123     private static final String userdir = System.getProperty("user.dir", ".");
       
   124     private static final Path multirelease = Paths.get(userdir, "multi-release.jar");
       
   125 
       
   126     private final HttpServer server;
       
   127 
       
   128     public SimpleHttpServer() throws IOException {
       
   129         server = HttpServer.create();
       
   130     }
       
   131 
       
   132     public void start() throws IOException {
       
   133         server.bind(new InetSocketAddress(0), 0);
       
   134         server.createContext("/multi-release-jar", t -> {
       
   135             try (InputStream is = t.getRequestBody()) {
       
   136                 is.readAllBytes();  // probably not necessary to consume request
       
   137                 byte[] bytes = Files.readAllBytes(multirelease);
       
   138                 t.sendResponseHeaders(200, bytes.length);
       
   139                 try (OutputStream os = t.getResponseBody()) {
       
   140                     os.write(bytes);
       
   141                 }
       
   142             }
       
   143         });
       
   144         server.setExecutor(null); // creates a default executor
       
   145         server.start();
       
   146     }
       
   147 
       
   148     public void stop() {
       
   149         server.stop(0);
       
   150     }
       
   151 
       
   152     int getPort() {
       
   153         return server.getAddress().getPort();
       
   154     }
       
   155 }
       
   156