1 22 package org.jboss.test.jmx.compliance.openmbean; 23 24 import junit.framework.TestCase; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.util.Arrays ; 31 import java.util.HashSet ; 32 import java.util.Set ; 33 34 import javax.management.MBeanOperationInfo ; 35 import javax.management.MBeanParameterInfo ; 36 import javax.management.MBeanNotificationInfo ; 37 import javax.management.openmbean.OpenMBeanAttributeInfo ; 38 import javax.management.openmbean.OpenMBeanAttributeInfoSupport ; 39 import javax.management.openmbean.OpenMBeanConstructorInfo ; 40 import javax.management.openmbean.OpenMBeanConstructorInfoSupport ; 41 import javax.management.openmbean.OpenMBeanInfoSupport ; 42 import javax.management.openmbean.OpenMBeanParameterInfo ; 43 import javax.management.openmbean.OpenMBeanParameterInfoSupport ; 44 import javax.management.openmbean.OpenMBeanOperationInfo ; 45 import javax.management.openmbean.OpenMBeanOperationInfoSupport ; 46 import javax.management.openmbean.OpenType ; 47 import javax.management.openmbean.SimpleType ; 48 49 54 public class OpenMBeanInfoSupportTestCase 55 extends TestCase 56 { 57 59 61 63 66 public OpenMBeanInfoSupportTestCase(String s) 67 { 68 super(s); 69 } 70 71 73 public void testOpenMBeanInfoSupport() 74 throws Exception 75 { 76 OpenMBeanInfoSupport info = new OpenMBeanInfoSupport ( 77 "name", "description", null, null, null, null); 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 info = new OpenMBeanInfoSupport ( 86 "name", "description", new OpenMBeanAttributeInfoSupport [0], new OpenMBeanConstructorInfoSupport [0], 87 new OpenMBeanOperationInfoSupport [0], new MBeanNotificationInfo [0]); 88 assertEquals("name", info.getClassName()); 89 assertEquals("description", info.getDescription()); 90 assertEquals(0, info.getAttributes().length); 91 assertEquals(0, info.getConstructors().length); 92 assertEquals(0, info.getNotifications().length); 93 assertEquals(0, info.getOperations().length); 94 95 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 96 { 97 new OpenMBeanParameterInfoSupport ( 98 "name", "description", SimpleType.STRING) 99 }; 100 101 OpenMBeanAttributeInfoSupport [] attributes = new OpenMBeanAttributeInfoSupport [] 102 { 103 new OpenMBeanAttributeInfoSupport ( 104 "name", "description", SimpleType.STRING, true, true, false) 105 }; 106 OpenMBeanConstructorInfoSupport [] constructors = new OpenMBeanConstructorInfoSupport [] 107 { 108 new OpenMBeanConstructorInfoSupport ( 109 "name", "description", parms) 110 }; 111 OpenMBeanOperationInfoSupport [] operations = new OpenMBeanOperationInfoSupport [] 112 { 113 new OpenMBeanOperationInfoSupport ( 114 "name", "description", parms, 115 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO) 116 }; 117 MBeanNotificationInfo [] notifications = new MBeanNotificationInfo [] 118 { 119 new MBeanNotificationInfo (new String [] { "type1", "type" }, "name", "description") 120 }; 121 info = new OpenMBeanInfoSupport ( 122 "name", "description", attributes, constructors, 123 operations, notifications); 124 assertEquals("name", info.getClassName()); 125 assertEquals("description", info.getDescription()); 126 assertEquals(1, info.getAttributes().length); 127 assertEquals(1, info.getConstructors().length); 128 assertEquals(1, info.getNotifications().length); 129 assertEquals(1, info.getOperations().length); 130 } 131 132 public void testEquals() 133 throws Exception 134 { 135 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 136 { 137 new OpenMBeanParameterInfoSupport ( 138 "name", "description", SimpleType.STRING) 139 }; 140 141 OpenMBeanAttributeInfoSupport [] attributes = new OpenMBeanAttributeInfoSupport [] 142 { 143 new OpenMBeanAttributeInfoSupport ( 144 "name", "description", SimpleType.STRING, true, true, false) 145 }; 146 OpenMBeanConstructorInfoSupport [] constructors = new OpenMBeanConstructorInfoSupport [] 147 { 148 new OpenMBeanConstructorInfoSupport ( 149 "name", "description", parms) 150 }; 151 OpenMBeanOperationInfoSupport [] operations = new OpenMBeanOperationInfoSupport [] 152 { 153 new OpenMBeanOperationInfoSupport ( 154 "name", "description", parms, 155 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO) 156 }; 157 MBeanNotificationInfo [] notifications = new MBeanNotificationInfo [] 158 { 159 new MBeanNotificationInfo (new String [] { "type1", "type" }, "name", "description") 160 }; 161 OpenMBeanInfoSupport info = new OpenMBeanInfoSupport ( 162 "name", "description", attributes, constructors, 163 operations, notifications); 164 165 assertTrue("Null is not equal to any instance", info.equals(null) == false); 166 assertTrue("Instance is only equal another OpenMBeanInfo instance", info.equals(new Object ()) == false); 167 assertTrue("Instance should equal itself", info.equals(info)); 168 169 OpenMBeanInfoSupport info2 = new OpenMBeanInfoSupport ( 170 "name", "description", attributes, constructors, 171 operations, notifications); 172 assertTrue("Instances with same values should be equal", info.equals(info2)); 173 assertTrue("Instances with same values should be equal", info2.equals(info)); 174 175 info2 = new OpenMBeanInfoSupport ( 176 "name2", "description", attributes, constructors, 177 operations, notifications); 178 assertTrue("Instances with different class names are not equal", info.equals(info2) == false); 179 assertTrue("Instances with different class names are not equal", info2.equals(info) == false); 180 181 info2 = new OpenMBeanInfoSupport ( 182 "name", "description2", attributes, constructors, 183 operations, notifications); 184 assertTrue("Instances with different descriptions are equal", info.equals(info2)); 185 assertTrue("Instances with different descriptions are equal", info2.equals(info)); 186 187 OpenMBeanAttributeInfoSupport [] attributes2 = new OpenMBeanAttributeInfoSupport [] 188 { 189 new OpenMBeanAttributeInfoSupport ( 190 "name2", "description", SimpleType.STRING, true, true, false) 191 }; 192 193 info2 = new OpenMBeanInfoSupport ( 194 "name", "description", attributes2, constructors, 195 operations, notifications); 196 assertTrue("Instances with different attributes are not equal", info.equals(info2) == false); 197 assertTrue("Instances with different attributes are not equal", info2.equals(info) == false); 198 199 attributes2 = new OpenMBeanAttributeInfoSupport [] 200 { 201 new OpenMBeanAttributeInfoSupport ( 202 "name2", "description", SimpleType.STRING, true, true, false), 203 new OpenMBeanAttributeInfoSupport ( 204 "name3", "description", SimpleType.STRING, true, true, false) 205 }; 206 207 info2 = new OpenMBeanInfoSupport ( 208 "name", "description", attributes2, constructors, 209 operations, notifications); 210 assertTrue("Instances with different numbers of attributes are not equal", info.equals(info2) == false); 211 assertTrue("Instances with different numbers of attributes are not equal", info2.equals(info) == false); 212 213 info2 = new OpenMBeanInfoSupport ( 214 "name", "description", null, constructors, 215 operations, notifications); 216 assertTrue("Instances with and without attributes are not equal", info.equals(info2) == false); 217 assertTrue("Instances with and without attributes are not equal", info2.equals(info) == false); 218 219 OpenMBeanConstructorInfoSupport [] constructors2 = new OpenMBeanConstructorInfoSupport [] 220 { 221 new OpenMBeanConstructorInfoSupport ( 222 "name2", "description", parms) 223 }; 224 225 info2 = new OpenMBeanInfoSupport ( 226 "name", "description", attributes, constructors2, 227 operations, notifications); 228 assertTrue("Instances with different constructors are not equal", info.equals(info2) == false); 229 assertTrue("Instances with different constructors are not equal", info2.equals(info) == false); 230 231 constructors2 = new OpenMBeanConstructorInfoSupport [] 232 { 233 new OpenMBeanConstructorInfoSupport ( 234 "name2", "description", parms), 235 new OpenMBeanConstructorInfoSupport ( 236 "name3", "description", parms) 237 }; 238 239 info2 = new OpenMBeanInfoSupport ( 240 "name", "description", attributes, constructors2, 241 operations, notifications); 242 assertTrue("Instances with different numbers of constructors are not equal", info.equals(info2) == false); 243 assertTrue("Instances with different numbers of constructors are not equal", info2.equals(info) == false); 244 245 info2 = new OpenMBeanInfoSupport ( 246 "name", "description", attributes, null, 247 operations, notifications); 248 assertTrue("Instances with and without constructors are not equal", info.equals(info2) == false); 249 assertTrue("Instances with and without constructors are not equal", info2.equals(info) == false); 250 251 OpenMBeanOperationInfoSupport [] operations2 = new OpenMBeanOperationInfoSupport [] 252 { 253 new OpenMBeanOperationInfoSupport ( 254 "name2", "description", parms, 255 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO) 256 }; 257 258 info2 = new OpenMBeanInfoSupport ( 259 "name", "description", attributes, constructors, 260 operations2, notifications); 261 assertTrue("Instances with different operations are not equal", info.equals(info2) == false); 262 assertTrue("Instances with different operations are not equal", info2.equals(info) == false); 263 264 operations2 = new OpenMBeanOperationInfoSupport [] 265 { 266 new OpenMBeanOperationInfoSupport ( 267 "name2", "description", parms, 268 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO), 269 new OpenMBeanOperationInfoSupport ( 270 "name3", "description", parms, 271 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO) 272 }; 273 274 info2 = new OpenMBeanInfoSupport ( 275 "name", "description", attributes, constructors, 276 operations2, notifications); 277 assertTrue("Instances with different numbers of operations are not equal", info.equals(info2) == false); 278 assertTrue("Instances with different numbers of operations are not equal", info2.equals(info) == false); 279 280 info2 = new OpenMBeanInfoSupport ( 281 "name", "description", attributes, constructors, 282 null, notifications); 283 assertTrue("Instances with and without operations are not equal", info.equals(info2) == false); 284 assertTrue("Instances with and without operations are not equal", info2.equals(info) == false); 285 286 MBeanNotificationInfo [] notifications2 = new MBeanNotificationInfo [] 287 { 288 new MBeanNotificationInfo (new String [] { "type", "type" }, "name2", "description") 289 }; 290 291 info2 = new OpenMBeanInfoSupport ( 292 "name", "description", attributes, constructors, 293 operations, notifications2); 294 assertTrue("Instances with different notifications are not equal", info.equals(info2) == false); 295 assertTrue("Instances with different notifications are not equal", info2.equals(info) == false); 296 297 notifications2 = new MBeanNotificationInfo [] 298 { 299 new MBeanNotificationInfo (new String [] { "type", "type" }, "name2", "description"), 300 new MBeanNotificationInfo (new String [] { "type", "type" }, "name3", "description") 301 }; 302 303 info2 = new OpenMBeanInfoSupport ( 304 "name", "description", attributes, constructors, 305 operations, notifications2); 306 assertTrue("Instances with different numbers of notifications are not equal", info.equals(info2) == false); 307 assertTrue("Instances with different numbers of notifications are not equal", info2.equals(info) == false); 308 309 info2 = new OpenMBeanInfoSupport ( 310 "name", "description", attributes, constructors, 311 operations, null); 312 assertTrue("Instances with and without notifications are not equal", info.equals(info2) == false); 313 assertTrue("Instances with and without notifications are not equal", info2.equals(info) == false); 314 } 315 316 public void testHashCode() 317 throws Exception 318 { 319 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 320 { 321 new OpenMBeanParameterInfoSupport ( 322 "name", "description", SimpleType.STRING) 323 }; 324 325 OpenMBeanAttributeInfoSupport [] attributes = new OpenMBeanAttributeInfoSupport [] 326 { 327 new OpenMBeanAttributeInfoSupport ( 328 "name", "description", SimpleType.STRING, true, true, false) 329 }; 330 OpenMBeanConstructorInfoSupport [] constructors = new OpenMBeanConstructorInfoSupport [] 331 { 332 new OpenMBeanConstructorInfoSupport ( 333 "name", "description", parms) 334 }; 335 OpenMBeanOperationInfoSupport [] operations = new OpenMBeanOperationInfoSupport [] 336 { 337 new OpenMBeanOperationInfoSupport ( 338 "name", "description", parms, 339 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO) 340 }; 341 MBeanNotificationInfo [] notifications = new MBeanNotificationInfo [] 342 { 343 new MBeanNotificationInfo ( 344 new String [] { "type1", "type" }, "name", "description") 345 }; 346 OpenMBeanInfoSupport info = new OpenMBeanInfoSupport ( 347 "name", "description", attributes, constructors, 348 operations, notifications); 349 350 int myHash = "name".hashCode() + 351 new HashSet (Arrays.asList(attributes)).hashCode() + 352 new HashSet (Arrays.asList(constructors)).hashCode() + 353 new HashSet (Arrays.asList(operations)).hashCode() + 354 new HashSet (Arrays.asList(notifications)).hashCode(); 355 assertEquals(myHash, info.hashCode()); 356 } 357 358 public void testToString() 359 throws Exception 360 { 361 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 362 { 363 new OpenMBeanParameterInfoSupport ( 364 "name", "description", SimpleType.STRING) 365 }; 366 367 OpenMBeanAttributeInfoSupport [] attributes = new OpenMBeanAttributeInfoSupport [] 368 { 369 new OpenMBeanAttributeInfoSupport ( 370 "name", "description", SimpleType.STRING, true, true, false) 371 }; 372 OpenMBeanConstructorInfoSupport [] constructors = new OpenMBeanConstructorInfoSupport [] 373 { 374 new OpenMBeanConstructorInfoSupport ( 375 "name", "description", parms) 376 }; 377 OpenMBeanOperationInfoSupport [] operations = new OpenMBeanOperationInfoSupport [] 378 { 379 new OpenMBeanOperationInfoSupport ( 380 "name", "description", parms, 381 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO) 382 }; 383 MBeanNotificationInfo [] notifications = new MBeanNotificationInfo [] 384 { 385 new MBeanNotificationInfo ( 386 new String [] { "type1", "type" }, "name", "description") 387 }; 388 OpenMBeanInfoSupport info = new OpenMBeanInfoSupport ( 389 "NAME", "DESCRIPTION", attributes, constructors, 390 operations, notifications); 391 392 String toString = info.toString(); 393 394 assertTrue("info.toString() should contain the name", 395 toString.indexOf("NAME") != -1); 396 assertTrue("info.toString() should contain the attributes", 397 toString.indexOf(Arrays.asList(attributes).toString()) != -1); 398 assertTrue("info.toString() should contain the constructors", 399 toString.indexOf(Arrays.asList(constructors).toString()) != -1); 400 assertTrue("info.toString() should contain the operations", 401 toString.indexOf(Arrays.asList(operations).toString()) != -1); 402 assertTrue("info.toString() should contain the notifications", 403 toString.indexOf(Arrays.asList(notifications).toString()) != -1); 404 } 405 406 public void testSerialization() 407 throws Exception 408 { 409 OpenMBeanParameterInfoSupport [] parms = new OpenMBeanParameterInfoSupport [] 410 { 411 new OpenMBeanParameterInfoSupport ( 412 "name", "description", SimpleType.STRING) 413 }; 414 415 OpenMBeanAttributeInfoSupport [] attributes = new OpenMBeanAttributeInfoSupport [] 416 { 417 new OpenMBeanAttributeInfoSupport ( 418 "name", "description", SimpleType.STRING, true, true, false) 419 }; 420 OpenMBeanConstructorInfoSupport [] constructors = new OpenMBeanConstructorInfoSupport [] 421 { 422 new OpenMBeanConstructorInfoSupport ( 423 "name", "description", parms) 424 }; 425 OpenMBeanOperationInfoSupport [] operations = new OpenMBeanOperationInfoSupport [] 426 { 427 new OpenMBeanOperationInfoSupport ( 428 "name", "description", parms, 429 SimpleType.STRING, MBeanOperationInfo.ACTION_INFO) 430 }; 431 MBeanNotificationInfo [] notifications = new MBeanNotificationInfo [] 432 { 433 new MBeanNotificationInfo ( 434 new String [] { "type1", "type" }, "name", "description") 435 }; 436 OpenMBeanInfoSupport info = new OpenMBeanInfoSupport ( 437 "name", "description", attributes, constructors, 438 operations, notifications); 439 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 441 ObjectOutputStream oos = new ObjectOutputStream (baos); 442 oos.writeObject(info); 443 444 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 446 ObjectInputStream ois = new ObjectInputStream (bais); 447 OpenMBeanInfoSupport result = (OpenMBeanInfoSupport ) ois.readObject(); 448 449 assertEquals(info.getClassName(), result.getClassName()); 450 assertEquals(Arrays.asList(info.getAttributes()), Arrays.asList(result.getAttributes())); 451 assertEquals(Arrays.asList(info.getConstructors()), Arrays.asList(result.getConstructors())); 452 assertEquals(Arrays.asList(info.getOperations()), Arrays.asList(result.getOperations())); 453 454 MBeanNotificationInfo origNotification = info.getNotifications()[0]; 456 MBeanNotificationInfo resultNotification = result.getNotifications()[0]; 457 assertEquals(origNotification.getName(), resultNotification.getName()); 458 assertEquals(origNotification.getDescription(), resultNotification.getDescription()); 459 assertEquals(Arrays.asList(origNotification.getNotifTypes()), Arrays.asList(resultNotification.getNotifTypes())); 460 } 461 462 public void testErrors() 463 throws Exception 464 { 465 boolean caught = false; 466 try 467 { 468 MyOpenMBeanAttributeInfoSupport[] infos = new MyOpenMBeanAttributeInfoSupport[] 469 { 470 new MyOpenMBeanAttributeInfoSupport() 471 }; 472 new OpenMBeanInfoSupport ( 473 "className", "description", infos, null, null, null); 474 } 475 catch (ArrayStoreException e) 476 { 477 caught = true; 478 } 479 if (caught == false) 480 fail("Expected ArrayStoreException for attributes not MBeanAttributeInfo"); 481 482 caught = false; 483 try 484 { 485 MyOpenMBeanConstructorInfoSupport[] infos = new MyOpenMBeanConstructorInfoSupport[] 486 { 487 new MyOpenMBeanConstructorInfoSupport() 488 }; 489 new OpenMBeanInfoSupport ( 490 "className", "description", null, infos, null, null); 491 } 492 catch (ArrayStoreException e) 493 { 494 caught = true; 495 } 496 if (caught == false) 497 fail("Expected ArrayStoreException for attributes not MBeanConstructorInfo"); 498 499 caught = false; 500 try 501 { 502 MyOpenMBeanOperationInfoSupport[] infos = new MyOpenMBeanOperationInfoSupport[] 503 { 504 new MyOpenMBeanOperationInfoSupport() 505 }; 506 new OpenMBeanInfoSupport ( 507 "className", "description", null, null, infos, null); 508 } 509 catch (ArrayStoreException e) 510 { 511 caught = true; 512 } 513 if (caught == false) 514 fail("Expected ArrayStoreException for attributes not MBeanOperationInfo"); 515 } 516 517 public static class MyOpenMBeanParameterInfo 518 implements OpenMBeanParameterInfo 519 { 520 public boolean equals(Object o) { return false; } 521 public Object getDefaultValue() { return null; } 522 public String getDescription() { return null; } 523 public Set getLegalValues() { return null; } 524 public Comparable getMaxValue() { return null; } 525 public Comparable getMinValue() { return null; } 526 public String getName() { return null; } 527 public OpenType getOpenType() { return null; } 528 public boolean hasDefaultValue() { return false; } 529 public boolean hasLegalValues() { return false; } 530 public int hashCode() { return 0; } 531 public boolean hasMaxValue() { return false; } 532 public boolean hasMinValue() { return false; } 533 public boolean isValue(Object o) { return false; } 534 public String toString() { return null; } 535 } 536 537 public static class MyOpenMBeanAttributeInfoSupport 538 extends MyOpenMBeanParameterInfo 539 implements OpenMBeanAttributeInfo 540 { 541 public boolean isIs() { return false; } 542 public boolean isReadable() { return false; } 543 public boolean isWritable() { return false; } 544 } 545 546 public static class MyOpenMBeanConstructorInfoSupport 547 implements OpenMBeanConstructorInfo 548 { 549 public boolean equals(Object o) { return false; } 550 public String getDescription() { return null; } 551 public String getName() { return null; } 552 public MBeanParameterInfo [] getSignature() { return null; } 553 public int hashCode() { return 0; } 554 public String toString() { return null; } 555 } 556 557 public static class MyOpenMBeanOperationInfoSupport 558 implements OpenMBeanOperationInfo 559 { 560 public boolean equals(Object o) { return false; } 561 public String getDescription() { return null; } 562 public int getImpact() { return 0; } 563 public String getName() { return null; } 564 public OpenType getReturnOpenType() { return null; } 565 public String getReturnType() { return null; } 566 public MBeanParameterInfo [] getSignature() { return null; } 567 public int hashCode() { return 0; } 568 public String toString() { return null; } 569 } 570 } 571 | Popular Tags |