1 /*
   2  * Copyright (c) 2015, 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 com.sun.management.HotSpotDiagnosticMXBean;
  25 import com.sun.management.VMOption;
  26 import java.lang.management.ManagementFactory;
  27 import java.util.Arrays;
  28 import java.util.Set;
  29 import java.util.stream.Collectors;
  30 import javax.management.MBeanServerConnection;
  31 import javax.management.openmbean.CompositeData;
  32 import javax.management.openmbean.CompositeType;
  33 import javax.management.openmbean.OpenType;
  34 
  35 import static javax.management.openmbean.SimpleType.*;
  36 
  37 /*
  38  * @test
  39  * @bug     8042901
  40  * @summary Check that MappedMXBeanType.toOpenTypeData supports VMOption
  41  * @modules jdk.management/com.sun.management
  42  * @author  Shanliang Jiang
  43  */
  44 public class VMOptionOpenDataTest {
  45     private static final String[] names = new String[] {
  46         "name", "value", "origin", "writeable"
  47     };
  48     private static final OpenType[] types = new OpenType[] {
  49         STRING, STRING, STRING, BOOLEAN
  50     };
  51 
  52     public static void main(String... args) throws Exception {
  53         MBeanServerConnection msc = ManagementFactory.getPlatformMBeanServer();
  54         HotSpotDiagnosticMXBean mxbean =
  55             ManagementFactory.getPlatformMXBean(msc, HotSpotDiagnosticMXBean.class);
  56 
  57 
  58         String[] signatures = new String[] {
  59             String.class.getName()
  60         };
  61         Object obj = msc.invoke(mxbean.getObjectName(), "getVMOption",
  62             new String[] { "PrintVMOptions"}, signatures);
  63 
  64         CompositeData data = (CompositeData)obj;
  65         validateType(data);
  66 
  67         VMOption option = mxbean.getVMOption("PrintVMOptions");
  68         VMOption o = VMOption.from(data);
  69         assertEquals(option, o);
  70     }
  71 
  72     private static void validateType(CompositeData data) {
  73         CompositeType type = data.getCompositeType();
  74         Set<String> keys = Arrays.stream(names).collect(Collectors.toSet());
  75         if (!type.keySet().equals(keys)) {
  76             throw new RuntimeException("key not matched: " + type.keySet().toString());
  77         }
  78         for (int i=0; i < names.length; i++) {
  79             OpenType t = type.getType(names[i]);
  80             if (t != types[i]) {
  81                 throw new AssertionError(names[i] + ": type not matched: " +
  82                     t + " expected: " + types[i]);
  83             }
  84         }
  85     }
  86 
  87     private static void assertEquals(VMOption o1, VMOption o2) {
  88         if (!o1.getName().equals(o2.getName()) ||
  89             !o1.getOrigin().equals(o2.getOrigin()) ||
  90             !o1.getValue().equals(o2.getValue()) ||
  91             o1.isWriteable() != o2.isWriteable()) {
  92             throw new AssertionError(o1 + " != " + o2);
  93         }
  94 
  95     }
  96 
  97 }