Path: blob/master/test/jdk/tools/jlink/bindservices/BindServices.java
41149 views
/*1* Copyright (c) 2017, 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.io.PrintWriter;25import java.io.StringWriter;26import java.nio.file.Files;27import java.nio.file.Path;28import java.nio.file.Paths;29import java.util.ArrayList;30import java.util.List;31import java.util.spi.ToolProvider;32import java.util.stream.Collectors;33import java.util.stream.Stream;3435import jdk.test.lib.compiler.CompilerUtils;36import static jdk.test.lib.process.ProcessTools.*;3738import org.testng.annotations.BeforeTest;39import org.testng.annotations.Test;40import static org.testng.Assert.*;4142/**43* @test44* @bug 817482645* @library /test/lib46* @modules jdk.compiler jdk.jlink47* @build BindServices jdk.test.lib.process.ProcessTools48* jdk.test.lib.compiler.CompilerUtils49* @run testng BindServices50*/5152public class BindServices {53private static final String JAVA_HOME = System.getProperty("java.home");54private static final String TEST_SRC = System.getProperty("test.src");5556private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");57private static final Path MODS_DIR = Paths.get("mods");5859private static final String MODULE_PATH =60Paths.get(JAVA_HOME, "jmods").toString() +61File.pathSeparator + MODS_DIR.toString();6263// the names of the modules in this test64private static String[] modules = new String[] {"m1", "m2", "m3"};656667private static boolean hasJmods() {68if (!Files.exists(Paths.get(JAVA_HOME, "jmods"))) {69System.err.println("Test skipped. NO jmods directory");70return false;71}72return true;73}7475/*76* Compiles all modules used by the test77*/78@BeforeTest79public void compileAll() throws Throwable {80if (!hasJmods()) return;8182for (String mn : modules) {83Path msrc = SRC_DIR.resolve(mn);84assertTrue(CompilerUtils.compile(msrc, MODS_DIR,85"--module-source-path", SRC_DIR.toString()));86}87}8889@Test90public void noServiceBinding() throws Throwable {91if (!hasJmods()) return;9293Path dir = Paths.get("noServiceBinding");9495// no service binding and does not link m2,m3 providers.96JLink.run("--output", dir.toString(),97"--module-path", MODULE_PATH,98"--add-modules", "m1").output();99100testImage(dir, "m1");101}102103@Test104public void fullServiceBinding() throws Throwable {105if (!hasJmods()) return;106107Path dir = Paths.get("fullServiceBinding");108109// full service binding110// m2 is a provider used by m1. During service binding, when m2 is111// resolved, m2 uses p2.T that causes m3 to be linked as it is a112// provider to p2.T113JLink.run("--output", dir.toString(),114"--module-path", MODULE_PATH,115"--add-modules", "m1",116"--bind-services",117"--limit-modules", "m1,m2,m3");118119testImage(dir, "m1", "m2", "m3");120}121122@Test123public void testVerbose() throws Throwable {124if (!hasJmods()) return;125126Path dir = Paths.get("verbose");127128List<String> output =129JLink.run("--output", dir.toString(),130"--module-path", MODULE_PATH,131"--add-modules", "m1",132"--bind-services",133"--verbose",134"--limit-modules", "m1,m2,m3").output();135136List<String> expected = List.of(137"m1 " + MODS_DIR.resolve("m1").toUri().toString(),138"m2 " + MODS_DIR.resolve("m2").toUri().toString(),139"m3 " + MODS_DIR.resolve("m3").toUri().toString(),140"java.base provides java.nio.file.spi.FileSystemProvider used by java.base",141"m1 provides p1.S used by m1",142"m2 provides p1.S used by m1",143"m2 provides p2.T used by m2",144"m3 provides p2.T used by m2",145"m3 provides p3.S not used by any observable module"146);147148assertTrue(output.containsAll(expected));149150testImage(dir, "m1", "m2", "m3");151}152153@Test154public void testVerboseAndNoBindServices() throws Throwable {155if (!hasJmods()) return;156157Path dir = Paths.get("verboseNoBind");158159List<String> output =160JLink.run("--output", dir.toString(),161"--module-path", MODULE_PATH,162"--verbose",163"--add-modules", "m1").output();164165assertTrue(output.contains("m1 provides p1.S used by m1"));166167testImage(dir, "m1");168}169170/*171* Tests the given ${java.home} to only contain the specified modules172*/173private void testImage(Path javaHome, String... modules) throws Throwable {174Path java = javaHome.resolve("bin").resolve("java");175String[] cmd = Stream.concat(176Stream.of(java.toString(), "-m", "m1/p1.Main"),177Stream.of(modules)).toArray(String[]::new);178179assertTrue(executeProcess(cmd).outputTo(System.out)180.errorTo(System.out)181.getExitValue() == 0);182}183184static class JLink {185static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")186.orElseThrow(() ->187new RuntimeException("jlink tool not found")188);189190static JLink run(String... options) {191JLink jlink = new JLink();192assertTrue(jlink.execute(options) == 0);193return jlink;194}195196final List<String> output = new ArrayList<>();197private int execute(String... options) {198System.out.println("jlink " +199Stream.of(options).collect(Collectors.joining(" ")));200201StringWriter writer = new StringWriter();202PrintWriter pw = new PrintWriter(writer);203int rc = JLINK_TOOL.run(pw, pw, options);204System.out.println(writer.toString());205Stream.of(writer.toString().split("\\v"))206.map(String::trim)207.forEach(output::add);208return rc;209}210211boolean contains(String s) {212return output.contains(s);213}214215List<String> output() {216return output;217}218}219}220221222