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