1 22 package org.jboss.test.jmx.compliance.relation; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ObjectInputStream ; 27 import java.io.ObjectOutputStream ; 28 29 import javax.management.NotCompliantMBeanException ; 30 import javax.management.relation.InvalidRoleInfoException ; 31 import javax.management.relation.RelationSupport ; 32 import javax.management.relation.RoleInfo ; 33 34 import junit.framework.TestCase; 35 36 46 public class RoleInfoTestCase 47 extends TestCase 48 { 49 51 53 56 public RoleInfoTestCase(String s) 57 { 58 super(s); 59 } 60 61 63 66 public void testBasic() 67 { 68 RoleInfo roleInfo = null; 69 70 try 72 { 73 roleInfo = new RoleInfo ("RoleName", RelationSupport .class.getName()); 74 } 75 catch (Exception e) 76 { 77 fail(e.toString()); 78 } 79 80 assertEquals(roleInfo.getName(), "RoleName"); 82 assertEquals(roleInfo.getRefMBeanClassName(), RelationSupport .class.getName()); 83 assertEquals(roleInfo.isReadable(), true); 84 assertEquals(roleInfo.isWritable(), true); 85 assertEquals(roleInfo.getMinDegree(), 1); 86 assertEquals(roleInfo.getMaxDegree(), 1); 87 assertEquals(roleInfo.getDescription(), null); 88 89 try 91 { 92 roleInfo = new RoleInfo ("RoleName", RelationSupport .class.getName(), 93 false, false); 94 } 95 catch (Exception e) 96 { 97 fail(e.toString()); 98 } 99 100 assertEquals(roleInfo.getName(), "RoleName"); 102 assertEquals(roleInfo.getRefMBeanClassName(), RelationSupport .class.getName()); 103 assertEquals(roleInfo.isReadable(), false); 104 assertEquals(roleInfo.isWritable(), false); 105 assertEquals(roleInfo.getMinDegree(), 1); 106 assertEquals(roleInfo.getMaxDegree(), 1); 107 assertEquals(roleInfo.getDescription(), null); 108 109 try 111 { 112 roleInfo = new RoleInfo ("RoleName", RelationSupport .class.getName(), 113 false, false, 23, 25, "Description"); 114 } 115 catch (Exception e) 116 { 117 fail(e.toString()); 118 } 119 120 assertEquals(roleInfo.getName(), "RoleName"); 122 assertEquals(roleInfo.getRefMBeanClassName(), RelationSupport .class.getName()); 123 assertEquals(roleInfo.isReadable(), false); 124 assertEquals(roleInfo.isWritable(), false); 125 assertEquals(roleInfo.getMinDegree(), 23); 126 assertEquals(roleInfo.getMaxDegree(), 25); 127 assertEquals(roleInfo.getDescription(), "Description"); 128 } 129 130 133 public void testErrorHandling() 134 { 135 boolean caught = false; 136 try 137 { 138 new RoleInfo (null); 139 } 140 catch (IllegalArgumentException e) 141 { 142 caught = true; 143 } 144 catch (Exception e) 145 { 146 fail(e.toString()); 147 } 148 if (caught == false) 149 fail("Copy Constructor accepts null role info"); 150 151 caught = false; 152 try 153 { 154 new RoleInfo (null, RelationSupport .class.getName()); 155 } 156 catch (IllegalArgumentException e) 157 { 158 caught = true; 159 } 160 catch (Exception e) 161 { 162 fail(e.toString()); 163 } 164 if (caught == false) 165 fail("Constructor accepts null role name (1)"); 166 167 caught = false; 168 try 169 { 170 new RoleInfo (null, RelationSupport .class.getName(), true, true); 171 } 172 catch (IllegalArgumentException e) 173 { 174 caught = true; 175 } 176 catch (Exception e) 177 { 178 fail(e.toString()); 179 } 180 if (caught == false) 181 fail("Constructor accepts null role name (2)"); 182 183 caught = false; 184 try 185 { 186 new RoleInfo (null, RelationSupport .class.getName(), true, true, 187 1, 1, "blah"); 188 } 189 catch (IllegalArgumentException e) 190 { 191 caught = true; 192 } 193 catch (Exception e) 194 { 195 fail(e.toString()); 196 } 197 if (caught == false) 198 fail("Constructor accepts null role name (3)"); 199 200 caught = false; 201 try 202 { 203 new RoleInfo ("RoleName", null); 204 } 205 catch (IllegalArgumentException e) 206 { 207 caught = true; 208 } 209 catch (Exception e) 210 { 211 fail(e.toString()); 212 } 213 if (caught == false) 214 fail("Constructor accepts null class name (1)"); 215 216 caught = false; 217 try 218 { 219 new RoleInfo ("RoleName", null, true, true); 220 } 221 catch (IllegalArgumentException e) 222 { 223 caught = true; 224 } 225 catch (Exception e) 226 { 227 fail(e.toString()); 228 } 229 if (caught == false) 230 fail("Constructor accepts null class name (2)"); 231 232 caught = false; 233 try 234 { 235 new RoleInfo ("RoleName", null, true, true, 236 1, 1, "blah"); 237 } 238 catch (IllegalArgumentException e) 239 { 240 caught = true; 241 } 242 catch (Exception e) 243 { 244 fail(e.toString()); 245 } 246 if (caught == false) 247 fail("Constructor accepts null class name (3)"); 248 249 caught = false; 250 try 251 { 252 new RoleInfo ("RoleName", "Inv alid"); 253 } 254 catch (ClassNotFoundException e) 255 { 256 caught = true; 257 } 258 catch (Exception e) 259 { 260 fail(e.toString()); 261 } 262 if (caught) 263 fail("Constructor accepts invalid class name (1) - disabled JMX1.2"); 264 265 caught = false; 266 try 267 { 268 new RoleInfo ("RoleName", "Inv alid", true, true); 269 } 270 catch (ClassNotFoundException e) 271 { 272 caught = true; 273 } 274 catch (Exception e) 275 { 276 fail(e.toString()); 277 } 278 if (caught) 279 fail("Constructor accepts invalid class name (2) - disabled JMX1.2"); 280 281 caught = false; 282 try 283 { 284 new RoleInfo ("RoleName", "Inv alid", true, true, 285 1, 1, "blah"); 286 } 287 catch (ClassNotFoundException e) 288 { 289 caught = true; 290 } 291 catch (Exception e) 292 { 293 fail(e.toString()); 294 } 295 if (caught) 296 fail("Constructor accepts invalid class name (3) - disabled JMX1.2"); 297 298 caught = false; 299 try 300 { 301 new RoleInfo ("RoleName", RoleInfo .class.getName()); 302 } 303 catch (NotCompliantMBeanException e) 304 { 305 caught = true; 306 } 307 catch (Exception e) 308 { 309 fail(e.toString()); 310 } 311 if (caught) 312 fail("Constructor accepts not compliant mbean (1) - disabled JMX1.2"); 313 314 caught = false; 315 try 316 { 317 new RoleInfo ("RoleName", RoleInfo .class.getName(), true, true); 318 } 319 catch (NotCompliantMBeanException e) 320 { 321 caught = true; 322 } 323 catch (Exception e) 324 { 325 fail(e.toString()); 326 } 327 if (caught) 328 fail("Constructor accepts not compliant mbean (2) - disabled JMX1.2"); 329 330 caught = false; 331 try 332 { 333 new RoleInfo ("RoleName", RoleInfo .class.getName(), true, true, 334 1, 1, "blah"); 335 } 336 catch (NotCompliantMBeanException e) 337 { 338 caught = true; 339 } 340 catch (Exception e) 341 { 342 fail(e.toString()); 343 } 344 if (caught) 345 fail("Constructor accepts not compliant mbean (3) - disabled JMX1.2"); 346 } 347 348 351 public void testConstructorCardinality() 352 { 353 try 355 { 356 new RoleInfo ("RoleName", RelationSupport .class.getName(), 357 false, false, 0, 0, "Description"); 358 } 359 catch (Exception e) 360 { 361 fail(e.toString()); 362 } 363 364 boolean caught = false; 365 try 366 { 367 new RoleInfo ("RoleName", RelationSupport .class.getName(), 368 false, false, 1, 0, "Description"); 369 } 370 catch (InvalidRoleInfoException e) 371 { 372 caught = true; 373 } 374 catch (Exception e) 375 { 376 fail(e.toString()); 377 } 378 if (caught == false) 379 fail("Shouldn't allow minimum of 1 and maximum of 0"); 380 381 caught = false; 382 try 383 { 384 new RoleInfo ("RoleName", RelationSupport .class.getName(), 385 false, false, RoleInfo.ROLE_CARDINALITY_INFINITY, 386 0, "Description"); 387 } 388 catch (InvalidRoleInfoException e) 389 { 390 caught = true; 391 } 392 catch (Exception e) 393 { 394 fail(e.toString()); 395 } 396 if (caught == false) 397 fail("Shouldn't allow infinite minimum without infinite maximum"); 398 } 399 400 403 public void testCheckDegrees() 404 { 405 RoleInfo roleInfo = null; 407 408 try 409 { 410 roleInfo = new RoleInfo ("RoleName", RelationSupport .class.getName(), 411 false, false, 23, 25, "Description"); 412 } 413 catch (Exception e) 414 { 415 fail(e.toString()); 416 } 417 assertEquals(true, roleInfo.checkMaxDegree(0)); 418 assertEquals(true, roleInfo.checkMaxDegree(22)); 419 assertEquals(true, roleInfo.checkMaxDegree(23)); 420 assertEquals(true, roleInfo.checkMaxDegree(24)); 421 assertEquals(true, roleInfo.checkMaxDegree(25)); 422 assertEquals(false, roleInfo.checkMaxDegree(26)); 423 assertEquals(false, roleInfo.checkMaxDegree(Integer.MAX_VALUE)); 424 425 assertEquals(false, roleInfo.checkMinDegree(0)); 426 assertEquals(false, roleInfo.checkMinDegree(22)); 427 assertEquals(true, roleInfo.checkMinDegree(23)); 428 assertEquals(true, roleInfo.checkMinDegree(24)); 429 assertEquals(true, roleInfo.checkMinDegree(25)); 430 assertEquals(true, roleInfo.checkMinDegree(26)); 431 assertEquals(true, roleInfo.checkMinDegree(Integer.MAX_VALUE)); 432 433 try 434 { 435 roleInfo = new RoleInfo ("RoleName", RelationSupport .class.getName(), 436 false, false, 25, 437 RoleInfo.ROLE_CARDINALITY_INFINITY, "Description"); 438 } 439 catch (Exception e) 440 { 441 fail(e.toString()); 442 } 443 assertEquals(true, roleInfo.checkMaxDegree(0)); 444 assertEquals(true, roleInfo.checkMaxDegree(24)); 445 assertEquals(true, roleInfo.checkMaxDegree(25)); 446 assertEquals(true, roleInfo.checkMaxDegree(26)); 447 assertEquals(true, roleInfo.checkMaxDegree(Integer.MAX_VALUE)); 448 449 assertEquals(false, roleInfo.checkMinDegree(0)); 450 assertEquals(false, roleInfo.checkMinDegree(24)); 451 assertEquals(true, roleInfo.checkMinDegree(25)); 452 assertEquals(true, roleInfo.checkMinDegree(26)); 453 assertEquals(true, roleInfo.checkMinDegree(Integer.MAX_VALUE)); 454 455 try 456 { 457 roleInfo = new RoleInfo ("RoleName", RelationSupport .class.getName(), 458 false, false, RoleInfo.ROLE_CARDINALITY_INFINITY, 459 RoleInfo.ROLE_CARDINALITY_INFINITY, "Description"); 460 } 461 catch (Exception e) 462 { 463 fail(e.toString()); 464 } 465 assertEquals(true, roleInfo.checkMaxDegree(0)); 466 assertEquals(true, roleInfo.checkMaxDegree(26)); 467 assertEquals(true, roleInfo.checkMaxDegree(Integer.MAX_VALUE)); 468 469 assertEquals(true, roleInfo.checkMinDegree(0)); 470 assertEquals(true, roleInfo.checkMinDegree(24)); 471 assertEquals(true, roleInfo.checkMinDegree(Integer.MAX_VALUE)); 472 } 473 474 477 public void testCopy() 478 { 479 RoleInfo roleInfo = null; 481 RoleInfo roleInfo2 = null; 482 483 try 484 { 485 roleInfo = new RoleInfo ("RoleName", RelationSupport .class.getName(), 486 false, false, 23, 25, "Description"); 487 roleInfo2 = new RoleInfo (roleInfo); 488 } 489 catch (Exception e) 490 { 491 fail(e.toString()); 492 } 493 494 assertEquals(roleInfo.getName(), roleInfo2.getName()); 496 assertEquals(roleInfo.getRefMBeanClassName(), roleInfo2.getRefMBeanClassName()); 497 assertEquals(roleInfo.isReadable(), roleInfo2.isReadable()); 498 assertEquals(roleInfo.isWritable(), roleInfo2.isWritable()); 499 assertEquals(roleInfo.getMinDegree(), roleInfo2.getMinDegree()); 500 assertEquals(roleInfo.getMaxDegree(), roleInfo2.getMaxDegree()); 501 assertEquals(roleInfo.getDescription(), roleInfo2.getDescription()); 502 } 503 504 507 public void testSerialization() 508 { 509 RoleInfo roleInfo = null; 511 RoleInfo roleInfo2 = null; 512 513 try 514 { 515 roleInfo = new RoleInfo ("RoleName", RelationSupport .class.getName(), 516 false, false, 23, 25, "Description"); 517 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 519 ObjectOutputStream oos = new ObjectOutputStream (baos); 520 oos.writeObject(roleInfo); 521 522 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 524 ObjectInputStream ois = new ObjectInputStream (bais); 525 roleInfo2 = (RoleInfo ) ois.readObject(); 526 } 527 catch (Exception e) 528 { 529 fail(e.toString()); 530 } 531 532 assertEquals(roleInfo.getName(), roleInfo2.getName()); 534 assertEquals(roleInfo.getRefMBeanClassName(), roleInfo2.getRefMBeanClassName()); 535 assertEquals(roleInfo.isReadable(), roleInfo2.isReadable()); 536 assertEquals(roleInfo.isWritable(), roleInfo2.isWritable()); 537 assertEquals(roleInfo.getMinDegree(), roleInfo2.getMinDegree()); 538 assertEquals(roleInfo.getMaxDegree(), roleInfo2.getMaxDegree()); 539 assertEquals(roleInfo.getDescription(), roleInfo2.getDescription()); 540 } 541 } 542 | Popular Tags |