Path: blob/master/test/jdk/tools/jlink/multireleasejar/JLinkMRJavaBaseVersionTest.java
41152 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 817747126* @summary jlink should use the version from java.base.jmod to find modules27* @bug 818513028* @summary jlink should throw error if target image and current JDK versions don't match29* @modules java.base/jdk.internal.module30* @library /test/lib31* @build jdk.test.lib.process.* CheckRuntimeVersion32* @run testng/othervm JLinkMRJavaBaseVersionTest33*/3435import java.io.ByteArrayInputStream;36import java.io.File;37import java.io.IOException;38import java.lang.module.ModuleFinder;39import java.lang.module.ModuleReference;40import java.nio.file.Files;41import java.nio.file.Path;42import java.nio.file.Paths;43import java.util.List;44import java.util.jar.JarFile;45import java.util.spi.ToolProvider;46import java.util.stream.Collectors;47import java.util.stream.Stream;4849import jdk.internal.module.ModulePath;50import jdk.test.lib.process.ProcessTools;51import org.testng.Assert;52import org.testng.annotations.BeforeClass;53import org.testng.annotations.Test;5455public class JLinkMRJavaBaseVersionTest {56private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")57.orElseThrow(() -> new RuntimeException("jar tool not found"));58private static final ToolProvider JAVAC_TOOL = ToolProvider.findFirst("javac")59.orElseThrow(() -> new RuntimeException("javac tool not found"));60private static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")61.orElseThrow(() -> new RuntimeException("jlink tool not found"));6263private static final Path javaHome = Paths.get(System.getProperty("java.home"));6465// resource text for version 966private byte[] resource9 = ("9 resource file").getBytes();67// resource text for current version68private byte[] resource = (Runtime.version().major() + " resource file").getBytes();6970static Path getJmods() {71Path jmods = Paths.get(System.getProperty("java9.home", javaHome.toString())).resolve("jmods");72if (Files.notExists(jmods)) {73throw new RuntimeException(jmods + " not found");74}75return jmods;76}7778@BeforeClass79public void initialize() throws IOException {80Path srcdir = Paths.get(System.getProperty("test.src"));8182// create class files from source83Path base = srcdir.resolve("base");84Path mr9 = Paths.get("mr9");85javac(base, mr9, base.toString());8687// current version88Path rt = srcdir.resolve("rt");89Path rtmods = Paths.get("rtmods");90javac(rt, rtmods, rt.toString());9192// build multi-release jar file with different module-info.class93String[] args = {94"-cf", "m1.jar",95"-C", rtmods.resolve("m1").toString(), ".",96"--release ", "9",97"-C", mr9.resolve("m1").toString(), "."9899};100JAR_TOOL.run(System.out, System.err, args);101}102103private void javac(Path source, Path destination, String srcpath) throws IOException {104String[] args = Stream.concat(105Stream.of("-d", destination.toString(), "--release", "9",106"--module-source-path", srcpath),107Files.walk(source)108.map(Path::toString)109.filter(s -> s.endsWith(".java"))110).toArray(String[]::new);111int rc = JAVAC_TOOL.run(System.out, System.err, args);112Assert.assertEquals(rc, 0);113}114115@Test116public void basicTest() throws Throwable {117if (Files.notExists(javaHome.resolve("lib").resolve("modules"))) {118// exploded image119return;120}121122Runtime.Version version = targetRuntimeVersion();123System.out.println("Testing jlink with " + getJmods() + " of target version " + version);124125// use jlink to build image from multi-release jar126if (jlink("m1.jar", "myimage")) {127return;128}129130// validate runtime image131Path java = Paths.get("myimage", "bin", "java");132ProcessTools.executeProcess(java.toString(), "-m", "m1/p.Main");133134// validate the image linked with the proper MR version135136if (!version.equalsIgnoreOptional(Runtime.version())) {137ProcessTools.executeProcess(java.toString(), "-cp", System.getProperty("test.classes"),138"CheckRuntimeVersion", String.valueOf(version.major()),139"java.base", "m1")140.shouldHaveExitValue(0);141}142}143144private Runtime.Version targetRuntimeVersion() {145ModuleReference mref = ModulePath.of(Runtime.version(), true, getJmods())146.find("java.base")147.orElseThrow(() -> new RuntimeException("java.base not found from " + getJmods()));148149return Runtime.Version.parse(mref.descriptor().version().get().toString());150}151152private boolean jlink(String jar, String image) {153List<String> args = List.of("--output", image,154"--add-modules", "m1",155"--module-path",156getJmods().toString() + File.pathSeparator + jar);157System.out.println("jlink " + args.stream().collect(Collectors.joining(" ")));158int exitCode = JLINK_TOOL.run(System.out, System.err, args.toArray(new String[0]));159boolean isJDK9 = System.getProperty("java9.home") != null;160if (isJDK9) {161Assert.assertNotEquals(exitCode, 0);162} else {163Assert.assertEquals(exitCode, 0);164}165return isJDK9;166}167}168169170