Path: blob/master/test/jdk/javax/management/MBeanInfo/MBeanInfoEqualsNPETest.java
41149 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.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*39* @run clean MBeanInfoEqualsNPETest40* @run build MBeanInfoEqualsNPETest41* @run main MBeanInfoEqualsNPETest42*/43public class MBeanInfoEqualsNPETest {44private static int failed = 0;4546public static void main(String[] args) throws Exception {47System.out.println("---MBeanInfoEqualsNPETest-main ...");4849// ----50System.out.println("\n---Testing on MBeanAttributeInfo...");51MBeanAttributeInfo mbeanAttributeInfo0 = new MBeanAttributeInfo(52"name", SimpleType.INTEGER.getClassName(), "description", true, true, false);53MBeanAttributeInfo mbeanAttributeInfo = new MBeanAttributeInfo(54null, SimpleType.INTEGER.getClassName(), "description", true, true, false);55test(mbeanAttributeInfo0, mbeanAttributeInfo, "class name");5657mbeanAttributeInfo = new MBeanAttributeInfo(58"name", null, "description", true, true, false);59test(mbeanAttributeInfo0, mbeanAttributeInfo, "type");6061mbeanAttributeInfo = new MBeanAttributeInfo(62"name", SimpleType.INTEGER.getClassName(), null, true, true, false);63test(mbeanAttributeInfo0, mbeanAttributeInfo, "description");6465// ----66System.out.println("\n---Testing on MBeanConstructorInfo...");67MBeanConstructorInfo mbeanConstructorInfo0 = new MBeanConstructorInfo(68"", "", new MBeanParameterInfo[]{}, new DescriptorSupport());69MBeanConstructorInfo mbeanConstructorInfo = new MBeanConstructorInfo(70null, "", new MBeanParameterInfo[]{}, new DescriptorSupport());71test(mbeanConstructorInfo0, mbeanConstructorInfo, "name");7273mbeanConstructorInfo = new MBeanConstructorInfo(74"", null, new MBeanParameterInfo[]{}, new DescriptorSupport());75test(mbeanConstructorInfo0, mbeanConstructorInfo, "description");7677mbeanConstructorInfo = new MBeanConstructorInfo(78"", "", null, new DescriptorSupport());79test(mbeanConstructorInfo0, mbeanConstructorInfo, "MBeanParameterInfo");8081mbeanConstructorInfo = new MBeanConstructorInfo(82"", "", new MBeanParameterInfo[]{}, null);83test(mbeanConstructorInfo0, mbeanConstructorInfo, "descriptor");8485// ----86System.out.println("\n---Testing on MBeanOperationInfo...");87MBeanOperationInfo mbeanOperationInfo0 = new MBeanOperationInfo(88"name", "description", new MBeanParameterInfo[]{}, "type",89MBeanOperationInfo.UNKNOWN, new DescriptorSupport());9091MBeanOperationInfo mbeanOperationInfo = new MBeanOperationInfo(92null, "description", new MBeanParameterInfo[]{}, "type",93MBeanOperationInfo.UNKNOWN, new DescriptorSupport());94test(mbeanOperationInfo0, mbeanOperationInfo, "name");9596mbeanOperationInfo = new MBeanOperationInfo(97"name", null, new MBeanParameterInfo[]{}, "type",98MBeanOperationInfo.UNKNOWN, new DescriptorSupport());99test(mbeanOperationInfo0, mbeanOperationInfo, "description");100101mbeanOperationInfo = new MBeanOperationInfo(102"name", "description", null, "type", 1, new DescriptorSupport());103test(mbeanOperationInfo0, mbeanOperationInfo, "MBeanParameterInfo");104105mbeanOperationInfo = new MBeanOperationInfo(106"name", "description", new MBeanParameterInfo[]{}, null,107MBeanOperationInfo.UNKNOWN, new DescriptorSupport());108test(mbeanOperationInfo0, mbeanOperationInfo, "type");109110mbeanOperationInfo = new MBeanOperationInfo(111"name", "description", new MBeanParameterInfo[]{}, null,112MBeanOperationInfo.UNKNOWN, null);113test(mbeanOperationInfo0, mbeanOperationInfo, "Descriptor");114115// ----116System.out.println("\n---Testing on MBeanParameterInfo...");117MBeanParameterInfo mbeanParameterInfo0 = new MBeanParameterInfo(118"name", "type", "description", new DescriptorSupport());119MBeanParameterInfo mbeanParameterInfo = new MBeanParameterInfo(120null, "type", "description", new DescriptorSupport());121test(mbeanParameterInfo0, mbeanParameterInfo, "name");122123mbeanParameterInfo = new MBeanParameterInfo(124"name", null, "description", new DescriptorSupport());125test(mbeanParameterInfo0, mbeanParameterInfo, "type");126127mbeanParameterInfo = new MBeanParameterInfo(128"name", "type", null, new DescriptorSupport());129test(mbeanParameterInfo0, mbeanParameterInfo, "description");130131mbeanParameterInfo = new MBeanParameterInfo(132"name", "type", "description", null);133test(mbeanParameterInfo0, mbeanParameterInfo, "Descriptor");134135// ----136System.out.println("\n---Testing on MBeanFeatureInfo ...");137MBeanFeatureInfo mbeanFeatureInfo0 = new MBeanFeatureInfo(138"name", "description", new DescriptorSupport());139MBeanFeatureInfo mbeanFeatureInfo = new MBeanFeatureInfo(140null, "description", new DescriptorSupport());141test(mbeanFeatureInfo0, mbeanFeatureInfo, "name");142143mbeanFeatureInfo = new MBeanFeatureInfo(144"name", null, new DescriptorSupport());145test(mbeanParameterInfo0, mbeanParameterInfo, "description");146147mbeanFeatureInfo = new MBeanFeatureInfo(148"name", "description", null);149test(mbeanParameterInfo0, mbeanParameterInfo, "Descriptor");150151// ----152System.out.println("\n---Testing on MBeanInfo...");153String className = "toto";154String description = "titi";155MBeanAttributeInfo[] attrInfos = new MBeanAttributeInfo[]{};156MBeanConstructorInfo[] constrInfos = new MBeanConstructorInfo[]{};157MBeanOperationInfo[] operaInfos = new MBeanOperationInfo[]{};158MBeanNotificationInfo[] notifInfos = new MBeanNotificationInfo[]{};159160MBeanInfo minfo0 = new MBeanInfo("toto", description, attrInfos, constrInfos, operaInfos, notifInfos);161MBeanInfo minfo = new MBeanInfo(null, description, attrInfos, constrInfos, operaInfos, notifInfos);162test(minfo0, minfo, "class name");163164minfo = new MBeanInfo(className, null, attrInfos, constrInfos, operaInfos, notifInfos);165test(minfo0, minfo, "description");166167minfo = new MBeanInfo(className, description, null, constrInfos, operaInfos, notifInfos);168test(minfo0, minfo, "attrInfos");169170minfo = new MBeanInfo(className, description, attrInfos, null, operaInfos, notifInfos);171test(minfo0, minfo, "constrInfos");172173minfo = new MBeanInfo(className, description, attrInfos, constrInfos, null, notifInfos);174test(minfo0, minfo, "operaInfos");175176minfo = new MBeanInfo(className, description, attrInfos, constrInfos, operaInfos, null);177test(minfo0, minfo, "notifInfos");178179if (failed > 0) {180throw new RuntimeException("Test failed: "+failed);181} else {182System.out.println("---Test: PASSED");183}184}185186private static void test(Object obj1, Object obj2, String param) {187try {188obj1.equals(obj2);189System.out.println("OK-1: "+obj1.getClass().getSimpleName()+".equals worked with a null paramer: "+param);190} catch (NullPointerException npe) {191System.out.println("--->KO-1!!! "+obj1.getClass().getSimpleName()+".equals got NPE with a null paramer: "+param);192npe.printStackTrace();193failed++;194}195196try {197obj2.equals(obj1);198System.out.println("OK-2: "+obj2.getClass().getSimpleName()+".equals worked with a null paramer: "+param);199} catch (NullPointerException npe) {200System.out.println("--->KO-2!!! "+obj2.getClass().getSimpleName()+".equals got NPE with a null paramer: "+param);201npe.printStackTrace();202failed++;203}204205try {206obj1.equals(null);207obj2.equals(null);208209System.out.println("OK-3: "+obj1.getClass().getSimpleName()+".equals worked with a null field.");210} catch (NullPointerException npe) {211System.out.println("--->KO-3!!! "+obj1.getClass().getSimpleName()+".equals got NPE with a null field.");212npe.printStackTrace();213failed++;214}215}216}217218219