Path: blob/master/test/jdk/javax/management/openmbean/OpenMBeanInfoHashCodeNPETest.java
41152 views
/*1* Copyright (c) 2013, 2015, 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.MBeanNotificationInfo;24import javax.management.modelmbean.DescriptorSupport;25import javax.management.openmbean.OpenMBeanAttributeInfo;26import javax.management.openmbean.OpenMBeanAttributeInfoSupport;27import javax.management.openmbean.OpenMBeanConstructorInfo;28import javax.management.openmbean.OpenMBeanConstructorInfoSupport;29import javax.management.openmbean.OpenMBeanInfo;30import javax.management.openmbean.OpenMBeanInfoSupport;31import javax.management.openmbean.OpenMBeanOperationInfo;32import javax.management.openmbean.OpenMBeanOperationInfoSupport;33import javax.management.openmbean.OpenMBeanParameterInfo;34import javax.management.openmbean.OpenMBeanParameterInfoSupport;35import javax.management.openmbean.SimpleType;3637/*38* @test39* @bug 802352940* @summary Test that OpenMBean*Info.hashCode do not throw NPE41* @author Shanliang JIANG42*43* @run clean OpenMBeanInfoHashCodeNPETest44* @run build OpenMBeanInfoHashCodeNPETest45* @run main OpenMBeanInfoHashCodeNPETest46*/47public class OpenMBeanInfoHashCodeNPETest {48private static int failed = 0;4950public static void main(String[] args) throws Exception {51System.out.println("---OpenMBeanInfoHashCodeNPETest-main ...");5253// ----54System.out.println("\n---Testing on OpenMBeanInfohashCodeTest...");55OpenMBeanAttributeInfo openMBeanAttributeInfo = new OpenMBeanAttributeInfoSupport(56"name", "description", SimpleType.INTEGER, true, true, false, null, new Integer[]{1, 2, 3});57test(openMBeanAttributeInfo, "defaultValue");5859openMBeanAttributeInfo = new OpenMBeanAttributeInfoSupport(60"name", "description", SimpleType.INTEGER, true, true, false, 1, null);61test(openMBeanAttributeInfo, "legalValues");6263// ----64System.out.println("\n---Testing on OpenMBeanConstructorInfoSupport...");65OpenMBeanConstructorInfo openMBeanConstructorInfo;6667openMBeanConstructorInfo = new OpenMBeanConstructorInfoSupport(68"name", "description", null, new DescriptorSupport());69test(openMBeanConstructorInfo, "sigs");7071openMBeanConstructorInfo = new OpenMBeanConstructorInfoSupport(72"name", "description", new OpenMBeanParameterInfo[]{}, null);73test(openMBeanConstructorInfo, "Descriptor");7475// ----76System.out.println("\n---Testing on OpenMBeanOperationInfoSupport...");77OpenMBeanOperationInfo openMBeanOperationInfo;7879openMBeanOperationInfo = new OpenMBeanOperationInfoSupport(80"name", "description", null, SimpleType.INTEGER, 1, new DescriptorSupport());81test(openMBeanOperationInfo, "sigs");8283openMBeanOperationInfo = new OpenMBeanOperationInfoSupport(84"name", "description", new OpenMBeanParameterInfo[]{}, SimpleType.INTEGER, 1, null);85test(openMBeanOperationInfo, "Descriptor");8687// ----88System.out.println("\n---Testing on OpenMBeanParameterInfoSupport 1...");89OpenMBeanParameterInfo openMBeanParameterInfo;9091openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(92"name", "description", SimpleType.INTEGER, null, -1, 1);93test(openMBeanParameterInfo, "default value");9495openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(96"name", "description", SimpleType.INTEGER, 0, null, 1);97test(openMBeanParameterInfo, "min value");9899openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(100"name", "description", SimpleType.INTEGER, 0, -1, null);101test(openMBeanParameterInfo, "max value");102103// ----104System.out.println("\n---Testing on OpenMBeanParameterInfoSupport 2...");105openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(106"name", "description", SimpleType.INTEGER, 1, new Integer[]{-1, 1, 2});107108openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(109"name", "description", SimpleType.INTEGER, null, new Integer[]{-1, 1, 2});110test(openMBeanParameterInfo, "default value");111112openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(113"name", "description", SimpleType.INTEGER, 1, null);114test(openMBeanParameterInfo, "legal values");115116// ----117System.out.println("\n---Testing on OpenMBeanInfoSupport...");118String className = "toto";119String description = "titi";120OpenMBeanAttributeInfo[] attrInfos = new OpenMBeanAttributeInfo[]{};121OpenMBeanConstructorInfo[] constrInfos = new OpenMBeanConstructorInfo[]{};122OpenMBeanOperationInfo[] operaInfos = new OpenMBeanOperationInfo[]{};123MBeanNotificationInfo[] notifInfos = new MBeanNotificationInfo[]{};124125OpenMBeanInfo ominfo = new OpenMBeanInfoSupport(null, description, attrInfos, constrInfos, operaInfos, notifInfos);126test(ominfo, "class name");127128ominfo = new OpenMBeanInfoSupport(className, null, attrInfos, constrInfos, operaInfos, notifInfos);129test(ominfo, "description");130131ominfo = new OpenMBeanInfoSupport(className, description, null, constrInfos, operaInfos, notifInfos);132test(ominfo, "attrInfos");133134ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, null, operaInfos, notifInfos);135test(ominfo, "constructor infos");136137ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, constrInfos, null, notifInfos);138test(ominfo, "operation infos");139140ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, constrInfos, operaInfos, null);141test(ominfo, "notif infos");142143if (failed > 0) {144throw new RuntimeException("Test failed: "+failed);145} else {146System.out.println("---Test: PASSED");147}148}149150private static void test(Object obj, String param) {151try {152obj.hashCode();153System.out.println("OK-1: "+obj.getClass().getSimpleName()+154".hashCode worked with a null paramer: "+param);155} catch (NullPointerException npe) {156System.out.println("--->KO-1!!! "+obj.getClass().getSimpleName()+157".hashCode got NPE with null paramer: "+param);158npe.printStackTrace();159failed++;160}161162try {163obj.toString();164System.out.println("OK-1: "+obj.getClass().getSimpleName()+165".toString worked with a null paramer: "+param);166} catch (NullPointerException npe) {167System.out.println("--->KO-1!!! "+obj.getClass().getSimpleName()+168".toString got NPE with null paramer: "+param);169npe.printStackTrace();170failed++;171}172}173}174175176