Path: blob/master/test/jdk/tools/jlink/plugins/SystemModuleDescriptors/CompiledVersionTest.java
41153 views
/*1* Copyright (c) 2016, 2018, 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*/2223import java.io.File;24import java.nio.file.Files;25import java.nio.file.Path;26import java.nio.file.Paths;27import java.util.Arrays;28import java.util.stream.Collectors;29import java.util.stream.Stream;3031import jdk.test.lib.compiler.CompilerUtils;32import jdk.test.lib.util.FileUtils;33import static jdk.test.lib.process.ProcessTools.*;343536import org.testng.annotations.BeforeTest;37import org.testng.annotations.Test;38import static org.testng.Assert.*;3940/**41* @test42* @library /test/lib43* @modules jdk.compiler jdk.jlink44* @build jdk.test.lib.compiler.CompilerUtils45* jdk.test.lib.util.FileUtils46* jdk.test.lib.Platform47* CompiledVersionTest jdk.test.lib.process.ProcessTools48* @run testng CompiledVersionTest49*/5051public class CompiledVersionTest {52private static final String JAVA_HOME = System.getProperty("java.home");53private static final String TEST_SRC = System.getProperty("test.src");5455private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");56private static final Path MODS_DIR = Paths.get("mods");57private static final Path IMAGE = Paths.get("image");58private static final Path JMODS = Paths.get(JAVA_HOME, "jmods");59private static final String MAIN_MID = "test/jdk.test.Main";6061// the names of the modules in this test62private static String[] modules = new String[] { "m1", "m2", "test"};63private static String[] versions = new String[] { "1.0", "2-ea", "3-internal"};646566private static boolean hasJmods() {67if (!Files.exists(JMODS)) {68System.err.println("Test skipped. NO jmods directory");69return false;70}71return true;72}7374/*75* Compiles all modules used by the test76*/77@BeforeTest78public void compileAll() throws Throwable {79if (!hasJmods()) return;8081for (int i=0; i < modules.length; i++) {82String mn = modules[i];83String version = versions[i];84Path msrc = SRC_DIR.resolve(mn);85if (version.equals("0")) {86assertTrue(CompilerUtils.compile(msrc, MODS_DIR,87"--add-exports", "java.base/jdk.internal.module=m1",88"--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1",89"--module-source-path", SRC_DIR.toString()));90} else {91assertTrue(CompilerUtils.compile(msrc, MODS_DIR,92"--add-exports", "java.base/jdk.internal.module=m1",93"--add-exports", "java.base/jdk.internal.org.objectweb.asm=m1",94"--module-source-path", SRC_DIR.toString(),95"--module-version", version));96}97}9899if (Files.exists(IMAGE)) {100FileUtils.deleteFileTreeUnchecked(IMAGE);101}102103createImage(IMAGE, modules);104}105106private void createImage(Path outputDir, String... modules) throws Throwable {107Path jlink = Paths.get(JAVA_HOME, "bin", "jlink");108String mp = JMODS.toString() + File.pathSeparator + MODS_DIR.toString();109assertTrue(executeProcess(jlink.toString(), "--output", outputDir.toString(),110"--add-modules", Arrays.stream(modules).collect(Collectors.joining(",")),111"--module-path", mp)112.outputTo(System.out)113.errorTo(System.out)114.getExitValue() == 0);115}116117/*118* Test the image created when linking with a module with119* no Packages attribute120*/121@Test122public void testCompiledVersions() throws Throwable {123if (!hasJmods()) return;124125Path java = IMAGE.resolve("bin").resolve("java");126Stream<String> options = Stream.concat(127Stream.of(java.toString(), "-m", MAIN_MID, String.valueOf(modules.length)),128Stream.concat(Arrays.stream(modules), Arrays.stream(versions))129);130131assertTrue(executeProcess(options.toArray(String[]::new))132.outputTo(System.out)133.errorTo(System.out)134.getExitValue() == 0);135}136}137138139