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.Role ; 35 import javax.management.relation.RoleList ; 36 37 import junit.framework.TestCase; 38 39 49 public class RoleListTestCase 50 extends TestCase 51 { 52 54 boolean setUpDone = false; 56 Role role1; 57 Role role2; 58 59 61 64 public RoleListTestCase(String s) 65 { 66 super(s); 67 } 68 69 71 74 public void testEmptyConstructors() 75 { 76 setUpRoles(); 77 78 RoleList empty = new RoleList (); 80 assertEquals(0, empty.size()); 81 empty = new RoleList (100); 82 assertEquals(0, empty.size()); 83 } 84 85 88 public void testBasicConstructor() 89 { 90 setUpRoles(); 91 92 ArrayList roles = new ArrayList (); 93 roles.add(role1); 94 roles.add(role2); 95 RoleList full = new RoleList (roles); 96 assertEquals(2, full.size()); 97 assertEquals(role1, full.get(0)); 98 assertEquals(role2, full.get(1)); 99 Iterator iterator = full.iterator(); 100 assertEquals(role1, iterator.next()); 101 assertEquals(role2, iterator.next()); 102 } 103 104 107 public void testBasicConstructorOrdering() 108 { 109 setUpRoles(); 110 111 ArrayList roles = new ArrayList (); 112 roles.add(role2); 113 roles.add(role1); 114 RoleList full = new RoleList (roles); 115 assertEquals(2, full.size()); 116 assertEquals(role2, full.get(0)); 117 assertEquals(role1, full.get(1)); 118 Iterator iterator = full.iterator(); 119 assertEquals(role2, iterator.next()); 120 assertEquals(role1, iterator.next()); 121 } 122 123 126 public void testBasicConstructorDuplicates() 127 { 128 setUpRoles(); 129 130 ArrayList roles = new ArrayList (); 132 roles.add(role1); 133 roles.add(role1); 134 RoleList full = new RoleList (roles); 135 assertEquals(2, full.size()); 136 assertEquals(role1, full.get(0)); 137 assertEquals(role1, full.get(1)); 138 Iterator iterator = full.iterator(); 139 assertEquals(role1, iterator.next()); 140 assertEquals(role1, iterator.next()); 141 } 142 143 146 public void testErrorHandling() 147 { 148 setUpRoles(); 149 150 ArrayList roles = new ArrayList (); 152 roles.add(role1); 153 roles.add(null); 154 155 boolean caught = false; 157 try 158 { 159 new RoleList (roles); 160 } 161 catch (IllegalArgumentException e) 162 { 163 caught = true; 164 } 165 if (caught == false) 166 fail ("Constructor accepts null roles"); 167 168 roles = new ArrayList (); 170 roles.add(role1); 171 roles.add(new Object ()); 172 caught = false; 173 try 174 { 175 new RoleList (roles); 176 } 177 catch (IllegalArgumentException e) 178 { 179 caught = true; 180 } 181 if (caught == false) 182 fail ("Constructor accepts non roles"); 183 } 184 185 188 public void testSingleAppend() 189 { 190 setUpRoles(); 191 192 RoleList list = new RoleList (); 194 list.add(role1); 195 assertEquals(1, list.size()); 196 assertEquals(role1.toString(), list.get(0).toString()); 197 Iterator iterator = list.iterator(); 198 assertEquals(role1.toString(), iterator.next().toString()); 199 200 list.add(role2); 202 assertEquals(2, list.size()); 203 assertEquals(role1.toString(), list.get(0).toString()); 204 assertEquals(role2.toString(), list.get(1).toString()); 205 iterator = list.iterator(); 206 assertEquals(role1.toString(), iterator.next().toString()); 207 assertEquals(role2.toString(), iterator.next().toString()); 208 209 boolean caught = false; 211 try 212 { 213 list.add(null); 214 } 215 catch (IllegalArgumentException e) 216 { 217 caught = true; 218 } 219 if (caught == false) 220 fail ("addRole(null) shouldn't work"); 221 } 222 223 226 public void testSingleAdd() 227 { 228 setUpRoles(); 229 230 RoleList list = new RoleList (); 232 list.add(role1); 233 list.add(role2); 234 235 list.add(1, role1); 237 assertEquals(3, list.size()); 238 assertEquals(role1.toString(), list.get(0).toString()); 239 assertEquals(role1.toString(), list.get(1).toString()); 240 assertEquals(role2.toString(), list.get(2).toString()); 241 Iterator iterator = list.iterator(); 242 assertEquals(role1.toString(), iterator.next().toString()); 243 assertEquals(role1.toString(), iterator.next().toString()); 244 assertEquals(role2.toString(), iterator.next().toString()); 245 246 boolean caught = false; 248 try 249 { 250 list.add(4, role1); 251 } 252 catch (IndexOutOfBoundsException e) 253 { 254 caught = true; 255 } 256 if (caught == false) 257 fail ("Shouldn't be able to add a role 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 setUpRoles(); 279 280 RoleList list = new RoleList (); 282 list.add(role1); 283 list.add(role2); 284 285 list.set(1, role1); 287 assertEquals(2, list.size()); 288 assertEquals(role1.toString(), list.get(0).toString()); 289 assertEquals(role1.toString(), list.get(1).toString()); 290 Iterator iterator = list.iterator(); 291 assertEquals(role1.toString(), iterator.next().toString()); 292 assertEquals(role1.toString(), iterator.next().toString()); 293 294 boolean caught = false; 296 try 297 { 298 list.set(4, role1); 299 } 300 catch (IndexOutOfBoundsException e) 301 { 302 caught = true; 303 } 304 if (caught == false) 305 fail ("Shouldn't be able to set a role outside of valid range"); 306 307 caught = false; 309 try 310 { 311 list.add(1, null); 312 } 313 catch (IllegalArgumentException e) 314 { 315 caught = true; 316 } 317 if (caught == false) 318 fail ("Shouldn't be able to set a null at an index"); 319 } 320 321 324 public void testMultipleAdd() 325 { 326 setUpRoles(); 327 328 RoleList list = new RoleList (); 330 list.add(role1); 331 list.add(role1); 332 RoleList listToAdd = new RoleList (); 333 listToAdd.add(role2); 334 listToAdd.add(role2); 335 336 list.addAll(listToAdd); 338 assertEquals(4, list.size()); 339 assertEquals(role1.toString(), list.get(0).toString()); 340 assertEquals(role1.toString(), list.get(1).toString()); 341 assertEquals(role2.toString(), list.get(2).toString()); 342 assertEquals(role2.toString(), list.get(3).toString()); 343 Iterator iterator = list.iterator(); 344 assertEquals(role1.toString(), iterator.next().toString()); 345 assertEquals(role1.toString(), iterator.next().toString()); 346 assertEquals(role2.toString(), iterator.next().toString()); 347 assertEquals(role2.toString(), iterator.next().toString()); 348 349 boolean caught = false; 351 try 352 { 353 list.addAll(null); 354 } 355 catch (Exception e) 356 { 357 caught = true; 358 } 359 if (caught == true) 360 fail ("Should be able to addAll a null"); 361 } 362 363 366 public void testMultipleLocationAdd() 367 { 368 setUpRoles(); 369 370 RoleList list = new RoleList (); 372 list.add(role1); 373 list.add(role1); 374 RoleList listToAdd = new RoleList (); 375 listToAdd.add(role2); 376 listToAdd.add(role2); 377 378 list.addAll(1, listToAdd); 380 assertEquals(4, list.size()); 381 assertEquals(role1.toString(), list.get(0).toString()); 382 assertEquals(role2.toString(), list.get(1).toString()); 383 assertEquals(role2.toString(), list.get(2).toString()); 384 assertEquals(role1.toString(), list.get(3).toString()); 385 Iterator iterator = list.iterator(); 386 assertEquals(role1.toString(), iterator.next().toString()); 387 assertEquals(role2.toString(), iterator.next().toString()); 388 assertEquals(role2.toString(), iterator.next().toString()); 389 assertEquals(role1.toString(), iterator.next().toString()); 390 391 boolean caught = false; 393 try 394 { 395 list.addAll(6, listToAdd); 396 } 397 catch (IndexOutOfBoundsException e) 398 { 399 caught = true; 400 } 401 if (caught == false) 402 fail ("Shouldn't be able to addAll a role outside of valid range"); 403 404 caught = false; 406 try 407 { 408 list.addAll(1, null); 409 } 410 catch (IllegalArgumentException e) 411 { 412 caught = true; 413 } 414 if (caught == false) 415 fail ("Shouldn't be able to addAll a null at an index"); 416 } 417 418 421 public void testClone() 422 { 423 setUpRoles(); 424 425 ArrayList roles = new ArrayList (); 426 roles.add(role1); 427 roles.add(role2); 428 RoleList full = new RoleList (roles); 429 RoleList clone = (RoleList ) full.clone(); 430 assertEquals(2, clone.size()); 431 assertEquals(role1.toString(), clone.get(0).toString()); 432 assertEquals(role2.toString(), clone.get(1).toString()); 433 Iterator iterator = clone.iterator(); 434 assertEquals(role1.toString(), iterator.next().toString()); 435 assertEquals(role2.toString(), iterator.next().toString()); 436 } 437 438 441 public void testSerialization() 442 { 443 setUpRoles(); 444 445 ArrayList roles = new ArrayList (); 446 roles.add(role1); 447 roles.add(role2); 448 RoleList full = new RoleList (roles); 449 RoleList copy = null; 450 451 try 452 { 453 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 455 ObjectOutputStream oos = new ObjectOutputStream (baos); 456 oos.writeObject(full); 457 458 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 460 ObjectInputStream ois = new ObjectInputStream (bais); 461 copy = (RoleList ) ois.readObject(); 462 } 463 catch (IOException ioe) 464 { 465 fail(ioe.toString()); 466 } 467 catch (ClassNotFoundException cnfe) 468 { 469 fail(cnfe.toString()); 470 } 471 472 assertEquals(2, copy.size()); 474 assertEquals(role1.toString(), copy.get(0).toString()); 475 assertEquals(role2.toString(), copy.get(1).toString()); 476 Iterator iterator = copy.iterator(); 477 assertEquals(role1.toString(), iterator.next().toString()); 478 assertEquals(role2.toString(), iterator.next().toString()); 479 } 480 481 483 private void setUpRoles() 484 { 485 if (setUpDone == true) 486 return; 487 try 488 { 489 ArrayList roleValue1 = new ArrayList (); 491 ObjectName a = new ObjectName (":a=a"); 492 ObjectName b = new ObjectName (":b=b"); 493 roleValue1.add(a); 494 roleValue1.add(b); 495 role1 = new Role ("RoleName1", roleValue1); 496 497 ArrayList roleValue2 = new ArrayList (); 498 ObjectName c = new ObjectName (":c=c"); 499 ObjectName d = new ObjectName (":d=d"); 500 roleValue2.add(c); 501 roleValue2.add(d); 502 role2 = new Role ("RoleName2", roleValue2); 503 } 504 catch (MalformedObjectNameException mfone) 505 { 506 fail(mfone.toString()); 507 } 508 setUpDone = true; 509 } 510 } 511 | Popular Tags |