1 22 package org.jboss.test.jmx.compliance.relation; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.IOException ; 27 import java.io.ObjectInputStream ; 28 import java.io.ObjectOutputStream ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 32 import javax.management.MalformedObjectNameException ; 33 import javax.management.ObjectName ; 34 import javax.management.relation.RoleStatus ; 35 import javax.management.relation.RoleUnresolved ; 36 import javax.management.relation.RoleUnresolvedList ; 37 38 import junit.framework.TestCase; 39 40 50 public class RoleUnresolvedListTestCase 51 extends TestCase 52 { 53 55 boolean setUpDone = false; 57 RoleUnresolved roleUnresolved1; 58 RoleUnresolved roleUnresolved2; 59 60 62 65 public RoleUnresolvedListTestCase(String s) 66 { 67 super(s); 68 } 69 70 72 75 public void testEmptyConstructors() 76 { 77 setUpRoleUnresolveds(); 78 79 RoleUnresolvedList empty = new RoleUnresolvedList (); 81 assertEquals(0, empty.size()); 82 empty = new RoleUnresolvedList (100); 83 assertEquals(0, empty.size()); 84 } 85 86 89 public void testBasicConstructor() 90 { 91 setUpRoleUnresolveds(); 92 93 ArrayList roleUnresolveds = new ArrayList (); 94 roleUnresolveds.add(roleUnresolved1); 95 roleUnresolveds.add(roleUnresolved2); 96 RoleUnresolvedList full = new RoleUnresolvedList (roleUnresolveds); 97 assertEquals(2, full.size()); 98 assertEquals(roleUnresolved1, full.get(0)); 99 assertEquals(roleUnresolved2, full.get(1)); 100 Iterator iterator = full.iterator(); 101 assertEquals(roleUnresolved1, iterator.next()); 102 assertEquals(roleUnresolved2, iterator.next()); 103 } 104 105 108 public void testBasicConstructorOrdering() 109 { 110 setUpRoleUnresolveds(); 111 112 ArrayList roleUnresolveds = new ArrayList (); 113 roleUnresolveds.add(roleUnresolved2); 114 roleUnresolveds.add(roleUnresolved1); 115 RoleUnresolvedList full = new RoleUnresolvedList (roleUnresolveds); 116 assertEquals(2, full.size()); 117 assertEquals(roleUnresolved2, full.get(0)); 118 assertEquals(roleUnresolved1, full.get(1)); 119 Iterator iterator = full.iterator(); 120 assertEquals(roleUnresolved2, iterator.next()); 121 assertEquals(roleUnresolved1, iterator.next()); 122 } 123 124 127 public void testBasicConstructorDuplicates() 128 { 129 setUpRoleUnresolveds(); 130 131 ArrayList roleUnresolveds = new ArrayList (); 133 roleUnresolveds.add(roleUnresolved1); 134 roleUnresolveds.add(roleUnresolved1); 135 RoleUnresolvedList full = new RoleUnresolvedList (roleUnresolveds); 136 assertEquals(2, full.size()); 137 assertEquals(roleUnresolved1, full.get(0)); 138 assertEquals(roleUnresolved1, full.get(1)); 139 Iterator iterator = full.iterator(); 140 assertEquals(roleUnresolved1, iterator.next()); 141 assertEquals(roleUnresolved1, iterator.next()); 142 } 143 144 147 public void testErrorHandling() 148 { 149 setUpRoleUnresolveds(); 150 151 ArrayList roleUnresolveds = new ArrayList (); 153 roleUnresolveds.add(roleUnresolved1); 154 roleUnresolveds.add(null); 155 156 boolean caught = false; 158 try 159 { 160 new RoleUnresolvedList (roleUnresolveds); 161 } 162 catch (IllegalArgumentException e) 163 { 164 caught = true; 165 } 166 if (caught == false) 167 fail ("Constructor accepts null roleUnresolveds"); 168 169 roleUnresolveds = new ArrayList (); 171 roleUnresolveds.add(roleUnresolved1); 172 roleUnresolveds.add(new Object ()); 173 caught = false; 174 try 175 { 176 new RoleUnresolvedList (roleUnresolveds); 177 } 178 catch (IllegalArgumentException e) 179 { 180 caught = true; 181 } 182 if (caught == false) 183 fail ("Constructor accepts non roleUnresolveds"); 184 } 185 186 189 public void testSingleAppend() 190 { 191 setUpRoleUnresolveds(); 192 193 RoleUnresolvedList list = new RoleUnresolvedList (); 195 list.add(roleUnresolved1); 196 assertEquals(1, list.size()); 197 assertEquals(roleUnresolved1.toString(), list.get(0).toString()); 198 Iterator iterator = list.iterator(); 199 assertEquals(roleUnresolved1.toString(), iterator.next().toString()); 200 201 list.add(roleUnresolved2); 203 assertEquals(2, list.size()); 204 assertEquals(roleUnresolved1.toString(), list.get(0).toString()); 205 assertEquals(roleUnresolved2.toString(), list.get(1).toString()); 206 iterator = list.iterator(); 207 assertEquals(roleUnresolved1.toString(), iterator.next().toString()); 208 assertEquals(roleUnresolved2.toString(), iterator.next().toString()); 209 210 boolean caught = false; 212 try 213 { 214 list.add(null); 215 } 216 catch (IllegalArgumentException e) 217 { 218 caught = true; 219 } 220 if (caught == false) 221 fail ("RoleUnresolved add(null) shouldn't work"); 222 } 223 224 227 public void testSingleAdd() 228 { 229 setUpRoleUnresolveds(); 230 231 RoleUnresolvedList list = new RoleUnresolvedList (); 232 list.add(roleUnresolved1); 233 list.add(roleUnresolved2); 234 235 list.add(1, roleUnresolved1); 237 assertEquals(3, list.size()); 238 assertEquals(roleUnresolved1.toString(), list.get(0).toString()); 239 assertEquals(roleUnresolved1.toString(), list.get(1).toString()); 240 assertEquals(roleUnresolved2.toString(), list.get(2).toString()); 241 Iterator iterator = list.iterator(); 242 assertEquals(roleUnresolved1.toString(), iterator.next().toString()); 243 assertEquals(roleUnresolved1.toString(), iterator.next().toString()); 244 assertEquals(roleUnresolved2.toString(), iterator.next().toString()); 245 246 boolean caught = false; 248 try 249 { 250 list.add(4, roleUnresolved1); 251 } 252 catch (IndexOutOfBoundsException e) 253 { 254 caught = true; 255 } 256 if (caught == false) 257 fail ("Shouldn't be able to add a roleUnresolved outside of valid range"); 258 259 caught = false; 261 try 262 { 263 list.add(1, null); 264 } 265 catch (IllegalArgumentException e) 266 { 267 caught = true; 268 } 269 if (caught == false) 270 fail ("Shouldn't be able to add a null at an index"); 271 } 272 273 276 public void testSingleSet() 277 { 278 setUpRoleUnresolveds(); 279 280 RoleUnresolvedList list = new RoleUnresolvedList (); 281 list.add(roleUnresolved1); 282 list.add(roleUnresolved2); 283 284 list.set(1, roleUnresolved1); 286 assertEquals(2, list.size()); 287 assertEquals(roleUnresolved1.toString(), list.get(0).toString()); 288 assertEquals(roleUnresolved1.toString(), list.get(1).toString()); 289 Iterator iterator = list.iterator(); 290 assertEquals(roleUnresolved1.toString(), iterator.next().toString()); 291 assertEquals(roleUnresolved1.toString(), iterator.next().toString()); 292 293 boolean caught = false; 295 try 296 { 297 list.set(4, roleUnresolved1); 298 } 299 catch (IndexOutOfBoundsException e) 300 { 301 caught = true; 302 } 303 if (caught == false) 304 fail ("Shouldn't be able to set a roleUnresolved outside of valid range"); 305 306 caught = false; 308 try 309 { 310 list.add(1, null); 311 } 312 catch (IllegalArgumentException e) 313 { 314 caught = true; 315 } 316 if (caught == false) 317 fail ("Shouldn't be able to set a null at an index"); 318 } 319 320 323 public void testMultipleAdd() 324 { 325 setUpRoleUnresolveds(); 326 327 RoleUnresolvedList list = new RoleUnresolvedList (); 328 list.add(roleUnresolved1); 329 list.add(roleUnresolved1); 330 RoleUnresolvedList listToAdd = new RoleUnresolvedList (); 331 listToAdd.add(roleUnresolved2); 332 listToAdd.add(roleUnresolved2); 333 334 list.addAll(listToAdd); 336 assertEquals(4, list.size()); 337 assertEquals(roleUnresolved1.toString(), list.get(0).toString()); 338 assertEquals(roleUnresolved1.toString(), list.get(1).toString()); 339 assertEquals(roleUnresolved2.toString(), list.get(2).toString()); 340 assertEquals(roleUnresolved2.toString(), list.get(3).toString()); 341 Iterator iterator = list.iterator(); 342 assertEquals(roleUnresolved1.toString(), iterator.next().toString()); 343 assertEquals(roleUnresolved1.toString(), iterator.next().toString()); 344 assertEquals(roleUnresolved2.toString(), iterator.next().toString()); 345 assertEquals(roleUnresolved2.toString(), iterator.next().toString()); 346 347 boolean caught = false; 349 try 350 { 351 list.addAll(null); 352 } 353 catch (Exception e) 354 { 355 caught = true; 356 } 357 if (caught == true) 358 fail ("Should be able to addAll a null"); 359 } 360 361 364 public void testMultipleLocationAdd() 365 { 366 setUpRoleUnresolveds(); 367 368 RoleUnresolvedList list = new RoleUnresolvedList (); 369 list.add(roleUnresolved1); 370 list.add(roleUnresolved1); 371 RoleUnresolvedList listToAdd = new RoleUnresolvedList (); 372 listToAdd.add(roleUnresolved2); 373 listToAdd.add(roleUnresolved2); 374 375 list.addAll(1, listToAdd); 377 assertEquals(4, list.size()); 378 assertEquals(roleUnresolved1.toString(), list.get(0).toString()); 379 assertEquals(roleUnresolved2.toString(), list.get(1).toString()); 380 assertEquals(roleUnresolved2.toString(), list.get(2).toString()); 381 assertEquals(roleUnresolved1.toString(), list.get(3).toString()); 382 Iterator iterator = list.iterator(); 383 assertEquals(roleUnresolved1.toString(), iterator.next().toString()); 384 assertEquals(roleUnresolved2.toString(), iterator.next().toString()); 385 assertEquals(roleUnresolved2.toString(), iterator.next().toString()); 386 assertEquals(roleUnresolved1.toString(), iterator.next().toString()); 387 388 boolean caught = false; 390 try 391 { 392 list.addAll(6, listToAdd); 393 } 394 catch (IndexOutOfBoundsException e) 395 { 396 caught = true; 397 } 398 if (caught == false) 399 fail ("Shouldn't be able to addAll a roleUnresolved outside of valid range"); 400 401 caught = false; 403 try 404 { 405 list.addAll(1, null); 406 } 407 catch (IllegalArgumentException e) 408 { 409 caught = true; 410 } 411 if (caught == false) 412 fail ("Shouldn't be able to addAll a null at an index"); 413 } 414 415 418 public void testClone() 419 { 420 setUpRoleUnresolveds(); 421 422 try 423 { 424 ArrayList roleUnresolveds = new ArrayList (); 425 roleUnresolveds.add(roleUnresolved1); 426 roleUnresolveds.add(roleUnresolved2); 427 RoleUnresolvedList full = new RoleUnresolvedList (roleUnresolveds); 428 RoleUnresolvedList clone = (RoleUnresolvedList ) full.clone(); 429 assertEquals(2, clone.size()); 430 assertEquals(roleUnresolved1.toString(), clone.get(0).toString()); 431 assertEquals(roleUnresolved2.toString(), clone.get(1).toString()); 432 Iterator iterator = clone.iterator(); 433 assertEquals(roleUnresolved1.toString(), iterator.next().toString()); 434 assertEquals(roleUnresolved2.toString(), iterator.next().toString()); 435 } 436 catch (IllegalArgumentException e) 437 { 438 fail("FAILS IN RI: roleUnresolvedList -> RoleList?"); 439 } 440 } 441 442 445 public void testSerialization() 446 { 447 setUpRoleUnresolveds(); 448 449 ArrayList roleUnresolveds = new ArrayList (); 450 roleUnresolveds.add(roleUnresolved1); 451 roleUnresolveds.add(roleUnresolved2); 452 RoleUnresolvedList full = new RoleUnresolvedList (roleUnresolveds); 453 RoleUnresolvedList copy = null; 454 455 try 456 { 457 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 459 ObjectOutputStream oos = new ObjectOutputStream (baos); 460 oos.writeObject(full); 461 462 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 464 ObjectInputStream ois = new ObjectInputStream (bais); 465 copy = (RoleUnresolvedList ) ois.readObject(); 466 } 467 catch (IOException ioe) 468 { 469 fail(ioe.toString()); 470 } 471 catch (ClassNotFoundException cnfe) 472 { 473 fail(cnfe.toString()); 474 } 475 476 assertEquals(2, copy.size()); 478 assertEquals(roleUnresolved1.toString(), copy.get(0).toString()); 479 assertEquals(roleUnresolved2.toString(), copy.get(1).toString()); 480 Iterator iterator = copy.iterator(); 481 assertEquals(roleUnresolved1.toString(), iterator.next().toString()); 482 assertEquals(roleUnresolved2.toString(), iterator.next().toString()); 483 } 484 485 487 private void setUpRoleUnresolveds() 488 { 489 if (setUpDone == true) 490 return; 491 try 492 { 493 ArrayList roleValue1 = new ArrayList (); 495 ObjectName a = new ObjectName (":a=a"); 496 ObjectName b = new ObjectName (":b=b"); 497 roleValue1.add(a); 498 roleValue1.add(b); 499 roleUnresolved1 = new RoleUnresolved ("RoleName1", roleValue1, 500 RoleStatus.ROLE_NOT_READABLE); 501 502 ArrayList roleValue2 = new ArrayList (); 503 ObjectName c = new ObjectName (":c=c"); 504 ObjectName d = new ObjectName (":d=d"); 505 roleValue2.add(c); 506 roleValue2.add(d); 507 roleUnresolved2 = new RoleUnresolved ("RoleName2", roleValue2, 508 RoleStatus.ROLE_NOT_READABLE); 509 } 510 catch (MalformedObjectNameException mfone) 511 { 512 fail(mfone.toString()); 513 } 514 setUpDone = true; 515 } 516 } 517 | Popular Tags |