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