Path: blob/master/test/jdk/javax/management/mxbean/ComparatorExceptionTest.java
41152 views
/*1* Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 660165226* @summary Test exception when SortedMap or SortedSet has non-null Comparator27* @author Eamonn McManus28*/2930import java.util.SortedMap;31import java.util.SortedSet;32import java.util.TreeMap;33import java.util.TreeSet;34import javax.management.MBeanServer;35import javax.management.MBeanServerFactory;36import javax.management.ObjectName;3738public class ComparatorExceptionTest {39public static interface TestMXBean {40public SortedSet<String> getSortedSet();41public SortedMap<String, String> getSortedMap();42}4344public static class TestImpl implements TestMXBean {45public SortedSet<String> getSortedSet() {46return new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);47}4849public SortedMap<String, String> getSortedMap() {50return new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);51}52}5354private static String failure;5556private static void fail(String why) {57failure = "FAILED: " + why;58System.out.println(failure);59}6061public static void main(String[] args) throws Exception {62MBeanServer mbs = MBeanServerFactory.newMBeanServer();63ObjectName name = new ObjectName("a:b=c");64mbs.registerMBean(new TestImpl(), name);6566for (String attr : new String[] {"SortedSet", "SortedMap"}) {67try {68Object value = mbs.getAttribute(name, attr);69fail("get " + attr + " did not throw exception");70} catch (Exception e) {71Throwable t = e;72while (!(t instanceof IllegalArgumentException)) {73if (t == null)74break;75t = t.getCause();76}77if (t != null)78System.out.println("Correct exception for " + attr);79else {80fail("get " + attr + " got wrong exception");81e.printStackTrace(System.out);82}83}84}8586if (failure != null)87throw new Exception(failure);88}89}909192