1 /* 2 * Copyright (c) 2004, 2015, 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 /* 25 * @test 26 * @bug 4967769 4980655 4980668 4981214 27 * @summary Test that ModelMBeanInfoSupport.setDescriptor rejects 28 * null descriptor, that empty arrays are handled correctly, 29 * that getDescriptors("mbean") works, and that default values for 30 * MBean descriptors are correctly assigned. 31 * @author Eamonn McManus 32 * @modules java.management 33 * @run clean InfoSupportTest 34 * @run build InfoSupportTest 35 * @run main InfoSupportTest 36 */ 37 38 import java.util.*; 39 import javax.management.Descriptor; 40 import javax.management.RuntimeOperationsException; 41 import javax.management.modelmbean.*; 42 43 public class InfoSupportTest { 44 public static void main(String[] args) throws Exception { 45 boolean ok = true; 46 47 ok &= testSetDescriptorNull(); 48 ok &= testEmptyArrayParameters(); 49 ok &= testGetDescriptorsForMBean(); 50 51 if (ok) 52 System.out.println("Test passed"); 53 else { 54 System.out.println("TEST FAILED"); 55 System.exit(1); 56 } 57 } 58 59 private static boolean testSetDescriptorNull() { 60 System.out.println("Testing that " + 61 "ModelMBeanInfoSupport.setDescriptor(null, \"x\")" + 62 " throws an exception"); 63 64 ModelMBeanAttributeInfo mmbai = 65 new ModelMBeanAttributeInfo("attribute", 66 "java.lang.String", 67 "an attribute", 68 true, true, false); 69 ModelMBeanInfo mmbi = 70 new ModelMBeanInfoSupport("bogus.class.name", 71 "an MBean", 72 new ModelMBeanAttributeInfo[] {mmbai}, 73 null, null, null); 74 try { 75 mmbi.setDescriptor(null, "attribute"); 76 } catch (RuntimeOperationsException e) { 77 if (e.getTargetException() instanceof IllegalArgumentException) { 78 System.out.println("Test passes: got a " + 79 "RuntimeOperationsException wrapping an " + 80 "IllegalArgumentException"); 81 return true; 82 } else { 83 System.out.println("TEST FAILS: wrong exception:"); 84 e.printStackTrace(System.out); 85 return false; 86 } 87 } catch (Exception e) { 88 System.out.println("TEST FAILS: wrong exception:"); 89 e.printStackTrace(System.out); 90 return false; 91 } 92 93 System.out.println("TEST FAILS: exception not thrown"); 94 return false; 95 } 96 97 private static boolean testEmptyArrayParameters() 98 throws Exception { 99 100 System.out.println("Test that empty and null array parameters " + 101 "produce the right type from getters"); 102 103 boolean ok = true; 104 105 ModelMBeanInfoSupport mmbi; 106 107 mmbi = 108 new ModelMBeanInfoSupport("bogus.class.name", "description", 109 null, null, null, null); 110 ok &= checkMMBI(mmbi, "null parameters, no descriptor"); 111 112 mmbi = 113 new ModelMBeanInfoSupport("bogus.class.name", "description", 114 null, null, null, null, null); 115 ok &= checkMMBI(mmbi, "null parameters, null descriptor"); 116 117 mmbi = 118 new ModelMBeanInfoSupport("bogus.class.name", "description", 119 new ModelMBeanAttributeInfo[0], 120 new ModelMBeanConstructorInfo[0], 121 new ModelMBeanOperationInfo[0], 122 new ModelMBeanNotificationInfo[0]); 123 ok &= checkMMBI(mmbi, "empty parameters, no descriptor"); 124 125 mmbi = 126 new ModelMBeanInfoSupport("bogus.class.name", "description", 127 new ModelMBeanAttributeInfo[0], 128 new ModelMBeanConstructorInfo[0], 129 new ModelMBeanOperationInfo[0], 130 new ModelMBeanNotificationInfo[0], 131 null); 132 ok &= checkMMBI(mmbi, "empty parameters, null descriptor"); 133 134 return ok; 135 } 136 137 private static boolean checkMMBI(ModelMBeanInfoSupport mmbi, String what) 138 throws Exception { 139 String bad = ""; 140 141 if (!(mmbi.getAttributes() instanceof ModelMBeanAttributeInfo[])) 142 bad += " attributes"; 143 if (!(mmbi.getConstructors() instanceof ModelMBeanConstructorInfo[])) 144 bad += " constructors"; 145 if (!(mmbi.getOperations() instanceof ModelMBeanOperationInfo[])) 146 bad += " operations"; 147 if (!(mmbi.getNotifications() instanceof ModelMBeanNotificationInfo[])) 148 bad += " notifications"; 149 150 if (bad.equals("")) { 151 System.out.println("..." + what + ": OK"); 152 return true; 153 } else { 154 System.out.println("..." + what + ": FAILS for:" + bad); 155 return false; 156 } 157 } 158 159 private static boolean testGetDescriptorsForMBean() 160 throws Exception { 161 System.out.println("Test getDescriptors(\"mbean\")"); 162 163 boolean ok = true; 164 165 ModelMBeanInfo mmbi; 166 Descriptor[] mbeanDescrs; 167 168 mmbi = new ModelMBeanInfoSupport("bogus.class.name", "description", 169 null, null, null, null); 170 try { 171 mbeanDescrs = mmbi.getDescriptors("mbean"); 172 if (mbeanDescrs.length == 1 && mbeanDescrs[0] != null) 173 System.out.println("...default MBean descriptor: OK"); 174 else { 175 System.out.println("...default MBean descriptor: bad array: " + 176 Arrays.asList(mbeanDescrs)); 177 ok = false; 178 } 179 } catch (Exception e) { 180 System.out.println("...default MBean descriptor: got exception:"); 181 e.printStackTrace(System.out); 182 ok = false; 183 } 184 185 String[] fields = new String[] {"descriptorType", "name"}; 186 String[] values = new String[] {"mbean", "whatsit"}; 187 String[] defaultFields = new String[] { 188 "displayName", "persistPolicy", "log", "visibility", 189 }; 190 String[] defaultValues = new String[] { 191 "bogus.class.name", "never", "F", "1", 192 }; 193 Descriptor d = new DescriptorSupport(fields, values); 194 195 mmbi = new ModelMBeanInfoSupport("bogus.class.name", "description", 196 null, null, null, null, d); 197 try { 198 mbeanDescrs = mmbi.getDescriptors("mbean"); 199 } catch (Exception e) { 200 System.out.println("...explicit MBean descriptor: got exception:"); 201 e.printStackTrace(System.out); 202 mbeanDescrs = new Descriptor[] {mmbi.getMBeanDescriptor()}; 203 } 204 205 if (mbeanDescrs.length == 1) { 206 Descriptor dd = mbeanDescrs[0]; 207 boolean thisok = true; 208 209 // Check that the fields we supplied survived in the copy 210 // and were not changed in the original 211 for (int i = 0; i < fields.length; i++) { 212 String field = fields[i]; 213 String value; 214 value = (String) dd.getFieldValue(field); 215 if (!values[i].equals(value)) { 216 System.out.println("...explicit MBean descriptor: " + 217 "value of " + field + " mutated: " + 218 value); 219 thisok = false; 220 } 221 value = (String) d.getFieldValue(field); 222 if (!values[i].equals(value)) { 223 System.out.println("...explicit MBean descriptor: " + 224 "value of " + field + " changed in " + 225 "original: " + value); 226 thisok = false; 227 } 228 } 229 230 // Check that the default fields were added 231 for (int i = 0; i < defaultFields.length; i++) { 232 String field = defaultFields[i]; 233 String value = (String) dd.getFieldValue(field); 234 if (!defaultValues[i].equals(value)) { 235 System.out.println("...explicit MBean descriptor: " + 236 "default value of " + field + 237 " wrong: " + value + " should be " + 238 defaultValues[i]); 239 thisok = false; 240 } 241 } 242 243 // Check that the original did not acquire new fields 244 if (d.getFieldNames().length != fields.length) { 245 Collection c = new TreeSet(Arrays.asList(d.getFieldNames())); 246 c.removeAll(Arrays.asList(fields)); 247 System.out.println("...explicit MBean descriptor: " + 248 "acquired new fields: " + c); 249 thisok = false; 250 } 251 252 if (thisok) 253 System.out.println("...explicit MBean descriptor: OK"); 254 else 255 ok = false; 256 } else { 257 System.out.println("...explicit MBean descriptor: bad array: " + 258 Arrays.asList(mbeanDescrs)); 259 ok = false; 260 } 261 262 return ok; 263 } 264 }