1 /*
   2  * Copyright (c) 2019, Google 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 /*
  26  * @test
  27  * @summary Test with symbolic linked lib/modules
  28  * @bug 8220095
  29  * @requires (os.family == "linux")
  30  * @library /test/lib
  31  * @modules java.management
  32  *          jdk.jlink
  33  * @run driver ModulesSymLink
  34  */
  35 
  36 import java.io.File;
  37 import java.nio.file.Files;
  38 import java.nio.file.Path;
  39 import java.nio.file.Paths;
  40 import jdk.test.lib.process.ProcessTools;
  41 import jdk.test.lib.process.OutputAnalyzer;
  42 
  43 public class ModulesSymLink {
  44     static String java_home;
  45     static String test_jdk;
  46 
  47     public static void main(String[] args) throws Throwable {
  48         java_home = System.getProperty("java.home");
  49         test_jdk = System.getProperty("user.dir") + File.separator +
  50                    "modulessymlink_jdk" + Long.toString(System.currentTimeMillis());
  51 
  52         constructTestJDK();
  53 
  54         ProcessBuilder pb = new ProcessBuilder(
  55             test_jdk + File.separator + "bin" + File.separator + "java",
  56             "-version");
  57         OutputAnalyzer out = new OutputAnalyzer(pb.start());
  58         out.shouldHaveExitValue(0);
  59     }
  60 
  61     // 1) Create a test JDK binary (jlink is used to help simplify the process,
  62     //    alternatively a test JDK could be copied from JAVA_HOME.)
  63     // 2) Rename the test JDK's lib/modules to lib/0.
  64     // 3) Then create a link to lib/0 as lib/modules.
  65     static void constructTestJDK() throws Throwable {
  66         Path jlink = Paths.get(java_home, "bin", "jlink");
  67         OutputAnalyzer out = ProcessTools.executeProcess(jlink.toString(),
  68                   "--output", test_jdk,
  69                   "--add-modules", "java.base");
  70         out.shouldHaveExitValue(0);
  71 
  72         String modules_prefix = test_jdk + File.separator + "lib" + File.separator;
  73         Path modules = Paths.get(test_jdk, "lib", "modules");
  74         Path renamed_modules = Paths.get(test_jdk, "lib", "0");
  75         Files.move(modules, renamed_modules);
  76         Files.createSymbolicLink(modules, renamed_modules);
  77     }
  78 }