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