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  * @author  Shanliang Jiang
42  */
43 public class VMOptionOpenDataTest {
44     private static final String[] names = new String[] {
45         "name", "value", "origin", "writeable"
46     };
47     private static final OpenType[] types = new OpenType[] {
48         STRING, STRING, STRING, BOOLEAN
49     };
50 
51     public static void main(String... args) throws Exception {
52         MBeanServerConnection msc = ManagementFactory.getPlatformMBeanServer();
53         HotSpotDiagnosticMXBean mxbean =
54             ManagementFactory.getPlatformMXBean(msc, HotSpotDiagnosticMXBean.class);
55 
56 
57         String[] signatures = new String[] {
58             String.class.getName()
59         };
60         Object obj = msc.invoke(mxbean.getObjectName(), "getVMOption",
61             new String[] { "PrintVMOptions"}, signatures);
62 
63         CompositeData data = (CompositeData)obj;
64         validateType(data);
65 
66         VMOption option = mxbean.getVMOption("PrintVMOptions");
67         VMOption o = VMOption.from(data);
68         assertEquals(option, o);
69     }
70 
71     private static void validateType(CompositeData data) {
72         CompositeType type = data.getCompositeType();
73         Set<String> keys = Arrays.stream(names).collect(Collectors.toSet());
74         if (!type.keySet().equals(keys)) {
75             throw new RuntimeException("key not matched: " + type.keySet().toString());
76         }
77         for (int i=0; i < names.length; i++) {
78             OpenType t = type.getType(names[i]);
79             if (t != types[i]) {
80                 throw new AssertionError(names[i] + ": type not matched: " +
81                     t + " expected: " + types[i]);
82             }
83         }
84     }
85 
86     private static void assertEquals(VMOption o1, VMOption o2) {
87         if (!o1.getName().equals(o2.getName()) ||
88             !o1.getOrigin().equals(o2.getOrigin()) ||
89             !o1.getValue().equals(o2.getValue()) ||
90             o1.isWriteable() != o2.isWriteable()) {
91             throw new AssertionError(o1 + " != " + o2);
92         }
93 
94     }
95 
96 }
97