1 /*
   2  * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6601652
  27  * @summary Test exception when SortedMap or SortedSet has non-null Comparator
  28  * @author Eamonn McManus
  29  */
  30 
  31 import java.util.SortedMap;
  32 import java.util.SortedSet;
  33 import java.util.TreeMap;
  34 import java.util.TreeSet;
  35 import javax.management.MBeanServer;
  36 import javax.management.MBeanServerFactory;
  37 import javax.management.ObjectName;
  38 
  39 public class ComparatorExceptionTest {
  40     public static interface TestMXBean {
  41         public SortedSet<String> getSortedSet();
  42         public SortedMap<String, String> getSortedMap();
  43     }
  44 
  45     public static class TestImpl implements TestMXBean {
  46         public SortedSet<String> getSortedSet() {
  47             return new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
  48         }
  49 
  50         public SortedMap<String, String> getSortedMap() {
  51             return new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
  52         }
  53     }
  54 
  55     private static String failure;
  56 
  57     private static void fail(String why) {
  58         failure = "FAILED: " + why;
  59         System.out.println(failure);
  60     }
  61 
  62     public static void main(String[] args) throws Exception {
  63         MBeanServer mbs = MBeanServerFactory.newMBeanServer();
  64         ObjectName name = new ObjectName("a:b=c");
  65         mbs.registerMBean(new TestImpl(), name);
  66 
  67         for (String attr : new String[] {"SortedSet", "SortedMap"}) {
  68             try {
  69                 Object value = mbs.getAttribute(name, attr);
  70                 fail("get " + attr + " did not throw exception");
  71             } catch (Exception e) {
  72                 Throwable t = e;
  73                 while (!(t instanceof IllegalArgumentException)) {
  74                     if (t == null)
  75                         break;
  76                     t = t.getCause();
  77                 }
  78                 if (t != null)
  79                     System.out.println("Correct exception for " + attr);
  80                 else {
  81                     fail("get " + attr + " got wrong exception");
  82                     e.printStackTrace(System.out);
  83                 }
  84             }
  85         }
  86 
  87         if (failure != null)
  88             throw new Exception(failure);
  89     }
  90 }