1 /**
   2  * Copyright (c) 2015, 2017, 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 jlink test of --add-module ALL-MODULE-PATH
  27  * @library /lib/testlibrary /test/lib
  28  * @modules jdk.compiler
  29  * @build jdk.testlibrary.ProcessTools
  30  *        jdk.testlibrary.OutputAnalyzer
  31  *        jdk.test.lib.compiler.CompilerUtils
  32  * @run testng AllModulePath
  33  */
  34 
  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.io.PrintWriter;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import java.nio.file.attribute.BasicFileAttributes;
  42 import java.util.ArrayList;
  43 import java.util.Arrays;
  44 import java.util.HashSet;
  45 import java.util.List;
  46 import java.util.Set;
  47 import java.util.stream.Collectors;
  48 import java.util.stream.Stream;
  49 import java.util.spi.ToolProvider;
  50 
  51 import jdk.test.lib.compiler.CompilerUtils;
  52 import jdk.testlibrary.ProcessTools;
  53 
  54 import org.testng.annotations.BeforeClass;
  55 import org.testng.annotations.Test;
  56 import static org.testng.Assert.*;
  57 
  58 public class AllModulePath {
  59 
  60     private final Path JMODS = Paths.get(System.getProperty("test.jdk")).resolve("jmods");
  61     private final Path SRC = Paths.get(System.getProperty("test.src")).resolve("src");
  62     private final Path MODS = Paths.get("mods");
  63 
  64     private final static Set<String> MODULES = Set.of("test", "m1");
  65 
  66     static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
  67         .orElseThrow(() ->
  68             new RuntimeException("jlink tool not found")
  69         );
  70 
  71     @BeforeClass
  72     public void setup() throws Throwable {
  73         if (Files.notExists(JMODS)) {
  74             return;
  75         }
  76 
  77         Files.createDirectories(MODS);
  78 
  79         for (String mn : MODULES) {
  80             Path mod = MODS.resolve(mn);
  81             if (!CompilerUtils.compile(SRC.resolve(mn), mod)) {
  82                 throw new AssertionError("Compilation failure. See log.");
  83             }
  84         }
  85     }
  86 
  87     @Test
  88     public void testAllModulePath() throws Throwable {
  89         if (Files.notExists(JMODS)) {
  90             return;
  91         }
  92 
  93         // create custom image
  94         Path image = Paths.get("image");
  95         createImage(image, "--add-modules", "ALL-MODULE-PATH");
  96 
  97         Set<String> modules = new HashSet<>();
  98         Files.find(JMODS, 1, (Path p, BasicFileAttributes attr) ->
  99                                 p.toString().endsWith(".jmod"))
 100              .map(p -> JMODS.relativize(p).toString())
 101              .map(n -> n.substring(0, n.length()-5))
 102              .forEach(modules::add);
 103         modules.add("m1");
 104         modules.add("test");
 105         checkModules(image, modules);
 106     }
 107 
 108     @Test
 109     public void testLimitModules() throws Throwable {
 110         if (Files.notExists(JMODS)) {
 111             return;
 112         }
 113 
 114         // create custom image
 115         Path image = Paths.get("image1");
 116         createImage(image,
 117                     "--add-modules", "ALL-MODULE-PATH",
 118                     "--limit-modules", "m1");
 119 
 120         checkModules(image, Set.of("m1", "java.base"));
 121     }
 122 
 123     @Test
 124     public void testAddModules() throws Throwable {
 125         if (Files.notExists(JMODS)) {
 126             return;
 127         }
 128 
 129         // create custom image
 130         Path image = Paths.get("image2");
 131         createImage(image,
 132                     "--add-modules", "m1,test",
 133                     "--add-modules", "ALL-MODULE-PATH",
 134                     "--limit-modules", "java.base");
 135 
 136         checkModules(image, Set.of("m1", "test", "java.base"));
 137     }
 138 
 139     /*
 140      * check the modules linked in the image
 141      */
 142     private void checkModules(Path image, Set<String> modules) throws Throwable {
 143         Path cmd = findTool(image, "java");
 144 
 145         List<String> options = new ArrayList<>();
 146         options.add(cmd.toString());
 147         options.add("-m");
 148         options.add("m1/p.ListModules");
 149         options.addAll(modules);
 150 
 151         ProcessBuilder pb = new ProcessBuilder(options);
 152         ProcessTools.executeCommand(pb)
 153                     .shouldHaveExitValue(0);
 154     }
 155 
 156     private Path findTool(Path image, String tool)  {
 157         String suffix = System.getProperty("os.name").startsWith("Windows")
 158                             ? ".exe" : "";
 159 
 160         Path cmd = image.resolve("bin").resolve(tool + suffix);
 161         if (Files.notExists(cmd)) {
 162             throw new RuntimeException(cmd + " not found");
 163         }
 164         return cmd;
 165     }
 166 
 167     private void createImage(Path image, String... options) throws IOException {
 168         String modulepath = JMODS.toString() + File.pathSeparator + MODS.toString();
 169         List<String> opts = List.of("--module-path", modulepath,
 170                                     "--output", image.toString());
 171         String[] args = Stream.concat(opts.stream(), Arrays.stream(options))
 172                               .toArray(String[]::new);
 173 
 174         System.out.println("jlink " + Arrays.stream(args).collect(Collectors.joining(" ")));
 175         PrintWriter pw = new PrintWriter(System.out);
 176         int rc = JLINK_TOOL.run(pw, pw, args);
 177         assertTrue(rc == 0);
 178     }
 179 }