Path: blob/master/test/jdk/javax/management/MBeanInfo/MBeanInfoHashCodeNPETest.java
41149 views
/*1* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.management.MBeanAttributeInfo;24import javax.management.MBeanConstructorInfo;25import javax.management.MBeanInfo;26import javax.management.MBeanNotificationInfo;27import javax.management.MBeanOperationInfo;28import javax.management.MBeanParameterInfo;29import javax.management.modelmbean.DescriptorSupport;30import javax.management.openmbean.SimpleType;3132/*33* @test34* @bug 802366935* @summary Test that hashCode()throws NullPointerException36* @author Shanliang JIANG37*38* @run clean MBeanInfoHashCodeNPETest39* @run build MBeanInfoHashCodeNPETest40* @run main MBeanInfoHashCodeNPETest41*/42public class MBeanInfoHashCodeNPETest {43private static int failed = 0;4445public static void main(String[] args) throws Exception {46System.out.println("---MBeanInfoHashCodeNPETest-main ...");4748// ----49System.out.println("\n---Testing on MBeanAttributeInfo...");50MBeanAttributeInfo mbeanAttributeInfo = new MBeanAttributeInfo(51null, SimpleType.INTEGER.getClassName(), "description", true, true, false);52test(mbeanAttributeInfo, "class name");5354mbeanAttributeInfo = new MBeanAttributeInfo(55"name", null, "description", true, true, false);56test(mbeanAttributeInfo, "type");5758mbeanAttributeInfo = new MBeanAttributeInfo(59"name", SimpleType.INTEGER.getClassName(), null, true, true, false);60test(mbeanAttributeInfo, "description");6162// ----63System.out.println("\n---Testing on MBeanConstructorInfo...");64MBeanConstructorInfo mbeanConstructorInfo = new MBeanConstructorInfo(65null, "", new MBeanParameterInfo[]{}, new DescriptorSupport());66test(mbeanConstructorInfo, "name");6768mbeanConstructorInfo = new MBeanConstructorInfo(69"", null, new MBeanParameterInfo[]{}, new DescriptorSupport());70test(mbeanConstructorInfo, "description");7172mbeanConstructorInfo = new MBeanConstructorInfo(73"", "", null, new DescriptorSupport());74test(mbeanConstructorInfo, "MBeanParameterInfo");7576mbeanConstructorInfo = new MBeanConstructorInfo(77"", "", new MBeanParameterInfo[]{}, null);78test(mbeanConstructorInfo, "descriptor");7980// ----81System.out.println("\n---Testing on MBeanOperationInfo...");82MBeanOperationInfo mbeanOperationInfo = new MBeanOperationInfo(83null, "description", new MBeanParameterInfo[]{}, "type", 1, new DescriptorSupport());84test(mbeanOperationInfo, "name");8586mbeanOperationInfo = new MBeanOperationInfo(87"name", null, new MBeanParameterInfo[]{}, "type", 1, new DescriptorSupport());88test(mbeanOperationInfo, "description");8990mbeanOperationInfo = new MBeanOperationInfo(91"name", "description", null, "type", 1, new DescriptorSupport());92test(mbeanOperationInfo, "MBeanParameterInfo");9394mbeanOperationInfo = new MBeanOperationInfo(95"name", "description", new MBeanParameterInfo[]{}, null, 1, new DescriptorSupport());96test(mbeanOperationInfo, "type");9798mbeanOperationInfo = new MBeanOperationInfo(99"name", "description", new MBeanParameterInfo[]{}, "type", 1, null);100test(mbeanOperationInfo, "Descriptor");101102// ----103System.out.println("\n---Testing on MBeanParameterInfo...");104MBeanParameterInfo mbeanParameterInfo = new MBeanParameterInfo(105null, "type", "description", new DescriptorSupport());106test(mbeanParameterInfo, "name");107108mbeanParameterInfo = new MBeanParameterInfo(109"name", null, "description", new DescriptorSupport());110test(mbeanParameterInfo, "description");111112mbeanParameterInfo = new MBeanParameterInfo(113"name", "type", null, new DescriptorSupport());114test(mbeanParameterInfo, "description");115116mbeanParameterInfo = new MBeanParameterInfo(117"name", "type", "description", null);118test(mbeanParameterInfo, "Descriptor");119120// ----121System.out.println("\n---Testing on MBeanInfo...");122String className = "toto";123String description = "titi";124MBeanAttributeInfo[] attrInfos = new MBeanAttributeInfo[]{};125MBeanConstructorInfo[] constrInfos = new MBeanConstructorInfo[]{};126MBeanOperationInfo[] operaInfos = new MBeanOperationInfo[]{};127MBeanNotificationInfo[] notifInfos = new MBeanNotificationInfo[]{};128129MBeanInfo minfo = new MBeanInfo(null, description, attrInfos, constrInfos, operaInfos, notifInfos);130test(minfo, "class name");131132minfo = new MBeanInfo(className, description, attrInfos, constrInfos, operaInfos, notifInfos);133test(minfo, "name");134135minfo = new MBeanInfo(className, null, attrInfos, constrInfos, operaInfos, notifInfos);136test(minfo, "description");137138minfo = new MBeanInfo(className, description, null, constrInfos, operaInfos, notifInfos);139test(minfo, "attrInfos");140141minfo = new MBeanInfo(className, description, attrInfos, constrInfos, null, notifInfos);142test(minfo, "operaInfos");143144minfo = new MBeanInfo(className, description, attrInfos, constrInfos, operaInfos, null);145test(minfo, "notifInfos");146147Thread.sleep(100);148if (failed > 0) {149throw new RuntimeException("Test failed: "+failed);150} else {151System.out.println("---Test: PASSED");152}153}154155private static void test(Object obj, String param) {156try {157obj.hashCode();158System.out.println("OK: "+obj.getClass().getSimpleName()+".hashCode worked with a null "+param);159} catch (NullPointerException npe) {160System.out.println("--->KO!!! "+obj.getClass().getSimpleName()+".hashCode got NPE with a null "+param);161failed++;162}163164try {165obj.toString();166System.out.println("OK: "+obj.getClass().getSimpleName()+".toString worked with a null "+param);167} catch (NullPointerException npe) {168System.out.println("--->KO!!! "+obj.getClass().getSimpleName()+".toString got NPE.");169failed++;170}171}172}173174175