6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()
authoremcmanus
Fri, 04 Jul 2008 18:55:37 +0200
changeset 831 50f701930577
parent 830 8f4d875a65fc
child 832 5484c7a35278
child 833 bfa2bef7517c
child 836 d81b1f62fb82
child 903 c324f61770cf
6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator() Summary: Forward-port this bug fix from JDK 6 Reviewed-by: dfuchs, lmalvent
jdk/src/share/classes/com/sun/jmx/mbeanserver/DefaultMXBeanMappingFactory.java
jdk/test/javax/management/mxbean/ComparatorExceptionTest.java
jdk/test/javax/management/mxbean/MXBeanTest.java
jdk/test/javax/management/mxbean/SameObjectTwoNamesTest.java
--- a/jdk/src/share/classes/com/sun/jmx/mbeanserver/DefaultMXBeanMappingFactory.java	Wed Jul 02 09:37:42 2008 -0700
+++ b/jdk/src/share/classes/com/sun/jmx/mbeanserver/DefaultMXBeanMappingFactory.java	Fri Jul 04 18:55:37 2008 +0200
@@ -686,7 +686,7 @@
                     final String msg =
                         "Cannot convert SortedSet with non-null comparator: " +
                         comparator;
-                    throw new OpenDataException(msg);
+                    throw openDataException(msg, new IllegalArgumentException(msg));
                 }
             }
             final Object[] openArray = (Object[])
@@ -800,7 +800,7 @@
                     final String msg =
                         "Cannot convert SortedMap with non-null comparator: " +
                         comparator;
-                    throw new OpenDataException(msg);
+                    throw openDataException(msg, new IllegalArgumentException(msg));
                 }
             }
             final TabularType tabularType = (TabularType) getOpenType();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/management/mxbean/ComparatorExceptionTest.java	Fri Jul 04 18:55:37 2008 +0200
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc.  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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6601652
+ * @summary Test exception when SortedMap or SortedSet has non-null Comparator
+ * @author Eamonn McManus
+ */
+
+import java.util.SortedMap;
+import java.util.SortedSet;
+import java.util.TreeMap;
+import java.util.TreeSet;
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.ObjectName;
+
+public class ComparatorExceptionTest {
+    public static interface TestMXBean {
+        public SortedSet<String> getSortedSet();
+        public SortedMap<String, String> getSortedMap();
+    }
+
+    public static class TestImpl implements TestMXBean {
+        public SortedSet<String> getSortedSet() {
+            return new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
+        }
+
+        public SortedMap<String, String> getSortedMap() {
+            return new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
+        }
+    }
+
+    private static String failure;
+
+    private static void fail(String why) {
+        failure = "FAILED: " + why;
+        System.out.println(failure);
+    }
+
+    public static void main(String[] args) throws Exception {
+        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
+        ObjectName name = new ObjectName("a:b=c");
+        mbs.registerMBean(new TestImpl(), name);
+
+        for (String attr : new String[] {"SortedSet", "SortedMap"}) {
+            try {
+                Object value = mbs.getAttribute(name, attr);
+                fail("get " + attr + " did not throw exception");
+            } catch (Exception e) {
+                Throwable t = e;
+                while (!(t instanceof IllegalArgumentException)) {
+                    if (t == null)
+                        break;
+                    t = t.getCause();
+                }
+                if (t != null)
+                    System.out.println("Correct exception for " + attr);
+                else {
+                    fail("get " + attr + " got wrong exception");
+                    e.printStackTrace(System.out);
+                }
+            }
+        }
+
+        if (failure != null)
+            throw new Exception(failure);
+    }
+}
--- a/jdk/test/javax/management/mxbean/MXBeanTest.java	Wed Jul 02 09:37:42 2008 -0700
+++ b/jdk/test/javax/management/mxbean/MXBeanTest.java	Fri Jul 04 18:55:37 2008 +0200
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 6175517 6278707 6318827 6305746 6392303
+ * @bug 6175517 6278707 6318827 6305746 6392303 6600709
  * @summary General MXBean test.
  * @author Eamonn McManus
  * @run clean MXBeanTest MerlinMXBean TigerMXBean
@@ -40,7 +40,8 @@
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
-import javax.management.Attribute;
+import java.util.Map;
+import java.util.SortedMap;
 import javax.management.JMX;
 import javax.management.MBeanAttributeInfo;
 import javax.management.MBeanInfo;
@@ -55,10 +56,6 @@
 import javax.management.openmbean.ArrayType;
 import javax.management.openmbean.CompositeData;
 import javax.management.openmbean.CompositeDataInvocationHandler;
-import javax.management.openmbean.OpenMBeanAttributeInfo;
-import javax.management.openmbean.OpenMBeanInfo;
-import javax.management.openmbean.OpenMBeanOperationInfo;
-import javax.management.openmbean.OpenMBeanParameterInfo;
 import javax.management.openmbean.OpenType;
 import javax.management.openmbean.SimpleType;
 import javax.management.openmbean.TabularData;
@@ -81,10 +78,8 @@
 
         if (failures == 0)
             System.out.println("Test passed");
-        else {
-            System.out.println("TEST FAILURES: " + failures);
-            System.exit(1);
-        }
+        else
+            throw new Exception("TEST FAILURES: " + failures);
     }
 
     private static int failures = 0;
@@ -561,6 +556,11 @@
                 return false;
             return deepEqual(o1, o2, namedMXBeans);
         }
+        if (o1 instanceof Map) {
+            if (!(o2 instanceof Map))
+                return false;
+            return equalMap((Map) o1, (Map) o2, namedMXBeans);
+        }
         if (o1 instanceof CompositeData && o2 instanceof CompositeData) {
             return compositeDataEqual((CompositeData) o1, (CompositeData) o2,
                                       namedMXBeans);
@@ -600,6 +600,21 @@
         return true;
     }
 
+    private static boolean equalMap(Map<?,?> m1, Map<?,?> m2,
+                                    NamedMXBeans namedMXBeans) {
+        if (m1.size() != m2.size())
+            return false;
+        if ((m1 instanceof SortedMap) != (m2 instanceof SortedMap))
+            return false;
+        for (Object k1 : m1.keySet()) {
+            if (!m2.containsKey(k1))
+                return false;
+            if (!equal(m1.get(k1), m2.get(k1), namedMXBeans))
+                return false;
+        }
+        return true;
+    }
+
     // This is needed to work around a bug (5095277)
     // in CompositeDataSupport.equals
     private static boolean compositeDataEqual(CompositeData cd1,
@@ -655,7 +670,7 @@
     /* I wanted to call this method toString(Object), but oddly enough
        this meant that I couldn't call it from the inner class
        MXBeanImplInvocationHandler, because the inherited Object.toString()
-       prevented that.  Surprising behaviour.  */
+       prevented that.  */
     static String string(Object o) {
         if (o == null)
             return "null";
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/management/mxbean/SameObjectTwoNamesTest.java	Fri Jul 04 18:55:37 2008 +0200
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2007 Sun Microsystems, Inc.  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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test SameObjectTwoNamesTest.java
+ * @bug 6283873
+ * @summary Check that registering the same MXBean under two different
+ * names produces an exception
+ * @author Alexander Shusherov
+ * @author Eamonn McManus
+ * @run main SameObjectTwoNamesTest
+ * @run main/othervm -Djmx.mxbean.multiname=true SameObjectTwoNamesTest
+ */
+
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.ObjectName;
+
+public class SameObjectTwoNamesTest {
+
+    public static void main(String[] args) throws Exception {
+        boolean expectException =
+                (System.getProperty("jmx.mxbean.multiname") == null);
+        try {
+            ObjectName objectName1 = new ObjectName("test:index=1");
+            ObjectName objectName2 = new ObjectName("test:index=2");
+            MBeanServer mbs = MBeanServerFactory.createMBeanServer();
+            MXBC_SimpleClass01 mxBeanObject = new MXBC_SimpleClass01();
+
+            mbs.registerMBean(mxBeanObject, objectName1);
+
+            mbs.registerMBean(mxBeanObject, objectName2);
+
+            if (expectException) {
+                throw new Exception("TEST FAILED: " +
+                        "InstanceAlreadyExistsException was not thrown");
+            } else
+                System.out.println("Correctly got no exception with compat property");
+        } catch (InstanceAlreadyExistsException e) {
+            if (expectException) {
+                System.out.println("Got expected InstanceAlreadyExistsException:");
+                e.printStackTrace(System.out);
+            } else {
+                throw new Exception(
+                        "TEST FAILED: Got exception even though compat property set", e);
+            }
+        }
+        System.out.println("TEST PASSED");
+    }
+
+    public interface MXBC_Simple01MXBean {}
+
+    public static class MXBC_SimpleClass01 implements MXBC_Simple01MXBean {}
+
+}