jdk/test/com/sun/management/VMOptionOpenDataTest.java
changeset 42338 a60f280f803c
parent 30820 0d4717a011d3
child 44423 306c020eb154
--- a/jdk/test/com/sun/management/VMOptionOpenDataTest.java	Wed Nov 23 16:16:35 2016 +0000
+++ b/jdk/test/com/sun/management/VMOptionOpenDataTest.java	Thu Dec 01 08:57:53 2016 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -21,58 +21,77 @@
  * questions.
  */
 
+import com.sun.management.HotSpotDiagnosticMXBean;
 import com.sun.management.VMOption;
-import java.io.InvalidObjectException;
-import java.util.Objects;
-import javax.management.openmbean.OpenDataException;
-import sun.management.MappedMXBeanType;
+import java.lang.management.ManagementFactory;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.stream.Collectors;
+import javax.management.MBeanServerConnection;
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.OpenType;
+
+import static javax.management.openmbean.SimpleType.*;
 
 /*
  * @test
  * @bug     8042901
  * @summary Check that MappedMXBeanType.toOpenTypeData supports VMOption
- * @modules java.management/sun.management
- *          jdk.management/com.sun.management
+ * @modules jdk.management/com.sun.management
  * @author  Shanliang Jiang
  */
 public class VMOptionOpenDataTest {
-    public static void main(String[] args) throws Exception {
-        System.out.println("--- VMOptionOpenDataTest-main: Checking that "
-                + "MappedMXBeanType.toOpenTypeData supports VMOption");
-        Exception failed = null;
-        try {
-            VMOption vo = new VMOption("toto", "titi", true, VMOption.Origin.OTHER);
-            System.out.println("--- Construct a VMOption object: \"" + vo + "\"");
+    private static final String[] names = new String[] {
+        "name", "value", "origin", "writeable"
+    };
+    private static final OpenType[] types = new OpenType[] {
+        STRING, STRING, STRING, BOOLEAN
+    };
 
-            Object open = MappedMXBeanType.toOpenTypeData(vo, VMOption.class);
-            System.out.println("--- Map it to an open type:  \"" + open +" \"");
+    public static void main(String... args) throws Exception {
+        MBeanServerConnection msc = ManagementFactory.getPlatformMBeanServer();
+        HotSpotDiagnosticMXBean mxbean =
+            ManagementFactory.getPlatformMXBean(msc, HotSpotDiagnosticMXBean.class);
+
 
-            Object back = MappedMXBeanType.toJavaTypeData(open, VMOption.class);
-            System.out.println("--- Map it back to java type:  \"" + back +" \"");
+        String[] signatures = new String[] {
+            String.class.getName()
+        };
+        Object obj = msc.invoke(mxbean.getObjectName(), "getVMOption",
+            new String[] { "PrintVMOptions"}, signatures);
 
-            if (back == null) {
-                failed = new RuntimeException("Failed, mapping back got null.");
-            } else if (!(back instanceof VMOption)) {
-                failed = new RuntimeException("Failed, not mapped back to a VMOption: "
-                        +back.getClass());
-            } else {
-                VMOption mapBack = (VMOption)back;
-                if (!Objects.equals(vo.getName(), mapBack.getName()) ||
-                        !Objects.equals(vo.getOrigin(), mapBack.getOrigin()) ||
-                        !Objects.equals(vo.getValue(), mapBack.getValue()) ||
-                        vo.isWriteable() != mapBack.isWriteable()) {
-                    failed = new RuntimeException(
-                            "Failed, failed to map back the original VMOtion.");
-                }
+        CompositeData data = (CompositeData)obj;
+        validateType(data);
+
+        VMOption option = mxbean.getVMOption("PrintVMOptions");
+        VMOption o = VMOption.from(data);
+        assertEquals(option, o);
+    }
+
+    private static void validateType(CompositeData data) {
+        CompositeType type = data.getCompositeType();
+        Set<String> keys = Arrays.stream(names).collect(Collectors.toSet());
+        if (!type.keySet().equals(keys)) {
+            throw new RuntimeException("key not matched: " + type.keySet().toString());
+        }
+        for (int i=0; i < names.length; i++) {
+            OpenType t = type.getType(names[i]);
+            if (t != types[i]) {
+                throw new AssertionError(names[i] + ": type not matched: " +
+                    t + " expected: " + types[i]);
             }
-        } catch (OpenDataException | InvalidObjectException ode) {
-            failed = ode;
-        }
-        if (failed == null) {
-            System.out.println("--- PASSED!");
-        } else {
-            System.out.println("--- Failed: "+failed.getMessage());
-            throw failed;
         }
     }
+
+    private static void assertEquals(VMOption o1, VMOption o2) {
+        if (!o1.getName().equals(o2.getName()) ||
+            !o1.getOrigin().equals(o2.getOrigin()) ||
+            !o1.getValue().equals(o2.getValue()) ||
+            o1.isWriteable() != o2.isWriteable()) {
+            throw new AssertionError(o1 + " != " + o2);
+        }
+
+    }
+
 }