1 
2 /*
3  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 import java.lang.reflect.Method;
27 
28 /*
29  * @test
30  * @bug 8129215
31  * @summary The test checks whether the SimpleIntrospector is honoring the
32  *          the JavaBeans property naming convention of always starting
33  *          with a lower-case letter
34  *
35  * @author Jaroslav Bachorik
36  * @modules java.management/com.sun.jmx.mbeanserver:open
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 
main(String .... args)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 
checkNumberValid()65     private static boolean checkNumberValid() throws Exception {
66         return checkGetter(false, "number");
67     }
68 
checkNumberInvalid()69     private static boolean checkNumberInvalid() throws Exception {
70         return checkGetter(true, "Number");
71     }
72 
checkAvailableValid()73     private static boolean checkAvailableValid() throws Exception {
74         return checkGetter(false, "available");
75     }
76 
checkAvailableInvalid()77     private static boolean checkAvailableInvalid() throws Exception {
78         return checkGetter(true, "Available");
79     }
80 
checkGetter(boolean nullExpected, String name)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 
getReadMethod(Class clz, String attr)100     private static Method getReadMethod(Class clz, String attr)
101     throws Exception {
102         return (Method)INTROSPECT_GETTER.invoke(null, clz, attr);
103     }
104 }
105