1/*
2 * Copyright (c) 2008, 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 6757225 6763051
27 * @summary Test that type names in MXBeans match their spec.
28 * @author Eamonn McManus
29 */
30
31import java.lang.reflect.Field;
32import java.lang.reflect.InvocationHandler;
33import java.lang.reflect.Method;
34import java.lang.reflect.Proxy;
35import java.util.List;
36import java.util.Map;
37import javax.management.MBeanAttributeInfo;
38import javax.management.MBeanInfo;
39import javax.management.MBeanServer;
40import javax.management.MBeanServerFactory;
41import javax.management.ObjectName;
42import javax.management.StandardMBean;
43import javax.management.openmbean.TabularData;
44import javax.management.openmbean.TabularType;
45
46public class TypeNameTest {
47    public static interface TestMXBean {
48        public int getInt();
49        public String IntName = "int";
50
51        public Map<String, Integer> getMapSI();
52        public String MapSIName = "java.util.Map<java.lang.String, java.lang.Integer>";
53
54        public Map<String, int[]> getMapSInts();
55        public String MapSIntsName = "java.util.Map<java.lang.String, int[]>";
56
57        public List<List<int[]>> getListListInts();
58        public String ListListIntsName = "java.util.List<java.util.List<int[]>>";
59    }
60
61    private static InvocationHandler nullIH = new InvocationHandler() {
62        public Object invoke(Object proxy, Method method, Object[] args)
63                throws Throwable {
64            return null;
65        }
66    };
67
68    static volatile String failure;
69
70    public static void main(String[] args) throws Exception {
71        TestMXBean testImpl = (TestMXBean) Proxy.newProxyInstance(
72                TestMXBean.class.getClassLoader(), new Class<?>[] {TestMXBean.class}, nullIH);
73        Object mxbean = new StandardMBean(testImpl, TestMXBean.class, true);
74        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
75        ObjectName name = new ObjectName("a:b=c");
76        mbs.registerMBean(mxbean, name);
77        MBeanInfo mbi = mbs.getMBeanInfo(name);
78        MBeanAttributeInfo[] mbais = mbi.getAttributes();
79        boolean sawTabular = false;
80        for (MBeanAttributeInfo mbai : mbais) {
81            String attrName = mbai.getName();
82            String attrTypeName = (String) mbai.getDescriptor().getFieldValue("originalType");
83            String fieldName = attrName + "Name";
84            Field nameField = TestMXBean.class.getField(fieldName);
85            String expectedTypeName = (String) nameField.get(null);
86
87            if (expectedTypeName.equals(attrTypeName)) {
88                System.out.println("OK: " + attrName + ": " + attrTypeName);
89            } else {
90                fail("For attribute " + attrName + " expected type name \"" +
91                        expectedTypeName + "\", found type name \"" + attrTypeName +
92                        "\"");
93            }
94
95            if (mbai.getType().equals(TabularData.class.getName())) {
96                sawTabular = true;
97                TabularType tt = (TabularType) mbai.getDescriptor().getFieldValue("openType");
98                if (tt.getTypeName().equals(attrTypeName)) {
99                    System.out.println("OK: TabularType name for " + attrName);
100                } else {
101                    fail("For attribute " + attrName + " expected TabularType " +
102                            "name \"" + attrTypeName + "\", found \"" +
103                            tt.getTypeName());
104                }
105            }
106        }
107
108        if (!sawTabular)
109            fail("Test bug: did not test TabularType name");
110
111        if (failure == null)
112            System.out.println("TEST PASSED");
113        else
114            throw new Exception("TEST FAILED: " + failure);
115    }
116
117    private static void fail(String why) {
118        System.out.println("FAIL: " + why);
119        failure = why;
120    }
121}
122