Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/openmbean/OpenMBeanInfoHashCodeNPETest.java
38838 views
/*1* Copyright (c) 2013, 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* @run clean OpenMBeanInfoHashCodeNPETest43* @run build OpenMBeanInfoHashCodeNPETest44* @run main OpenMBeanInfoHashCodeNPETest45*/46public class OpenMBeanInfoHashCodeNPETest {47private static int failed = 0;4849public static void main(String[] args) throws Exception {50System.out.println("---OpenMBeanInfoHashCodeNPETest-main ...");5152// ----53System.out.println("\n---Testing on OpenMBeanInfohashCodeTest...");54OpenMBeanAttributeInfo openMBeanAttributeInfo = new OpenMBeanAttributeInfoSupport(55"name", "description", SimpleType.INTEGER, true, true, false, null, new Integer[]{1, 2, 3});56test(openMBeanAttributeInfo, "defaultValue");5758openMBeanAttributeInfo = new OpenMBeanAttributeInfoSupport(59"name", "description", SimpleType.INTEGER, true, true, false, 1, null);60test(openMBeanAttributeInfo, "legalValues");6162// ----63System.out.println("\n---Testing on OpenMBeanConstructorInfoSupport...");64OpenMBeanConstructorInfo openMBeanConstructorInfo;6566openMBeanConstructorInfo = new OpenMBeanConstructorInfoSupport(67"name", "description", null, new DescriptorSupport());68test(openMBeanConstructorInfo, "sigs");6970openMBeanConstructorInfo = new OpenMBeanConstructorInfoSupport(71"name", "description", new OpenMBeanParameterInfo[]{}, null);72test(openMBeanConstructorInfo, "Descriptor");7374// ----75System.out.println("\n---Testing on OpenMBeanOperationInfoSupport...");76OpenMBeanOperationInfo openMBeanOperationInfo;7778openMBeanOperationInfo = new OpenMBeanOperationInfoSupport(79"name", "description", null, SimpleType.INTEGER, 1, new DescriptorSupport());80test(openMBeanOperationInfo, "sigs");8182openMBeanOperationInfo = new OpenMBeanOperationInfoSupport(83"name", "description", new OpenMBeanParameterInfo[]{}, SimpleType.INTEGER, 1, null);84test(openMBeanOperationInfo, "Descriptor");8586// ----87System.out.println("\n---Testing on OpenMBeanParameterInfoSupport 1...");88OpenMBeanParameterInfo openMBeanParameterInfo;8990openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(91"name", "description", SimpleType.INTEGER, null, -1, 1);92test(openMBeanParameterInfo, "default value");9394openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(95"name", "description", SimpleType.INTEGER, 0, null, 1);96test(openMBeanParameterInfo, "min value");9798openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(99"name", "description", SimpleType.INTEGER, 0, -1, null);100test(openMBeanParameterInfo, "max value");101102// ----103System.out.println("\n---Testing on OpenMBeanParameterInfoSupport 2...");104openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(105"name", "description", SimpleType.INTEGER, 1, new Integer[]{-1, 1, 2});106107openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(108"name", "description", SimpleType.INTEGER, null, new Integer[]{-1, 1, 2});109test(openMBeanParameterInfo, "default value");110111openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(112"name", "description", SimpleType.INTEGER, 1, null);113test(openMBeanParameterInfo, "legal values");114115// ----116System.out.println("\n---Testing on OpenMBeanInfoSupport...");117String className = "toto";118String description = "titi";119OpenMBeanAttributeInfo[] attrInfos = new OpenMBeanAttributeInfo[]{};120OpenMBeanConstructorInfo[] constrInfos = new OpenMBeanConstructorInfo[]{};121OpenMBeanOperationInfo[] operaInfos = new OpenMBeanOperationInfo[]{};122MBeanNotificationInfo[] notifInfos = new MBeanNotificationInfo[]{};123124OpenMBeanInfo ominfo = new OpenMBeanInfoSupport(null, description, attrInfos, constrInfos, operaInfos, notifInfos);125test(ominfo, "class name");126127ominfo = new OpenMBeanInfoSupport(className, null, attrInfos, constrInfos, operaInfos, notifInfos);128test(ominfo, "description");129130ominfo = new OpenMBeanInfoSupport(className, description, null, constrInfos, operaInfos, notifInfos);131test(ominfo, "attrInfos");132133ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, null, operaInfos, notifInfos);134test(ominfo, "constructor infos");135136ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, constrInfos, null, notifInfos);137test(ominfo, "operation infos");138139ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, constrInfos, operaInfos, null);140test(ominfo, "notif infos");141142if (failed > 0) {143throw new RuntimeException("Test failed: "+failed);144} else {145System.out.println("---Test: PASSED");146}147}148149private static void test(Object obj, String param) {150try {151obj.hashCode();152System.out.println("OK-1: "+obj.getClass().getSimpleName()+153".hashCode worked with a null paramer: "+param);154} catch (NullPointerException npe) {155System.out.println("--->KO-1!!! "+obj.getClass().getSimpleName()+156".hashCode got NPE with null paramer: "+param);157npe.printStackTrace();158failed++;159}160161try {162obj.toString();163System.out.println("OK-1: "+obj.getClass().getSimpleName()+164".toString worked with a null paramer: "+param);165} catch (NullPointerException npe) {166System.out.println("--->KO-1!!! "+obj.getClass().getSimpleName()+167".toString got NPE with null paramer: "+param);168npe.printStackTrace();169failed++;170}171}172}173174175