OpenMBeanInfoHashCodeNPETest.java revision 16930:13c06d444258
1/*
2 * Copyright (c) 2013, 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
24import javax.management.MBeanNotificationInfo;
25import javax.management.modelmbean.DescriptorSupport;
26import javax.management.openmbean.OpenMBeanAttributeInfo;
27import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
28import javax.management.openmbean.OpenMBeanConstructorInfo;
29import javax.management.openmbean.OpenMBeanConstructorInfoSupport;
30import javax.management.openmbean.OpenMBeanInfo;
31import javax.management.openmbean.OpenMBeanInfoSupport;
32import javax.management.openmbean.OpenMBeanOperationInfo;
33import javax.management.openmbean.OpenMBeanOperationInfoSupport;
34import javax.management.openmbean.OpenMBeanParameterInfo;
35import javax.management.openmbean.OpenMBeanParameterInfoSupport;
36import javax.management.openmbean.SimpleType;
37
38/*
39 * @test
40 * @bug 8023529
41 * @summary Test that OpenMBean*Info.hashCode do not throw NPE
42 * @author Shanliang JIANG
43 *
44 * @run clean OpenMBeanInfoHashCodeNPETest
45 * @run build OpenMBeanInfoHashCodeNPETest
46 * @run main OpenMBeanInfoHashCodeNPETest
47 */
48public class OpenMBeanInfoHashCodeNPETest {
49    private static int failed = 0;
50
51    public static void main(String[] args) throws Exception {
52        System.out.println("---OpenMBeanInfoHashCodeNPETest-main ...");
53
54        // ----
55        System.out.println("\n---Testing on OpenMBeanInfohashCodeTest...");
56        OpenMBeanAttributeInfo openMBeanAttributeInfo = new OpenMBeanAttributeInfoSupport(
57                "name", "description", SimpleType.INTEGER, true, true, false, null, new Integer[]{1, 2, 3});
58        test(openMBeanAttributeInfo, "defaultValue");
59
60        openMBeanAttributeInfo = new OpenMBeanAttributeInfoSupport(
61                "name", "description", SimpleType.INTEGER, true, true, false, 1, null);
62        test(openMBeanAttributeInfo, "legalValues");
63
64        // ----
65        System.out.println("\n---Testing on OpenMBeanConstructorInfoSupport...");
66        OpenMBeanConstructorInfo openMBeanConstructorInfo;
67
68        openMBeanConstructorInfo = new OpenMBeanConstructorInfoSupport(
69                "name", "description", null, new DescriptorSupport());
70        test(openMBeanConstructorInfo, "sigs");
71
72        openMBeanConstructorInfo = new OpenMBeanConstructorInfoSupport(
73                "name", "description", new OpenMBeanParameterInfo[]{}, null);
74        test(openMBeanConstructorInfo, "Descriptor");
75
76        // ----
77        System.out.println("\n---Testing on OpenMBeanOperationInfoSupport...");
78        OpenMBeanOperationInfo openMBeanOperationInfo;
79
80        openMBeanOperationInfo = new OpenMBeanOperationInfoSupport(
81                "name", "description", null,  SimpleType.INTEGER, 1, new DescriptorSupport());
82        test(openMBeanOperationInfo, "sigs");
83
84        openMBeanOperationInfo = new OpenMBeanOperationInfoSupport(
85                "name", "description", new OpenMBeanParameterInfo[]{},  SimpleType.INTEGER, 1, null);
86        test(openMBeanOperationInfo, "Descriptor");
87
88        // ----
89        System.out.println("\n---Testing on OpenMBeanParameterInfoSupport 1...");
90        OpenMBeanParameterInfo openMBeanParameterInfo;
91
92        openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(
93                "name", "description", SimpleType.INTEGER, null, -1, 1);
94        test(openMBeanParameterInfo, "default value");
95
96        openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(
97                "name", "description", SimpleType.INTEGER, 0, null, 1);
98        test(openMBeanParameterInfo, "min value");
99
100        openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(
101                "name", "description", SimpleType.INTEGER, 0, -1, null);
102        test(openMBeanParameterInfo, "max value");
103
104        // ----
105        System.out.println("\n---Testing on OpenMBeanParameterInfoSupport 2...");
106        openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(
107                "name", "description", SimpleType.INTEGER, 1, new Integer[]{-1, 1, 2});
108
109        openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(
110                "name", "description", SimpleType.INTEGER, null, new Integer[]{-1, 1, 2});
111        test(openMBeanParameterInfo, "default value");
112
113        openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(
114                "name", "description", SimpleType.INTEGER, 1, null);
115        test(openMBeanParameterInfo, "legal values");
116
117        // ----
118        System.out.println("\n---Testing on OpenMBeanInfoSupport...");
119        String className = "toto";
120        String description = "titi";
121        OpenMBeanAttributeInfo[] attrInfos = new OpenMBeanAttributeInfo[]{};
122        OpenMBeanConstructorInfo[] constrInfos = new OpenMBeanConstructorInfo[]{};
123        OpenMBeanOperationInfo[] operaInfos = new OpenMBeanOperationInfo[]{};
124        MBeanNotificationInfo[] notifInfos = new MBeanNotificationInfo[]{};
125
126        OpenMBeanInfo ominfo = new OpenMBeanInfoSupport(null, description, attrInfos, constrInfos, operaInfos, notifInfos);
127        test(ominfo, "class name");
128
129        ominfo = new OpenMBeanInfoSupport(className, null, attrInfos, constrInfos, operaInfos, notifInfos);
130        test(ominfo, "description");
131
132        ominfo = new OpenMBeanInfoSupport(className, description, null, constrInfos, operaInfos, notifInfos);
133        test(ominfo, "attrInfos");
134
135        ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, null, operaInfos, notifInfos);
136        test(ominfo, "constructor infos");
137
138        ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, constrInfos, null, notifInfos);
139        test(ominfo, "operation infos");
140
141        ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, constrInfos, operaInfos, null);
142        test(ominfo, "notif infos");
143
144        if (failed > 0) {
145            throw new RuntimeException("Test failed: "+failed);
146        } else {
147            System.out.println("---Test: PASSED");
148        }
149    }
150
151    private static void test(Object obj, String param) {
152        try {
153            obj.hashCode();
154            System.out.println("OK-1: "+obj.getClass().getSimpleName()+
155                    ".hashCode worked with a null paramer: "+param);
156        } catch (NullPointerException npe) {
157            System.out.println("--->KO-1!!! "+obj.getClass().getSimpleName()+
158                    ".hashCode got NPE with null paramer: "+param);
159            npe.printStackTrace();
160            failed++;
161        }
162
163        try {
164            obj.toString();
165            System.out.println("OK-1: "+obj.getClass().getSimpleName()+
166                    ".toString worked with a null paramer: "+param);
167        } catch (NullPointerException npe) {
168            System.out.println("--->KO-1!!! "+obj.getClass().getSimpleName()+
169                    ".toString got NPE with null paramer: "+param);
170            npe.printStackTrace();
171            failed++;
172        }
173    }
174}
175