1 /*
   2  * Copyright (c) 2005, 2016, 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.util.ArrayList;
  25 import java.util.Arrays;
  26 import java.util.List;
  27 import java.util.regex.Matcher;
  28 import java.util.regex.Pattern;
  29 import java.io.IOException;
  30 
  31 import jdk.test.lib.JDKToolLauncher;
  32 import jdk.test.lib.process.OutputAnalyzer;
  33 import jdk.test.lib.process.ProcessTools;
  34 import jdk.test.lib.apps.LingeredApp;
  35 
  36 /*
  37  * @test
  38  * @summary Unit test for jinfo utility
  39  * @modules java.base/jdk.internal.misc
  40  * @library /test/lib
  41  * @build jdk.test.lib.*
  42  * @build jdk.test.lib.apps.*
  43  * @build jdk.test.lib.process.*
  44  * @run main JInfoTest
  45  */
  46 public class JInfoTest {
  47 
  48     private static ProcessBuilder processBuilder = new ProcessBuilder();
  49 
  50     public static void main(String[] args) throws Exception {
  51         classNameMatch();
  52         setMultipleFlags();
  53         setFlag();
  54     }
  55 
  56     private static void setFlag() throws Exception {
  57         System.out.println("#### setFlag ####");
  58         LingeredApp app1 = new JInfoTestLingeredApp();
  59         LingeredApp app2 = new JInfoTestLingeredApp();
  60         try {
  61             ArrayList<String> params = new ArrayList<String>();
  62             LingeredApp.startApp(params, app1);
  63             LingeredApp.startApp(params, app2);
  64             OutputAnalyzer output = jinfo("-flag", "MinHeapFreeRatio=1", "JInfoTestLingeredApp");
  65             output.shouldHaveExitValue(0);
  66             output = jinfo("-flag", "MinHeapFreeRatio", "JInfoTestLingeredApp");
  67             output.shouldHaveExitValue(0);
  68             documentMatch(output.getStdout(), ".*MinHeapFreeRatio=1.*MinHeapFreeRatio=1.*");
  69         } finally {
  70             JInfoTestLingeredApp.stopApp(app1);
  71             JInfoTestLingeredApp.stopApp(app2);
  72         }
  73     }
  74 
  75     private static void setMultipleFlags() throws Exception {
  76         System.out.println("#### setMultipleFlags ####");
  77         OutputAnalyzer output = jinfo("-sysprops", "-flag", "MinHeapFreeRatio=1", "-flags", "JInfoTestLingeredApp");
  78         output.shouldHaveExitValue(1);
  79     }
  80 
  81     private static void classNameMatch() throws Exception {
  82         System.out.println("#### classNameMatch ####");
  83         LingeredApp app1 = new JInfoTestLingeredApp();
  84         LingeredApp app2 = new JInfoTestLingeredApp();
  85         try {
  86             ArrayList<String> params = new ArrayList<String>();
  87             LingeredApp.startApp(params, app1);
  88             LingeredApp.startApp(params, app2);
  89             OutputAnalyzer output = jinfo("JInfoTestLingeredApp");
  90             output.shouldHaveExitValue(0);
  91             // "HotSpot(TM)" written once per proc
  92             documentMatch(output.getStdout(), ".*HotSpot\\(TM\\).*HotSpot\\(TM\\).*");
  93         } finally {
  94             JInfoTestLingeredApp.stopApp(app1);
  95             JInfoTestLingeredApp.stopApp(app2);
  96         }
  97     }
  98 
  99     private static void documentMatch(String data, String pattern){
 100         Matcher matcher = Pattern.compile(pattern, Pattern.DOTALL).matcher(data);
 101         if (!matcher.find()) {
 102             throw new RuntimeException("'" + pattern + "' missing from stdout \n");
 103         }
 104     }
 105 
 106     private static OutputAnalyzer jinfo(String... toolArgs) throws Exception {
 107         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jinfo");
 108         if (toolArgs != null) {
 109             for (String toolArg : toolArgs) {
 110                 launcher.addToolArg(toolArg);
 111             }
 112         }
 113 
 114         processBuilder.command(launcher.getCommand());
 115         OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
 116 
 117         return output;
 118     }
 119 }
 120 
 121 // Sometime there is LingeredApp's from other test still around
 122 class JInfoTestLingeredApp extends LingeredApp {
 123 }
 124