Path: blob/master/test/jdk/tools/jlink/basic/AllModulePath.java
41149 views
/*1* Copyright (c) 2015, 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*/2223/*24* @test25* @summary jlink test of --add-module ALL-MODULE-PATH26* @library /test/lib27* @modules jdk.compiler28* @build jdk.test.lib.process.ProcessTools29* jdk.test.lib.process.OutputAnalyzer30* jdk.test.lib.compiler.CompilerUtils31* @run testng AllModulePath32*/3334import java.io.File;35import java.io.IOException;36import java.io.PrintWriter;37import java.nio.file.Files;38import java.nio.file.Path;39import java.nio.file.Paths;40import java.nio.file.attribute.BasicFileAttributes;41import java.util.ArrayList;42import java.util.Arrays;43import java.util.HashSet;44import java.util.List;45import java.util.Set;46import java.util.stream.Collectors;47import java.util.stream.Stream;48import java.util.spi.ToolProvider;4950import jdk.test.lib.compiler.CompilerUtils;51import jdk.test.lib.process.ProcessTools;5253import org.testng.annotations.BeforeClass;54import org.testng.annotations.Test;55import static org.testng.Assert.*;5657public class AllModulePath {5859private final Path JMODS = Paths.get(System.getProperty("test.jdk")).resolve("jmods");60private final Path SRC = Paths.get(System.getProperty("test.src")).resolve("src");61private final Path MODS = Paths.get("mods");6263private final static Set<String> MODULES = Set.of("test", "m1");6465static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")66.orElseThrow(() ->67new RuntimeException("jlink tool not found")68);6970@BeforeClass71public void setup() throws Throwable {72if (Files.notExists(JMODS)) {73return;74}7576Files.createDirectories(MODS);7778for (String mn : MODULES) {79Path mod = MODS.resolve(mn);80if (!CompilerUtils.compile(SRC.resolve(mn), mod)) {81throw new AssertionError("Compilation failure. See log.");82}83}84}8586@Test87public void testAllModulePath() throws Throwable {88if (Files.notExists(JMODS)) {89return;90}9192// create custom image93Path image = Paths.get("image");94createImage(image, "--add-modules", "ALL-MODULE-PATH");9596Set<String> modules = new HashSet<>();97Files.find(JMODS, 1, (Path p, BasicFileAttributes attr) ->98p.toString().endsWith(".jmod"))99.map(p -> JMODS.relativize(p).toString())100.map(n -> n.substring(0, n.length()-5))101.forEach(modules::add);102modules.add("m1");103modules.add("test");104checkModules(image, modules);105}106107@Test108public void testLimitModules() throws Throwable {109if (Files.notExists(JMODS)) {110return;111}112113// create custom image114Path image = Paths.get("image1");115createImage(image,116"--add-modules", "ALL-MODULE-PATH",117"--limit-modules", "m1");118119checkModules(image, Set.of("m1", "java.base"));120}121122@Test123public void testAddModules() throws Throwable {124if (Files.notExists(JMODS)) {125return;126}127128// create custom image129Path image = Paths.get("image2");130createImage(image,131"--add-modules", "m1,test",132"--add-modules", "ALL-MODULE-PATH",133"--limit-modules", "java.base");134135checkModules(image, Set.of("m1", "test", "java.base"));136}137138/*139* check the modules linked in the image140*/141private void checkModules(Path image, Set<String> modules) throws Throwable {142Path cmd = findTool(image, "java");143144List<String> options = new ArrayList<>();145options.add(cmd.toString());146options.add("-m");147options.add("m1/p.ListModules");148options.addAll(modules);149150ProcessBuilder pb = new ProcessBuilder(options);151ProcessTools.executeCommand(pb)152.shouldHaveExitValue(0);153}154155private Path findTool(Path image, String tool) {156String suffix = System.getProperty("os.name").startsWith("Windows")157? ".exe" : "";158159Path cmd = image.resolve("bin").resolve(tool + suffix);160if (Files.notExists(cmd)) {161throw new RuntimeException(cmd + " not found");162}163return cmd;164}165166private void createImage(Path image, String... options) throws IOException {167String modulepath = JMODS.toString() + File.pathSeparator + MODS.toString();168List<String> opts = List.of("--module-path", modulepath,169"--output", image.toString());170String[] args = Stream.concat(opts.stream(), Arrays.stream(options))171.toArray(String[]::new);172173System.out.println("jlink " + Arrays.stream(args).collect(Collectors.joining(" ")));174PrintWriter pw = new PrintWriter(System.out);175int rc = JLINK_TOOL.run(pw, pw, args);176assertTrue(rc == 0);177}178}179180181