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