1 /*
   2  * Copyright (c) 2019, Google Inc. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  * @test
  28  * @summary Test with symbolic linked lib/modules
  29  * @bug 8220095
  30  * @requires (os.family == "solaris" | os.family == "linux" | os.family == "mac")
  31  * @library /test/lib
  32  * @modules java.management
  33  *          jdk.jlink
  34  * @run driver ModulesSymLink
  35  */
  36 
  37 import java.io.File;
  38 import java.nio.file.Files;
  39 import java.nio.file.Path;
  40 import java.nio.file.Paths;
  41 import jdk.test.lib.process.ProcessTools;
  42 import jdk.test.lib.process.OutputAnalyzer;
  43 
  44 public class ModulesSymLink {
  45     static String java_home;
  46     static String test_jdk;
  47 
  48     public static void main(String[] args) throws Throwable {
  49         java_home = System.getProperty("java.home");
  50         test_jdk = System.getProperty("user.dir") + File.separator +
  51                    "modulessymlink_jdk" + Long.toString(System.currentTimeMillis());
  52 
  53         constructTestJDK();
  54 
  55         ProcessBuilder pb = new ProcessBuilder(
  56             test_jdk + File.separator + "bin" + File.separator + "java",
  57             "-version");
  58         OutputAnalyzer out = new OutputAnalyzer(pb.start());
  59         out.shouldHaveExitValue(0);
  60     }
  61 
  62     // 1) Create a test JDK binary (jlink is used to help simplify the process,
  63     //    alternatively a test JDK could be copied from JAVA_HOME.)
  64     // 2) Rename the test JDK's lib/modules to lib/0.
  65     // 3) Then create a link to lib/0 as lib/modules.
  66     static void constructTestJDK() throws Throwable {
  67         Path jlink = Paths.get(java_home, "bin", "jlink");
  68         OutputAnalyzer out = ProcessTools.executeProcess(jlink.toString(),
  69                   "--output", test_jdk,
  70                   "--add-modules", "java.base");
  71         out.shouldHaveExitValue(0);
  72 
  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 }