test/jdk/tools/jlink/ModuleNamesOrderTest.java
author mchung
Thu, 07 Dec 2017 09:23:15 -0800
changeset 48206 8b967e200e35
parent 47216 71c04702a3d5
permissions -rw-r--r--
8192945: Need stable sort for MODULES entry in the release file Reviewed-by: alanb, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42471
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
     1
/*
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
     4
 *
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
     8
 *
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    13
 * accompanied this code).
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    14
 *
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    18
 *
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    21
 * questions.
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    22
 */
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    23
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    24
import java.io.File;
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    25
import java.io.FileReader;
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    26
import java.io.IOException;
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    27
import java.nio.file.Path;
48206
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    28
import java.nio.file.Paths;
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    29
import java.util.List;
42471
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    30
import java.util.Properties;
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    31
import java.util.spi.ToolProvider;
48206
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    32
import java.util.stream.Collectors;
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    33
import java.util.stream.Stream;
42471
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    34
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    35
import tests.Helper;
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    36
import tests.JImageGenerator;
48206
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    37
import tests.JImageGenerator.JLinkTask;
42471
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    38
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    39
/*
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    40
 * @test
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    41
 * @bug 8168925
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    42
 * @summary MODULES property should be topologically ordered and space-separated list
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    43
 * @library ../lib
42690
93d0411bd67d 8171070: Test ModuleNamesOrderTest.java fails
sundar
parents: 42471
diff changeset
    44
 * @modules java.base/jdk.internal.jimage
93d0411bd67d 8171070: Test ModuleNamesOrderTest.java fails
sundar
parents: 42471
diff changeset
    45
 *          jdk.jdeps/com.sun.tools.classfile
93d0411bd67d 8171070: Test ModuleNamesOrderTest.java fails
sundar
parents: 42471
diff changeset
    46
 *          jdk.jlink/jdk.tools.jlink.internal
93d0411bd67d 8171070: Test ModuleNamesOrderTest.java fails
sundar
parents: 42471
diff changeset
    47
 *          jdk.jlink/jdk.tools.jmod
93d0411bd67d 8171070: Test ModuleNamesOrderTest.java fails
sundar
parents: 42471
diff changeset
    48
 *          jdk.jlink/jdk.tools.jimage
93d0411bd67d 8171070: Test ModuleNamesOrderTest.java fails
sundar
parents: 42471
diff changeset
    49
 *          jdk.compiler
48206
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    50
 *          jdk.scripting.nashorn
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    51
 *          jdk.scripting.nashorn.shell
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    52
 *
42471
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    53
 * @build tests.*
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    54
 * @run main ModuleNamesOrderTest
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    55
 */
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    56
public class ModuleNamesOrderTest {
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    57
    static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    58
        .orElseThrow(() ->
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    59
            new RuntimeException("jlink tool not found")
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    60
        );
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    61
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    62
    public static void main(String[] args) throws Exception {
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    63
        Helper helper = Helper.newHelper();
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    64
        if (helper == null) {
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    65
            System.err.println("Test not run");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    66
            return;
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    67
        }
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    68
48206
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    69
        testDependences(helper);
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    70
        testModulesOrder(helper);
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    71
    }
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    72
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    73
    private static List<String> modulesProperty(Path outputDir, String modulePath, String... roots)
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    74
        throws IOException
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    75
    {
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    76
        JLinkTask jlinkTask = JImageGenerator.getJLinkTask()
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    77
                                             .modulePath(modulePath)
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    78
                                             .output(outputDir);
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    79
        Stream.of(roots).forEach(jlinkTask::addMods);
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    80
        jlinkTask.call().assertSuccess();
42471
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    81
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    82
        File release = new File(outputDir.toString(), "release");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    83
        if (!release.exists()) {
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    84
            throw new AssertionError("release not generated");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    85
        }
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    86
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    87
        Properties props = new Properties();
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    88
        try (FileReader reader = new FileReader(release)) {
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    89
            props.load(reader);
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    90
        }
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    91
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    92
        String modules = props.getProperty("MODULES");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    93
        if (!modules.startsWith("\"java.base ")) {
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    94
            throw new AssertionError("MODULES should start with 'java.base'");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    95
        }
48206
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    96
        if (modules.charAt(0) != '"' || modules.charAt(modules.length()-1) != '"') {
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    97
            throw new AssertionError("MODULES value should be double quoted");
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
    98
        }
42471
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
    99
48206
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   100
        return Stream.of(modules.substring(1, modules.length()-1).split("\\s+"))
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   101
                     .collect(Collectors.toList());
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   102
    }
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   103
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   104
    private static void testDependences(Helper helper) throws IOException {
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   105
        Path outputDir = helper.createNewImageDir("test");
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   106
        List<String> modules = modulesProperty(outputDir, helper.defaultModulePath(),
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   107
            "jdk.scripting.nashorn");
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   108
        String last = modules.get(modules.size()-1);
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   109
        if (!last.equals("jdk.scripting.nashorn")) {
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   110
            throw new AssertionError("Unexpected MODULES value: " + modules);
42471
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   111
        }
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   112
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   113
        checkDependency(modules, "java.logging", "java.base");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   114
        checkDependency(modules, "jdk.dynalink", "java.logging");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   115
        checkDependency(modules, "java.scripting", "java.base");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   116
        checkDependency(modules, "jdk.scripting.nashorn", "java.logging");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   117
        checkDependency(modules, "jdk.scripting.nashorn", "jdk.dynalink");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   118
        checkDependency(modules, "jdk.scripting.nashorn", "java.scripting");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   119
    }
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   120
48206
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   121
    /*
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   122
     * Verify the MODULES list must be the same for the same module graph
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   123
     */
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   124
    private static void testModulesOrder(Helper helper) throws IOException {
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   125
        Path image1 = helper.createNewImageDir("test1");
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   126
        List<String> modules1 = modulesProperty(image1, helper.defaultModulePath(),
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   127
            "jdk.scripting.nashorn", "jdk.scripting.nashorn.shell");
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   128
        Path image2 = helper.createNewImageDir("test2");
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   129
        List<String> modules2 = modulesProperty(image2, helper.defaultModulePath(),
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   130
            "jdk.scripting.nashorn.shell", "jdk.scripting.nashorn");
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   131
        if (!modules1.equals(modules2)) {
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   132
            throw new AssertionError("MODULES should be a stable order: " +
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   133
                modules1 + " vs " + modules2);
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   134
        }
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   135
    }
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   136
8b967e200e35 8192945: Need stable sort for MODULES entry in the release file
mchung
parents: 47216
diff changeset
   137
    private static void checkDependency(List<String> modules, String fromMod, String toMod) {
42471
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   138
        int fromModIdx = modules.indexOf(fromMod);
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   139
        if (fromModIdx == -1) {
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   140
            throw new AssertionError(fromMod + " is missing in MODULES");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   141
        }
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   142
        int toModIdx = modules.indexOf(toMod);
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   143
        if (toModIdx == -1) {
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   144
            throw new AssertionError(toMod + " is missing in MODULES");
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   145
        }
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   146
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   147
        if (toModIdx > fromModIdx) {
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   148
            throw new AssertionError("in MODULES, " + fromMod + " should appear after " + toMod);
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   149
        }
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   150
    }
86b1da05a4b0 8168925: MODULES property should be topologically ordered and space-separated list
sundar
parents:
diff changeset
   151
}