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