jdk/test/sun/management/jmxremote/bootstrap/CustomLauncherTest.java
changeset 21367 679b312e3c5b
child 21404 0e54d286d846
equal deleted inserted replaced
21366:4564a4d5d03d 21367:679b312e3c5b
       
     1 /*
       
     2  * Copyright (c) 2013, 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.nio.file.FileSystem;
       
    26 import java.nio.file.FileSystems;
       
    27 import java.nio.file.Files;
       
    28 import java.nio.file.Path;
       
    29 import java.util.concurrent.TimeUnit;
       
    30 import java.util.concurrent.atomic.AtomicReference;
       
    31 
       
    32 import jdk.testlibrary.JdkFinder;
       
    33 import jdk.testlibrary.ProcessTools;
       
    34 
       
    35 /**
       
    36  * @test
       
    37  * @bug 6434402 8004926
       
    38  * @library ../../../../lib/testlibrary
       
    39  * @build TestManager TestApplication CustomLauncherTest
       
    40  * @run main CustomLauncherTest
       
    41  * @author Jaroslav Bachorik
       
    42  */
       
    43 public class CustomLauncherTest {
       
    44     private static final  String TEST_CLASSES = System.getProperty("test.classes");
       
    45     private static final  String TEST_JDK = System.getProperty("test.jdk");
       
    46 
       
    47     private static final  String TEST_SRC = System.getProperty("test.src");
       
    48     private static final  String OSNAME = System.getProperty("os.name");
       
    49     private static final  String ARCH;
       
    50     private static final  String LIBARCH;
       
    51 
       
    52     static {
       
    53         // magic with os.arch
       
    54         String osarch = System.getProperty("os.arch");
       
    55         switch (osarch) {
       
    56             case "i386":
       
    57             case "i486":
       
    58             case "i586":
       
    59             case "i686":
       
    60             case "i786":
       
    61             case "i886":
       
    62             case "i986": {
       
    63                 ARCH = "i586";
       
    64                 break;
       
    65             }
       
    66             case "x86_64":
       
    67             case "amd64": {
       
    68                 ARCH = "amd64";
       
    69                 break;
       
    70             }
       
    71             default: {
       
    72                 ARCH = osarch;
       
    73             }
       
    74         }
       
    75         LIBARCH = ARCH.equals("i586") ? "i386" : ARCH;
       
    76     }
       
    77 
       
    78     public static void main(String[] args) throws Exception {
       
    79         if (TEST_CLASSES == null || TEST_CLASSES.isEmpty()) {
       
    80             System.out.println("Test is designed to be run from jtreg only");
       
    81             return;
       
    82         }
       
    83 
       
    84         String PLATFORM = "";
       
    85         switch (OSNAME.toLowerCase()) {
       
    86             case "linux": {
       
    87                 PLATFORM = "linux";
       
    88                 break;
       
    89             }
       
    90             case "sunos": {
       
    91                 PLATFORM = "solaris";
       
    92                 break;
       
    93             }
       
    94             default: {
       
    95                 System.out.println("Test not designed to run on this operating " +
       
    96                                    "system (" + OSNAME + "), skipping...");
       
    97                 return;
       
    98             }
       
    99         }
       
   100 
       
   101         String LAUNCHER = TEST_SRC + File.separator + PLATFORM + "-" + ARCH +
       
   102                           File.separator + "launcher";
       
   103 
       
   104         final FileSystem FS = FileSystems.getDefault();
       
   105         final boolean hasLauncher = Files.isExecutable(FS.getPath(LAUNCHER));
       
   106         if (!hasLauncher) {
       
   107             System.out.println("Launcher [" + LAUNCHER + "] does not exist. Skipping the test.");
       
   108             return;
       
   109         }
       
   110 
       
   111         Path libjvmPath = findLibjvm(FS);
       
   112         if (libjvmPath == null) {
       
   113             throw new Error("Unable to locate 'libjvm.so' in " + TEST_JDK);
       
   114         }
       
   115 
       
   116         Process serverPrc = null, clientPrc = null;
       
   117 
       
   118         try {
       
   119             System.out.println("Starting custom launcher:");
       
   120             System.out.println("=========================");
       
   121             System.out.println("  launcher  : " + LAUNCHER);
       
   122             System.out.println("  libjvm    : " + libjvmPath.toString());
       
   123             System.out.println("  classpath : " + TEST_CLASSES);
       
   124             ProcessBuilder server = new ProcessBuilder(LAUNCHER, libjvmPath.toString(), TEST_CLASSES, "TestApplication");
       
   125 
       
   126             final AtomicReference<String> port = new AtomicReference<>();
       
   127             final AtomicReference<String> pid = new AtomicReference<>();
       
   128 
       
   129             serverPrc = ProcessTools.startProcess(
       
   130                 "Launcher",
       
   131                 server,
       
   132                 (String line) -> {
       
   133                     if (line.startsWith("port:")) {
       
   134                          port.set(line.split("\\:")[1]);
       
   135                      } else  if (line.startsWith("pid:")) {
       
   136                          pid.set(line.split("\\:")[1]);
       
   137                      } else if (line.startsWith("waiting")) {
       
   138                          return true;
       
   139                      }
       
   140                      return false;
       
   141                 },
       
   142                 5,
       
   143                 TimeUnit.SECONDS
       
   144             );
       
   145 
       
   146             System.out.println("Attaching test manager:");
       
   147             System.out.println("=========================");
       
   148             System.out.println("  PID           : " + pid.get());
       
   149             System.out.println("  shutdown port : " + port.get());
       
   150 
       
   151             ProcessBuilder client = ProcessTools.createJavaProcessBuilder(
       
   152                 "-cp",
       
   153                 TEST_CLASSES +
       
   154                     File.pathSeparator +
       
   155                     TEST_JDK +
       
   156                     File.separator +
       
   157                     "lib" +
       
   158                     File.separator +
       
   159                     "tools.jar",
       
   160                 "TestManager",
       
   161                 pid.get(),
       
   162                 port.get(),
       
   163                 "true"
       
   164             );
       
   165 
       
   166             clientPrc = ProcessTools.startProcess(
       
   167                 "TestManager",
       
   168                 client,
       
   169                 (String line) -> line.startsWith("Starting TestManager for PID"),
       
   170                 10,
       
   171                 TimeUnit.SECONDS
       
   172             );
       
   173 
       
   174             int clientExitCode = clientPrc.waitFor();
       
   175             int serverExitCode = serverPrc.waitFor();
       
   176 
       
   177             if (clientExitCode != 0 || serverExitCode != 0) {
       
   178                 throw new Error("Test failed");
       
   179             }
       
   180         } finally {
       
   181             if (clientPrc != null) {
       
   182                 clientPrc.destroy();
       
   183                 clientPrc.waitFor();
       
   184             }
       
   185             if (serverPrc != null) {
       
   186                 serverPrc.destroy();
       
   187                 serverPrc.waitFor();
       
   188             }
       
   189         }
       
   190     }
       
   191 
       
   192     private static Path findLibjvm(FileSystem FS) {
       
   193         Path libjvmPath = findLibjvm(FS.getPath(TEST_JDK, "jre", "lib", LIBARCH));
       
   194         if (libjvmPath == null) {
       
   195             libjvmPath = findLibjvm(FS.getPath(TEST_JDK, "lib", LIBARCH));
       
   196         }
       
   197         return libjvmPath;
       
   198     }
       
   199 
       
   200     private static Path findLibjvm(Path libPath) {
       
   201         // ARCH/libjvm.so -> ARCH/server/libjvm.so -> ARCH/client/libjvm.so
       
   202         Path libjvmPath = libPath.resolve("libjvm.so");
       
   203         if (isFileOk(libjvmPath)) {
       
   204             return libjvmPath;
       
   205         }
       
   206         libjvmPath = libPath.resolve("server/libjvm.so");
       
   207         if (isFileOk(libjvmPath)) {
       
   208             return libjvmPath;
       
   209         }
       
   210         libjvmPath = libPath.resolve("client/libjvm.so");
       
   211         if (isFileOk(libPath)) {
       
   212             return libjvmPath;
       
   213         }
       
   214 
       
   215         return null;
       
   216     }
       
   217 
       
   218     private static boolean isFileOk(Path path) {
       
   219         return Files.isRegularFile(path) && Files.isReadable(path);
       
   220     }
       
   221 }