1 /*
   2  * Copyright (c) 2015, 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  * @summary Basic test for jhsdb launcher
  27  * @library /test/lib/share/classes
  28  * @library /lib/testlibrary
  29  * @build jdk.testlibrary.*
  30  * @build jdk.test.lib.apps.*
  31  * @run main BasicLauncherTest
  32  */
  33 
  34 import java.io.BufferedReader;
  35 import java.io.IOException;
  36 import java.io.InputStreamReader;
  37 import java.util.ArrayList;
  38 import java.util.List;
  39 import java.util.Arrays;
  40 import jdk.testlibrary.JDKToolLauncher;
  41 import jdk.testlibrary.Utils;
  42 import jdk.testlibrary.OutputAnalyzer;
  43 import jdk.testlibrary.ProcessTools;
  44 import jdk.test.lib.apps.LingeredApp;
  45 import jdk.testlibrary.Platform;
  46 
  47 public class BasicLauncherTest {
  48 
  49     private final static String toolName = "jhsdb";
  50     private static LingeredApp theApp = null;
  51 
  52     /**
  53      *
  54      * @return exit code of tool
  55      */
  56     public static int launchCLHSDB()
  57         throws IOException {
  58 
  59         System.out.println("Starting LingeredApp");
  60         try {
  61             theApp = LingeredApp.startApp();
  62 
  63             System.out.println("Starting clhsdb against " + theApp.getPid());
  64             JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK(toolName);
  65             launcher.addToolArg("clhsdb");
  66             launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));
  67 
  68             ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
  69             processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
  70             Process toolProcess = processBuilder.start();
  71             toolProcess.getOutputStream().write("quit\n".getBytes());
  72             toolProcess.getOutputStream().close();
  73 
  74             // By default child process output stream redirected to pipe, so we are reading it in foreground.
  75             BufferedReader reader = new BufferedReader(new InputStreamReader(toolProcess.getInputStream()));
  76 
  77             String line;
  78             while ((line = reader.readLine()) != null) {
  79                 System.out.println(line.trim());
  80             }
  81 
  82             toolProcess.waitFor();
  83 
  84             return toolProcess.exitValue();
  85         } catch (Exception ex) {
  86             throw new RuntimeException("Test ERROR " + ex, ex);
  87         } finally {
  88             LingeredApp.stopApp(theApp);
  89         }
  90     }
  91 
  92     /**
  93      *
  94      * @param vmArgs  - vm and java arguments to launch test app
  95      * @return exit code of tool
  96      */
  97     public static void launch(String expectedMessage, List<String> toolArgs)
  98         throws IOException {
  99 
 100         System.out.println("Starting LingeredApp");
 101         try {
 102             theApp = LingeredApp.startApp();
 103 
 104             System.out.println("Starting " + toolName + " " + toolArgs.get(0) + " against " + theApp.getPid());
 105             JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK(toolName);
 106 
 107             for (String cmd : toolArgs) {
 108                 launcher.addToolArg(cmd);
 109             }
 110 
 111             launcher.addToolArg("--pid=" + Long.toString(theApp.getPid()));
 112 
 113             ProcessBuilder processBuilder = new ProcessBuilder(launcher.getCommand());
 114             processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT);
 115             OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);;
 116             output.shouldContain(expectedMessage);
 117             output.shouldHaveExitValue(0);
 118 
 119         } catch (Exception ex) {
 120             throw new RuntimeException("Test ERROR " + ex, ex);
 121         } finally {
 122             LingeredApp.stopApp(theApp);
 123         }
 124     }
 125 
 126     public static void launch(String expectedMessage, String... toolArgs)
 127         throws IOException {
 128 
 129         launch(expectedMessage, Arrays.asList(toolArgs));
 130     }
 131 
 132     public static void main(String[] args)
 133         throws IOException {
 134 
 135         if (!Platform.shouldSAAttach()) {
 136             // Silently skip the test if we don't have enough permissions to attach
 137             System.err.println("Error! Insufficient permissions to attach.");
 138             return;
 139         }
 140 
 141         launchCLHSDB();
 142 
 143         launch("No deadlocks found", "jstack");
 144         launch("compiler detected", "jmap");
 145         launch("Java System Properties", "jinfo");
 146         launch("compiler detected.", "jsnap");
 147 
 148         // The test throws RuntimeException on error.
 149         // IOException is thrown if LingeredApp can't start because of some bad
 150         // environment condition
 151         System.out.println("Test PASSED");
 152     }
 153 }