1 /*
   2  * Copyright (c) 2011, 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  * @bug     7104647
  27  * @summary Basic Test for HotSpotDiagnosticMXBean.getDiagnosticCommandInfo()
  28  * @author  Frederic Parain
  29  *
  30  * @run main GetDiagnosticCommandInfo
  31  */
  32 
  33 import com.sun.management.HotSpotDiagnosticMXBean;
  34 import com.sun.management.DiagnosticCommandInfo;
  35 import com.sun.management.DiagnosticCommandArgumentInfo;
  36 import com.sun.management.VMOption;
  37 import java.lang.management.ManagementFactory;
  38 import java.util.List;
  39 import javax.management.MBeanServer;
  40 
  41 public class GetDiagnosticCommandInfo {
  42     private static String HOTSPOT_DIAGNOSTIC_MXBEAN_NAME =
  43         "com.sun.management:type=HotSpotDiagnostic";
  44 
  45     public static void main(String[] args) throws Exception {
  46         HotSpotDiagnosticMXBean mbean =
  47             ManagementFactory.getPlatformMXBean(HotSpotDiagnosticMXBean.class);
  48         checkDiagnosticCommandArguments(mbean);
  49 
  50         MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
  51         mbean = ManagementFactory.newPlatformMXBeanProxy(mbs,
  52                     HOTSPOT_DIAGNOSTIC_MXBEAN_NAME,
  53                     HotSpotDiagnosticMXBean.class);
  54         checkDiagnosticCommandArguments(mbean);
  55     }
  56 
  57     private static void checkDiagnosticCommandArguments(HotSpotDiagnosticMXBean mbean) {
  58         // check getDiagnosticCommandInfo()
  59         StringBuilder sb = new StringBuilder();
  60         List<DiagnosticCommandInfo> infoList = mbean.getDiagnosticCommandInfo();
  61         for(DiagnosticCommandInfo info : infoList) {
  62             printCommandInfo(info,sb);
  63         }
  64         // check getDiagnosticCommandInfo(List<String>)
  65         List<String> commands = mbean.getDiagnosticCommands();
  66         List<DiagnosticCommandInfo> list2 =
  67             mbean.getDiagnosticCommandInfo(commands);
  68         for(DiagnosticCommandInfo info : list2) {
  69             printCommandInfo(info,sb);
  70         }
  71         // check getDiagnosticCommandInfo(String)
  72         for(String cmd : commands) {
  73             DiagnosticCommandInfo info2 = mbean.getDiagnosticCommandInfo(cmd);
  74             printCommandInfo(info2,sb);
  75         }
  76         System.out.println(sb.toString());
  77     }
  78 
  79     private static void printCommandInfo(DiagnosticCommandInfo info,
  80                                          StringBuilder sb) {
  81         sb.append("\t").append(info.getName()).append(":\n");
  82         sb.append("\t\tDescription=").append(info.getDescription()).append("\n");
  83         sb.append("\t\tImpact=").append(info.getImpact()).append("\n");
  84         sb.append("\t\tStatus=");
  85         if (info.isEnabled()) {
  86             sb.append("Enabled\n");
  87         } else {
  88             sb.append("Disbled\n");
  89         }
  90         sb.append("\t\tArguments=");
  91         for(DiagnosticCommandArgumentInfo arg : info.getArgumentsInfo()) {
  92             printArgumentInfo(arg,sb);
  93         }
  94     }
  95 
  96     private static void printArgumentInfo(DiagnosticCommandArgumentInfo info,
  97                                           StringBuilder sb) {
  98         sb.append("\t\t\t").append(info.getName()).append(":\n");
  99         sb.append("\t\t\t\tType=").append(info.getType()).append("\n");
 100         sb.append("\t\t\t\tDescription=").append(info.getDescription()).append("\n");
 101         if(info.getDefault() != null) {
 102             sb.append("\t\t\t\tDefault=").append(info.getDefault()).append("\n");
 103         }
 104         if(info.isMandatory()) {
 105             sb.append("\t\t\t\tMandatory\n");
 106         } else {
 107             sb.append("\t\t\t\tOptional\n");
 108         }
 109         if(info.isOption()) {
 110             sb.append("\t\t\t\tIs an option\n");
 111         } else {
 112             sb.append("\t\t\t\tIs an argument expected at position");
 113             sb.append(info.getPosition());
 114             sb.append("\n");
 115         }
 116     }
 117 }