jdk/test/tools/jar/multiRelease/RuntimeTest.java
author xiaofeya
Wed, 11 Jan 2017 08:53:59 -0800
changeset 43065 23e5bc13ec20
child 45280 49ede353bf43
permissions -rw-r--r--
8075884: check runtime usage tests with multi-release jar files Reviewed-by: psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43065
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
     1
/*
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
     4
 *
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
     7
 * published by the Free Software Foundation.
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
     8
 *
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    13
 * accompanied this code).
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    14
 *
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    18
 *
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    21
 * questions.
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    22
 */
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    23
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    24
/*
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    25
 * @test
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    26
 * @summary Test Multi-Release jar usage in runtime
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    27
 * @library /test/lib
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    28
 * @library /lib/testlibrary
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    29
 * @modules jdk.compiler
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    30
 * @build jdk.test.lib.JDKToolFinder jdk.test.lib.JDKToolLauncher
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    31
 *        jdk.test.lib.process.OutputAnalyzer
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    32
 *        jdk.test.lib.process.ProcessTools
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    33
 *        CompilerUtils RuntimeTest
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    34
 * @run testng RuntimeTest
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    35
 */
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    36
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    37
import static org.testng.Assert.*;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    38
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    39
import java.io.BufferedReader;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    40
import java.io.File;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    41
import java.io.IOException;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    42
import java.io.InputStream;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    43
import java.io.InputStreamReader;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    44
import java.lang.reflect.InvocationTargetException;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    45
import java.lang.reflect.Method;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    46
import java.net.URL;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    47
import java.net.URLClassLoader;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    48
import java.nio.file.Files;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    49
import java.nio.file.Path;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    50
import java.nio.file.Paths;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    51
import java.nio.file.StandardCopyOption;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    52
import java.util.ArrayList;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    53
import java.util.Arrays;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    54
import java.util.List;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    55
import java.util.stream.Stream;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    56
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    57
import org.testng.annotations.BeforeClass;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    58
import org.testng.annotations.DataProvider;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    59
import org.testng.annotations.Test;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    60
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    61
import jdk.test.lib.JDKToolFinder;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    62
import jdk.test.lib.JDKToolLauncher;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    63
import jdk.test.lib.process.OutputAnalyzer;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    64
import jdk.test.lib.process.ProcessTools;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    65
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    66
public class RuntimeTest {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    67
    public static final int SUCCESS = 0;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    68
    private final String src = System.getProperty("test.src", ".");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    69
    private final String usr = System.getProperty("user.dir", ".");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    70
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    71
    @DataProvider(name = "jarFiles")
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    72
    Object[][] jarFiles() {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    73
        return new Object[][] { { "MV_BOTH.jar", 9, 9, 9 },
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    74
                { "MV_ONLY_9.jar", 9, 9, 9 },
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    75
                { "NON_MV.jar", 8, 8, 8 } };
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    76
    }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    77
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    78
    @BeforeClass
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    79
    protected void setUpTest() throws Throwable {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    80
        compile();
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    81
        Path classes = Paths.get("classes");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    82
        jar("cfm", "MV_BOTH.jar", "manifest.txt",
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    83
                "-C", classes.resolve("base").toString(), ".",
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    84
                "--release", "9", "-C", classes.resolve("v9").toString(), ".",
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    85
                "--release", "10", "-C", classes.resolve("v10").toString(), ".")
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    86
                .shouldHaveExitValue(0);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    87
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    88
        jar("cfm", "MV_ONLY_9.jar", "manifest.txt",
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    89
                "-C", classes.resolve("base").toString(), ".",
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    90
                "--release", "9", "-C", classes.resolve("v9").toString(), ".")
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    91
                .shouldHaveExitValue(0);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    92
        jar("cfm", "NON_MV.jar", "manifest.txt",
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    93
                "-C", classes.resolve("base").toString(), ".")
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    94
                .shouldHaveExitValue(0);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    95
    }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    96
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    97
    @Test(dataProvider = "jarFiles")
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    98
    public void testClasspath(String jar, int mainVer, int helperVer,
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
    99
            int resVer) throws Throwable {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   100
        String[] command = { "-cp", jar, "testpackage.Main" };
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   101
        System.out.println("Command arguments:" + Arrays.asList(command));
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   102
        System.out.println();
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   103
        java(command).shouldHaveExitValue(SUCCESS)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   104
                .shouldContain("Main version: " + mainVer)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   105
                .shouldContain("Helpers version: " + helperVer)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   106
                .shouldContain("Resource version: " + resVer);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   107
    }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   108
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   109
    @Test(dataProvider = "jarFiles")
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   110
    void testMVJarAsLib(String jar, int mainVer, int helperVer, int resVer)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   111
            throws Throwable {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   112
        String[] apps = { "UseByImport", "UseByReflection" };
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   113
        for (String app : apps) {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   114
            String[] command = {"-cp",
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   115
                    jar + File.pathSeparatorChar + "classes/test/", app };
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   116
            System.out.println("Command arguments:" + Arrays.asList(command));
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   117
            System.out.println();
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   118
            java(command).shouldHaveExitValue(SUCCESS)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   119
                    .shouldContain("Main version: " + mainVer)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   120
                    .shouldContain("Helpers version: " + helperVer)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   121
                    .shouldContain("Resource version: " + resVer);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   122
        }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   123
    }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   124
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   125
    @Test(dataProvider = "jarFiles")
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   126
    void testJavaJar(String jar, int mainVer, int helperVer, int resVer)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   127
            throws Throwable {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   128
        String[] command = { "-jar", jar };
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   129
        System.out.println("Command arguments:" + Arrays.asList(command));
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   130
        System.out.println();
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   131
        java(command).shouldHaveExitValue(SUCCESS)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   132
                .shouldContain("Main version: " + mainVer)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   133
                .shouldContain("Helpers version: " + helperVer)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   134
                .shouldContain("Resource version: " + resVer);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   135
    }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   136
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   137
    @Test(dataProvider = "jarFiles")
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   138
    void testURLClassLoader(String jarName, int mainVer, int helperVer,
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   139
            int resVer) throws ClassNotFoundException, NoSuchMethodException,
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   140
            IllegalAccessException, IllegalArgumentException,
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   141
            InvocationTargetException, IOException {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   142
        Path pathToJAR = Paths.get(jarName).toAbsolutePath();
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   143
        URL jarURL1 = new URL("jar:file:" + pathToJAR + "!/");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   144
        URL jarURL2 = new URL("file:///" + pathToJAR);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   145
        testURLClassLoaderURL(jarURL1, mainVer, helperVer, resVer);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   146
        testURLClassLoaderURL(jarURL2, mainVer, helperVer, resVer);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   147
    }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   148
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   149
    private static void testURLClassLoaderURL(URL jarURL,
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   150
            int mainVersionExpected, int helperVersionExpected,
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   151
            int resourceVersionExpected) throws ClassNotFoundException,
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   152
            NoSuchMethodException, IllegalAccessException,
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   153
            IllegalArgumentException, InvocationTargetException, IOException {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   154
        System.out.println(
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   155
                "Testing URLClassLoader MV JAR support for URL: " + jarURL);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   156
        URL[] urls = { jarURL };
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   157
        int mainVersionActual;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   158
        int helperVersionActual;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   159
        int resourceVersionActual;
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   160
        try (URLClassLoader cl = URLClassLoader.newInstance(urls)) {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   161
            Class c = cl.loadClass("testpackage.Main");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   162
            Method getMainVersion = c.getMethod("getMainVersion");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   163
            mainVersionActual = (int) getMainVersion.invoke(null);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   164
            Method getHelperVersion = c.getMethod("getHelperVersion");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   165
            helperVersionActual = (int) getHelperVersion.invoke(null);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   166
            try (InputStream ris = cl.getResourceAsStream("versionResource");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   167
                    BufferedReader br = new BufferedReader(
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   168
                            new InputStreamReader(ris))) {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   169
                resourceVersionActual = Integer.parseInt(br.readLine());
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   170
            }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   171
        }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   172
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   173
        assertEquals(mainVersionActual, mainVersionExpected,
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   174
                         "Test failed: Expected Main class version: "
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   175
                         + mainVersionExpected + " Actual version: "
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   176
                         + mainVersionActual);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   177
        assertEquals(helperVersionActual, helperVersionExpected,
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   178
                         "Test failed: Expected Helper class version: "
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   179
                         + helperVersionExpected + " Actual version: "
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   180
                         + helperVersionActual);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   181
        assertEquals(resourceVersionActual, resourceVersionExpected,
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   182
                         "Test failed: Expected resource version: "
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   183
                         + resourceVersionExpected + " Actual version: "
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   184
                         + resourceVersionActual);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   185
    }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   186
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   187
    @Test(dataProvider = "jarFiles")
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   188
    void testJjs(String jar, int mainVer, int helperVer, int resVer)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   189
            throws Throwable {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   190
        JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jjs");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   191
        launcher.addToolArg("-cp").addToolArg(jar)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   192
                .addToolArg(src + "/data/runtimetest/MVJarJJSTestScript.js");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   193
        ProcessTools.executeCommand(launcher.getCommand())
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   194
                .shouldHaveExitValue(SUCCESS)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   195
                .shouldContain("Main version: " + mainVer)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   196
                .shouldContain("Helpers version: " + helperVer)
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   197
                .shouldContain("Resource version: " + resVer);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   198
    }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   199
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   200
    private static OutputAnalyzer jar(String... args) throws Throwable {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   201
        JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jar");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   202
        Stream.of(args).forEach(launcher::addToolArg);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   203
        return ProcessTools.executeCommand(launcher.getCommand());
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   204
    }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   205
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   206
    private void compile() throws Throwable {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   207
        String[] vers = { "base", "v9", "v10" };
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   208
        for (String ver : vers) {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   209
            Path classes = Paths.get(usr, "classes", ver);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   210
            Files.createDirectories(classes);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   211
            Path source = Paths.get(src, "data", "runtimetest", ver);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   212
            assertTrue(CompilerUtils.compile(source, classes));
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   213
            Files.copy(source.resolve("versionResource"),
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   214
                    classes.resolve("versionResource"),
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   215
                    StandardCopyOption.REPLACE_EXISTING);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   216
        }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   217
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   218
        Path classes = Paths.get(usr, "classes", "test");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   219
        Files.createDirectory(classes);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   220
        Path source = Paths.get(src, "data", "runtimetest", "test");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   221
        assertTrue(
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   222
                CompilerUtils.compile(source, classes, "-cp", "classes/base/"));
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   223
        Files.copy(Paths.get(src, "data", "runtimetest", "manifest.txt"),
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   224
                Paths.get(usr, "manifest.txt"),
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   225
                StandardCopyOption.REPLACE_EXISTING);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   226
    }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   227
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   228
    OutputAnalyzer java(String... args) throws Throwable {
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   229
        String java = JDKToolFinder.getJDKTool("java");
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   230
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   231
        List<String> commands = new ArrayList<>();
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   232
        commands.add(java);
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   233
        Stream.of(args).forEach(x -> commands.add(x));
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   234
        return ProcessTools.executeCommand(new ProcessBuilder(commands));
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   235
    }
23e5bc13ec20 8075884: check runtime usage tests with multi-release jar files
xiaofeya
parents:
diff changeset
   236
}