Path: blob/master/test/jdk/tools/jlink/ModuleNamesOrderTest.java
41145 views
/*1* Copyright (c) 2016, 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.FileReader;25import java.io.IOException;26import java.nio.file.Path;27import java.nio.file.Paths;28import java.util.List;29import java.util.Properties;30import java.util.spi.ToolProvider;31import java.util.stream.Collectors;32import java.util.stream.Stream;3334import tests.Helper;35import tests.JImageGenerator;36import tests.JImageGenerator.JLinkTask;3738/*39* @test40* @bug 816892541* @summary MODULES property should be topologically ordered and space-separated list42* @library ../lib43* @modules java.base/jdk.internal.jimage44* jdk.jdeps/com.sun.tools.classfile45* jdk.jlink/jdk.tools.jlink.internal46* jdk.jlink/jdk.tools.jmod47* jdk.jlink/jdk.tools.jimage48* jdk.compiler49* jdk.jshell50*51* @build tests.*52* @run main ModuleNamesOrderTest53*/54public class ModuleNamesOrderTest {55static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")56.orElseThrow(() ->57new RuntimeException("jlink tool not found")58);5960public static void main(String[] args) throws Exception {61Helper helper = Helper.newHelper();62if (helper == null) {63System.err.println("Test not run");64return;65}6667testDependences(helper);68testModulesOrder(helper);69}7071private static List<String> modulesProperty(Path outputDir, String modulePath, String... roots)72throws IOException73{74JLinkTask jlinkTask = JImageGenerator.getJLinkTask()75.modulePath(modulePath)76.output(outputDir);77Stream.of(roots).forEach(jlinkTask::addMods);78jlinkTask.call().assertSuccess();7980File release = new File(outputDir.toString(), "release");81if (!release.exists()) {82throw new AssertionError("release not generated");83}8485Properties props = new Properties();86try (FileReader reader = new FileReader(release)) {87props.load(reader);88}8990String modules = props.getProperty("MODULES");91if (!modules.startsWith("\"java.base ")) {92throw new AssertionError("MODULES should start with 'java.base'");93}94if (modules.charAt(0) != '"' || modules.charAt(modules.length()-1) != '"') {95throw new AssertionError("MODULES value should be double quoted");96}9798return Stream.of(modules.substring(1, modules.length()-1).split("\\s+"))99.collect(Collectors.toList());100}101102private static void testDependences(Helper helper) throws IOException {103Path outputDir = helper.createNewImageDir("test");104List<String> modules = modulesProperty(outputDir, helper.defaultModulePath(),105"jdk.jshell");106String last = modules.get(modules.size()-1);107if (!last.equals("jdk.jshell")) {108throw new AssertionError("Unexpected MODULES value: " + modules);109}110111checkDependency(modules, "java.logging", "java.base");112checkDependency(modules, "jdk.compiler", "java.compiler");113checkDependency(modules, "jdk.jshell", "java.logging");114checkDependency(modules, "jdk.jshell", "jdk.compiler");115}116117/*118* Verify the MODULES list must be the same for the same module graph119*/120private static void testModulesOrder(Helper helper) throws IOException {121Path image1 = helper.createNewImageDir("test1");122List<String> modules1 = modulesProperty(image1, helper.defaultModulePath(),123"jdk.jshell");124Path image2 = helper.createNewImageDir("test2");125List<String> modules2 = modulesProperty(image2, helper.defaultModulePath(),126"jdk.jshell");127if (!modules1.equals(modules2)) {128throw new AssertionError("MODULES should be a stable order: " +129modules1 + " vs " + modules2);130}131}132133private static void checkDependency(List<String> modules, String fromMod, String toMod) {134int fromModIdx = modules.indexOf(fromMod);135if (fromModIdx == -1) {136throw new AssertionError(fromMod + " is missing in MODULES");137}138int toModIdx = modules.indexOf(toMod);139if (toModIdx == -1) {140throw new AssertionError(toMod + " is missing in MODULES");141}142143if (toModIdx > fromModIdx) {144throw new AssertionError("in MODULES, " + fromMod + " should appear after " + toMod);145}146}147}148149150