Path: blob/master/test/jdk/javax/management/openmbean/OpenMBeanInfoEqualsNPETest.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.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*44* @run clean OpenMBeanInfoEqualsNPETest45* @run build OpenMBeanInfoEqualsNPETest46* @run main OpenMBeanInfoEqualsNPETest47*/48public class OpenMBeanInfoEqualsNPETest {49private static int failed = 0;5051public static void main(String[] args) throws Exception {52System.out.println("---OpenMBeanInfoEqualsNPETest-main ...");5354// ----55System.out.println("\n---Testing on OpenMBeanAttributeInfoSupport...");56OpenMBeanAttributeInfo openMBeanAttributeInfo0 = new OpenMBeanAttributeInfoSupport(57"name", "description", SimpleType.INTEGER, true, true, false, 1, new Integer[]{1, 2, 3});58OpenMBeanAttributeInfo openMBeanAttributeInfo = new OpenMBeanAttributeInfoSupport(59"name", "description", SimpleType.INTEGER, true, true, false, null, new Integer[]{1, 2, 3});60test(openMBeanAttributeInfo0, openMBeanAttributeInfo, "defaultValue");6162openMBeanAttributeInfo = new OpenMBeanAttributeInfoSupport(63"name", "description", SimpleType.INTEGER, true, true, false, 1, null);64test(openMBeanAttributeInfo0, openMBeanAttributeInfo, "legalValues");6566// ----67System.out.println("\n---Testing on OpenMBeanConstructorInfoSupport...");68OpenMBeanConstructorInfo openMBeanConstructorInfo0 = new OpenMBeanConstructorInfoSupport(69"name", "description", new OpenMBeanParameterInfo[]{}, new DescriptorSupport());70OpenMBeanConstructorInfo openMBeanConstructorInfo;7172openMBeanConstructorInfo = new OpenMBeanConstructorInfoSupport(73"name", "description", null, new DescriptorSupport());74test(openMBeanConstructorInfo0, openMBeanConstructorInfo, "sigs");7576openMBeanConstructorInfo = new OpenMBeanConstructorInfoSupport(77"name", "description", new OpenMBeanParameterInfo[]{}, null);78test(openMBeanConstructorInfo0, openMBeanConstructorInfo, "Descriptor");7980// ----81System.out.println("\n---Testing on OpenMBeanOperationInfoSupport...");82OpenMBeanOperationInfo openMBeanOperationInfo0 = new OpenMBeanOperationInfoSupport(83"name", "description", new OpenMBeanParameterInfo[]{}, SimpleType.INTEGER, 1, new DescriptorSupport());84OpenMBeanOperationInfo openMBeanOperationInfo;8586openMBeanOperationInfo = new OpenMBeanOperationInfoSupport(87"name", "description", null, SimpleType.INTEGER, 1, new DescriptorSupport());88test(openMBeanOperationInfo0, openMBeanOperationInfo, "sigs");8990openMBeanOperationInfo = new OpenMBeanOperationInfoSupport(91"name", "description", new OpenMBeanParameterInfo[]{}, SimpleType.INTEGER, MBeanOperationInfo.UNKNOWN, null);92test(openMBeanOperationInfo0, openMBeanOperationInfo, "Descriptor");9394// ----95System.out.println("\n---Testing on OpenMBeanParameterInfoSupport 1...");96OpenMBeanParameterInfo openMBeanParameterInfo0 = new OpenMBeanParameterInfoSupport(97"name", "description", SimpleType.INTEGER, 0, -1, 1);98OpenMBeanParameterInfo openMBeanParameterInfo;99100openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(101"name", "description", SimpleType.INTEGER, null, -1, 1);102test(openMBeanParameterInfo0, openMBeanParameterInfo, "default value");103104openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(105"name", "description", SimpleType.INTEGER, 0, null, 1);106test(openMBeanParameterInfo0, openMBeanParameterInfo, "min value");107108openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(109"name", "description", SimpleType.INTEGER, 0, -1, null);110test(openMBeanParameterInfo0, openMBeanParameterInfo, "max value");111112// ----113System.out.println("\n---Testing on OpenMBeanParameterInfoSupport 2...");114openMBeanParameterInfo0 = new OpenMBeanParameterInfoSupport(115"name", "description", SimpleType.INTEGER, 1, new Integer[]{-1, 1, 2});116117openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(118"name", "description", SimpleType.INTEGER, null, new Integer[]{-1, 1, 2});119test(openMBeanParameterInfo0, openMBeanParameterInfo, "default value");120121openMBeanParameterInfo = new OpenMBeanParameterInfoSupport(122"name", "description", SimpleType.INTEGER, 1, null);123test(openMBeanParameterInfo0, openMBeanParameterInfo, "legal values");124125// ----126System.out.println("\n---Testing on OpenMBeanInfoSupport...");127String className = "toto";128String description = "titi";129OpenMBeanAttributeInfo[] attrInfos = new OpenMBeanAttributeInfo[]{};130OpenMBeanConstructorInfo[] constrInfos = new OpenMBeanConstructorInfo[]{};131OpenMBeanOperationInfo[] operaInfos = new OpenMBeanOperationInfo[]{};132MBeanNotificationInfo[] notifInfos = new MBeanNotificationInfo[]{};133134OpenMBeanInfo ominfo0 = new OpenMBeanInfoSupport("toto", description, attrInfos, constrInfos, operaInfos, notifInfos);135OpenMBeanInfo ominfo = new OpenMBeanInfoSupport(null, description, attrInfos, constrInfos, operaInfos, notifInfos);136test(ominfo0, ominfo, "class name");137138ominfo = new OpenMBeanInfoSupport(className, null, attrInfos, constrInfos, operaInfos, notifInfos);139test(ominfo0, ominfo, "description");140141ominfo = new OpenMBeanInfoSupport(className, description, null, constrInfos, operaInfos, notifInfos);142test(ominfo0, ominfo, "attrInfos");143144ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, null, operaInfos, notifInfos);145test(ominfo0, ominfo, "constructor infos");146147ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, constrInfos, null, notifInfos);148test(ominfo0, ominfo, "operation infos");149150ominfo = new OpenMBeanInfoSupport(className, description, attrInfos, constrInfos, operaInfos, null);151test(ominfo0, ominfo, "notif infos");152153if (failed > 0) {154throw new RuntimeException("Test failed: "+failed);155} else {156System.out.println("---Test: PASSED");157}158}159160private static void test(Object obj1, Object obj2, String param) {161try {162obj1.equals(obj2);163System.out.println("OK-1: "+obj1.getClass().getSimpleName()+164".equals worked with a null field: "+param);165} catch (NullPointerException npe) {166System.out.println("--->KO-1!!! "+obj1.getClass().getSimpleName()+167".equals got NPE with a null field: "+param);168npe.printStackTrace();169failed++;170}171172try {173obj2.equals(obj1);174System.out.println("OK-2: "+obj2.getClass().getSimpleName()+175".equals worked with a null field: "+param);176} catch (NullPointerException npe) {177System.out.println("--->KO-2!!! "+obj2.getClass().getSimpleName()+178".equals got NPE with a null field: "+param);179npe.printStackTrace();180failed++;181}182183try {184obj1.equals(null);185obj2.equals(null);186187System.out.println("OK-3: "+obj1.getClass().getSimpleName()+188".equals worked with a null object.");189} catch (NullPointerException npe) {190System.out.println("--->KO-3!!! "+obj1.getClass().getSimpleName()+191".equals got NPE with a null object.");192npe.printStackTrace();193failed++;194}195}196}197198199