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