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  * @modules java.management
  30  */
  31 
  32 import java.util.SortedMap;
  33 import java.util.SortedSet;
  34 import java.util.TreeMap;
  35 import java.util.TreeSet;
  36 import javax.management.MBeanServer;
  37 import javax.management.MBeanServerFactory;
  38 import javax.management.ObjectName;
  39 
  40 public class ComparatorExceptionTest {
  41     public static interface TestMXBean {
  42         public SortedSet<String> getSortedSet();
  43         public SortedMap<String, String> getSortedMap();
  44     }
  45 
  46     public static class TestImpl implements TestMXBean {
  47         public SortedSet<String> getSortedSet() {
  48             return new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
  49         }
  50 
  51         public SortedMap<String, String> getSortedMap() {
  52             return new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
  53         }
  54     }
  55 
  56     private static String failure;
  57 
  58     private static void fail(String why) {
  59         failure = "FAILED: " + why;
  60         System.out.println(failure);
  61     }
  62 
  63     public static void main(String[] args) throws Exception {
  64         MBeanServer mbs = MBeanServerFactory.newMBeanServer();
  65         ObjectName name = new ObjectName("a:b=c");
  66         mbs.registerMBean(new TestImpl(), name);
  67 
  68         for (String attr : new String[] {"SortedSet", "SortedMap"}) {
  69             try {
  70                 Object value = mbs.getAttribute(name, attr);
  71                 fail("get " + attr + " did not throw exception");
  72             } catch (Exception e) {
  73                 Throwable t = e;
  74                 while (!(t instanceof IllegalArgumentException)) {
  75                     if (t == null)
  76                         break;
  77                     t = t.getCause();
  78                 }
  79                 if (t != null)
  80                     System.out.println("Correct exception for " + attr);
  81                 else {
  82                     fail("get " + attr + " got wrong exception");
  83                     e.printStackTrace(System.out);
  84                 }
  85             }
  86         }
  87 
  88         if (failure != null)
  89             throw new Exception(failure);
  90     }
  91 }