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.Arrays;
  25 
  26 import jdk.testlibrary.JDKToolLauncher;
  27 import jdk.testlibrary.OutputAnalyzer;
  28 import jdk.testlibrary.ProcessTools;
  29 
  30 /*
  31  * @test
  32  * @summary Unit test for jinfo utility
  33  * @library /lib/testlibrary
  34  * @build jdk.testlibrary.*
  35  * @run main BasicJInfoTest
  36  */
  37 public class BasicJInfoTest {
  38 
  39     private static ProcessBuilder processBuilder = new ProcessBuilder();
  40 
  41     public static void main(String[] args) throws Exception {
  42         testJinfoNoArgs();
  43         testJinfoFlags();
  44         testJinfoProps();
  45         testJinfoFlagInvalid();
  46     }
  47 
  48     private static void testJinfoNoArgs() throws Exception {
  49         OutputAnalyzer output = jinfo();
  50         output.shouldContain("-XX");
  51         output.shouldContain("test.jdk=");
  52         output.shouldHaveExitValue(0);
  53     }
  54 
  55     private static void testJinfoFlagInvalid() throws Exception {
  56         OutputAnalyzer output = jinfo("-flag");
  57         output.shouldHaveExitValue(1);
  58     }
  59 
  60     private static void testJinfoFlags() throws Exception {
  61         OutputAnalyzer output = jinfo("-flags");
  62         output.shouldContain("-XX");
  63         output.shouldHaveExitValue(0);
  64     }
  65 
  66     private static void testJinfoProps() throws Exception {
  67         OutputAnalyzer output = jinfo("-props");
  68         output.shouldContain("test.jdk=");
  69         output.shouldHaveExitValue(0);
  70     }
  71 
  72     private static OutputAnalyzer jinfo(String... toolArgs) throws Exception {
  73         JDKToolLauncher launcher = JDKToolLauncher.createUsingTestJDK("jinfo");
  74         if (toolArgs != null) {
  75             for (String toolArg : toolArgs) {
  76                 launcher.addToolArg(toolArg);
  77             }
  78         }
  79         launcher.addToolArg(Long.toString(ProcessTools.getProcessId()));
  80 
  81         processBuilder.command(launcher.getCommand());
  82         System.out.println(Arrays.toString(processBuilder.command().toArray()).replace(",", ""));
  83         OutputAnalyzer output = ProcessTools.executeProcess(processBuilder);
  84         System.out.println(output.getOutput());
  85 
  86         return output;
  87     }
  88 
  89 }