1 /*
   2  * Copyright (c) 2016, 2019, 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 /*
  25  * @test
  26  * @bug 8210408
  27  * @summary Test unnamed module to find resource bundles exported from a named
  28  *          module.
  29  * @library /test/lib
  30  *          ..
  31  * @build jdk.test.lib.JDKToolLauncher
  32  *        jdk.test.lib.Utils
  33  *        jdk.test.lib.compiler.CompilerUtils
  34  *        jdk.test.lib.process.ProcessTools
  35  *        ModuleTestUtil
  36  * @run main UnNamedTest
  37  */
  38 
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import java.util.List;
  42 
  43 import jdk.test.lib.JDKToolLauncher;
  44 import jdk.test.lib.Utils;
  45 import jdk.test.lib.process.ProcessTools;
  46 
  47 public class UnNamedTest {
  48     private static final Path SRC_DIR = Paths.get(Utils.TEST_SRC, "src");
  49     private static final Path MODS_DIR = Paths.get(Utils.TEST_CLASSES, "mods");
  50 
  51     private static final List<String> LOCALE_LIST = List.of("de", "fr", "ja",
  52             "zh-tw", "en", "de");
  53 
  54     public static void main(String[] args) throws Throwable {
  55         ModuleTestUtil.prepareModule(SRC_DIR, MODS_DIR, "bundles", ".properties");
  56         compileCmd();
  57         runCmd();
  58     }
  59 
  60     private static void compileCmd() throws Throwable {
  61         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("javac");
  62         launcher.addToolArg("-d")
  63                 .addToolArg(Utils.TEST_CLASSES)
  64                 .addToolArg(Paths.get(Utils.TEST_SRC, "Main.java").toString());
  65 
  66         int exitCode = ProcessTools.executeCommand(launcher.getCommand())
  67                                    .getExitValue();
  68         if (exitCode != 0) {
  69             throw new RuntimeException("Compile of the test failed. "
  70                     + "Unexpected exit code: " + exitCode);
  71         }
  72     }
  73 
  74     private static void runCmd() throws Throwable {
  75         // access resource bundles that are exported private unconditionally.
  76         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("java");
  77         launcher.addToolArg("-ea")
  78                 .addToolArg("-esa")
  79                 .addToolArg("-cp")
  80                 .addToolArg(Utils.TEST_CLASSES)
  81                 .addToolArg("--module-path")
  82                 .addToolArg(MODS_DIR.toString())
  83                 .addToolArg("--add-modules")
  84                 .addToolArg("bundles")
  85                 .addToolArg("Main");
  86         LOCALE_LIST.forEach(launcher::addToolArg);
  87 
  88         int exitCode = ProcessTools.executeCommand(launcher.getCommand())
  89                                    .getExitValue();
  90         if (exitCode != 0) {
  91             throw new RuntimeException("Execution of the test1 failed. "
  92                     + "Unexpected exit code: " + exitCode);
  93         }
  94 
  95         // --add-exports can't open resources
  96         launcher = JDKToolLauncher.createUsingTestJDK("java");
  97         launcher.addToolArg("-ea")
  98                 .addToolArg("-esa")
  99                 .addToolArg("-cp")
 100                 .addToolArg(Utils.TEST_CLASSES)
 101                 .addToolArg("--module-path")
 102                 .addToolArg(MODS_DIR.toString())
 103                 .addToolArg("--add-modules")
 104                 .addToolArg("bundles")
 105                 .addToolArg("--add-opens")
 106                 .addToolArg("bundles/jdk.test.internal.resources=ALL-UNNAMED")
 107                 .addToolArg("Main");
 108         LOCALE_LIST.forEach(launcher::addToolArg);
 109 
 110         exitCode = ProcessTools.executeCommand(launcher.getCommand())
 111                                .getExitValue();
 112         if (exitCode != 0) {
 113             throw new RuntimeException("Execution of the test2 failed. "
 114                     + "Unexpected exit code: " + exitCode);
 115         }
 116     }
 117 }