1 /*
   2  * Copyright (c) 2005, 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 6281446
  27  * @summary Test that the exception thrown by DynamicMBean.getMBeanInfo()
  28  *          keeps the init cause.
  29  * @author Luis-Miguel Alventosa
  30  * @run clean GetMBeanInfoExceptionTest
  31  * @run build GetMBeanInfoExceptionTest
  32  * @run main GetMBeanInfoExceptionTest
  33  */
  34 
  35 import javax.management.Attribute;
  36 import javax.management.AttributeList;
  37 import javax.management.AttributeNotFoundException;
  38 import javax.management.DynamicMBean;
  39 import javax.management.InvalidAttributeValueException;
  40 import javax.management.MBeanException;
  41 import javax.management.MBeanInfo;
  42 import javax.management.MBeanServer;
  43 import javax.management.MBeanServerFactory;
  44 import javax.management.NotCompliantMBeanException;
  45 import javax.management.ObjectName;
  46 import javax.management.ReflectionException;
  47 
  48 public class GetMBeanInfoExceptionTest {
  49 
  50     public static class TestDynamicMBean implements DynamicMBean {
  51 
  52         public Object getAttribute(String attribute) throws
  53             AttributeNotFoundException,
  54             MBeanException,
  55             ReflectionException {
  56             return null;
  57         }
  58 
  59         public void setAttribute(Attribute attribute) throws
  60             AttributeNotFoundException,
  61             InvalidAttributeValueException,
  62             MBeanException,
  63             ReflectionException {
  64         }
  65 
  66         public AttributeList getAttributes(String[] attributes) {
  67             return null;
  68         }
  69 
  70         public AttributeList setAttributes(AttributeList attributes) {
  71             return null;
  72         }
  73 
  74         public Object invoke(String op, Object params[], String sign[]) throws
  75             MBeanException,
  76             ReflectionException {
  77             return null;
  78         }
  79 
  80         public MBeanInfo getMBeanInfo() {
  81             throw new IllegalArgumentException("GetMBeanInfoExceptionTest");
  82         }
  83     }
  84 
  85     public static void main(String[] args) throws Exception {
  86 
  87         int error = 0;
  88 
  89         // Instantiate the MBean server
  90         //
  91         System.out.println("Create the MBean server");
  92         MBeanServer mbs = MBeanServerFactory.createMBeanServer();
  93 
  94         // Register the MBean
  95         //
  96         System.out.println("Create a TestDynamicMBean");
  97         TestDynamicMBean obj = new TestDynamicMBean();
  98         ObjectName n = new ObjectName("d:k=v");
  99         try {
 100             mbs.registerMBean(obj, n);
 101             System.out.println("Didn't get expected NotCompliantMBeanException");
 102             error++;
 103         } catch (NotCompliantMBeanException e) {
 104             boolean found = false;
 105             Throwable t = e.getCause();
 106             while (t != null) {
 107                 if (t instanceof IllegalArgumentException &&
 108                     "GetMBeanInfoExceptionTest".equals(t.getMessage())) {
 109                     found = true;
 110                 }
 111                 t = t.getCause();
 112             }
 113             if (found) {
 114                 System.out.println("Found expected IllegalArgumentException");
 115             } else {
 116                 System.out.println("Didn't find expected IllegalArgumentException");
 117                 error++;
 118             }
 119         } catch (Exception e) {
 120             System.out.println("Got " + e.getClass().getName() +
 121                 "instead of expected NotCompliantMBeanException");
 122             error++;
 123         }
 124 
 125         if (error > 0) {
 126             System.out.println("Test failed");
 127             throw new IllegalArgumentException("Test failed");
 128         } else {
 129             System.out.println("Test passed");
 130         }
 131     }
 132 }