1 22 package org.jboss.test.jmx.compliance.metadata; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.util.Arrays ; 29 30 import javax.management.MBeanAttributeInfo ; 31 import javax.management.MBeanConstructorInfo ; 32 import javax.management.MBeanInfo ; 33 import javax.management.MBeanNotificationInfo ; 34 import javax.management.MBeanOperationInfo ; 35 import javax.management.MBeanParameterInfo ; 36 37 import junit.framework.TestCase; 38 39 44 public class MBeanInfoTEST 45 extends TestCase 46 { 47 49 51 53 56 public MBeanInfoTEST(String s) 57 { 58 super(s); 59 } 60 61 63 public void testMBeanInfo() 64 throws Exception 65 { 66 MBeanInfo info = new MBeanInfo ( 67 "name", "description", null, null, null, null); 68 assertEquals("name", info.getClassName()); 69 assertEquals("description", info.getDescription()); 70 assertEquals(0, info.getAttributes().length); 71 assertEquals(0, info.getConstructors().length); 72 assertEquals(0, info.getNotifications().length); 73 assertEquals(0, info.getOperations().length); 74 75 info = new MBeanInfo ( 76 "name", "description", new MBeanAttributeInfo [0], new MBeanConstructorInfo [0], 77 new MBeanOperationInfo [0], new MBeanNotificationInfo [0]); 78 assertEquals("name", info.getClassName()); 79 assertEquals("description", info.getDescription()); 80 assertEquals(0, info.getAttributes().length); 81 assertEquals(0, info.getConstructors().length); 82 assertEquals(0, info.getNotifications().length); 83 assertEquals(0, info.getOperations().length); 84 85 MBeanParameterInfo [] parms = new MBeanParameterInfo [] 86 { 87 new MBeanParameterInfo ( 88 "name", "type", "description") 89 }; 90 91 MBeanAttributeInfo [] attributes = new MBeanAttributeInfo [] 92 { 93 new MBeanAttributeInfo ( 94 "name", "type", "description", true, true, false) 95 }; 96 MBeanConstructorInfo [] constructors = new MBeanConstructorInfo [] 97 { 98 new MBeanConstructorInfo ( 99 "name", "description", parms) 100 }; 101 MBeanOperationInfo [] operations = new MBeanOperationInfo [] 102 { 103 new MBeanOperationInfo ( 104 "name", "description", parms, 105 "type", MBeanOperationInfo.ACTION_INFO) 106 }; 107 MBeanNotificationInfo [] notifications = new MBeanNotificationInfo [] 108 { 109 new MBeanNotificationInfo (new String [] { "type1", "type" }, "name", "description") 110 }; 111 info = new MBeanInfo ( 112 "name", "description", attributes, constructors, 113 operations, notifications); 114 assertEquals("name", info.getClassName()); 115 assertEquals("description", info.getDescription()); 116 assertEquals(1, info.getAttributes().length); 117 assertEquals(1, info.getConstructors().length); 118 assertEquals(1, info.getNotifications().length); 119 assertEquals(1, info.getOperations().length); 120 } 121 122 public void testEquals() 123 throws Exception 124 { 125 MBeanParameterInfo [] parms = new MBeanParameterInfo [] 126 { 127 new MBeanParameterInfo ( 128 "name", "type", "description") 129 }; 130 131 MBeanAttributeInfo [] attributes = new MBeanAttributeInfo [] 132 { 133 new MBeanAttributeInfo ( 134 "name", "type", "description", true, true, false) 135 }; 136 MBeanConstructorInfo [] constructors = new MBeanConstructorInfo [] 137 { 138 new MBeanConstructorInfo ( 139 "name", "description", parms) 140 }; 141 MBeanOperationInfo [] operations = new MBeanOperationInfo [] 142 { 143 new MBeanOperationInfo ( 144 "name", "description", parms, 145 "type", MBeanOperationInfo.ACTION_INFO) 146 }; 147 MBeanNotificationInfo [] notifications = new MBeanNotificationInfo [] 148 { 149 new MBeanNotificationInfo (new String [] { "type1", "type" }, "name", "description") 150 }; 151 MBeanInfo info = new MBeanInfo ( 152 "name", "description", attributes, constructors, 153 operations, notifications); 154 155 assertTrue("Null is not equal to any instance", info.equals(null) == false); 156 assertTrue("Instance is only equal another MBeanInfo instance", info.equals(new Object ()) == false); 157 assertTrue("Instance should equal itself", info.equals(info)); 158 159 MBeanInfo info2 = new MBeanInfo ( 160 "name", "description", attributes, constructors, 161 operations, notifications); 162 assertTrue("Instances with same values should be equal", info.equals(info2)); 163 assertTrue("Instances with same values should be equal", info2.equals(info)); 164 165 info2 = new MBeanInfo ( 166 "name2", "description", attributes, constructors, 167 operations, notifications); 168 assertTrue("Instances with different class names are not equal", info.equals(info2) == false); 169 assertTrue("Instances with different class names are not equal", info2.equals(info) == false); 170 171 info2 = new MBeanInfo ( 172 "name", "description2", attributes, constructors, 173 operations, notifications); 174 assertTrue("Instances with different descriptions are not equal", info.equals(info2) == false); 175 assertTrue("Instances with different descriptions are not equal", info2.equals(info) == false); 176 177 MBeanAttributeInfo [] attributes2 = new MBeanAttributeInfo [] 178 { 179 new MBeanAttributeInfo ( 180 "name2", "type", "description", true, true, false) 181 }; 182 183 info2 = new MBeanInfo ( 184 "name", "description", attributes2, constructors, 185 operations, notifications); 186 assertTrue("Instances with different attributes are not equal", info.equals(info2) == false); 187 assertTrue("Instances with different attributes are not equal", info2.equals(info) == false); 188 189 attributes2 = new MBeanAttributeInfo [] 190 { 191 new MBeanAttributeInfo ( 192 "name2", "type", "description", true, true, false), 193 new MBeanAttributeInfo ( 194 "name3", "type", "description", true, true, false) 195 }; 196 197 info2 = new MBeanInfo ( 198 "name", "description", attributes2, constructors, 199 operations, notifications); 200 assertTrue("Instances with different numbers of attributes are not equal", info.equals(info2) == false); 201 assertTrue("Instances with different numbers of attributes are not equal", info2.equals(info) == false); 202 203 info2 = new MBeanInfo ( 204 "name", "description", null, constructors, 205 operations, notifications); 206 assertTrue("Instances with and without attributes are not equal", info.equals(info2) == false); 207 assertTrue("Instances with and without attributes are not equal", info2.equals(info) == false); 208 209 MBeanConstructorInfo [] constructors2 = new MBeanConstructorInfo [] 210 { 211 new MBeanConstructorInfo ( 212 "name2", "description", parms) 213 }; 214 215 info2 = new MBeanInfo ( 216 "name", "description", attributes, constructors2, 217 operations, notifications); 218 assertTrue("Instances with different constructors are not equal", info.equals(info2) == false); 219 assertTrue("Instances with different constructors are not equal", info2.equals(info) == false); 220 221 constructors2 = new MBeanConstructorInfo [] 222 { 223 new MBeanConstructorInfo ( 224 "name2", "description", parms), 225 new MBeanConstructorInfo ( 226 "name3", "description", parms) 227 }; 228 229 info2 = new MBeanInfo ( 230 "name", "description", attributes, constructors2, 231 operations, notifications); 232 assertTrue("Instances with different numbers of constructors are not equal", info.equals(info2) == false); 233 assertTrue("Instances with different numbers of constructors are not equal", info2.equals(info) == false); 234 235 info2 = new MBeanInfo ( 236 "name", "description", attributes, null, 237 operations, notifications); 238 assertTrue("Instances with and without constructors are not equal", info.equals(info2) == false); 239 assertTrue("Instances with and without constructors are not equal", info2.equals(info) == false); 240 241 MBeanOperationInfo [] operations2 = new MBeanOperationInfo [] 242 { 243 new MBeanOperationInfo ( 244 "name2", "description", parms, 245 "type", MBeanOperationInfo.ACTION_INFO) 246 }; 247 248 info2 = new MBeanInfo ( 249 "name", "description", attributes, constructors, 250 operations2, notifications); 251 assertTrue("Instances with different operations are not equal", info.equals(info2) == false); 252 assertTrue("Instances with different operations are not equal", info2.equals(info) == false); 253 254 operations2 = new MBeanOperationInfo [] 255 { 256 new MBeanOperationInfo ( 257 "name2", "description", parms, 258 "type", MBeanOperationInfo.ACTION_INFO), 259 new MBeanOperationInfo ( 260 "name3", "description", parms, 261 "type", MBeanOperationInfo.ACTION_INFO) 262 }; 263 264 info2 = new MBeanInfo ( 265 "name", "description", attributes, constructors, 266 operations2, notifications); 267 assertTrue("Instances with different numbers of operations are not equal", info.equals(info2) == false); 268 assertTrue("Instances with different numbers of operations are not equal", info2.equals(info) == false); 269 270 info2 = new MBeanInfo ( 271 "name", "description", attributes, constructors, 272 null, notifications); 273 assertTrue("Instances with and without operations are not equal", info.equals(info2) == false); 274 assertTrue("Instances with and without operations are not equal", info2.equals(info) == false); 275 276 MBeanNotificationInfo [] notifications2 = new MBeanNotificationInfo [] 277 { 278 new MBeanNotificationInfo (new String [] { "type", "type" }, "name2", "description") 279 }; 280 281 info2 = new MBeanInfo ( 282 "name", "description", attributes, constructors, 283 operations, notifications2); 284 assertTrue("Instances with different notifications are not equal", info.equals(info2) == false); 285 assertTrue("Instances with different notifications are not equal", info2.equals(info) == false); 286 287 notifications2 = new MBeanNotificationInfo [] 288 { 289 new MBeanNotificationInfo (new String [] { "type", "type" }, "name2", "description"), 290 new MBeanNotificationInfo (new String [] { "type", "type" }, "name3", "description") 291 }; 292 293 info2 = new MBeanInfo ( 294 "name", "description", attributes, constructors, 295 operations, notifications2); 296 assertTrue("Instances with different numbers of notifications are not equal", info.equals(info2) == false); 297 assertTrue("Instances with different numbers of notifications are not equal", info2.equals(info) == false); 298 299 info2 = new MBeanInfo ( 300 "name", "description", attributes, constructors, 301 operations, null); 302 assertTrue("Instances with and without notifications are not equal", info.equals(info2) == false); 303 assertTrue("Instances with and without notifications are not equal", info2.equals(info) == false); 304 } 305 306 public void testHashCode() 307 throws Exception 308 { 309 MBeanParameterInfo [] parms = new MBeanParameterInfo [] 310 { 311 new MBeanParameterInfo ( 312 "name", "type", "description") 313 }; 314 315 MBeanAttributeInfo [] attributes = new MBeanAttributeInfo [] 316 { 317 new MBeanAttributeInfo ( 318 "name", "type", "description", true, true, false) 319 }; 320 MBeanConstructorInfo [] constructors = new MBeanConstructorInfo [] 321 { 322 new MBeanConstructorInfo ( 323 "name", "description", parms) 324 }; 325 MBeanOperationInfo [] operations = new MBeanOperationInfo [] 326 { 327 new MBeanOperationInfo ( 328 "name", "description", parms, 329 "type", MBeanOperationInfo.ACTION_INFO) 330 }; 331 MBeanNotificationInfo [] notifications = new MBeanNotificationInfo [] 332 { 333 new MBeanNotificationInfo ( 334 new String [] { "type1", "type" }, "name", "description") 335 }; 336 MBeanInfo info1 = new MBeanInfo ( 337 "name", "description", attributes, constructors, 338 operations, notifications); 339 MBeanInfo info2 = new MBeanInfo ( 340 "name", "description", attributes, constructors, 341 operations, notifications); 342 343 assertTrue("Different instances with the same hashcode are equal", info1.hashCode() == info2.hashCode()); 344 } 345 346 public void testSerialization() 347 throws Exception 348 { 349 MBeanParameterInfo [] parms = new MBeanParameterInfo [] 350 { 351 new MBeanParameterInfo ( 352 "name", "type", "description") 353 }; 354 355 MBeanAttributeInfo [] attributes = new MBeanAttributeInfo [] 356 { 357 new MBeanAttributeInfo ( 358 "name", "type", "description", true, true, false) 359 }; 360 MBeanConstructorInfo [] constructors = new MBeanConstructorInfo [] 361 { 362 new MBeanConstructorInfo ( 363 "name", "description", parms) 364 }; 365 MBeanOperationInfo [] operations = new MBeanOperationInfo [] 366 { 367 new MBeanOperationInfo ( 368 "name", "description", parms, 369 "type", MBeanOperationInfo.ACTION_INFO) 370 }; 371 MBeanNotificationInfo [] notifications = new MBeanNotificationInfo [] 372 { 373 new MBeanNotificationInfo ( 374 new String [] { "type1", "type" }, "name", "description") 375 }; 376 MBeanInfo info = new MBeanInfo ( 377 "name", "description", attributes, constructors, 378 operations, notifications); 379 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 381 ObjectOutputStream oos = new ObjectOutputStream (baos); 382 oos.writeObject(info); 383 384 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 386 ObjectInputStream ois = new ObjectInputStream (bais); 387 MBeanInfo result = (MBeanInfo ) ois.readObject(); 388 389 assertEquals(info.getClassName(), result.getClassName()); 390 assertEquals(Arrays.asList(info.getAttributes()), Arrays.asList(result.getAttributes())); 391 assertEquals(Arrays.asList(info.getConstructors()), Arrays.asList(result.getConstructors())); 392 assertEquals(Arrays.asList(info.getOperations()), Arrays.asList(result.getOperations())); 393 394 MBeanNotificationInfo origNotification = info.getNotifications()[0]; 396 MBeanNotificationInfo resultNotification = result.getNotifications()[0]; 397 assertEquals(origNotification.getName(), resultNotification.getName()); 398 assertEquals(origNotification.getDescription(), resultNotification.getDescription()); 399 assertEquals(Arrays.asList(origNotification.getNotifTypes()), Arrays.asList(resultNotification.getNotifTypes())); 400 } 401 } 402 | Popular Tags |