1 8 package test.javax.management.openmbean; 9 10 13 14 import java.util.Iterator ; 15 import java.util.Set ; 16 import javax.management.openmbean.ArrayType ; 17 import javax.management.openmbean.OpenDataException ; 18 import javax.management.openmbean.OpenMBeanAttributeInfoSupport ; 19 import javax.management.openmbean.SimpleType ; 20 21 import junit.framework.TestCase; 22 23 24 public class OpenMBeanAttributeInfoSupportTest extends TestCase 25 { 26 27 28 public OpenMBeanAttributeInfoSupportTest(String s) 29 { 30 super(s); 31 } 32 33 protected void setUp() throws Exception 34 { 35 super.setUp(); 36 37 } 38 39 protected void tearDown() throws Exception 40 { 41 super.tearDown(); 42 } 43 44 public void testEquals() throws Exception 45 { 46 try 47 { 48 49 OpenMBeanAttributeInfoSupport o1 = 50 new OpenMBeanAttributeInfoSupport ("name", "The name", SimpleType.STRING, false, true, false); 51 OpenMBeanAttributeInfoSupport o2 = 52 new OpenMBeanAttributeInfoSupport ("name", "The name", SimpleType.STRING, false, true, false); 53 OpenMBeanAttributeInfoSupport o3 = 54 new OpenMBeanAttributeInfoSupport ("name", "The name", SimpleType.STRING, true, false, false); 55 56 assertTrue(!o1.equals(null)); 57 assertTrue(o1.equals(o2)); 58 assertEquals(o1.hashCode(), o2.hashCode()); 59 assertTrue(!o1.equals(o3)); 60 61 } 62 catch (Exception e) 63 { 64 e.printStackTrace(); 65 } 66 67 68 } 69 70 public void testSixParamCtor() throws Exception 71 { 72 OpenMBeanAttributeInfoSupport info = 73 new OpenMBeanAttributeInfoSupport ("price", 74 "how much it costs", 75 SimpleType.FLOAT, 76 true, 77 false, 78 false); 79 assertTrue("Null info constructed", info != null); 80 assertTrue("OpenType should be FLOAT", 81 info.getOpenType().equals(SimpleType.FLOAT)); 82 assertTrue("Attribute should be readable", info.isReadable()); 83 assertFalse("Attribute should not be writeable", info.isWritable()); 84 assertFalse("Attribute is not 'is", info.isIs()); 85 86 try 87 { 88 info = 89 new OpenMBeanAttributeInfoSupport (null, 90 "how much it costs", 91 SimpleType.FLOAT, 92 true, 93 false, 94 false); 95 fail("Expecting exception for null name"); 96 } 97 catch (IllegalArgumentException x) 98 { 99 assertTrue(true); 100 } 101 102 try 103 { 104 info = 105 new OpenMBeanAttributeInfoSupport ("", 106 "how much it costs", 107 SimpleType.FLOAT, 108 true, 109 false, 110 false); 111 fail("Expecting exception for empty name"); 112 } 113 catch (IllegalArgumentException x) 114 { 115 assertTrue(true); 116 } 117 118 try 119 { 120 info = 121 new OpenMBeanAttributeInfoSupport ("price", 122 null, 123 SimpleType.FLOAT, 124 true, 125 false, 126 false); 127 fail("Expecting exception for null description"); 128 } 129 catch (IllegalArgumentException x) 130 { 131 assertTrue(true); 132 } 133 134 try 135 { 136 info = 137 new OpenMBeanAttributeInfoSupport ("price", 138 "", 139 SimpleType.FLOAT, 140 true, 141 false, 142 false); 143 fail("Expecting exception for empty description"); 144 } 145 catch (IllegalArgumentException x) 146 { 147 assertTrue(true); 148 } 149 150 try 151 { 152 info = 153 new OpenMBeanAttributeInfoSupport ("price", 154 "how much it costs", 155 null, 156 true, 157 false, 158 false); 159 fail("Expecting exception for null type"); 160 } 161 catch (IllegalArgumentException x) 162 { 163 assertTrue(true); 164 } 165 } 166 167 public void testSevenParamCtor() throws Exception 168 { 169 Float defaultvalue = new Float (1.00); 170 OpenMBeanAttributeInfoSupport info = 171 new OpenMBeanAttributeInfoSupport ("price", 172 "how much it costs", 173 SimpleType.FLOAT, 174 true, 175 false, 176 false, 177 defaultvalue); 178 assertTrue("Null info constructed", info != null); 179 assertTrue("Expecting default value of 1.00", defaultvalue.equals((Float )info.getDefaultValue())); 180 181 info = 182 new OpenMBeanAttributeInfoSupport ("price", 183 "how much it costs", 184 SimpleType.FLOAT, 185 true, 186 false, 187 false, 188 null); 189 assertTrue("Null info constructed", info != null); 190 assertFalse("There should be no default value", info.hasDefaultValue()); 191 } 192 193 public void testEightParamCtor() throws Exception 194 { 195 Float [] legalvalues = {new Float (0.75), new Float (1.00), new Float (1.50)}; 196 Float defaultvalue = new Float (1.00); 197 OpenMBeanAttributeInfoSupport info = 198 new OpenMBeanAttributeInfoSupport ("price", 199 "how much it costs", 200 SimpleType.FLOAT, 201 true, 202 false, 203 false, 204 defaultvalue, 205 legalvalues); 206 assertTrue("Null info constructed", info != null); 207 Set legalset = info.getLegalValues(); 208 assertTrue("Legal set is the wrong size", legalset.size() == legalvalues.length); 209 assertTrue("0.75 not in legal set", 210 legalset.contains(new Float (0.75))); 211 assertTrue("1.00 not in legal set", 212 legalset.contains(new Float (1.00))); 213 assertTrue("1.50 not in legal set", 214 legalset.contains(new Float (1.50))); 215 216 info = 217 new OpenMBeanAttributeInfoSupport ("price", 218 "how much it costs", 219 SimpleType.FLOAT, 220 true, 221 false, 222 false, 223 defaultvalue, 224 null); 225 assertTrue("Null info constructed", info != null); 226 assertFalse("There should be no legal value set for null", 227 info.hasLegalValues()); 228 229 info = 230 new OpenMBeanAttributeInfoSupport ("price", 231 "how much it costs", 232 SimpleType.FLOAT, 233 true, 234 false, 235 false, 236 defaultvalue, 237 new Float [0]); 238 assertTrue("Null info constructed", info != null); 239 assertFalse("There should be no legal value set for Float[0]", 240 info.hasLegalValues()); 241 242 info = 243 new OpenMBeanAttributeInfoSupport ("price", 244 "how much it costs", 245 SimpleType.FLOAT, 246 true, 247 false, 248 false, 249 null, 250 legalvalues); 251 assertTrue("Null info constructed", info != null); 252 assertFalse("Has a default value", info.hasDefaultValue()); 253 254 try 255 { 256 info = 257 new OpenMBeanAttributeInfoSupport ("price", 258 "how much it costs", 259 SimpleType.FLOAT, 260 true, 261 false, 262 false, 263 "Invalid Default Value", 264 new Float [0]); 265 fail("Expecting exception for invalid default value"); 266 } 267 catch (OpenDataException x) 268 { 269 assertTrue(true); 270 } 271 272 try 273 { 274 info = 275 new OpenMBeanAttributeInfoSupport ("price", 276 "how much it costs", 277 SimpleType.FLOAT, 278 true, 279 false, 280 false, 281 defaultvalue, 282 new Object []{new Float (0.75), "$1.50"}); 283 fail("Expecting exception for invalid legal value"); 284 } 285 catch (OpenDataException x) 286 { 287 assertTrue(true); 288 } 289 290 try 291 { 292 info = 293 new OpenMBeanAttributeInfoSupport ("price", 294 "how much it costs", 295 new ArrayType (1, SimpleType.FLOAT), 296 true, 297 false, 298 false, 299 defaultvalue, 300 null); 301 fail("Expecting exception for non null default w/ArrayType attribute"); 302 } 303 catch (OpenDataException x) 304 { 305 assertTrue(true); 306 } 307 308 try 309 { 310 info = 311 new OpenMBeanAttributeInfoSupport ("price", 312 "how much it costs", 313 new ArrayType (1, SimpleType.FLOAT), 314 true, 315 false, 316 false, 317 null, 318 new Float []{new Float (0.75), new Float (1.50)}); 319 fail("Expecting exception for non null legal set w/ArrayType attribute"); 320 } 321 catch (OpenDataException x) 322 { 323 assertTrue(true); 324 } 325 326 try 327 { 328 info = 329 new OpenMBeanAttributeInfoSupport ("price", 330 "how much it costs", 331 new ArrayType (1, SimpleType.FLOAT), 332 true, 333 false, 334 false, 335 new Float (0.25), 336 legalvalues); 337 fail("Expecting exception for default not in legal set"); 338 } 339 catch (OpenDataException x) 340 { 341 assertTrue(true); 342 } 343 344 try 345 { 346 info = 347 new OpenMBeanAttributeInfoSupport ("price", 348 "how much it costs", 349 SimpleType.INTEGER, 350 true, 351 false, 352 false, 353 new Integer (1), 354 new Integer []{new Integer (0), new Integer (2)}); 355 fail("Expecting exception for default not in legal set"); 356 } 357 catch (OpenDataException x) 358 { 359 assertTrue(true); 360 } 361 } 362 363 public void testNineParameCtor() throws Exception 364 { 365 Float defaultvalue = new Float (1.00); 366 Float minvalue = new Float (0.75); 367 Float maxvalue = new Float (1.50); 368 OpenMBeanAttributeInfoSupport info = 369 new OpenMBeanAttributeInfoSupport ("price", 370 "how much it costs", 371 SimpleType.FLOAT, 372 true, 373 false, 374 false, 375 defaultvalue, 376 minvalue, 377 maxvalue); 378 assertTrue("Null info constructed", info != null); 379 assertTrue("Expecting min value of 0.75", 380 info.hasMinValue() && minvalue.equals((Float )info.getMinValue())); 381 assertTrue("Expecting max value of 1.50", 382 info.hasMaxValue() && maxvalue.equals((Float )info.getMaxValue())); 383 384 info = 385 new OpenMBeanAttributeInfoSupport ("price", 386 "how much it costs", 387 SimpleType.FLOAT, 388 true, 389 false, 390 false, 391 defaultvalue, 392 null, 393 maxvalue); 394 assertTrue("Null info constructed", info != null); 395 assertFalse("Not expecting a min value", 396 info.hasMinValue()); 397 assertTrue("Expecting max value of 1.50", 398 info.hasMaxValue() && maxvalue.equals((Float )info.getMaxValue())); 399 400 info = 401 new OpenMBeanAttributeInfoSupport ("price", 402 "how much it costs", 403 SimpleType.FLOAT, 404 true, 405 false, 406 false, 407 defaultvalue, 408 minvalue, 409 null); 410 assertTrue("Null info constructed", info != null); 411 assertTrue("Expecting min value of 0.75", 412 info.hasMinValue() && minvalue.equals((Float )info.getMinValue())); 413 assertFalse("Not expecting a max value", 414 info.hasMaxValue()); 415 416 try 417 { 418 info = 419 new OpenMBeanAttributeInfoSupport ("price", 420 "how much it costs", 421 SimpleType.FLOAT, 422 true, 423 false, 424 false, 425 "1.00", 426 minvalue, 427 maxvalue); 428 fail("Expecting exception for bad default value type"); 429 } 430 catch (OpenDataException x) 431 { 432 assertTrue(true); 433 } 434 435 try 436 { 437 info = 438 new OpenMBeanAttributeInfoSupport ("price", 439 "how much it costs", 440 SimpleType.FLOAT, 441 true, 442 false, 443 false, 444 defaultvalue, 445 "0.75", 446 maxvalue); 447 fail("Expecting exception for bad min value type"); 448 } 449 catch (OpenDataException x) 450 { 451 assertTrue(true); 452 } 453 454 try 455 { 456 info = 457 new OpenMBeanAttributeInfoSupport ("price", 458 "how much it costs", 459 SimpleType.FLOAT, 460 true, 461 false, 462 false, 463 defaultvalue, 464 minvalue, 465 "1.50"); 466 fail("Expecting exception for bad min value type"); 467 } 468 catch (OpenDataException x) 469 { 470 assertTrue(true); 471 } 472 473 try 474 { 475 info = 476 new OpenMBeanAttributeInfoSupport ("price", 477 "how much it costs", 478 new ArrayType (1, SimpleType.FLOAT), 479 true, 480 false, 481 false, 482 defaultvalue, 483 minvalue, 484 maxvalue); 485 fail("Expecting exception for non-null default value w/ArrayType attribute"); 486 } 487 catch (OpenDataException x) 488 { 489 assertTrue(true); 490 } 491 492 try 493 { 494 info = 495 new OpenMBeanAttributeInfoSupport ("price", 496 "how much it costs", 497 SimpleType.FLOAT, 498 true, 499 false, 500 false, 501 defaultvalue, 502 maxvalue, 503 minvalue); 504 fail("Expecting exception for min > max"); 505 } 506 catch (OpenDataException x) 507 { 508 assertTrue(true); 509 } 510 511 try 512 { 513 info = 514 new OpenMBeanAttributeInfoSupport ("price", 515 "how much it costs", 516 SimpleType.FLOAT, 517 true, 518 false, 519 false, 520 minvalue, 521 defaultvalue, 522 maxvalue); 523 fail("Expecting exception for default < min"); 524 } 525 catch (OpenDataException x) 526 { 527 assertTrue(true); 528 } 529 } 530 531 public void testSimpleInfoHashCode() 532 { 533 OpenMBeanAttributeInfoSupport info = 534 new OpenMBeanAttributeInfoSupport ("price", 535 "how much it costs", 536 SimpleType.FLOAT, 537 true, 538 false, 539 false); 540 assertTrue("Unexpected hash code for simple info", info.hashCode() == hashCode(info)); 541 } 542 543 public void testDefaultValueHashCode() throws Exception 544 { 545 OpenMBeanAttributeInfoSupport info = 546 new OpenMBeanAttributeInfoSupport ("price", 547 "how much it costs", 548 SimpleType.FLOAT, 549 true, 550 false, 551 false, 552 new Float (1.00)); 553 assertTrue("Unexpected hash code for info w/default value", info.hashCode() == hashCode(info)); 554 } 555 556 public void testNullDefaultValueHashCode() throws Exception 557 { 558 OpenMBeanAttributeInfoSupport info = 559 new OpenMBeanAttributeInfoSupport ("price", 560 "how much it costs", 561 SimpleType.FLOAT, 562 true, 563 false, 564 false, 565 null); 566 assertTrue("Unexpected hash code for info w/null default value", 567 info.hashCode() == hashCode(info)); 568 } 569 570 public void testLegalValueHashCode() throws Exception 571 { 572 OpenMBeanAttributeInfoSupport info = 573 new OpenMBeanAttributeInfoSupport ("price", 574 "how much it costs", 575 SimpleType.FLOAT, 576 true, 577 false, 578 false, 579 new Float (1.00), 580 new Float []{ 581 new Float (0.75), 582 new Float (1.00), 583 new Float (1.50)}); 584 assertTrue("Unexpected hash code for info w/legal values", 585 info.hashCode() == hashCode(info)); 586 } 587 588 public void testEmptyLegalValueHashCode() throws Exception 589 { 590 OpenMBeanAttributeInfoSupport info = 591 new OpenMBeanAttributeInfoSupport ("price", 592 "how much it costs", 593 SimpleType.FLOAT, 594 true, 595 false, 596 false, 597 new Float (1.00), 598 new Float [0]); 599 assertTrue("Unexpected hash code for info w/empty legal values", 600 info.hashCode() == hashCode(info)); 601 } 602 603 public void testMinMaxValueHashCode() throws Exception 604 { 605 OpenMBeanAttributeInfoSupport info = 606 new OpenMBeanAttributeInfoSupport ("price", 607 "how much it costs", 608 SimpleType.FLOAT, 609 true, 610 false, 611 false, 612 new Float (1.00), 613 new Float (0.75), 614 new Float (1.50)); 615 assertTrue("Unexpected hash code for info w/minmax values", 616 info.hashCode() == hashCode(info)); 617 } 618 619 public void testNullMinValueHashCode() throws Exception 620 { 621 OpenMBeanAttributeInfoSupport info = 622 new OpenMBeanAttributeInfoSupport ("price", 623 "how much it costs", 624 SimpleType.FLOAT, 625 true, 626 false, 627 false, 628 new Float (1.00), 629 null, 630 new Float (1.50)); 631 assertTrue("Unexpected hash code for info w/null min values", 632 info.hashCode() == hashCode(info)); 633 } 634 635 public void testNullMaxValueHashCode() throws Exception 636 { 637 OpenMBeanAttributeInfoSupport info = 638 new OpenMBeanAttributeInfoSupport ("price", 639 "how much it costs", 640 SimpleType.FLOAT, 641 true, 642 false, 643 false, 644 new Float (1.00), 645 new Float (0.75), 646 null); 647 assertTrue("Unexpected hash code for info w/empty legal values", 648 info.hashCode() == hashCode(info)); 649 } 650 651 private int hashCode(OpenMBeanAttributeInfoSupport info) 652 { 653 int result = info.getName().hashCode(); 654 result += info.getOpenType().hashCode(); 655 result += (info.hasDefaultValue() == false) ? 0 : info.getDefaultValue().hashCode(); 656 result += (info.hasLegalValues() == false) ? 0 : hashCode(info.getLegalValues()); 657 result += (info.hasMinValue() == false) ? 0 : info.getMinValue().hashCode(); 658 result += (info.hasMaxValue() == false) ? 0 : info.getMaxValue().hashCode(); 659 return result; 660 } 661 662 private int hashCode(Set legalvalues) 663 { 664 int result = 0; 665 Iterator i = legalvalues.iterator(); 666 while (i.hasNext()) 667 { 668 Object v = i.next(); 669 result += v.hashCode(); 670 } 671 return result; 672 } 673 } 674 | Popular Tags |