8016221: A unit test should not use a fix port to run a jmx connector
authorsjiang
Wed, 24 Jul 2013 15:47:10 +0200
changeset 19042 d6abf1c83aac
parent 19041 eaa98095d287
child 19043 42407be709a3
8016221: A unit test should not use a fix port to run a jmx connector Reviewed-by: jbachorik, dfuchs
jdk/test/com/sun/management/DiagnosticCommandMBean/DcmdMBeanDoubleInvocationTest.java
jdk/test/com/sun/management/DiagnosticCommandMBean/DcmdMBeanInvocationTest.java
jdk/test/com/sun/management/DiagnosticCommandMBean/DcmdMBeanTest.java
--- a/jdk/test/com/sun/management/DiagnosticCommandMBean/DcmdMBeanDoubleInvocationTest.java	Tue Jul 23 13:21:07 2013 -0700
+++ b/jdk/test/com/sun/management/DiagnosticCommandMBean/DcmdMBeanDoubleInvocationTest.java	Wed Jul 24 15:47:10 2013 +0200
@@ -25,25 +25,15 @@
  * @test
  * @bug     7150256
  * @summary Basic Test for the DiagnosticCommandMBean
- * @author  Frederic Parain
+ * @author  Frederic Parain, Shanliang JIANG
  *
- * @run main/othervm -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=8125 DcmdMBeanDoubleInvocationTest
+ * @run main/othervm DcmdMBeanDoubleInvocationTest
  */
 
 
-import java.io.IOException;
 import java.lang.management.ManagementFactory;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import javax.management.Descriptor;
-import javax.management.InstanceNotFoundException;
-import javax.management.IntrospectionException;
-import javax.management.MBeanInfo;
-import javax.management.MBeanOperationInfo;
 import javax.management.MBeanServer;
-import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
-import javax.management.ReflectionException;
 import javax.management.*;
 import javax.management.remote.*;
 
@@ -52,39 +42,42 @@
     private static String HOTSPOT_DIAGNOSTIC_MXBEAN_NAME =
         "com.sun.management:type=DiagnosticCommand";
 
-    public static void main(String[] args) {
-        MBeanServerConnection mbs = null;
-        try {
-            JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8125/jmxrmi");
-            JMXConnector connector = JMXConnectorFactory.connect(url);
-            mbs = connector.getMBeanServerConnection();
-        } catch(Throwable t) {
-            t.printStackTrace();
-        }
-        ObjectName name;
+    public static void main(String[] args) throws Exception {
+        System.out.println("--->JRCMD MBean Test: invocation on \"help VM.version\" ...");
+
+        ObjectName name = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);
+        String[] helpArgs = {"-all", "\n", "VM.version"};
+        Object[] dcmdArgs = {helpArgs};
+        String[] signature = {String[].class.getName()};
+
+        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
+        JMXConnectorServer cs = null;
+        JMXConnector cc = null;
         try {
-            name = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);
-            MBeanInfo info = mbs.getMBeanInfo(name);
-            String[] helpArgs = {"-all", "\n", "VM.version"};
-            Object[] dcmdArgs = {helpArgs};
-            String[] signature = {String[].class.getName()};
-            String result = (String) mbs.invoke(name, "help", dcmdArgs, signature);
+            cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
+            cs.start();
+            JMXServiceURL addr = cs.getAddress();
+            cc = JMXConnectorFactory.connect(addr);
+            MBeanServerConnection mbsc = cc.getMBeanServerConnection();
+
+            String result = (String) mbsc.invoke(name, "help", dcmdArgs, signature);
             System.out.println(result);
-         } catch (RuntimeMBeanException ex) {
+
+            throw new Error("Test failed: Double commands have not been detected");
+        } catch (RuntimeMBeanException ex) {
             if (ex.getCause() instanceof IllegalArgumentException) {
-                System.out.println("Test passed");
-                return;
+                System.out.println("JTest passed: Double commands have been detected");
             } else {
                 ex.printStackTrace();
-                throw new RuntimeException("TEST FAILED");
+                throw new Error("TEST FAILED: got unknown exception "+ex);
             }
-        } catch (InstanceNotFoundException | IntrospectionException
-                | ReflectionException | MalformedObjectNameException
-                | MBeanException|IOException ex) {
-            ex.printStackTrace();
-            throw new RuntimeException("TEST FAILED");
+        } finally {
+            try {
+                cc.close();
+                cs.stop();
+            } catch (Exception e) {
+            }
         }
-        System.out.println("Double commands have not been detected");
-        throw new RuntimeException("TEST FAILED");
     }
 }
--- a/jdk/test/com/sun/management/DiagnosticCommandMBean/DcmdMBeanInvocationTest.java	Tue Jul 23 13:21:07 2013 -0700
+++ b/jdk/test/com/sun/management/DiagnosticCommandMBean/DcmdMBeanInvocationTest.java	Wed Jul 24 15:47:10 2013 +0200
@@ -25,25 +25,15 @@
  * @test
  * @bug     7150256
  * @summary Basic Test for the DiagnosticCommandMBean
- * @author  Frederic Parain
+ * @author  Frederic Parain, Shanliang JIANG
  *
- * @run main/othervm -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=8129 DcmdMBeanInvocationTest
+ * @run main/othervm DcmdMBeanInvocationTest
  */
 
 
-import java.io.IOException;
 import java.lang.management.ManagementFactory;
-import java.util.logging.Level;
-import java.util.logging.Logger;
-import javax.management.Descriptor;
-import javax.management.InstanceNotFoundException;
-import javax.management.IntrospectionException;
-import javax.management.MBeanInfo;
-import javax.management.MBeanOperationInfo;
 import javax.management.MBeanServer;
-import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
-import javax.management.ReflectionException;
 import javax.management.*;
 import javax.management.remote.*;
 
@@ -52,30 +42,35 @@
     private static String HOTSPOT_DIAGNOSTIC_MXBEAN_NAME =
         "com.sun.management:type=DiagnosticCommand";
 
-    public static void main(String[] args) {
-        MBeanServerConnection mbs = null;
-        try {
-            JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8129/jmxrmi");
-            JMXConnector connector = JMXConnectorFactory.connect(url);
-            mbs = connector.getMBeanServerConnection();
-        } catch(Throwable t) {
-            t.printStackTrace();
-        }
-        ObjectName name;
+    public static void main(String[] args) throws Exception {
+        System.out.println("--->JRCMD MBean Test: invocation on \"help -all\" ...");
+
+        ObjectName name = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);
+        String[] helpArgs = {"-all"};
+        Object[] dcmdArgs = {helpArgs};
+        String[] signature = {String[].class.getName()};
+
+        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
+        JMXConnectorServer cs = null;
+        JMXConnector cc = null;
         try {
-            name = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);
-            MBeanInfo info = mbs.getMBeanInfo(name);
-            String[] helpArgs = {"-all"};
-            Object[] dcmdArgs = {helpArgs};
-            String[] signature = {String[].class.getName()};
-            String result = (String) mbs.invoke(name, "help", dcmdArgs, signature);
+            cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
+            cs.start();
+            JMXServiceURL addr = cs.getAddress();
+            cc = JMXConnectorFactory.connect(addr);
+            MBeanServerConnection mbsc = cc.getMBeanServerConnection();
+
+            String result = (String) mbsc.invoke(name, "help", dcmdArgs, signature);
             System.out.println(result);
-        } catch (InstanceNotFoundException | IntrospectionException
-                | ReflectionException | MalformedObjectNameException
-                | MBeanException|IOException ex) {
-            ex.printStackTrace();
-            throw new RuntimeException("TEST FAILED");
+        } finally {
+            try {
+                cc.close();
+                cs.stop();
+            } catch (Exception e) {
+            }
         }
+
         System.out.println("Test passed");
     }
 }
--- a/jdk/test/com/sun/management/DiagnosticCommandMBean/DcmdMBeanTest.java	Tue Jul 23 13:21:07 2013 -0700
+++ b/jdk/test/com/sun/management/DiagnosticCommandMBean/DcmdMBeanTest.java	Wed Jul 24 15:47:10 2013 +0200
@@ -25,25 +25,18 @@
  * @test
  * @bug     7150256
  * @summary Basic Test for the DiagnosticCommandMBean
- * @author  Frederic Parain
+ * @author  Frederic Parain, Shanliang JIANG
  *
- * @run main/othervm -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=8127 DcmdMBeanTest
+ * @run main/othervm DcmdMBeanTest
  */
 
 
-import java.io.IOException;
 import java.lang.management.ManagementFactory;
-import java.util.logging.Level;
-import java.util.logging.Logger;
 import javax.management.Descriptor;
-import javax.management.InstanceNotFoundException;
-import javax.management.IntrospectionException;
 import javax.management.MBeanInfo;
 import javax.management.MBeanOperationInfo;
 import javax.management.MBeanServer;
-import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
-import javax.management.ReflectionException;
 import javax.management.*;
 import javax.management.remote.*;
 
@@ -52,34 +45,42 @@
     private static String HOTSPOT_DIAGNOSTIC_MXBEAN_NAME =
         "com.sun.management:type=DiagnosticCommand";
 
-    public static void main(String[] args) {
-        MBeanServerConnection mbs = null;
+    public static void main(String[] args) throws Exception {
+        System.out.println("--->JRCMD MBean Test: invocation on \"operation info\"...");
+
+        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
+        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
+        JMXConnectorServer cs = null;
+        JMXConnector cc = null;
         try {
-            JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8127/jmxrmi");
-            JMXConnector connector = JMXConnectorFactory.connect(url);
-            mbs = connector.getMBeanServerConnection();
-        } catch(Throwable t) {
-            t.printStackTrace();
-        }
-        ObjectName name;
-        try {
-            name = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);
-            MBeanInfo info = mbs.getMBeanInfo(name);
+            cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
+            cs.start();
+            JMXServiceURL addr = cs.getAddress();
+            cc = JMXConnectorFactory.connect(addr);
+            MBeanServerConnection mbsc = cc.getMBeanServerConnection();
+            ObjectName name = new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME);
+            MBeanInfo info = mbsc.getMBeanInfo(name);
+
             // the test should check that the MBean doesn't have any
             // Attribute, notification or constructor. Current version only
             // check operations
-            System.out.println("Class Name:"+info.getClassName());
-            System.out.println("Description:"+info.getDescription());
+            System.out.println("Class Name:" + info.getClassName());
+            System.out.println("Description:" + info.getDescription());
             MBeanOperationInfo[] opInfo = info.getOperations();
             System.out.println("Operations:");
-            for(int i=0; i<opInfo.length; i++) {
+            for (int i = 0; i < opInfo.length; i++) {
                 printOperation(opInfo[i]);
                 System.out.println("\n@@@@@@\n");
             }
-        } catch (InstanceNotFoundException|IntrospectionException|ReflectionException
-                 |MalformedObjectNameException|IOException ex) {
-            Logger.getLogger(DcmdMBeanTest.class.getName()).log(Level.SEVERE, null, ex);
+        } finally {
+            try {
+                cc.close();
+                cs.stop();
+            } catch (Exception e) {
+            }
         }
+
+        System.out.println("Test passed");
     }
 
     static void printOperation(MBeanOperationInfo info) {
@@ -110,4 +111,3 @@
         }
     }
 }
-