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.Role; 22 import javax.management.relation.RoleList; 23 24 import junit.framework.TestCase; 25 26 36 public class RoleListTestCase 37 extends TestCase 38 { 39 41 boolean setUpDone = false; 43 Role role1; 44 Role role2; 45 46 48 51 public RoleListTestCase(String s) 52 { 53 super(s); 54 } 55 56 58 61 public void testEmptyConstructors() 62 { 63 setUpRoles(); 64 65 RoleList empty = new RoleList(); 67 assertEquals(0, empty.size()); 68 empty = new RoleList(100); 69 assertEquals(0, empty.size()); 70 } 71 72 75 public void testBasicConstructor() 76 { 77 setUpRoles(); 78 79 ArrayList roles = new ArrayList(); 80 roles.add(role1); 81 roles.add(role2); 82 RoleList full = new RoleList(roles); 83 assertEquals(2, full.size()); 84 assertEquals(role1, full.get(0)); 85 assertEquals(role2, full.get(1)); 86 Iterator iterator = full.iterator(); 87 assertEquals(role1, iterator.next()); 88 assertEquals(role2, iterator.next()); 89 } 90 91 94 public void testBasicConstructorOrdering() 95 { 96 setUpRoles(); 97 98 ArrayList roles = new ArrayList(); 99 roles.add(role2); 100 roles.add(role1); 101 RoleList full = new RoleList(roles); 102 assertEquals(2, full.size()); 103 assertEquals(role2, full.get(0)); 104 assertEquals(role1, full.get(1)); 105 Iterator iterator = full.iterator(); 106 assertEquals(role2, iterator.next()); 107 assertEquals(role1, iterator.next()); 108 } 109 110 113 public void testBasicConstructorDuplicates() 114 { 115 setUpRoles(); 116 117 ArrayList roles = new ArrayList(); 119 roles.add(role1); 120 roles.add(role1); 121 RoleList full = new RoleList(roles); 122 assertEquals(2, full.size()); 123 assertEquals(role1, full.get(0)); 124 assertEquals(role1, full.get(1)); 125 Iterator iterator = full.iterator(); 126 assertEquals(role1, iterator.next()); 127 assertEquals(role1, iterator.next()); 128 } 129 130 133 public void testErrorHandling() 134 { 135 setUpRoles(); 136 137 ArrayList roles = new ArrayList(); 139 roles.add(role1); 140 roles.add(null); 141 142 boolean caught = false; 144 try 145 { 146 new RoleList(roles); 147 } 148 catch (IllegalArgumentException e) 149 { 150 caught = true; 151 } 152 if (caught == false) 153 fail ("Constructor accepts null roles"); 154 155 roles = new ArrayList(); 157 roles.add(role1); 158 roles.add(new Object()); 159 caught = false; 160 try 161 { 162 new RoleList(roles); 163 } 164 catch (IllegalArgumentException e) 165 { 166 caught = true; 167 } 168 if (caught == false) 169 fail ("Constructor accepts non roles"); 170 } 171 172 175 public void testSingleAppend() 176 { 177 setUpRoles(); 178 179 RoleList list = new RoleList(); 181 list.add(role1); 182 assertEquals(1, list.size()); 183 assertEquals(role1.toString(), list.get(0).toString()); 184 Iterator iterator = list.iterator(); 185 assertEquals(role1.toString(), iterator.next().toString()); 186 187 list.add(role2); 189 assertEquals(2, list.size()); 190 assertEquals(role1.toString(), list.get(0).toString()); 191 assertEquals(role2.toString(), list.get(1).toString()); 192 iterator = list.iterator(); 193 assertEquals(role1.toString(), iterator.next().toString()); 194 assertEquals(role2.toString(), iterator.next().toString()); 195 196 boolean caught = false; 198 try 199 { 200 list.add(null); 201 } 202 catch (IllegalArgumentException e) 203 { 204 caught = true; 205 } 206 if (caught == false) 207 fail ("addRole(null) shouldn't work"); 208 } 209 210 213 public void testSingleAdd() 214 { 215 setUpRoles(); 216 217 RoleList list = new RoleList(); 219 list.add(role1); 220 list.add(role2); 221 222 list.add(1, role1); 224 assertEquals(3, list.size()); 225 assertEquals(role1.toString(), list.get(0).toString()); 226 assertEquals(role1.toString(), list.get(1).toString()); 227 assertEquals(role2.toString(), list.get(2).toString()); 228 Iterator iterator = list.iterator(); 229 assertEquals(role1.toString(), iterator.next().toString()); 230 assertEquals(role1.toString(), iterator.next().toString()); 231 assertEquals(role2.toString(), iterator.next().toString()); 232 233 boolean caught = false; 235 try 236 { 237 list.add(4, role1); 238 } 239 catch (IndexOutOfBoundsException e) 240 { 241 caught = true; 242 } 243 if (caught == false) 244 fail ("Shouldn't be able to add a role 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 setUpRoles(); 266 267 RoleList list = new RoleList(); 269 list.add(role1); 270 list.add(role2); 271 272 list.set(1, role1); 274 assertEquals(2, list.size()); 275 assertEquals(role1.toString(), list.get(0).toString()); 276 assertEquals(role1.toString(), list.get(1).toString()); 277 Iterator iterator = list.iterator(); 278 assertEquals(role1.toString(), iterator.next().toString()); 279 assertEquals(role1.toString(), iterator.next().toString()); 280 281 boolean caught = false; 283 try 284 { 285 list.set(4, role1); 286 } 287 catch (IndexOutOfBoundsException e) 288 { 289 caught = true; 290 } 291 if (caught == false) 292 fail ("Shouldn't be able to set a role outside of valid range"); 293 294 caught = false; 296 try 297 { 298 list.add(1, null); 299 } 300 catch (IllegalArgumentException e) 301 { 302 caught = true; 303 } 304 if (caught == false) 305 fail ("Shouldn't be able to set a null at an index"); 306 } 307 308 311 public void testMultipleAdd() 312 { 313 setUpRoles(); 314 315 RoleList list = new RoleList(); 317 list.add(role1); 318 list.add(role1); 319 RoleList listToAdd = new RoleList(); 320 listToAdd.add(role2); 321 listToAdd.add(role2); 322 323 list.addAll(listToAdd); 325 assertEquals(4, list.size()); 326 assertEquals(role1.toString(), list.get(0).toString()); 327 assertEquals(role1.toString(), list.get(1).toString()); 328 assertEquals(role2.toString(), list.get(2).toString()); 329 assertEquals(role2.toString(), list.get(3).toString()); 330 Iterator iterator = list.iterator(); 331 assertEquals(role1.toString(), iterator.next().toString()); 332 assertEquals(role1.toString(), iterator.next().toString()); 333 assertEquals(role2.toString(), iterator.next().toString()); 334 assertEquals(role2.toString(), iterator.next().toString()); 335 336 boolean caught = false; 338 try 339 { 340 list.addAll(null); 341 } 342 catch (Exception e) 343 { 344 caught = true; 345 } 346 if (caught == true) 347 fail ("Should be able to addAll a null"); 348 } 349 350 353 public void testMultipleLocationAdd() 354 { 355 setUpRoles(); 356 357 RoleList list = new RoleList(); 359 list.add(role1); 360 list.add(role1); 361 RoleList listToAdd = new RoleList(); 362 listToAdd.add(role2); 363 listToAdd.add(role2); 364 365 list.addAll(1, listToAdd); 367 assertEquals(4, list.size()); 368 assertEquals(role1.toString(), list.get(0).toString()); 369 assertEquals(role2.toString(), list.get(1).toString()); 370 assertEquals(role2.toString(), list.get(2).toString()); 371 assertEquals(role1.toString(), list.get(3).toString()); 372 Iterator iterator = list.iterator(); 373 assertEquals(role1.toString(), iterator.next().toString()); 374 assertEquals(role2.toString(), iterator.next().toString()); 375 assertEquals(role2.toString(), iterator.next().toString()); 376 assertEquals(role1.toString(), iterator.next().toString()); 377 378 boolean caught = false; 380 try 381 { 382 list.addAll(6, listToAdd); 383 } 384 catch (IndexOutOfBoundsException e) 385 { 386 caught = true; 387 } 388 if (caught == false) 389 fail ("Shouldn't be able to addAll a role outside of valid range"); 390 391 caught = false; 393 try 394 { 395 list.addAll(1, null); 396 } 397 catch (IllegalArgumentException e) 398 { 399 caught = true; 400 } 401 if (caught == false) 402 fail ("Shouldn't be able to addAll a null at an index"); 403 } 404 405 408 public void testClone() 409 { 410 setUpRoles(); 411 412 ArrayList roles = new ArrayList(); 413 roles.add(role1); 414 roles.add(role2); 415 RoleList full = new RoleList(roles); 416 RoleList clone = (RoleList) full.clone(); 417 assertEquals(2, clone.size()); 418 assertEquals(role1.toString(), clone.get(0).toString()); 419 assertEquals(role2.toString(), clone.get(1).toString()); 420 Iterator iterator = clone.iterator(); 421 assertEquals(role1.toString(), iterator.next().toString()); 422 assertEquals(role2.toString(), iterator.next().toString()); 423 } 424 425 428 public void testSerialization() 429 { 430 setUpRoles(); 431 432 ArrayList roles = new ArrayList(); 433 roles.add(role1); 434 roles.add(role2); 435 RoleList full = new RoleList(roles); 436 RoleList copy = null; 437 438 try 439 { 440 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 442 ObjectOutputStream oos = new ObjectOutputStream(baos); 443 oos.writeObject(full); 444 445 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 447 ObjectInputStream ois = new ObjectInputStream(bais); 448 copy = (RoleList) ois.readObject(); 449 } 450 catch (IOException ioe) 451 { 452 fail(ioe.toString()); 453 } 454 catch (ClassNotFoundException cnfe) 455 { 456 fail(cnfe.toString()); 457 } 458 459 assertEquals(2, copy.size()); 461 assertEquals(role1.toString(), copy.get(0).toString()); 462 assertEquals(role2.toString(), copy.get(1).toString()); 463 Iterator iterator = copy.iterator(); 464 assertEquals(role1.toString(), iterator.next().toString()); 465 assertEquals(role2.toString(), iterator.next().toString()); 466 } 467 468 470 private void setUpRoles() 471 { 472 if (setUpDone == true) 473 return; 474 try 475 { 476 ArrayList roleValue1 = new ArrayList(); 478 ObjectName a = new ObjectName(":a=a"); 479 ObjectName b = new ObjectName(":b=b"); 480 roleValue1.add(a); 481 roleValue1.add(b); 482 role1 = new Role("RoleName1", roleValue1); 483 484 ArrayList roleValue2 = new ArrayList(); 485 ObjectName c = new ObjectName(":c=c"); 486 ObjectName d = new ObjectName(":d=d"); 487 roleValue2.add(c); 488 roleValue2.add(d); 489 role2 = new Role("RoleName2", roleValue2); 490 } 491 catch (MalformedObjectNameException mfone) 492 { 493 fail(mfone.toString()); 494 } 495 setUpDone = true; 496 } 497 } 498 | Popular Tags |