1 /*
   2  * Copyright (c) 2016, 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  * @summary Test Multi-Release jar usage in runtime
  27  * @library /test/lib
  28  * @library /lib/testlibrary
  29  * @modules jdk.compiler
  30  * @build jdk.test.lib.JDKToolFinder jdk.test.lib.JDKToolLauncher
  31  *        jdk.test.lib.process.OutputAnalyzer
  32  *        jdk.test.lib.process.ProcessTools
  33  *        CompilerUtils RuntimeTest
  34  * @run testng RuntimeTest
  35  */
  36 
  37 import static org.testng.Assert.*;
  38 
  39 import java.io.BufferedReader;
  40 import java.io.File;
  41 import java.io.IOException;
  42 import java.io.InputStream;
  43 import java.io.InputStreamReader;
  44 import java.lang.reflect.InvocationTargetException;
  45 import java.lang.reflect.Method;
  46 import java.net.URL;
  47 import java.net.URLClassLoader;
  48 import java.nio.file.Files;
  49 import java.nio.file.Path;
  50 import java.nio.file.Paths;
  51 import java.nio.file.StandardCopyOption;
  52 import java.util.ArrayList;
  53 import java.util.Arrays;
  54 import java.util.List;
  55 import java.util.stream.Stream;
  56 
  57 import org.testng.annotations.BeforeClass;
  58 import org.testng.annotations.DataProvider;
  59 import org.testng.annotations.Test;
  60 
  61 import jdk.test.lib.JDKToolFinder;
  62 import jdk.test.lib.JDKToolLauncher;
  63 import jdk.test.lib.process.OutputAnalyzer;
  64 import jdk.test.lib.process.ProcessTools;
  65 
  66 public class RuntimeTest {
  67     public static final int SUCCESS = 0;
  68     private final String src = System.getProperty("test.src", ".");
  69     private final String usr = System.getProperty("user.dir", ".");
  70 
  71     @DataProvider(name = "jarFiles")
  72     Object[][] jarFiles() {
  73         return new Object[][] { { "MV_BOTH.jar", 9, 9, 9 },
  74                 { "MV_ONLY_9.jar", 9, 9, 9 },
  75                 { "NON_MV.jar", 8, 8, 8 } };
  76     }
  77 
  78     @BeforeClass
  79     protected void setUpTest() throws Throwable {
  80         compile();
  81         Path classes = Paths.get("classes");
  82         jar("cfm", "MV_BOTH.jar", "manifest.txt",
  83                 "-C", classes.resolve("base").toString(), ".",
  84                 "--release", "9", "-C", classes.resolve("v9").toString(), ".",
  85                 "--release", "10", "-C", classes.resolve("v10").toString(), ".")
  86                 .shouldHaveExitValue(0);
  87 
  88         jar("cfm", "MV_ONLY_9.jar", "manifest.txt",
  89                 "-C", classes.resolve("base").toString(), ".",
  90                 "--release", "9", "-C", classes.resolve("v9").toString(), ".")
  91                 .shouldHaveExitValue(0);
  92         jar("cfm", "NON_MV.jar", "manifest.txt",
  93                 "-C", classes.resolve("base").toString(), ".")
  94                 .shouldHaveExitValue(0);
  95     }
  96 
  97     @Test(dataProvider = "jarFiles")
  98     public void testClasspath(String jar, int mainVer, int helperVer,
  99             int resVer) throws Throwable {
 100         String[] command = { "-cp", jar, "testpackage.Main" };
 101         System.out.println("Command arguments:" + Arrays.asList(command));
 102         System.out.println();
 103         java(command).shouldHaveExitValue(SUCCESS)
 104                 .shouldContain("Main version: " + mainVer)
 105                 .shouldContain("Helpers version: " + helperVer)
 106                 .shouldContain("Resource version: " + resVer);
 107     }
 108 
 109     @Test(dataProvider = "jarFiles")
 110     void testMVJarAsLib(String jar, int mainVer, int helperVer, int resVer)
 111             throws Throwable {
 112         String[] apps = { "UseByImport", "UseByReflection" };
 113         for (String app : apps) {
 114             String[] command = {"-cp",
 115                     jar + File.pathSeparatorChar + "classes/test/", app };
 116             System.out.println("Command arguments:" + Arrays.asList(command));
 117             System.out.println();
 118             java(command).shouldHaveExitValue(SUCCESS)
 119                     .shouldContain("Main version: " + mainVer)
 120                     .shouldContain("Helpers version: " + helperVer)
 121                     .shouldContain("Resource version: " + resVer);
 122         }
 123     }
 124 
 125     @Test(dataProvider = "jarFiles")
 126     void testJavaJar(String jar, int mainVer, int helperVer, int resVer)
 127             throws Throwable {
 128         String[] command = { "-jar", jar };
 129         System.out.println("Command arguments:" + Arrays.asList(command));
 130         System.out.println();
 131         java(command).shouldHaveExitValue(SUCCESS)
 132                 .shouldContain("Main version: " + mainVer)
 133                 .shouldContain("Helpers version: " + helperVer)
 134                 .shouldContain("Resource version: " + resVer);
 135     }
 136 
 137     @Test(dataProvider = "jarFiles")
 138     void testURLClassLoader(String jarName, int mainVer, int helperVer,
 139             int resVer) throws ClassNotFoundException, NoSuchMethodException,
 140             IllegalAccessException, IllegalArgumentException,
 141             InvocationTargetException, IOException {
 142         Path pathToJAR = Paths.get(jarName).toAbsolutePath();
 143         URL jarURL1 = new URL("jar:file:" + pathToJAR + "!/");
 144         URL jarURL2 = new URL("file:///" + pathToJAR);
 145         testURLClassLoaderURL(jarURL1, mainVer, helperVer, resVer);
 146         testURLClassLoaderURL(jarURL2, mainVer, helperVer, resVer);
 147     }
 148 
 149     private static void testURLClassLoaderURL(URL jarURL,
 150             int mainVersionExpected, int helperVersionExpected,
 151             int resourceVersionExpected) throws ClassNotFoundException,
 152             NoSuchMethodException, IllegalAccessException,
 153             IllegalArgumentException, InvocationTargetException, IOException {
 154         System.out.println(
 155                 "Testing URLClassLoader MV JAR support for URL: " + jarURL);
 156         URL[] urls = { jarURL };
 157         int mainVersionActual;
 158         int helperVersionActual;
 159         int resourceVersionActual;
 160         try (URLClassLoader cl = URLClassLoader.newInstance(urls)) {
 161             Class c = cl.loadClass("testpackage.Main");
 162             Method getMainVersion = c.getMethod("getMainVersion");
 163             mainVersionActual = (int) getMainVersion.invoke(null);
 164             Method getHelperVersion = c.getMethod("getHelperVersion");
 165             helperVersionActual = (int) getHelperVersion.invoke(null);
 166             try (InputStream ris = cl.getResourceAsStream("versionResource");
 167                     BufferedReader br = new BufferedReader(
 168                             new InputStreamReader(ris))) {
 169                 resourceVersionActual = Integer.parseInt(br.readLine());
 170             }
 171         }
 172 
 173         assertEquals(mainVersionActual, mainVersionExpected,
 174                          "Test failed: Expected Main class version: "
 175                          + mainVersionExpected + " Actual version: "
 176                          + mainVersionActual);
 177         assertEquals(helperVersionActual, helperVersionExpected,
 178                          "Test failed: Expected Helper class version: "
 179                          + helperVersionExpected + " Actual version: "
 180                          + helperVersionActual);
 181         assertEquals(resourceVersionActual, resourceVersionExpected,
 182                          "Test failed: Expected resource version: "
 183                          + resourceVersionExpected + " Actual version: "
 184                          + resourceVersionActual);
 185     }
 186 
 187     @Test(dataProvider = "jarFiles")
 188     void testJjs(String jar, int mainVer, int helperVer, int resVer)
 189             throws Throwable {
 190         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jjs");
 191         launcher.addToolArg("-cp").addToolArg(jar)
 192                 .addToolArg(src + "/data/runtimetest/MVJarJJSTestScript.js");
 193         ProcessTools.executeCommand(launcher.getCommand())
 194                 .shouldHaveExitValue(SUCCESS)
 195                 .shouldContain("Main version: " + mainVer)
 196                 .shouldContain("Helpers version: " + helperVer)
 197                 .shouldContain("Resource version: " + resVer);
 198     }
 199 
 200     private static OutputAnalyzer jar(String... args) throws Throwable {
 201         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jar");
 202         Stream.of(args).forEach(launcher::addToolArg);
 203         return ProcessTools.executeCommand(launcher.getCommand());
 204     }
 205 
 206     private void compile() throws Throwable {
 207         String[] vers = { "base", "v9", "v10" };
 208         for (String ver : vers) {
 209             Path classes = Paths.get(usr, "classes", ver);
 210             Files.createDirectories(classes);
 211             Path source = Paths.get(src, "data", "runtimetest", ver);
 212             assertTrue(CompilerUtils.compile(source, classes));
 213             Files.copy(source.resolve("versionResource"),
 214                     classes.resolve("versionResource"),
 215                     StandardCopyOption.REPLACE_EXISTING);
 216         }
 217 
 218         Path classes = Paths.get(usr, "classes", "test");
 219         Files.createDirectory(classes);
 220         Path source = Paths.get(src, "data", "runtimetest", "test");
 221         assertTrue(
 222                 CompilerUtils.compile(source, classes, "-cp", "classes/base/"));
 223         Files.copy(Paths.get(src, "data", "runtimetest", "manifest.txt"),
 224                 Paths.get(usr, "manifest.txt"),
 225                 StandardCopyOption.REPLACE_EXISTING);
 226     }
 227 
 228     OutputAnalyzer java(String... args) throws Throwable {
 229         String java = JDKToolFinder.getJDKTool("java");
 230 
 231         List<String> commands = new ArrayList<>();
 232         commands.add(java);
 233         Stream.of(args).forEach(x -> commands.add(x));
 234         return ProcessTools.executeCommand(new ProcessBuilder(commands));
 235     }
 236 }