Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/openmbean/OpenMBeanInfoEqualsNPETest.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.MBeanOperationInfo;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;3738/*39* @test40* @bug 802352941* @summary Test that OpenMBean*Info.equals do not throw NPE42* @author Shanliang JIANG43* @run clean OpenMBeanInfoEqualsNPETest44* @run build OpenMBeanInfoEqualsNPETest45* @run main OpenMBeanInfoEqualsNPETest46*/47public class OpenMBeanInfoEqualsNPETest {48private static int failed = 0;4950public static void main(String[] args) throws Exception {51System.out.println("---OpenMBeanInfoEqualsNPETest-main ...");5253// ----54System.out.println("\n---Testing on OpenMBeanAttributeInfoSupport...");55OpenMBeanAttributeInfo openMBeanAttributeInfo0 = new OpenMBeanAttributeInfoSupport(56"name", "description", SimpleType.INTEGER, true, true, false, 1, new Integer[]{1, 2, 3});57OpenMBeanAttributeInfo openMBeanAttributeInfo = new OpenMBeanAttributeInfoSupport(58"name", "description", SimpleType.INTEGER, true, true, false, null, new Integer[]{1, 2, 3});59test(openMBeanAttributeInfo0, openMBeanAttributeInfo, "defaultValue");6061openMBeanAttributeInfo = new OpenMBeanAttributeInfoSupport(62"name", "description", SimpleType.INTEGER, true, true, false, 1, null);63test(openMBeanAttributeInfo0, openMBeanAttributeInfo, "legalValues");6465// ----66System.out.println("\n---Testing on OpenMBeanConstructorInfoSupport...");67OpenMBeanConstructorInfo openMBeanConstructorInfo0 = new OpenMBeanConstructorInfoSupport(68"name", "description", new OpenMBeanParameterInfo[]{}, new DescriptorSupport());69OpenMBeanConstructorInfo openMBeanConstructorInfo;7071openMBeanConstructorInfo = new OpenMBeanConstructorInfoSupport(72"name", "description", null, new DescriptorSupport());73test(openMBeanConstructorInfo0, openMBeanConstructorInfo, "sigs");7475openMBeanConstructorInfo = new OpenMBeanConstructorInfoSupport(76"name", "description", new OpenMBeanParameterInfo[]{}, null);77test(openMBeanConstructorInfo0, openMBeanConstructorInfo, "Descriptor");7879// ----80System.out.println("\n---Testing on OpenMBeanOperationInfoSupport...");81OpenMBeanOperationInfo openMBeanOperationInfo0 = new OpenMBeanOperationInfoSupport(82"name", "description", new OpenMBeanParameterInfo[]{}, SimpleType.INTEGER, 1, new DescriptorSupport());83OpenMBeanOperationInfo openMBeanOperationInfo;8485openMBeanOperationInfo = new OpenMBeanOperationInfoSupport(86"name", "description", null, SimpleType.INTEGER, 1, new DescriptorSupport());87test(openMBeanOperationInfo0, openMBeanOperationInfo, "sigs");8889openMBeanOperationInfo = new OpenMBeanOperationInfoSupport(90"name", "description", new OpenMBeanParameterInfo[]{}, SimpleType.INTEGER, MBeanOperationInfo.UNKNOWN, null);91test(openMBeanOperationInfo0, openMBeanOperationInfo, "Descriptor");9293// ----94System.out.println("\n---Testing on OpenMBeanParameterInfoSupport 1...");95OpenMBeanParameterInfo openMBeanParameterInfo0 = new OpenMBeanParameterInfoSupport(96"name", "description", SimpleType.INTEGER, 0, -1, 1);97OpenMBeanParameterInfo openMBeanParameterInfo;9899openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(100"name", "description", SimpleType.INTEGER, null, -1, 1);101test(openMBeanParameterInfo0, openMBeanParameterInfo, "default value");102103openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(104"name", "description", SimpleType.INTEGER, 0, null, 1);105test(openMBeanParameterInfo0, openMBeanParameterInfo, "min value");106107openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(108"name", "description", SimpleType.INTEGER, 0, -1, null);109test(openMBeanParameterInfo0, openMBeanParameterInfo, "max value");110111// ----112System.out.println("\n---Testing on OpenMBeanParameterInfoSupport 2...");113openMBeanParameterInfo0 = new OpenMBeanParameterInfoSupport(114"name", "description", SimpleType.INTEGER, 1, new Integer[]{-1, 1, 2});115116openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(117"name", "description", SimpleType.INTEGER, null, new Integer[]{-1, 1, 2});118test(openMBeanParameterInfo0, openMBeanParameterInfo, "default value");119120openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(121"name", "description", SimpleType.INTEGER, 1, null);122test(openMBeanParameterInfo0, openMBeanParameterInfo, "legal values");123124// ----125System.out.println("\n---Testing on OpenMBeanInfoSupport...");126String className = "toto";127String description = "titi";128OpenMBeanAttributeInfo[] attrInfos = new OpenMBeanAttributeInfo[]{};129OpenMBeanConstructorInfo[] constrInfos = new OpenMBeanConstructorInfo[]{};130OpenMBeanOperationInfo[] operaInfos = new OpenMBeanOperationInfo[]{};131MBeanNotificationInfo[] notifInfos = new MBeanNotificationInfo[]{};132133OpenMBeanInfo ominfo0 = new OpenMBeanInfoSupport("toto", description, attrInfos, constrInfos, operaInfos, notifInfos);134OpenMBeanInfo ominfo = new OpenMBeanInfoSupport(null, description, attrInfos, constrInfos, operaInfos, notifInfos);135test(ominfo0, ominfo, "class name");136137ominfo = new OpenMBeanInfoSupport(className, null, attrInfos, constrInfos, operaInfos, notifInfos);138test(ominfo0, ominfo, "description");139140ominfo = new OpenMBeanInfoSupport(className, description, null, constrInfos, operaInfos, notifInfos);141test(ominfo0, ominfo, "attrInfos");142143ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, null, operaInfos, notifInfos);144test(ominfo0, ominfo, "constructor infos");145146ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, constrInfos, null, notifInfos);147test(ominfo0, ominfo, "operation infos");148149ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, constrInfos, operaInfos, null);150test(ominfo0, ominfo, "notif infos");151152if (failed > 0) {153throw new RuntimeException("Test failed: "+failed);154} else {155System.out.println("---Test: PASSED");156}157}158159private static void test(Object obj1, Object obj2, String param) {160try {161obj1.equals(obj2);162System.out.println("OK-1: "+obj1.getClass().getSimpleName()+163".equals worked with a null field: "+param);164} catch (NullPointerException npe) {165System.out.println("--->KO-1!!! "+obj1.getClass().getSimpleName()+166".equals got NPE with a null field: "+param);167npe.printStackTrace();168failed++;169}170171try {172obj2.equals(obj1);173System.out.println("OK-2: "+obj2.getClass().getSimpleName()+174".equals worked with a null field: "+param);175} catch (NullPointerException npe) {176System.out.println("--->KO-2!!! "+obj2.getClass().getSimpleName()+177".equals got NPE with a null field: "+param);178npe.printStackTrace();179failed++;180}181182try {183obj1.equals(null);184obj2.equals(null);185186System.out.println("OK-3: "+obj1.getClass().getSimpleName()+187".equals worked with a null object.");188} catch (NullPointerException npe) {189System.out.println("--->KO-3!!! "+obj1.getClass().getSimpleName()+190".equals got NPE with a null object.");191npe.printStackTrace();192failed++;193}194}195}196197198