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