6601652: MXBeans: no IllegalArgumentException in the ex. chain for SortedSet/Map with a non-null comparator()

Forward-port this bug fix from JDK 6

Reviewed-by: dfuchs, lmalvent
This commit is contained in:
Eamonn McManus 2008-07-04 18:55:37 +02:00
parent a9cd129c27
commit 5967d518b5
4 changed files with 194 additions and 13 deletions
jdk
src/share/classes/com/sun/jmx/mbeanserver
test/javax/management/mxbean

@ -686,7 +686,7 @@ public class DefaultMXBeanMappingFactory extends MXBeanMappingFactory {
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 @@ public class DefaultMXBeanMappingFactory extends MXBeanMappingFactory {
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();

@ -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);
}
}

@ -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.Arrays;
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.StandardMBean;
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 @@ public class MXBeanTest {
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 @@ public class MXBeanTest {
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 @@ public class MXBeanTest {
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 @@ public class MXBeanTest {
/* 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";

@ -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 {}
}