1 /*
   2  * Copyright (c) 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 import java.lang.reflect.Method;
  26 
  27 /*
  28  * @test
  29  * @bug 8129215
  30  * @summary The test checks whether the SimpleIntrospector is honoring the
  31  *          the JavaBeans property naming convention of always starting
  32  *          with a lower-case letter
  33  *
  34  * @author Jaroslav Bachorik
  35  * @modules java.management/com.sun.jmx.mbeanserver:open
  36  *
  37  * @run clean SimpleIntrospectorTest
  38  * @run build SimpleIntrospectorTest BeanClass
  39  * @run main SimpleIntrospectorTest
  40  */
  41 public class SimpleIntrospectorTest {
  42     private static Method INTROSPECT_GETTER;
  43 
  44     public static void main(String ... args) throws Exception {
  45         Class clz = Class.forName(
  46             "com.sun.jmx.mbeanserver.Introspector$SimpleIntrospector"
  47         );
  48         INTROSPECT_GETTER = clz.getDeclaredMethod(
  49             "getReadMethod",
  50             Class.class,
  51             String.class
  52         );
  53         INTROSPECT_GETTER.setAccessible(true);
  54         boolean result = true;
  55         result &= checkNumberValid();
  56         result &= checkNumberInvalid();
  57         result &= checkAvailableValid();
  58         result &= checkAvailableInvalid();
  59 
  60         if (!result) {
  61             throw new Error();
  62         }
  63     }
  64 
  65     private static boolean checkNumberValid() throws Exception {
  66         return checkGetter(false, "number");
  67     }
  68 
  69     private static boolean checkNumberInvalid() throws Exception {
  70         return checkGetter(true, "Number");
  71     }
  72 
  73     private static boolean checkAvailableValid() throws Exception {
  74         return checkGetter(false, "available");
  75     }
  76 
  77     private static boolean checkAvailableInvalid() throws Exception {
  78         return checkGetter(true, "Available");
  79     }
  80 
  81     private static boolean checkGetter(boolean nullExpected, String name)
  82     throws Exception {
  83         Method m = getReadMethod(BeanClass.class, name);
  84         boolean result = (m != null);
  85         if (nullExpected) result = !result;
  86 
  87         if (result) {
  88             return true;
  89         }
  90         if (nullExpected) {
  91             System.err.println("SimpleIntrospector resolved an unknown getter " +
  92                                "for attribute '"+ name +"'");
  93         } else {
  94             System.err.println("SimpleIntrospector fails to resolve getter " +
  95                                "for attribute '"+ name +"'");
  96         }
  97         return false;
  98     }
  99 
 100     private static Method getReadMethod(Class clz, String attr)
 101     throws Exception {
 102         return (Method)INTROSPECT_GETTER.invoke(null, clz, attr);
 103     }
 104 }