1/*
2 * Copyright (c) 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
24import java.io.File;
25import java.io.FileReader;
26import java.io.IOException;
27import java.io.PrintWriter;
28import java.io.StringWriter;
29import java.nio.file.Path;
30import java.util.Properties;
31import java.util.spi.ToolProvider;
32
33import tests.Helper;
34import tests.JImageGenerator;
35
36/*
37 * @test
38 * @bug 8168925
39 * @summary MODULES property should be topologically ordered and space-separated list
40 * @library ../lib
41 * @modules java.base/jdk.internal.jimage
42 *          jdk.jdeps/com.sun.tools.classfile
43 *          jdk.jlink/jdk.tools.jlink.internal
44 *          jdk.jlink/jdk.tools.jmod
45 *          jdk.jlink/jdk.tools.jimage
46 *          jdk.compiler
47 * @build tests.*
48 * @run main ModuleNamesOrderTest
49 */
50public class ModuleNamesOrderTest {
51    static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
52        .orElseThrow(() ->
53            new RuntimeException("jlink tool not found")
54        );
55
56    public static void main(String[] args) throws Exception {
57        Helper helper = Helper.newHelper();
58        if (helper == null) {
59            System.err.println("Test not run");
60            return;
61        }
62
63        Path outputDir = helper.createNewImageDir("image8168925");
64        JImageGenerator.getJLinkTask()
65                .modulePath(helper.defaultModulePath())
66                .output(outputDir)
67                .addMods("jdk.scripting.nashorn")
68                .call().assertSuccess();
69
70        File release = new File(outputDir.toString(), "release");
71        if (!release.exists()) {
72            throw new AssertionError("release not generated");
73        }
74
75        Properties props = new Properties();
76        try (FileReader reader = new FileReader(release)) {
77            props.load(reader);
78        }
79
80        String modules = props.getProperty("MODULES");
81        if (!modules.startsWith("\"java.base ")) {
82            throw new AssertionError("MODULES should start with 'java.base'");
83        }
84
85        if (!modules.endsWith(" jdk.scripting.nashorn\"")) {
86            throw new AssertionError("MODULES end with 'jdk.scripting.nashorn'");
87        }
88
89        checkDependency(modules, "java.logging", "java.base");
90        checkDependency(modules, "jdk.dynalink", "java.logging");
91        checkDependency(modules, "java.scripting", "java.base");
92        checkDependency(modules, "jdk.scripting.nashorn", "java.logging");
93        checkDependency(modules, "jdk.scripting.nashorn", "jdk.dynalink");
94        checkDependency(modules, "jdk.scripting.nashorn", "java.scripting");
95    }
96
97    private static void checkDependency(String modules, String fromMod, String toMod) {
98        int fromModIdx = modules.indexOf(fromMod);
99        if (fromModIdx == -1) {
100            throw new AssertionError(fromMod + " is missing in MODULES");
101        }
102        int toModIdx = modules.indexOf(toMod);
103        if (toModIdx == -1) {
104            throw new AssertionError(toMod + " is missing in MODULES");
105        }
106
107        if (toModIdx > fromModIdx) {
108            throw new AssertionError("in MODULES, " + fromMod + " should appear after " + toMod);
109        }
110    }
111}
112