670N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
670N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
670N/A *
670N/A * This code is free software; you can redistribute it and/or modify it
670N/A * under the terms of the GNU General Public License version 2 only, as
670N/A * published by the Free Software Foundation.
670N/A *
670N/A * This code is distributed in the hope that it will be useful, but WITHOUT
670N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
670N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
670N/A * version 2 for more details (a copy is included in the LICENSE file that
670N/A * accompanied this code).
670N/A *
670N/A * You should have received a copy of the GNU General Public License version
670N/A * 2 along with this work; if not, write to the Free Software Foundation,
670N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
670N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
670N/A */
670N/A
670N/A/*
670N/A * @test
689N/A * @bug 6757225 6763051
670N/A * @summary Test that type names in MXBeans match their spec.
670N/A * @author Eamonn McManus
670N/A */
670N/A
670N/Aimport java.lang.reflect.Field;
670N/Aimport java.lang.reflect.InvocationHandler;
670N/Aimport java.lang.reflect.Method;
670N/Aimport java.lang.reflect.Proxy;
670N/Aimport java.util.List;
670N/Aimport java.util.Map;
670N/Aimport javax.management.MBeanAttributeInfo;
670N/Aimport javax.management.MBeanInfo;
670N/Aimport javax.management.MBeanServer;
670N/Aimport javax.management.MBeanServerFactory;
670N/Aimport javax.management.ObjectName;
670N/Aimport javax.management.StandardMBean;
688N/Aimport javax.management.openmbean.TabularData;
688N/Aimport javax.management.openmbean.TabularType;
670N/A
670N/Apublic class TypeNameTest {
670N/A public static interface TestMXBean {
670N/A public int getInt();
670N/A public String IntName = "int";
670N/A
670N/A public Map<String, Integer> getMapSI();
670N/A public String MapSIName = "java.util.Map<java.lang.String, java.lang.Integer>";
670N/A
670N/A public Map<String, int[]> getMapSInts();
670N/A public String MapSIntsName = "java.util.Map<java.lang.String, int[]>";
670N/A
670N/A public List<List<int[]>> getListListInts();
670N/A public String ListListIntsName = "java.util.List<java.util.List<int[]>>";
670N/A }
670N/A
670N/A private static InvocationHandler nullIH = new InvocationHandler() {
670N/A public Object invoke(Object proxy, Method method, Object[] args)
670N/A throws Throwable {
670N/A return null;
670N/A }
670N/A };
670N/A
688N/A static volatile String failure;
670N/A
670N/A public static void main(String[] args) throws Exception {
670N/A TestMXBean testImpl = (TestMXBean) Proxy.newProxyInstance(
670N/A TestMXBean.class.getClassLoader(), new Class<?>[] {TestMXBean.class}, nullIH);
670N/A Object mxbean = new StandardMBean(testImpl, TestMXBean.class, true);
670N/A MBeanServer mbs = MBeanServerFactory.newMBeanServer();
670N/A ObjectName name = new ObjectName("a:b=c");
670N/A mbs.registerMBean(mxbean, name);
670N/A MBeanInfo mbi = mbs.getMBeanInfo(name);
670N/A MBeanAttributeInfo[] mbais = mbi.getAttributes();
688N/A boolean sawTabular = false;
670N/A for (MBeanAttributeInfo mbai : mbais) {
670N/A String attrName = mbai.getName();
670N/A String attrTypeName = (String) mbai.getDescriptor().getFieldValue("originalType");
670N/A String fieldName = attrName + "Name";
670N/A Field nameField = TestMXBean.class.getField(fieldName);
670N/A String expectedTypeName = (String) nameField.get(null);
688N/A
670N/A if (expectedTypeName.equals(attrTypeName)) {
670N/A System.out.println("OK: " + attrName + ": " + attrTypeName);
670N/A } else {
688N/A fail("For attribute " + attrName + " expected type name \"" +
670N/A expectedTypeName + "\", found type name \"" + attrTypeName +
688N/A "\"");
688N/A }
688N/A
688N/A if (mbai.getType().equals(TabularData.class.getName())) {
688N/A sawTabular = true;
688N/A TabularType tt = (TabularType) mbai.getDescriptor().getFieldValue("openType");
688N/A if (tt.getTypeName().equals(attrTypeName)) {
688N/A System.out.println("OK: TabularType name for " + attrName);
688N/A } else {
688N/A fail("For attribute " + attrName + " expected TabularType " +
688N/A "name \"" + attrTypeName + "\", found \"" +
688N/A tt.getTypeName());
688N/A }
670N/A }
670N/A }
688N/A
688N/A if (!sawTabular)
688N/A fail("Test bug: did not test TabularType name");
688N/A
670N/A if (failure == null)
670N/A System.out.println("TEST PASSED");
670N/A else
670N/A throw new Exception("TEST FAILED: " + failure);
670N/A }
688N/A
688N/A private static void fail(String why) {
688N/A System.out.println("FAIL: " + why);
688N/A failure = why;
688N/A }
670N/A}