1 /* 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import javax.management.MBeanAttributeInfo; 25 import javax.management.MBeanConstructorInfo; 26 import javax.management.MBeanFeatureInfo; 27 import javax.management.MBeanInfo; 28 import javax.management.MBeanNotificationInfo; 29 import javax.management.MBeanOperationInfo; 30 import javax.management.MBeanParameterInfo; 31 import javax.management.modelmbean.DescriptorSupport; 32 import javax.management.openmbean.SimpleType; 33 34 /* 35 * @test 36 * @bug 8023954 37 * @summary Test that MBean*Info.equals do not throw NPE 38 * @author Shanliang JIANG 39 * @run clean MBeanInfoEqualsNPETest 40 * @run build MBeanInfoEqualsNPETest 41 * @run main MBeanInfoEqualsNPETest 42 */ 43 public class MBeanInfoEqualsNPETest { 44 private static int failed = 0; 45 46 public static void main(String[] args) throws Exception { 47 System.out.println("---MBeanInfoEqualsNPETest-main ..."); 48 49 // ---- 50 System.out.println("\n---Testing on MBeanAttributeInfo..."); 51 MBeanAttributeInfo mbeanAttributeInfo0 = new MBeanAttributeInfo( 52 "name", SimpleType.INTEGER.getClassName(), "description", true, true, false); 53 MBeanAttributeInfo mbeanAttributeInfo = new MBeanAttributeInfo( 54 null, SimpleType.INTEGER.getClassName(), "description", true, true, false); 55 test(mbeanAttributeInfo0, mbeanAttributeInfo, "class name"); 56 57 mbeanAttributeInfo = new MBeanAttributeInfo( 58 "name", null, "description", true, true, false); 59 test(mbeanAttributeInfo0, mbeanAttributeInfo, "type"); 60 61 mbeanAttributeInfo = new MBeanAttributeInfo( 62 "name", SimpleType.INTEGER.getClassName(), null, true, true, false); 63 test(mbeanAttributeInfo0, mbeanAttributeInfo, "description"); 64 65 // ---- 66 System.out.println("\n---Testing on MBeanConstructorInfo..."); 67 MBeanConstructorInfo mbeanConstructorInfo0 = new MBeanConstructorInfo( 68 "", "", new MBeanParameterInfo[]{}, new DescriptorSupport()); 69 MBeanConstructorInfo mbeanConstructorInfo = new MBeanConstructorInfo( 70 null, "", new MBeanParameterInfo[]{}, new DescriptorSupport()); 71 test(mbeanConstructorInfo0, mbeanConstructorInfo, "name"); 72 73 mbeanConstructorInfo = new MBeanConstructorInfo( 74 "", null, new MBeanParameterInfo[]{}, new DescriptorSupport()); 75 test(mbeanConstructorInfo0, mbeanConstructorInfo, "description"); 76 77 mbeanConstructorInfo = new MBeanConstructorInfo( 78 "", "", null, new DescriptorSupport()); 79 test(mbeanConstructorInfo0, mbeanConstructorInfo, "MBeanParameterInfo"); 80 81 mbeanConstructorInfo = new MBeanConstructorInfo( 82 "", "", new MBeanParameterInfo[]{}, null); 83 test(mbeanConstructorInfo0, mbeanConstructorInfo, "descriptor"); 84 85 // ---- 86 System.out.println("\n---Testing on MBeanOperationInfo..."); 87 MBeanOperationInfo mbeanOperationInfo0 = new MBeanOperationInfo( 88 "name", "description", new MBeanParameterInfo[]{}, "type", 89 MBeanOperationInfo.UNKNOWN, new DescriptorSupport()); 90 91 MBeanOperationInfo mbeanOperationInfo = new MBeanOperationInfo( 92 null, "description", new MBeanParameterInfo[]{}, "type", 93 MBeanOperationInfo.UNKNOWN, new DescriptorSupport()); 94 test(mbeanOperationInfo0, mbeanOperationInfo, "name"); 95 96 mbeanOperationInfo = new MBeanOperationInfo( 97 "name", null, new MBeanParameterInfo[]{}, "type", 98 MBeanOperationInfo.UNKNOWN, new DescriptorSupport()); 99 test(mbeanOperationInfo0, mbeanOperationInfo, "description"); 100 101 mbeanOperationInfo = new MBeanOperationInfo( 102 "name", "description", null, "type", 1, new DescriptorSupport()); 103 test(mbeanOperationInfo0, mbeanOperationInfo, "MBeanParameterInfo"); 104 105 mbeanOperationInfo = new MBeanOperationInfo( 106 "name", "description", new MBeanParameterInfo[]{}, null, 107 MBeanOperationInfo.UNKNOWN, new DescriptorSupport()); 108 test(mbeanOperationInfo0, mbeanOperationInfo, "type"); 109 110 mbeanOperationInfo = new MBeanOperationInfo( 111 "name", "description", new MBeanParameterInfo[]{}, null, 112 MBeanOperationInfo.UNKNOWN, null); 113 test(mbeanOperationInfo0, mbeanOperationInfo, "Descriptor"); 114 115 // ---- 116 System.out.println("\n---Testing on MBeanParameterInfo..."); 117 MBeanParameterInfo mbeanParameterInfo0 = new MBeanParameterInfo( 118 "name", "type", "description", new DescriptorSupport()); 119 MBeanParameterInfo mbeanParameterInfo = new MBeanParameterInfo( 120 null, "type", "description", new DescriptorSupport()); 121 test(mbeanParameterInfo0, mbeanParameterInfo, "name"); 122 123 mbeanParameterInfo = new MBeanParameterInfo( 124 "name", null, "description", new DescriptorSupport()); 125 test(mbeanParameterInfo0, mbeanParameterInfo, "type"); 126 127 mbeanParameterInfo = new MBeanParameterInfo( 128 "name", "type", null, new DescriptorSupport()); 129 test(mbeanParameterInfo0, mbeanParameterInfo, "description"); 130 131 mbeanParameterInfo = new MBeanParameterInfo( 132 "name", "type", "description", null); 133 test(mbeanParameterInfo0, mbeanParameterInfo, "Descriptor"); 134 135 // ---- 136 System.out.println("\n---Testing on MBeanFeatureInfo ..."); 137 MBeanFeatureInfo mbeanFeatureInfo0 = new MBeanFeatureInfo( 138 "name", "description", new DescriptorSupport()); 139 MBeanFeatureInfo mbeanFeatureInfo = new MBeanFeatureInfo( 140 null, "description", new DescriptorSupport()); 141 test(mbeanFeatureInfo0, mbeanFeatureInfo, "name"); 142 143 mbeanFeatureInfo = new MBeanFeatureInfo( 144 "name", null, new DescriptorSupport()); 145 test(mbeanParameterInfo0, mbeanParameterInfo, "description"); 146 147 mbeanFeatureInfo = new MBeanFeatureInfo( 148 "name", "description", null); 149 test(mbeanParameterInfo0, mbeanParameterInfo, "Descriptor"); 150 151 // ---- 152 System.out.println("\n---Testing on MBeanInfo..."); 153 String className = "toto"; 154 String description = "titi"; 155 MBeanAttributeInfo[] attrInfos = new MBeanAttributeInfo[]{}; 156 MBeanConstructorInfo[] constrInfos = new MBeanConstructorInfo[]{}; 157 MBeanOperationInfo[] operaInfos = new MBeanOperationInfo[]{}; 158 MBeanNotificationInfo[] notifInfos = new MBeanNotificationInfo[]{}; 159 160 MBeanInfo minfo0 = new MBeanInfo("toto", description, attrInfos, constrInfos, operaInfos, notifInfos); 161 MBeanInfo minfo = new MBeanInfo(null, description, attrInfos, constrInfos, operaInfos, notifInfos); 162 test(minfo0, minfo, "class name"); 163 164 minfo = new MBeanInfo(className, null, attrInfos, constrInfos, operaInfos, notifInfos); 165 test(minfo0, minfo, "description"); 166 167 minfo = new MBeanInfo(className, description, null, constrInfos, operaInfos, notifInfos); 168 test(minfo0, minfo, "attrInfos"); 169 170 minfo = new MBeanInfo(className, description, attrInfos, null, operaInfos, notifInfos); 171 test(minfo0, minfo, "constrInfos"); 172 173 minfo = new MBeanInfo(className, description, attrInfos, constrInfos, null, notifInfos); 174 test(minfo0, minfo, "operaInfos"); 175 176 minfo = new MBeanInfo(className, description, attrInfos, constrInfos, operaInfos, null); 177 test(minfo0, minfo, "notifInfos"); 178 179 if (failed > 0) { 180 throw new RuntimeException("Test failed: "+failed); 181 } else { 182 System.out.println("---Test: PASSED"); 183 } 184 } 185 186 private static void test(Object obj1, Object obj2, String param) { 187 try { 188 obj1.equals(obj2); 189 System.out.println("OK-1: "+obj1.getClass().getSimpleName()+".equals worked with a null paramer: "+param); 190 } catch (NullPointerException npe) { 191 System.out.println("--->KO-1!!! "+obj1.getClass().getSimpleName()+".equals got NPE with a null paramer: "+param); 192 npe.printStackTrace(); 193 failed++; 194 } 195 196 try { 197 obj2.equals(obj1); 198 System.out.println("OK-2: "+obj2.getClass().getSimpleName()+".equals worked with a null paramer: "+param); 199 } catch (NullPointerException npe) { 200 System.out.println("--->KO-2!!! "+obj2.getClass().getSimpleName()+".equals got NPE with a null paramer: "+param); 201 npe.printStackTrace(); 202 failed++; 203 } 204 205 try { 206 obj1.equals(null); 207 obj2.equals(null); 208 209 System.out.println("OK-3: "+obj1.getClass().getSimpleName()+".equals worked with a null field."); 210 } catch (NullPointerException npe) { 211 System.out.println("--->KO-3!!! "+obj1.getClass().getSimpleName()+".equals got NPE with a null field."); 212 npe.printStackTrace(); 213 failed++; 214 } 215 } 216 }