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 
  24 import java.io.File;
  25 import java.io.FileReader;
  26 import java.io.IOException;
  27 import java.nio.file.Path;
  28 import java.nio.file.Paths;
  29 import java.util.List;
  30 import java.util.Properties;
  31 import java.util.spi.ToolProvider;
  32 import java.util.stream.Collectors;
  33 import java.util.stream.Stream;
  34 
  35 import tests.Helper;
  36 import tests.JImageGenerator;
  37 import tests.JImageGenerator.JLinkTask;
  38 
  39 /*
  40  * @test
  41  * @ignore
  42  * @bug 8168925
  43  * @summary MODULES property should be topologically ordered and space-separated list
  44  * @library ../lib
  45  * @modules java.base/jdk.internal.jimage
  46  *          jdk.jdeps/com.sun.tools.classfile
  47  *          jdk.jlink/jdk.tools.jlink.internal
  48  *          jdk.jlink/jdk.tools.jmod
  49  *          jdk.jlink/jdk.tools.jimage
  50  *          jdk.compiler
  51  *          jdk.scripting.nashorn
  52  *          jdk.scripting.nashorn.shell
  53  *
  54  * @build tests.*
  55  * @run main ModuleNamesOrderTest
  56  */
  57 public class ModuleNamesOrderTest {
  58     static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
  59         .orElseThrow(() ->
  60             new RuntimeException("jlink tool not found")
  61         );
  62 
  63     public static void main(String[] args) throws Exception {
  64         Helper helper = Helper.newHelper();
  65         if (helper == null) {
  66             System.err.println("Test not run");
  67             return;
  68         }
  69 
  70         testDependences(helper);
  71         testModulesOrder(helper);
  72     }
  73 
  74     private static List<String> modulesProperty(Path outputDir, String modulePath, String... roots)
  75         throws IOException
  76     {
  77         JLinkTask jlinkTask = JImageGenerator.getJLinkTask()
  78                                              .modulePath(modulePath)
  79                                              .output(outputDir);
  80         Stream.of(roots).forEach(jlinkTask::addMods);
  81         jlinkTask.call().assertSuccess();
  82 
  83         File release = new File(outputDir.toString(), "release");
  84         if (!release.exists()) {
  85             throw new AssertionError("release not generated");
  86         }
  87 
  88         Properties props = new Properties();
  89         try (FileReader reader = new FileReader(release)) {
  90             props.load(reader);
  91         }
  92 
  93         String modules = props.getProperty("MODULES");
  94         if (!modules.startsWith("\"java.base ")) {
  95             throw new AssertionError("MODULES should start with 'java.base'");
  96         }
  97         if (modules.charAt(0) != '"' || modules.charAt(modules.length()-1) != '"') {
  98             throw new AssertionError("MODULES value should be double quoted");
  99         }
 100 
 101         return Stream.of(modules.substring(1, modules.length()-1).split("\\s+"))
 102                      .collect(Collectors.toList());
 103     }
 104 
 105     private static void testDependences(Helper helper) throws IOException {
 106         Path outputDir = helper.createNewImageDir("test");
 107         List<String> modules = modulesProperty(outputDir, helper.defaultModulePath(),
 108             "jdk.scripting.nashorn");
 109         String last = modules.get(modules.size()-1);
 110         if (!last.equals("jdk.scripting.nashorn")) {
 111             throw new AssertionError("Unexpected MODULES value: " + modules);
 112         }
 113 
 114         checkDependency(modules, "java.logging", "java.base");
 115         checkDependency(modules, "jdk.dynalink", "java.logging");
 116         checkDependency(modules, "java.scripting", "java.base");
 117         checkDependency(modules, "jdk.scripting.nashorn", "java.logging");
 118         checkDependency(modules, "jdk.scripting.nashorn", "jdk.dynalink");
 119         checkDependency(modules, "jdk.scripting.nashorn", "java.scripting");
 120     }
 121 
 122     /*
 123      * Verify the MODULES list must be the same for the same module graph
 124      */
 125     private static void testModulesOrder(Helper helper) throws IOException {
 126         Path image1 = helper.createNewImageDir("test1");
 127         List<String> modules1 = modulesProperty(image1, helper.defaultModulePath(),
 128             "jdk.scripting.nashorn", "jdk.scripting.nashorn.shell");
 129         Path image2 = helper.createNewImageDir("test2");
 130         List<String> modules2 = modulesProperty(image2, helper.defaultModulePath(),
 131             "jdk.scripting.nashorn.shell", "jdk.scripting.nashorn");
 132         if (!modules1.equals(modules2)) {
 133             throw new AssertionError("MODULES should be a stable order: " +
 134                 modules1 + " vs " + modules2);
 135         }
 136     }
 137 
 138     private static void checkDependency(List<String> modules, String fromMod, String toMod) {
 139         int fromModIdx = modules.indexOf(fromMod);
 140         if (fromModIdx == -1) {
 141             throw new AssertionError(fromMod + " is missing in MODULES");
 142         }
 143         int toModIdx = modules.indexOf(toMod);
 144         if (toModIdx == -1) {
 145             throw new AssertionError(toMod + " is missing in MODULES");
 146         }
 147 
 148         if (toModIdx > fromModIdx) {
 149             throw new AssertionError("in MODULES, " + fromMod + " should appear after " + toMod);
 150         }
 151     }
 152 }