Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/MBeanInfo/MBeanInfoEqualsNPETest.java
38841 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.MBeanAttributeInfo;24import javax.management.MBeanConstructorInfo;25import javax.management.MBeanFeatureInfo;26import javax.management.MBeanInfo;27import javax.management.MBeanNotificationInfo;28import javax.management.MBeanOperationInfo;29import javax.management.MBeanParameterInfo;30import javax.management.modelmbean.DescriptorSupport;31import javax.management.openmbean.SimpleType;3233/*34* @test35* @bug 802395436* @summary Test that MBean*Info.equals do not throw NPE37* @author Shanliang JIANG38* @run clean MBeanInfoEqualsNPETest39* @run build MBeanInfoEqualsNPETest40* @run main MBeanInfoEqualsNPETest41*/42public class MBeanInfoEqualsNPETest {43private static int failed = 0;4445public static void main(String[] args) throws Exception {46System.out.println("---MBeanInfoEqualsNPETest-main ...");4748// ----49System.out.println("\n---Testing on MBeanAttributeInfo...");50MBeanAttributeInfo mbeanAttributeInfo0 = new MBeanAttributeInfo(51"name", SimpleType.INTEGER.getClassName(), "description", true, true, false);52MBeanAttributeInfo mbeanAttributeInfo = new MBeanAttributeInfo(53null, SimpleType.INTEGER.getClassName(), "description", true, true, false);54test(mbeanAttributeInfo0, mbeanAttributeInfo, "class name");5556mbeanAttributeInfo = new MBeanAttributeInfo(57"name", null, "description", true, true, false);58test(mbeanAttributeInfo0, mbeanAttributeInfo, "type");5960mbeanAttributeInfo = new MBeanAttributeInfo(61"name", SimpleType.INTEGER.getClassName(), null, true, true, false);62test(mbeanAttributeInfo0, mbeanAttributeInfo, "description");6364// ----65System.out.println("\n---Testing on MBeanConstructorInfo...");66MBeanConstructorInfo mbeanConstructorInfo0 = new MBeanConstructorInfo(67"", "", new MBeanParameterInfo[]{}, new DescriptorSupport());68MBeanConstructorInfo mbeanConstructorInfo = new MBeanConstructorInfo(69null, "", new MBeanParameterInfo[]{}, new DescriptorSupport());70test(mbeanConstructorInfo0, mbeanConstructorInfo, "name");7172mbeanConstructorInfo = new MBeanConstructorInfo(73"", null, new MBeanParameterInfo[]{}, new DescriptorSupport());74test(mbeanConstructorInfo0, mbeanConstructorInfo, "description");7576mbeanConstructorInfo = new MBeanConstructorInfo(77"", "", null, new DescriptorSupport());78test(mbeanConstructorInfo0, mbeanConstructorInfo, "MBeanParameterInfo");7980mbeanConstructorInfo = new MBeanConstructorInfo(81"", "", new MBeanParameterInfo[]{}, null);82test(mbeanConstructorInfo0, mbeanConstructorInfo, "descriptor");8384// ----85System.out.println("\n---Testing on MBeanOperationInfo...");86MBeanOperationInfo mbeanOperationInfo0 = new MBeanOperationInfo(87"name", "description", new MBeanParameterInfo[]{}, "type",88MBeanOperationInfo.UNKNOWN, new DescriptorSupport());8990MBeanOperationInfo mbeanOperationInfo = new MBeanOperationInfo(91null, "description", new MBeanParameterInfo[]{}, "type",92MBeanOperationInfo.UNKNOWN, new DescriptorSupport());93test(mbeanOperationInfo0, mbeanOperationInfo, "name");9495mbeanOperationInfo = new MBeanOperationInfo(96"name", null, new MBeanParameterInfo[]{}, "type",97MBeanOperationInfo.UNKNOWN, new DescriptorSupport());98test(mbeanOperationInfo0, mbeanOperationInfo, "description");99100mbeanOperationInfo = new MBeanOperationInfo(101"name", "description", null, "type", 1, new DescriptorSupport());102test(mbeanOperationInfo0, mbeanOperationInfo, "MBeanParameterInfo");103104mbeanOperationInfo = new MBeanOperationInfo(105"name", "description", new MBeanParameterInfo[]{}, null,106MBeanOperationInfo.UNKNOWN, new DescriptorSupport());107test(mbeanOperationInfo0, mbeanOperationInfo, "type");108109mbeanOperationInfo = new MBeanOperationInfo(110"name", "description", new MBeanParameterInfo[]{}, null,111MBeanOperationInfo.UNKNOWN, null);112test(mbeanOperationInfo0, mbeanOperationInfo, "Descriptor");113114// ----115System.out.println("\n---Testing on MBeanParameterInfo...");116MBeanParameterInfo mbeanParameterInfo0 = new MBeanParameterInfo(117"name", "type", "description", new DescriptorSupport());118MBeanParameterInfo mbeanParameterInfo = new MBeanParameterInfo(119null, "type", "description", new DescriptorSupport());120test(mbeanParameterInfo0, mbeanParameterInfo, "name");121122mbeanParameterInfo = new MBeanParameterInfo(123"name", null, "description", new DescriptorSupport());124test(mbeanParameterInfo0, mbeanParameterInfo, "type");125126mbeanParameterInfo = new MBeanParameterInfo(127"name", "type", null, new DescriptorSupport());128test(mbeanParameterInfo0, mbeanParameterInfo, "description");129130mbeanParameterInfo = new MBeanParameterInfo(131"name", "type", "description", null);132test(mbeanParameterInfo0, mbeanParameterInfo, "Descriptor");133134// ----135System.out.println("\n---Testing on MBeanFeatureInfo ...");136MBeanFeatureInfo mbeanFeatureInfo0 = new MBeanFeatureInfo(137"name", "description", new DescriptorSupport());138MBeanFeatureInfo mbeanFeatureInfo = new MBeanFeatureInfo(139null, "description", new DescriptorSupport());140test(mbeanFeatureInfo0, mbeanFeatureInfo, "name");141142mbeanFeatureInfo = new MBeanFeatureInfo(143"name", null, new DescriptorSupport());144test(mbeanParameterInfo0, mbeanParameterInfo, "description");145146mbeanFeatureInfo = new MBeanFeatureInfo(147"name", "description", null);148test(mbeanParameterInfo0, mbeanParameterInfo, "Descriptor");149150// ----151System.out.println("\n---Testing on MBeanInfo...");152String className = "toto";153String description = "titi";154MBeanAttributeInfo[] attrInfos = new MBeanAttributeInfo[]{};155MBeanConstructorInfo[] constrInfos = new MBeanConstructorInfo[]{};156MBeanOperationInfo[] operaInfos = new MBeanOperationInfo[]{};157MBeanNotificationInfo[] notifInfos = new MBeanNotificationInfo[]{};158159MBeanInfo minfo0 = new MBeanInfo("toto", description, attrInfos, constrInfos, operaInfos, notifInfos);160MBeanInfo minfo = new MBeanInfo(null, description, attrInfos, constrInfos, operaInfos, notifInfos);161test(minfo0, minfo, "class name");162163minfo = new MBeanInfo(className, null, attrInfos, constrInfos, operaInfos, notifInfos);164test(minfo0, minfo, "description");165166minfo = new MBeanInfo(className, description, null, constrInfos, operaInfos, notifInfos);167test(minfo0, minfo, "attrInfos");168169minfo = new MBeanInfo(className, description, attrInfos, null, operaInfos, notifInfos);170test(minfo0, minfo, "constrInfos");171172minfo = new MBeanInfo(className, description, attrInfos, constrInfos, null, notifInfos);173test(minfo0, minfo, "operaInfos");174175minfo = new MBeanInfo(className, description, attrInfos, constrInfos, operaInfos, null);176test(minfo0, minfo, "notifInfos");177178if (failed > 0) {179throw new RuntimeException("Test failed: "+failed);180} else {181System.out.println("---Test: PASSED");182}183}184185private static void test(Object obj1, Object obj2, String param) {186try {187obj1.equals(obj2);188System.out.println("OK-1: "+obj1.getClass().getSimpleName()+".equals worked with a null paramer: "+param);189} catch (NullPointerException npe) {190System.out.println("--->KO-1!!! "+obj1.getClass().getSimpleName()+".equals got NPE with a null paramer: "+param);191npe.printStackTrace();192failed++;193}194195try {196obj2.equals(obj1);197System.out.println("OK-2: "+obj2.getClass().getSimpleName()+".equals worked with a null paramer: "+param);198} catch (NullPointerException npe) {199System.out.println("--->KO-2!!! "+obj2.getClass().getSimpleName()+".equals got NPE with a null paramer: "+param);200npe.printStackTrace();201failed++;202}203204try {205obj1.equals(null);206obj2.equals(null);207208System.out.println("OK-3: "+obj1.getClass().getSimpleName()+".equals worked with a null field.");209} catch (NullPointerException npe) {210System.out.println("--->KO-3!!! "+obj1.getClass().getSimpleName()+".equals got NPE with a null field.");211npe.printStackTrace();212failed++;213}214}215}216217218