1 /* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.apache.harmony.tests.java.util; 18 19 import java.util.ArrayList; 20 import java.util.Collection; 21 import java.util.EnumSet; 22 import java.util.Iterator; 23 import java.util.NoSuchElementException; 24 import java.util.Set; 25 26 import junit.framework.TestCase; 27 28 import org.apache.harmony.testframework.serialization.SerializationTest; 29 30 public class EnumSetTest extends TestCase { 31 static final boolean disableRIBugs = true; 32 33 static enum EnumWithInnerClass { 34 a, b, c, d, e, f { 35 }, 36 } 37 38 enum EnumWithAllInnerClass { 39 a {}, 40 b {}, 41 } 42 43 static enum EnumFoo { 44 a, b,c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, ll, 45 } 46 47 static enum EmptyEnum { 48 // expected 49 } 50 51 static enum HugeEnumWithInnerClass { 52 a{}, b{}, c{}, d{}, e{}, f{}, g{}, h{}, i{}, j{}, k{}, l{}, m{}, n{}, o{}, p{}, q{}, r{}, s{}, t{}, u{}, v{}, w{}, x{}, y{}, z{}, A{}, B{}, C{}, D{}, E{}, F{}, G{}, H{}, I{}, J{}, K{}, L{}, M{}, N{}, O{}, P{}, Q{}, R{}, S{}, T{}, U{}, V{}, W{}, X{}, Y{}, Z{}, aa{}, bb{}, cc{}, dd{}, ee{}, ff{}, gg{}, hh{}, ii{}, jj{}, kk{}, ll{}, mm{}, 53 } 54 55 static enum HugeEnum { 56 a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, ll, mm, 57 } 58 59 static enum HugeEnumCount { 60 NO1, NO2, NO3, NO4, NO5, NO6, NO7, NO8, NO9, NO10, NO11, NO12, NO13, NO14, NO15, NO16, NO17, NO18, NO19, NO20, 61 NO21, NO22, NO23, NO24, NO25, NO26, NO27, NO28, NO29, NO30, NO31, NO32, NO33, NO34, NO35, NO36, NO37, NO38, NO39, NO40, 62 NO41, NO42, NO43, NO44, NO45, NO46, NO47, NO48, NO49, NO50, NO51, NO52, NO53, NO54, NO55, NO56, NO57, NO58, NO59, NO60, 63 NO61, NO62, NO63, NO64, NO65, NO66, NO67, NO68, NO69, NO70, NO71, NO72, NO73, NO74, NO75, NO76, NO77, NO78, NO79, NO80, 64 NO81, NO82, NO83, NO84, NO85, NO86, NO87, NO88, NO89, NO90, NO91, NO92, NO93, NO94, NO95, NO96, NO97, NO98, NO99, NO100, 65 NO101, NO102, NO103, NO104, NO105, NO106, NO107, NO108, NO109, NO110, NO111, NO112, NO113, NO114, NO115, NO116, NO117, NO118, NO119, NO120, 66 NO121, NO122, NO123, NO124, NO125, NO126, NO127, NO128, NO129, NO130, 67 } 68 69 /** 70 * java.util.EnumSet#noneOf(java.lang.Class) 71 */ 72 @SuppressWarnings("unchecked") 73 public void test_NoneOf_LClass() { 74 try { 75 EnumSet.noneOf((Class) null); 76 fail("Should throw NullPointerException"); 77 } catch (NullPointerException e) { 78 // expected 79 } 80 81 try { 82 EnumSet.noneOf(Enum.class); 83 fail("Should throw ClassCastException"); 84 } catch (ClassCastException cce) { 85 // expected 86 } 87 88 Class<EnumWithAllInnerClass> c = (Class<EnumWithAllInnerClass>) EnumWithAllInnerClass.a 89 .getClass(); 90 try { 91 EnumSet.noneOf(c); 92 fail("Should throw ClassCastException"); 93 } catch (ClassCastException e) { 94 // expected 95 } 96 97 EnumSet<EnumWithAllInnerClass> setWithInnerClass = EnumSet 98 .noneOf(EnumWithAllInnerClass.class); 99 assertNotNull(setWithInnerClass); 100 101 // test enum type with more than 64 elements 102 Class<HugeEnumWithInnerClass> hc = (Class<HugeEnumWithInnerClass>) HugeEnumWithInnerClass.a 103 .getClass(); 104 try { 105 EnumSet.noneOf(hc); 106 fail("Should throw ClassCastException"); 107 } catch (ClassCastException e) { 108 // expected 109 } 110 111 EnumSet<HugeEnumWithInnerClass> hugeSetWithInnerClass = EnumSet 112 .noneOf(HugeEnumWithInnerClass.class); 113 assertNotNull(hugeSetWithInnerClass); 114 } 115 116 /** 117 * java.util.HugeEnumSet#iterator() 118 */ 119 public void test_iterator_HugeEnumSet() { 120 EnumSet<HugeEnumCount> set; 121 Object[] array; 122 123 // Test HugeEnumSet with 65 elements 124 // which is more than the bits of Long 125 set = EnumSet.range(HugeEnumCount.NO1, HugeEnumCount.NO65); 126 array = set.toArray(); 127 for (HugeEnumCount count : set) { 128 assertEquals(count, (HugeEnumCount) array[count.ordinal()]); 129 } 130 131 // Test HugeEnumSet with 130 elements 132 // which is more than twice of the bits of Long 133 set = EnumSet.range(HugeEnumCount.NO1, HugeEnumCount.NO130); 134 array = set.toArray(); 135 for (HugeEnumCount count : set) { 136 assertEquals(count, (HugeEnumCount) array[count.ordinal()]); 137 } 138 } 139 140 public void testRemoveIteratorRemoveFromHugeEnumSet() { 141 EnumSet<HugeEnumCount> set = EnumSet.noneOf(HugeEnumCount.class); 142 set.add(HugeEnumCount.NO64); 143 set.add(HugeEnumCount.NO65); 144 set.add(HugeEnumCount.NO128); 145 Iterator<HugeEnumCount> iterator = set.iterator(); 146 assertTrue(iterator.hasNext()); 147 assertEquals(HugeEnumCount.NO64, iterator.next()); 148 assertTrue(iterator.hasNext()); 149 iterator.remove(); 150 assertEquals(HugeEnumCount.NO65, iterator.next()); 151 assertTrue(iterator.hasNext()); 152 assertEquals(HugeEnumCount.NO128, iterator.next()); 153 assertFalse(iterator.hasNext()); 154 assertEquals(EnumSet.of(HugeEnumCount.NO65, HugeEnumCount.NO128), set); 155 iterator.remove(); 156 assertEquals(EnumSet.of(HugeEnumCount.NO65), set); 157 } 158 159 /** 160 * java.util.EnumSet#allOf(java.lang.Class) 161 */ 162 @SuppressWarnings("unchecked") 163 public void test_AllOf_LClass() { 164 try { 165 EnumSet.allOf((Class) null); 166 fail("Should throw NullPointerException"); 167 } catch (NullPointerException e) { 168 // expected 169 } 170 171 try { 172 EnumSet.allOf(Enum.class); 173 fail("Should throw ClassCastException"); 174 } catch (ClassCastException cce) { 175 // expected 176 } 177 178 EnumSet<EnumFoo> enumSet = EnumSet.allOf(EnumFoo.class); 179 assertEquals("Size of enumSet should be 64", 64, enumSet.size()); 180 181 assertFalse( 182 "enumSet should not contain null value", enumSet.contains(null)); 183 assertTrue( 184 "enumSet should contain EnumFoo.a", enumSet.contains(EnumFoo.a)); 185 assertTrue( 186 "enumSet should contain EnumFoo.b", enumSet.contains(EnumFoo.b)); 187 188 enumSet.add(EnumFoo.a); 189 assertEquals("Should be equal", 64, enumSet.size()); 190 191 EnumSet<EnumFoo> anotherSet = EnumSet.allOf(EnumFoo.class); 192 assertEquals("Should be equal", enumSet, anotherSet); 193 assertNotSame("Should not be identical", enumSet, anotherSet); 194 195 // test enum with more than 64 elements 196 EnumSet<HugeEnum> hugeEnumSet = EnumSet.allOf(HugeEnum.class); 197 assertEquals(65, hugeEnumSet.size()); 198 199 assertFalse(hugeEnumSet.contains(null)); 200 assertTrue(hugeEnumSet.contains(HugeEnum.a)); 201 assertTrue(hugeEnumSet.contains(HugeEnum.b)); 202 203 hugeEnumSet.add(HugeEnum.a); 204 assertEquals(65, hugeEnumSet.size()); 205 206 EnumSet<HugeEnum> anotherHugeSet = EnumSet.allOf(HugeEnum.class); 207 assertEquals(hugeEnumSet, anotherHugeSet); 208 assertNotSame(hugeEnumSet, anotherHugeSet); 209 210 } 211 212 /** 213 * java.util.EnumSet#add(E) 214 */ 215 @SuppressWarnings("unchecked") 216 public void test_add_E() { 217 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 218 set.add(EnumFoo.a); 219 set.add(EnumFoo.b); 220 221 try { 222 set.add(null); 223 fail("Should throw NullPointerException"); 224 } catch (NullPointerException e) { 225 // expected 226 } 227 228 // test enum type with more than 64 elements 229 Set rawSet = set; 230 try { 231 rawSet.add(HugeEnumWithInnerClass.b); 232 fail("Should throw ClassCastException"); 233 } catch (ClassCastException e) { 234 // expected 235 } 236 237 set.clear(); 238 try { 239 set.add(null); 240 fail("Should throw NullPointerException"); 241 } catch (NullPointerException e) { 242 // expected 243 } 244 245 boolean result = set.add(EnumFoo.a); 246 assertEquals("Size should be 1:", 1, set.size()); 247 assertTrue("Return value should be true", result); 248 249 result = set.add(EnumFoo.a); 250 assertEquals("Size should be 1:", 1, set.size()); 251 assertFalse("Return value should be false", result); 252 253 set.add(EnumFoo.b); 254 assertEquals("Size should be 2:", 2, set.size()); 255 256 rawSet = set; 257 try { 258 rawSet.add(EnumWithAllInnerClass.a); 259 fail("Should throw ClassCastException"); 260 } catch(ClassCastException e) { 261 // expected 262 } 263 264 try { 265 rawSet.add(EnumWithInnerClass.a); 266 fail("Should throw ClassCastException"); 267 } catch(ClassCastException e) { 268 // expected 269 } 270 271 try { 272 rawSet.add(new Object()); 273 fail("Should throw ClassCastException"); 274 } catch(ClassCastException e) { 275 // expected 276 } 277 278 // test enum type with more than 64 elements 279 Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 280 result = hugeSet.add(HugeEnum.a); 281 assertTrue(result); 282 283 result = hugeSet.add(HugeEnum.a); 284 assertFalse(result); 285 286 try { 287 hugeSet.add(null); 288 fail("Should throw NullPointerException"); 289 } catch (NullPointerException e) { 290 // expected 291 } 292 293 rawSet = hugeSet; 294 try { 295 rawSet.add(HugeEnumWithInnerClass.b); 296 fail("Should throw ClassCastException"); 297 } catch (ClassCastException e) { 298 // expected 299 } 300 301 try { 302 rawSet.add(new Object()); 303 fail("Should throw ClassCastException"); 304 } catch (ClassCastException e) { 305 // expected 306 } 307 308 result = hugeSet.add(HugeEnum.mm); 309 assertTrue(result); 310 result = hugeSet.add(HugeEnum.mm); 311 assertFalse(result); 312 assertEquals(2, hugeSet.size()); 313 314 } 315 316 /** 317 * java.util.EnumSet#addAll(Collection) 318 */ 319 @SuppressWarnings( { "unchecked", "boxing" }) 320 public void test_addAll_LCollection() { 321 322 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 323 assertEquals("Size should be 0:", 0, set.size()); 324 325 try { 326 set.addAll(null); 327 fail("Should throw NullPointerException"); 328 } catch (NullPointerException e) { 329 // expected 330 } 331 332 Set emptySet = EnumSet.noneOf(EmptyEnum.class); 333 Enum[] elements = EmptyEnum.class.getEnumConstants(); 334 for(int i = 0; i < elements.length; i++) { 335 emptySet.add(elements[i]); 336 } 337 boolean result = set.addAll(emptySet); 338 assertFalse(result); 339 340 Collection<EnumFoo> collection = new ArrayList<EnumFoo>(); 341 collection.add(EnumFoo.a); 342 collection.add(EnumFoo.b); 343 result = set.addAll(collection); 344 assertTrue("addAll should be successful", result); 345 assertEquals("Size should be 2:", 2, set.size()); 346 347 set = EnumSet.noneOf(EnumFoo.class); 348 349 Collection rawCollection = new ArrayList<Integer>(); 350 result = set.addAll(rawCollection); 351 assertFalse(result); 352 rawCollection.add(1); 353 try { 354 set.addAll(rawCollection); 355 fail("Should throw ClassCastException"); 356 } catch (ClassCastException e) { 357 // expected 358 } 359 360 Set<EnumFoo> fullSet = EnumSet.noneOf(EnumFoo.class); 361 fullSet.add(EnumFoo.a); 362 fullSet.add(EnumFoo.b); 363 result = set.addAll(fullSet); 364 assertTrue("addAll should be successful", result); 365 assertEquals("Size of set should be 2", 2, set.size()); 366 367 try { 368 fullSet.addAll(null); 369 fail("Should throw NullPointerException"); 370 } catch (NullPointerException e) { 371 // expected 372 } 373 374 Set fullSetWithSubclass = EnumSet.noneOf(EnumWithInnerClass.class); 375 elements = EnumWithInnerClass.class.getEnumConstants(); 376 for(int i = 0; i < elements.length; i++) { 377 fullSetWithSubclass.add(elements[i]); 378 } 379 try { 380 set.addAll(fullSetWithSubclass); 381 fail("Should throw ClassCastException"); 382 } catch (ClassCastException e) { 383 // expected 384 } 385 Set<EnumWithInnerClass> setWithSubclass = fullSetWithSubclass; 386 result = setWithSubclass.addAll(setWithSubclass); 387 assertFalse("Should return false", result); 388 389 Set<EnumWithInnerClass> anotherSetWithSubclass = EnumSet 390 .noneOf(EnumWithInnerClass.class); 391 elements = EnumWithInnerClass.class.getEnumConstants(); 392 for(int i = 0; i < elements.length; i++) { 393 anotherSetWithSubclass.add((EnumWithInnerClass) elements[i]); 394 } 395 result = setWithSubclass.addAll(anotherSetWithSubclass); 396 assertFalse("Should return false", result); 397 398 anotherSetWithSubclass.remove(EnumWithInnerClass.a); 399 result = setWithSubclass.addAll(anotherSetWithSubclass); 400 assertFalse("Should return false", result); 401 402 // test enum type with more than 64 elements 403 Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 404 assertEquals(0, hugeSet.size()); 405 406 try { 407 hugeSet.addAll(null); 408 fail("Should throw NullPointerException"); 409 } catch (NullPointerException e) { 410 // expected 411 } 412 413 hugeSet = EnumSet.allOf(HugeEnum.class); 414 result = hugeSet.addAll(hugeSet); 415 assertFalse(result); 416 417 hugeSet = EnumSet.noneOf(HugeEnum.class); 418 Collection<HugeEnum> hugeCollection = new ArrayList<HugeEnum>(); 419 hugeCollection.add(HugeEnum.a); 420 hugeCollection.add(HugeEnum.b); 421 result = hugeSet.addAll(hugeCollection); 422 assertTrue(result); 423 assertEquals(2, set.size()); 424 425 hugeSet = EnumSet.noneOf(HugeEnum.class); 426 427 rawCollection = new ArrayList<Integer>(); 428 result = hugeSet.addAll(rawCollection); 429 assertFalse(result); 430 rawCollection.add(1); 431 try { 432 hugeSet.addAll(rawCollection); 433 fail("Should throw ClassCastException"); 434 } catch (ClassCastException e) { 435 // expected 436 } 437 438 EnumSet<HugeEnum> aHugeSet = EnumSet.noneOf(HugeEnum.class); 439 aHugeSet.add(HugeEnum.a); 440 aHugeSet.add(HugeEnum.b); 441 result = hugeSet.addAll(aHugeSet); 442 assertTrue(result); 443 assertEquals(2, hugeSet.size()); 444 445 try { 446 aHugeSet.addAll(null); 447 fail("Should throw NullPointerException"); 448 } catch (NullPointerException e) { 449 // expected 450 } 451 452 Set hugeSetWithSubclass = EnumSet.allOf(HugeEnumWithInnerClass.class); 453 try { 454 hugeSet.addAll(hugeSetWithSubclass); 455 fail("Should throw ClassCastException"); 456 } catch (ClassCastException e) { 457 // expected 458 } 459 Set<HugeEnumWithInnerClass> hugeSetWithInnerSubclass = hugeSetWithSubclass; 460 result = hugeSetWithInnerSubclass.addAll(hugeSetWithInnerSubclass); 461 assertFalse(result); 462 463 Set<HugeEnumWithInnerClass> anotherHugeSetWithSubclass = EnumSet 464 .allOf(HugeEnumWithInnerClass.class); 465 result = hugeSetWithSubclass.addAll(anotherHugeSetWithSubclass); 466 assertFalse(result); 467 468 anotherHugeSetWithSubclass.remove(HugeEnumWithInnerClass.a); 469 result = setWithSubclass.addAll(anotherSetWithSubclass); 470 assertFalse(result); 471 472 } 473 474 /** 475 * java.util.EnumSet#remove(Object) 476 */ 477 @SuppressWarnings("CollectionIncompatibleType") 478 public void test_remove_LOject() { 479 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 480 Enum[] elements = EnumFoo.class.getEnumConstants(); 481 for(int i = 0; i < elements.length; i++) { 482 set.add((EnumFoo) elements[i]); 483 } 484 485 boolean result = set.remove(null); 486 assertFalse("'set' does not contain null", result); 487 488 result = set.remove(EnumFoo.a); 489 assertTrue("Should return true", result); 490 result = set.remove(EnumFoo.a); 491 assertFalse("Should return false", result); 492 493 assertEquals("Size of set should be 63:", 63, set.size()); 494 495 result = set.remove(EnumWithInnerClass.a); 496 assertFalse("Should return false", result); 497 result = set.remove(EnumWithInnerClass.f); 498 assertFalse("Should return false", result); 499 500 // test enum with more than 64 elements 501 Set<HugeEnum> hugeSet = EnumSet.allOf(HugeEnum.class); 502 503 result = hugeSet.remove(null); 504 assertFalse("'set' does not contain null", result); 505 506 result = hugeSet.remove(HugeEnum.a); 507 assertTrue("Should return true", result); 508 result = hugeSet.remove(HugeEnum.a); 509 assertFalse("Should return false", result); 510 511 assertEquals("Size of set should be 64:", 64, hugeSet.size()); 512 513 result = hugeSet.remove(HugeEnumWithInnerClass.a); 514 assertFalse("Should return false", result); 515 result = hugeSet.remove(HugeEnumWithInnerClass.f); 516 assertFalse("Should return false", result); 517 } 518 519 /** 520 * java.util.EnumSet#equals(Object) 521 */ 522 public void test_equals_LObject() { 523 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 524 Enum[] elements = EnumFoo.class.getEnumConstants(); 525 for(int i = 0; i < elements.length; i++) { 526 set.add((EnumFoo) elements[i]); 527 } 528 529 assertFalse("Should return false", set.equals(null)); 530 assertFalse( 531 "Should return false", set.equals(new Object())); 532 533 Set<EnumFoo> anotherSet = EnumSet.noneOf(EnumFoo.class); 534 elements = EnumFoo.class.getEnumConstants(); 535 for(int i = 0; i < elements.length; i++) { 536 anotherSet.add((EnumFoo) elements[i]); 537 } 538 assertTrue("Should return true", set.equals(anotherSet)); 539 540 anotherSet.remove(EnumFoo.a); 541 assertFalse( 542 "Should return false", set.equals(anotherSet)); 543 544 Set<EnumWithInnerClass> setWithInnerClass = EnumSet 545 .noneOf(EnumWithInnerClass.class); 546 elements = EnumWithInnerClass.class.getEnumConstants(); 547 for(int i = 0; i < elements.length; i++) { 548 setWithInnerClass.add((EnumWithInnerClass) elements[i]); 549 } 550 551 assertFalse( 552 "Should return false", set.equals(setWithInnerClass)); 553 554 setWithInnerClass.clear(); 555 set.clear(); 556 assertTrue("Should be equal", set.equals(setWithInnerClass)); 557 558 // test enum type with more than 64 elements 559 Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 560 assertTrue(hugeSet.equals(set)); 561 562 hugeSet = EnumSet.allOf(HugeEnum.class); 563 assertFalse(hugeSet.equals(null)); 564 assertFalse(hugeSet.equals(new Object())); 565 566 Set<HugeEnum> anotherHugeSet = EnumSet.allOf(HugeEnum.class); 567 anotherHugeSet.remove(HugeEnum.a); 568 assertFalse(hugeSet.equals(anotherHugeSet)); 569 570 Set<HugeEnumWithInnerClass> hugeSetWithInnerClass = EnumSet 571 .allOf(HugeEnumWithInnerClass.class); 572 assertFalse(hugeSet.equals(hugeSetWithInnerClass)); 573 hugeSetWithInnerClass.clear(); 574 hugeSet.clear(); 575 assertTrue(hugeSet.equals(hugeSetWithInnerClass)); 576 } 577 578 /** 579 * java.util.EnumSet#clear() 580 */ 581 public void test_clear() { 582 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 583 set.add(EnumFoo.a); 584 set.add(EnumFoo.b); 585 assertEquals("Size should be 2", 2, set.size()); 586 587 set.clear(); 588 589 assertEquals("Size should be 0", 0, set.size()); 590 591 // test enum type with more than 64 elements 592 Set<HugeEnum> hugeSet = EnumSet.allOf(HugeEnum.class); 593 assertEquals(65, hugeSet.size()); 594 595 boolean result = hugeSet.contains(HugeEnum.aa); 596 assertTrue(result); 597 598 hugeSet.clear(); 599 assertEquals(0, hugeSet.size()); 600 result = hugeSet.contains(HugeEnum.aa); 601 assertFalse(result); 602 } 603 604 /** 605 * java.util.EnumSet#size() 606 */ 607 public void test_size() { 608 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 609 set.add(EnumFoo.a); 610 set.add(EnumFoo.b); 611 assertEquals("Size should be 2", 2, set.size()); 612 613 // test enum type with more than 64 elements 614 Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 615 hugeSet.add(HugeEnum.a); 616 hugeSet.add(HugeEnum.bb); 617 assertEquals("Size should be 2", 2, hugeSet.size()); 618 } 619 620 /** 621 * java.util.EnumSet#complementOf(java.util.EnumSet) 622 */ 623 public void test_ComplementOf_LEnumSet() { 624 625 try { 626 EnumSet.complementOf((EnumSet<EnumFoo>) null); 627 fail("Should throw NullPointerException"); 628 } catch (NullPointerException npe) { 629 // expected 630 } 631 632 EnumSet<EnumWithInnerClass> set = EnumSet 633 .noneOf(EnumWithInnerClass.class); 634 set.add(EnumWithInnerClass.d); 635 set.add(EnumWithInnerClass.e); 636 set.add(EnumWithInnerClass.f); 637 638 assertEquals("Size should be 3:", 3, set.size()); 639 640 EnumSet<EnumWithInnerClass> complementOfE = EnumSet.complementOf(set); 641 assertTrue(set.contains(EnumWithInnerClass.d)); 642 assertEquals( 643 "complementOfE should have size 3", 3, complementOfE.size()); 644 assertTrue("complementOfE should contain EnumWithSubclass.a:", 645 complementOfE.contains(EnumWithInnerClass.a)); 646 assertTrue("complementOfE should contain EnumWithSubclass.b:", 647 complementOfE.contains(EnumWithInnerClass.b)); 648 assertTrue("complementOfE should contain EnumWithSubclass.c:", 649 complementOfE.contains(EnumWithInnerClass.c)); 650 651 // test enum type with more than 64 elements 652 EnumSet<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 653 assertEquals(0, hugeSet.size()); 654 Set<HugeEnum> complementHugeSet = EnumSet.complementOf(hugeSet); 655 assertEquals(65, complementHugeSet.size()); 656 657 hugeSet.add(HugeEnum.A); 658 hugeSet.add(HugeEnum.mm); 659 complementHugeSet = EnumSet.complementOf(hugeSet); 660 assertEquals(63, complementHugeSet.size()); 661 662 try { 663 EnumSet.complementOf((EnumSet<HugeEnum>) null); 664 fail("Should throw NullPointerException"); 665 } catch (NullPointerException npe) { 666 // expected 667 } 668 } 669 670 /** 671 * java.util.EnumSet#contains(Object) 672 */ 673 @SuppressWarnings("CollectionIncompatibleType") 674 public void test_contains_LObject() { 675 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 676 Enum[] elements = EnumFoo.class.getEnumConstants(); 677 for(int i = 0; i < elements.length; i++) { 678 set.add((EnumFoo)elements[i]); 679 } 680 boolean result = set.contains(null); 681 assertFalse("Should not contain null:", result); 682 683 result = set.contains(EnumFoo.a); 684 assertTrue("Should contain EnumFoo.a", result); 685 result = set.contains(EnumFoo.ll); 686 assertTrue("Should contain EnumFoo.ll", result); 687 688 result = set.contains(EnumFoo.b); 689 assertTrue("Should contain EnumFoo.b", result); 690 691 result = set.contains(new Object()); 692 assertFalse("Should not contain Object instance", result); 693 694 result = set.contains(EnumWithInnerClass.a); 695 assertFalse("Should not contain EnumWithSubclass.a", result); 696 697 set = EnumSet.noneOf(EnumFoo.class); 698 set.add(EnumFoo.aa); 699 set.add(EnumFoo.bb); 700 set.add(EnumFoo.cc); 701 702 assertEquals("Size of set should be 3", 3, set.size()); 703 assertTrue("set should contain EnumFoo.aa", set.contains(EnumFoo.aa)); 704 705 Set<EnumWithInnerClass> setWithSubclass = EnumSet 706 .noneOf(EnumWithInnerClass.class); 707 setWithSubclass.add(EnumWithInnerClass.a); 708 setWithSubclass.add(EnumWithInnerClass.b); 709 setWithSubclass.add(EnumWithInnerClass.c); 710 setWithSubclass.add(EnumWithInnerClass.d); 711 setWithSubclass.add(EnumWithInnerClass.e); 712 setWithSubclass.add(EnumWithInnerClass.f); 713 result = setWithSubclass.contains(EnumWithInnerClass.f); 714 assertTrue("Should contain EnumWithSubclass.f", result); 715 716 // test enum type with more than 64 elements 717 Set<HugeEnum> hugeSet = EnumSet.allOf(HugeEnum.class); 718 hugeSet.add(HugeEnum.a); 719 result = hugeSet.contains(HugeEnum.a); 720 assertTrue(result); 721 722 result = hugeSet.contains(HugeEnum.b); 723 assertTrue(result); 724 725 result = hugeSet.contains(null); 726 assertFalse(result); 727 728 result = hugeSet.contains(HugeEnum.a); 729 assertTrue(result); 730 731 result = hugeSet.contains(HugeEnum.ll); 732 assertTrue(result); 733 734 result = hugeSet.contains(new Object()); 735 assertFalse(result); 736 737 result = hugeSet.contains(Enum.class); 738 assertFalse(result); 739 740 } 741 742 /** 743 * java.util.EnumSet#containsAll(Collection) 744 */ 745 @SuppressWarnings( { "unchecked", "boxing", "CollectionIncompatibleType" }) 746 public void test_containsAll_LCollection() { 747 EnumSet<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 748 Enum[] elements = EnumFoo.class.getEnumConstants(); 749 for(int i = 0; i < elements.length; i++) { 750 set.add((EnumFoo)elements[i]); 751 } 752 try { 753 set.containsAll(null); 754 fail("Should throw NullPointerException"); 755 } catch (NullPointerException e) { 756 // expected 757 } 758 759 EnumSet<EmptyEnum> emptySet = EnumSet.noneOf(EmptyEnum.class); 760 elements = EmptyEnum.class.getEnumConstants(); 761 for(int i = 0; i < elements.length; i++) { 762 emptySet.add((EmptyEnum)elements[i]); 763 } 764 boolean result = set.containsAll(emptySet); 765 assertTrue("Should return true", result); 766 767 Collection rawCollection = new ArrayList(); 768 result = set.containsAll(rawCollection); 769 assertTrue("Should contain empty collection:", result); 770 771 rawCollection.add(1); 772 result = set.containsAll(rawCollection); 773 assertFalse("Should return false", result); 774 775 rawCollection.add(EnumWithInnerClass.a); 776 result = set.containsAll(rawCollection); 777 assertFalse("Should return false", result); 778 779 EnumSet rawSet = EnumSet.noneOf(EnumFoo.class); 780 result = set.containsAll(rawSet); 781 assertTrue("Should contain empty set", result); 782 783 emptySet = EnumSet.noneOf(EmptyEnum.class); 784 result = set.containsAll(emptySet); 785 assertTrue("No class cast should be performed on empty set", result); 786 787 Collection<EnumFoo> collection = new ArrayList<EnumFoo>(); 788 collection.add(EnumFoo.a); 789 result = set.containsAll(collection); 790 assertTrue("Should contain all elements in collection", result); 791 792 EnumSet<EnumFoo> fooSet = EnumSet.noneOf(EnumFoo.class); 793 fooSet.add(EnumFoo.a); 794 result = set.containsAll(fooSet); 795 assertTrue("Should return true", result); 796 797 set.clear(); 798 try { 799 set.containsAll(null); 800 fail("Should throw NullPointerException"); 801 } catch (NullPointerException e) { 802 // expected 803 } 804 805 Collection<EnumWithInnerClass> collectionWithSubclass = new ArrayList<EnumWithInnerClass>(); 806 collectionWithSubclass.add(EnumWithInnerClass.a); 807 result = set.containsAll(collectionWithSubclass); 808 assertFalse("Should return false", result); 809 810 EnumSet<EnumWithInnerClass> setWithSubclass = EnumSet 811 .noneOf(EnumWithInnerClass.class); 812 setWithSubclass.add(EnumWithInnerClass.a); 813 result = set.containsAll(setWithSubclass); 814 assertFalse("Should return false", result); 815 816 // test enum type with more than 64 elements 817 Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 818 hugeSet.add(HugeEnum.a); 819 hugeSet.add(HugeEnum.b); 820 hugeSet.add(HugeEnum.aa); 821 hugeSet.add(HugeEnum.bb); 822 hugeSet.add(HugeEnum.cc); 823 hugeSet.add(HugeEnum.dd); 824 825 Set<HugeEnum> anotherHugeSet = EnumSet.noneOf(HugeEnum.class); 826 hugeSet.add(HugeEnum.b); 827 hugeSet.add(HugeEnum.cc); 828 result = hugeSet.containsAll(anotherHugeSet); 829 assertTrue(result); 830 831 try { 832 hugeSet.containsAll(null); 833 fail("Should throw NullPointerException"); 834 } catch(NullPointerException e) { 835 // expected 836 } 837 838 Set<HugeEnumWithInnerClass> hugeSetWithInnerClass = EnumSet 839 .noneOf(HugeEnumWithInnerClass.class); 840 hugeSetWithInnerClass.add(HugeEnumWithInnerClass.a); 841 hugeSetWithInnerClass.add(HugeEnumWithInnerClass.b); 842 result = hugeSetWithInnerClass.containsAll(hugeSetWithInnerClass); 843 assertTrue(result); 844 result = hugeSet.containsAll(hugeSetWithInnerClass); 845 assertFalse(result); 846 847 rawCollection = new ArrayList(); 848 result = hugeSet.containsAll(rawCollection); 849 assertTrue("Should contain empty collection:", result); 850 851 rawCollection.add(1); 852 result = hugeSet.containsAll(rawCollection); 853 assertFalse("Should return false", result); 854 855 rawCollection.add(EnumWithInnerClass.a); 856 result = set.containsAll(rawCollection); 857 assertFalse("Should return false", result); 858 859 rawSet = EnumSet.noneOf(HugeEnum.class); 860 result = hugeSet.containsAll(rawSet); 861 assertTrue("Should contain empty set", result); 862 863 EnumSet<HugeEnumWithInnerClass> emptyHugeSet 864 = EnumSet.noneOf(HugeEnumWithInnerClass.class); 865 result = hugeSet.containsAll(emptyHugeSet); 866 assertTrue("No class cast should be performed on empty set", result); 867 868 Collection<HugeEnum> hugeCollection = new ArrayList<HugeEnum>(); 869 hugeCollection.add(HugeEnum.a); 870 result = hugeSet.containsAll(hugeCollection); 871 assertTrue("Should contain all elements in collection", result); 872 873 hugeSet.clear(); 874 try { 875 hugeSet.containsAll(null); 876 fail("Should throw NullPointerException"); 877 } catch (NullPointerException e) { 878 // expected 879 } 880 881 Collection<HugeEnumWithInnerClass> hugeCollectionWithSubclass = new ArrayList<HugeEnumWithInnerClass>(); 882 hugeCollectionWithSubclass.add(HugeEnumWithInnerClass.a); 883 result = hugeSet.containsAll(hugeCollectionWithSubclass); 884 assertFalse("Should return false", result); 885 886 EnumSet<HugeEnumWithInnerClass> hugeSetWithSubclass = EnumSet 887 .noneOf(HugeEnumWithInnerClass.class); 888 hugeSetWithSubclass.add(HugeEnumWithInnerClass.a); 889 result = hugeSet.containsAll(hugeSetWithSubclass); 890 assertFalse("Should return false", result); 891 } 892 893 /** 894 * java.util.EnumSet#copyOf(java.util.Collection) 895 */ 896 @SuppressWarnings("unchecked") 897 public void test_CopyOf_LCollection() { 898 try { 899 EnumSet.copyOf((Collection) null); 900 fail("Should throw NullPointerException"); 901 } catch (NullPointerException npe) { 902 // expected 903 } 904 905 Collection collection = new ArrayList(); 906 try { 907 EnumSet.copyOf(collection); 908 fail("Should throw IllegalArgumentException"); 909 } catch (IllegalArgumentException e) { 910 // expected 911 } 912 913 collection.add(new Object()); 914 try { 915 EnumSet.copyOf(collection); 916 fail("Should throw ClassCastException"); 917 } catch (ClassCastException e) { 918 // expected 919 } 920 921 Collection<EnumFoo> enumCollection = new ArrayList<EnumFoo>(); 922 enumCollection.add(EnumFoo.b); 923 924 EnumSet<EnumFoo> copyOfEnumCollection = EnumSet.copyOf(enumCollection); 925 assertEquals("Size of copyOfEnumCollection should be 1:", 926 1, copyOfEnumCollection.size()); 927 assertTrue("copyOfEnumCollection should contain EnumFoo.b:", 928 copyOfEnumCollection.contains(EnumFoo.b)); 929 930 enumCollection.add(null); 931 assertEquals("Size of enumCollection should be 2:", 932 2, enumCollection.size()); 933 934 try { 935 copyOfEnumCollection = EnumSet.copyOf(enumCollection); 936 fail("Should throw NullPointerException"); 937 } catch (NullPointerException npe) { 938 // expected 939 } 940 941 Collection rawEnumCollection = new ArrayList(); 942 rawEnumCollection.add(EnumFoo.a); 943 rawEnumCollection.add(EnumWithInnerClass.a); 944 try { 945 EnumSet.copyOf(rawEnumCollection); 946 fail("Should throw ClassCastException"); 947 } catch(ClassCastException e) { 948 // expected 949 } 950 951 // test enum type with more than 64 elements 952 Collection<HugeEnum> hugeEnumCollection = new ArrayList<HugeEnum>(); 953 hugeEnumCollection.add(HugeEnum.b); 954 955 EnumSet<HugeEnum> copyOfHugeEnumCollection = EnumSet.copyOf(hugeEnumCollection); 956 assertEquals(1, copyOfHugeEnumCollection.size()); 957 assertTrue(copyOfHugeEnumCollection.contains(HugeEnum.b)); 958 959 hugeEnumCollection.add(null); 960 assertEquals(2, hugeEnumCollection.size()); 961 962 try { 963 copyOfHugeEnumCollection = EnumSet.copyOf(hugeEnumCollection); 964 fail("Should throw NullPointerException"); 965 } catch (NullPointerException npe) { 966 // expected 967 } 968 969 rawEnumCollection = new ArrayList(); 970 rawEnumCollection.add(HugeEnum.a); 971 rawEnumCollection.add(HugeEnumWithInnerClass.a); 972 try { 973 EnumSet.copyOf(rawEnumCollection); 974 fail("Should throw ClassCastException"); 975 } catch(ClassCastException e) { 976 // expected 977 } 978 } 979 980 /** 981 * java.util.EnumSet#copyOf(java.util.EnumSet) 982 */ 983 @SuppressWarnings("unchecked") 984 public void test_CopyOf_LEnumSet() { 985 EnumSet<EnumWithInnerClass> enumSet = EnumSet 986 .noneOf(EnumWithInnerClass.class); 987 enumSet.add(EnumWithInnerClass.a); 988 enumSet.add(EnumWithInnerClass.f); 989 EnumSet<EnumWithInnerClass> copyOfE = EnumSet.copyOf(enumSet); 990 assertEquals("Size of enumSet and copyOfE should be equal", 991 enumSet.size(), copyOfE.size()); 992 993 assertTrue("EnumWithSubclass.a should be contained in copyOfE", 994 copyOfE.contains(EnumWithInnerClass.a)); 995 assertTrue("EnumWithSubclass.f should be contained in copyOfE", 996 copyOfE.contains(EnumWithInnerClass.f)); 997 998 Object[] enumValue = copyOfE.toArray(); 999 assertSame("enumValue[0] should be identical with EnumWithSubclass.a", 1000 enumValue[0], EnumWithInnerClass.a); 1001 assertSame("enumValue[1] should be identical with EnumWithSubclass.f", 1002 enumValue[1], EnumWithInnerClass.f); 1003 1004 try { 1005 EnumSet.copyOf((EnumSet) null); 1006 fail("Should throw NullPointerException"); 1007 } catch (NullPointerException npe) { 1008 // expected 1009 } 1010 1011 // test enum type with more than 64 elements 1012 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet 1013 .noneOf(HugeEnumWithInnerClass.class); 1014 hugeEnumSet.add(HugeEnumWithInnerClass.a); 1015 hugeEnumSet.add(HugeEnumWithInnerClass.f); 1016 EnumSet<HugeEnumWithInnerClass> copyOfHugeEnum = EnumSet.copyOf(hugeEnumSet); 1017 assertEquals(enumSet.size(), copyOfE.size()); 1018 1019 assertTrue(copyOfHugeEnum.contains(HugeEnumWithInnerClass.a)); 1020 assertTrue(copyOfHugeEnum.contains(HugeEnumWithInnerClass.f)); 1021 1022 Object[] hugeEnumValue = copyOfHugeEnum.toArray(); 1023 assertSame(hugeEnumValue[0], HugeEnumWithInnerClass.a); 1024 assertSame(hugeEnumValue[1], HugeEnumWithInnerClass.f); 1025 } 1026 1027 /** 1028 * java.util.EnumSet#removeAll(Collection) 1029 */ 1030 @SuppressWarnings({ "unchecked", "CollectionIncompatibleType" }) 1031 public void test_removeAll_LCollection() { 1032 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 1033 try { 1034 set.removeAll(null); 1035 fail("Should throw NullPointerException"); 1036 } catch (NullPointerException e) { 1037 // expected 1038 } 1039 1040 set = EnumSet.allOf(EnumFoo.class); 1041 assertEquals("Size of set should be 64:", 64, set.size()); 1042 1043 try { 1044 set.removeAll(null); 1045 fail("Should throw NullPointerException"); 1046 } catch (NullPointerException e) { 1047 // expected 1048 } 1049 1050 Collection<EnumFoo> collection = new ArrayList<EnumFoo>(); 1051 collection.add(EnumFoo.a); 1052 1053 boolean result = set.removeAll(collection); 1054 assertTrue("Should return true", result); 1055 assertEquals("Size of set should be 63", 63, set.size()); 1056 1057 collection = new ArrayList(); 1058 result = set.removeAll(collection); 1059 assertFalse("Should return false", result); 1060 1061 Set<EmptyEnum> emptySet = EnumSet.noneOf(EmptyEnum.class); 1062 result = set.removeAll(emptySet); 1063 assertFalse("Should return false", result); 1064 1065 EnumSet<EnumFoo> emptyFooSet = EnumSet.noneOf(EnumFoo.class); 1066 result = set.removeAll(emptyFooSet); 1067 assertFalse("Should return false", result); 1068 1069 emptyFooSet.add(EnumFoo.a); 1070 result = set.removeAll(emptyFooSet); 1071 assertFalse("Should return false", result); 1072 1073 Set<EnumWithInnerClass> setWithSubclass = EnumSet 1074 .noneOf(EnumWithInnerClass.class); 1075 result = set.removeAll(setWithSubclass); 1076 assertFalse("Should return false", result); 1077 1078 setWithSubclass.add(EnumWithInnerClass.a); 1079 result = set.removeAll(setWithSubclass); 1080 assertFalse("Should return false", result); 1081 1082 Set<EnumFoo> anotherSet = EnumSet.noneOf(EnumFoo.class); 1083 anotherSet.add(EnumFoo.a); 1084 1085 set = EnumSet.allOf(EnumFoo.class); 1086 result = set.removeAll(anotherSet); 1087 assertTrue("Should return true", result); 1088 assertEquals("Size of set should be 63:", 63, set.size()); 1089 1090 Set<EnumWithInnerClass> setWithInnerClass = EnumSet 1091 .noneOf(EnumWithInnerClass.class); 1092 setWithInnerClass.add(EnumWithInnerClass.a); 1093 setWithInnerClass.add(EnumWithInnerClass.b); 1094 1095 Set<EnumWithInnerClass> anotherSetWithInnerClass = EnumSet 1096 .noneOf(EnumWithInnerClass.class); 1097 anotherSetWithInnerClass.add(EnumWithInnerClass.c); 1098 anotherSetWithInnerClass.add(EnumWithInnerClass.d); 1099 result = anotherSetWithInnerClass.removeAll(setWithInnerClass); 1100 assertFalse("Should return false", result); 1101 1102 anotherSetWithInnerClass.add(EnumWithInnerClass.a); 1103 result = anotherSetWithInnerClass.removeAll(setWithInnerClass); 1104 assertTrue("Should return true", result); 1105 assertEquals("Size of anotherSetWithInnerClass should remain 2", 1106 2, anotherSetWithInnerClass.size()); 1107 1108 anotherSetWithInnerClass.remove(EnumWithInnerClass.c); 1109 anotherSetWithInnerClass.remove(EnumWithInnerClass.d); 1110 result = anotherSetWithInnerClass.remove(setWithInnerClass); 1111 assertFalse("Should return false", result); 1112 1113 Set rawSet = EnumSet.allOf(EnumWithAllInnerClass.class); 1114 result = rawSet.removeAll(EnumSet.allOf(EnumFoo.class)); 1115 assertFalse("Should return false", result); 1116 1117 setWithInnerClass = EnumSet.allOf(EnumWithInnerClass.class); 1118 anotherSetWithInnerClass = EnumSet.allOf(EnumWithInnerClass.class); 1119 setWithInnerClass.remove(EnumWithInnerClass.a); 1120 anotherSetWithInnerClass.remove(EnumWithInnerClass.f); 1121 result = setWithInnerClass.removeAll(anotherSetWithInnerClass); 1122 assertTrue("Should return true", result); 1123 assertEquals("Size of setWithInnerClass should be 1", 1, setWithInnerClass.size()); 1124 1125 result = setWithInnerClass.contains(EnumWithInnerClass.f); 1126 assertTrue("Should return true", result); 1127 1128 // test enum type with more than 64 elements 1129 Set<HugeEnum> hugeSet = EnumSet.allOf(HugeEnum.class); 1130 1131 Collection<HugeEnum> hugeCollection = new ArrayList<HugeEnum>(); 1132 hugeCollection.add(HugeEnum.a); 1133 1134 result = hugeSet.removeAll(hugeCollection); 1135 assertTrue(result); 1136 assertEquals(64, hugeSet.size()); 1137 1138 collection = new ArrayList(); 1139 result = hugeSet.removeAll(collection); 1140 assertFalse(result); 1141 1142 Set<HugeEnum> emptyHugeSet = EnumSet.noneOf(HugeEnum.class); 1143 result = hugeSet.removeAll(emptyHugeSet); 1144 assertFalse(result); 1145 1146 Set<HugeEnumWithInnerClass> hugeSetWithSubclass = EnumSet 1147 .noneOf(HugeEnumWithInnerClass.class); 1148 result = hugeSet.removeAll(hugeSetWithSubclass); 1149 assertFalse(result); 1150 1151 hugeSetWithSubclass.add(HugeEnumWithInnerClass.a); 1152 result = hugeSet.removeAll(hugeSetWithSubclass); 1153 assertFalse(result); 1154 1155 Set<HugeEnum> anotherHugeSet = EnumSet.noneOf(HugeEnum.class); 1156 anotherHugeSet.add(HugeEnum.a); 1157 1158 hugeSet = EnumSet.allOf(HugeEnum.class); 1159 result = hugeSet.removeAll(anotherHugeSet); 1160 assertTrue(result); 1161 assertEquals(63, set.size()); 1162 1163 Set<HugeEnumWithInnerClass> hugeSetWithInnerClass = EnumSet 1164 .noneOf(HugeEnumWithInnerClass.class); 1165 hugeSetWithInnerClass.add(HugeEnumWithInnerClass.a); 1166 hugeSetWithInnerClass.add(HugeEnumWithInnerClass.b); 1167 1168 Set<HugeEnumWithInnerClass> anotherHugeSetWithInnerClass = EnumSet 1169 .noneOf(HugeEnumWithInnerClass.class); 1170 anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.c); 1171 anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.d); 1172 result = anotherHugeSetWithInnerClass.removeAll(setWithInnerClass); 1173 assertFalse("Should return false", result); 1174 1175 anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.a); 1176 result = anotherHugeSetWithInnerClass.removeAll(hugeSetWithInnerClass); 1177 assertTrue(result); 1178 assertEquals(2, anotherHugeSetWithInnerClass.size()); 1179 1180 anotherHugeSetWithInnerClass.remove(HugeEnumWithInnerClass.c); 1181 anotherHugeSetWithInnerClass.remove(HugeEnumWithInnerClass.d); 1182 result = anotherHugeSetWithInnerClass.remove(hugeSetWithInnerClass); 1183 assertFalse(result); 1184 1185 rawSet = EnumSet.allOf(HugeEnumWithInnerClass.class); 1186 result = rawSet.removeAll(EnumSet.allOf(HugeEnum.class)); 1187 assertFalse(result); 1188 1189 hugeSetWithInnerClass = EnumSet.allOf(HugeEnumWithInnerClass.class); 1190 anotherHugeSetWithInnerClass = EnumSet.allOf(HugeEnumWithInnerClass.class); 1191 hugeSetWithInnerClass.remove(HugeEnumWithInnerClass.a); 1192 anotherHugeSetWithInnerClass.remove(HugeEnumWithInnerClass.f); 1193 result = hugeSetWithInnerClass.removeAll(anotherHugeSetWithInnerClass); 1194 assertTrue(result); 1195 assertEquals(1, hugeSetWithInnerClass.size()); 1196 1197 result = hugeSetWithInnerClass.contains(HugeEnumWithInnerClass.f); 1198 assertTrue(result); 1199 } 1200 1201 /** 1202 * java.util.EnumSet#retainAll(Collection) 1203 */ 1204 @SuppressWarnings({ "unchecked", "CollectionIncompatibleType" }) 1205 public void test_retainAll_LCollection() { 1206 Set<EnumFoo> set = EnumSet.allOf(EnumFoo.class); 1207 1208 try { 1209 set.retainAll(null); 1210 fail("Should throw NullPointerException"); 1211 } catch (NullPointerException e) { 1212 // expected 1213 } 1214 1215 set.clear(); 1216 try { 1217 set.retainAll(null); 1218 fail("Should throw NullPointerException"); 1219 } catch (NullPointerException e) { 1220 // expected 1221 } 1222 1223 Collection rawCollection = new ArrayList(); 1224 boolean result = set.retainAll(rawCollection); 1225 assertFalse("Should return false", result); 1226 1227 rawCollection.add(EnumFoo.a); 1228 result = set.retainAll(rawCollection); 1229 assertFalse("Should return false", result); 1230 1231 rawCollection.add(EnumWithInnerClass.a); 1232 result = set.retainAll(rawCollection); 1233 assertFalse("Should return false", result); 1234 assertEquals("Size of set should be 0:", 0, set.size()); 1235 1236 rawCollection.remove(EnumFoo.a); 1237 result = set.retainAll(rawCollection); 1238 assertFalse("Should return false", result); 1239 1240 Set<EnumFoo> anotherSet = EnumSet.allOf(EnumFoo.class); 1241 result = set.retainAll(anotherSet); 1242 assertFalse("Should return false", result); 1243 assertEquals("Size of set should be 0", 0, set.size()); 1244 1245 Set<EnumWithInnerClass> setWithInnerClass = EnumSet 1246 .allOf(EnumWithInnerClass.class); 1247 result = set.retainAll(setWithInnerClass); 1248 assertFalse("Should return false", result); 1249 assertEquals("Size of set should be 0", 0, set.size()); 1250 1251 setWithInnerClass = EnumSet.noneOf(EnumWithInnerClass.class); 1252 result = set.retainAll(setWithInnerClass); 1253 assertFalse("Should return false", result); 1254 1255 Set<EmptyEnum> emptySet = EnumSet.allOf(EmptyEnum.class); 1256 result = set.retainAll(emptySet); 1257 assertFalse("Should return false", result); 1258 1259 Set<EnumWithAllInnerClass> setWithAllInnerClass = EnumSet 1260 .allOf(EnumWithAllInnerClass.class); 1261 result = set.retainAll(setWithAllInnerClass); 1262 assertFalse("Should return false", result); 1263 1264 set.add(EnumFoo.a); 1265 result = set.retainAll(setWithInnerClass); 1266 assertTrue("Should return true", result); 1267 assertEquals("Size of set should be 0", 0, set.size()); 1268 1269 setWithInnerClass = EnumSet.allOf(EnumWithInnerClass.class); 1270 setWithInnerClass.remove(EnumWithInnerClass.f); 1271 Set<EnumWithInnerClass> anotherSetWithInnerClass = EnumSet 1272 .noneOf(EnumWithInnerClass.class); 1273 anotherSetWithInnerClass.add(EnumWithInnerClass.e); 1274 anotherSetWithInnerClass.add(EnumWithInnerClass.f); 1275 1276 result = setWithInnerClass.retainAll(anotherSetWithInnerClass); 1277 assertTrue("Should return true", result); 1278 result = setWithInnerClass.contains(EnumWithInnerClass.e); 1279 assertTrue("Should contain EnumWithInnerClass.e", result); 1280 result = setWithInnerClass.contains(EnumWithInnerClass.b); 1281 assertFalse("Should not contain EnumWithInnerClass.b", result); 1282 assertEquals("Size of set should be 1:", 1, setWithInnerClass.size()); 1283 1284 anotherSetWithInnerClass = EnumSet.allOf(EnumWithInnerClass.class); 1285 result = setWithInnerClass.retainAll(anotherSetWithInnerClass); 1286 1287 assertFalse("Return value should be false", result); 1288 1289 rawCollection = new ArrayList(); 1290 rawCollection.add(EnumWithInnerClass.e); 1291 rawCollection.add(EnumWithInnerClass.f); 1292 result = setWithInnerClass.retainAll(rawCollection); 1293 assertFalse("Should return false", result); 1294 1295 set = EnumSet.allOf(EnumFoo.class); 1296 set.remove(EnumFoo.a); 1297 anotherSet = EnumSet.noneOf(EnumFoo.class); 1298 anotherSet.add(EnumFoo.a); 1299 result = set.retainAll(anotherSet); 1300 assertTrue("Should return true", result); 1301 assertEquals("size should be 0", 0, set.size()); 1302 1303 // test enum type with more than 64 elements 1304 Set<HugeEnum> hugeSet = EnumSet.allOf(HugeEnum.class); 1305 1306 try { 1307 hugeSet.retainAll(null); 1308 fail("Should throw NullPointerException"); 1309 } catch (NullPointerException e) { 1310 // expected 1311 } 1312 1313 hugeSet.clear(); 1314 try { 1315 hugeSet.retainAll(null); 1316 fail("Should throw NullPointerException"); 1317 } catch (NullPointerException e) { 1318 // expected 1319 } 1320 1321 rawCollection = new ArrayList(); 1322 result = hugeSet.retainAll(rawCollection); 1323 assertFalse(result); 1324 1325 rawCollection.add(HugeEnum.a); 1326 result = hugeSet.retainAll(rawCollection); 1327 assertFalse(result); 1328 1329 rawCollection.add(HugeEnumWithInnerClass.a); 1330 result = hugeSet.retainAll(rawCollection); 1331 assertFalse(result); 1332 assertEquals(0, set.size()); 1333 1334 rawCollection.remove(HugeEnum.a); 1335 result = set.retainAll(rawCollection); 1336 assertFalse(result); 1337 1338 Set<HugeEnum> anotherHugeSet = EnumSet.allOf(HugeEnum.class); 1339 result = hugeSet.retainAll(anotherHugeSet); 1340 assertFalse(result); 1341 assertEquals(0, hugeSet.size()); 1342 1343 Set<HugeEnumWithInnerClass> hugeSetWithInnerClass = EnumSet 1344 .allOf(HugeEnumWithInnerClass.class); 1345 result = hugeSet.retainAll(hugeSetWithInnerClass); 1346 assertFalse(result); 1347 assertEquals(0, hugeSet.size()); 1348 1349 hugeSetWithInnerClass = EnumSet.noneOf(HugeEnumWithInnerClass.class); 1350 result = hugeSet.retainAll(hugeSetWithInnerClass); 1351 assertFalse(result); 1352 1353 Set<HugeEnumWithInnerClass> hugeSetWithAllInnerClass = EnumSet 1354 .allOf(HugeEnumWithInnerClass.class); 1355 result = hugeSet.retainAll(hugeSetWithAllInnerClass); 1356 assertFalse(result); 1357 1358 hugeSet.add(HugeEnum.a); 1359 result = hugeSet.retainAll(hugeSetWithInnerClass); 1360 assertTrue(result); 1361 assertEquals(0, hugeSet.size()); 1362 1363 hugeSetWithInnerClass = EnumSet.allOf(HugeEnumWithInnerClass.class); 1364 hugeSetWithInnerClass.remove(HugeEnumWithInnerClass.f); 1365 Set<HugeEnumWithInnerClass> anotherHugeSetWithInnerClass = EnumSet 1366 .noneOf(HugeEnumWithInnerClass.class); 1367 anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.e); 1368 anotherHugeSetWithInnerClass.add(HugeEnumWithInnerClass.f); 1369 1370 result = hugeSetWithInnerClass.retainAll(anotherHugeSetWithInnerClass); 1371 assertTrue(result); 1372 result = hugeSetWithInnerClass.contains(HugeEnumWithInnerClass.e); 1373 assertTrue("Should contain HugeEnumWithInnerClass.e", result); 1374 result = hugeSetWithInnerClass.contains(HugeEnumWithInnerClass.b); 1375 assertFalse("Should not contain HugeEnumWithInnerClass.b", result); 1376 assertEquals("Size of hugeSet should be 1:", 1, hugeSetWithInnerClass.size()); 1377 1378 anotherHugeSetWithInnerClass = EnumSet.allOf(HugeEnumWithInnerClass.class); 1379 result = hugeSetWithInnerClass.retainAll(anotherHugeSetWithInnerClass); 1380 1381 assertFalse("Return value should be false", result); 1382 1383 rawCollection = new ArrayList(); 1384 rawCollection.add(HugeEnumWithInnerClass.e); 1385 rawCollection.add(HugeEnumWithInnerClass.f); 1386 result = hugeSetWithInnerClass.retainAll(rawCollection); 1387 assertFalse(result); 1388 1389 hugeSet = EnumSet.allOf(HugeEnum.class); 1390 hugeSet.remove(HugeEnum.a); 1391 anotherHugeSet = EnumSet.noneOf(HugeEnum.class); 1392 anotherHugeSet.add(HugeEnum.a); 1393 result = hugeSet.retainAll(anotherHugeSet); 1394 assertTrue(result); 1395 assertEquals(0, hugeSet.size()); 1396 } 1397 1398 /** 1399 * java.util.EnumSet#iterator() 1400 */ 1401 @SuppressWarnings("CollectionIncompatibleType") 1402 public void test_iterator() { 1403 Set<EnumFoo> set = EnumSet.noneOf(EnumFoo.class); 1404 set.add(EnumFoo.a); 1405 set.add(EnumFoo.b); 1406 1407 Iterator<EnumFoo> iterator = set.iterator(); 1408 Iterator<EnumFoo> anotherIterator = set.iterator(); 1409 assertNotSame("Should not be same", iterator, anotherIterator); 1410 try { 1411 iterator.remove(); 1412 fail("Should throw IllegalStateException"); 1413 } catch (IllegalStateException e) { 1414 // expectedd 1415 } 1416 1417 assertTrue("Should has next element:", iterator.hasNext()); 1418 assertSame("Should be identical", EnumFoo.a, iterator.next()); 1419 iterator.remove(); 1420 assertTrue("Should has next element:", iterator.hasNext()); 1421 assertSame("Should be identical", EnumFoo.b, iterator.next()); 1422 assertFalse("Should not has next element:", iterator.hasNext()); 1423 assertFalse("Should not has next element:", iterator.hasNext()); 1424 1425 assertEquals("Size should be 1:", 1, set.size()); 1426 1427 try { 1428 iterator.next(); 1429 fail("Should throw NoSuchElementException"); 1430 } catch (NoSuchElementException e) { 1431 // expected 1432 } 1433 set = EnumSet.noneOf(EnumFoo.class); 1434 set.add(EnumFoo.a); 1435 iterator = set.iterator(); 1436 assertEquals("Should be equal", EnumFoo.a, iterator.next()); 1437 iterator.remove(); 1438 try { 1439 iterator.remove(); 1440 fail("Should throw IllegalStateException"); 1441 } catch(IllegalStateException e) { 1442 // expected 1443 } 1444 1445 Set<EmptyEnum> emptySet = EnumSet.allOf(EmptyEnum.class); 1446 Iterator<EmptyEnum> emptyIterator = emptySet.iterator(); 1447 try { 1448 emptyIterator.next(); 1449 fail("Should throw NoSuchElementException"); 1450 } catch (NoSuchElementException e) { 1451 // expected 1452 } 1453 1454 Set<EnumWithInnerClass> setWithSubclass = EnumSet 1455 .allOf(EnumWithInnerClass.class); 1456 setWithSubclass.remove(EnumWithInnerClass.e); 1457 Iterator<EnumWithInnerClass> iteratorWithSubclass = setWithSubclass 1458 .iterator(); 1459 assertSame("Should be same", EnumWithInnerClass.a, iteratorWithSubclass.next()); 1460 1461 assertTrue("Should return true", iteratorWithSubclass.hasNext()); 1462 assertSame("Should be same", EnumWithInnerClass.b, iteratorWithSubclass.next()); 1463 1464 setWithSubclass.remove(EnumWithInnerClass.c); 1465 assertTrue("Should return true", iteratorWithSubclass.hasNext()); 1466 assertSame("Should be same", EnumWithInnerClass.c, iteratorWithSubclass.next()); 1467 1468 assertTrue("Should return true", iteratorWithSubclass.hasNext()); 1469 assertSame("Should be same", EnumWithInnerClass.d, iteratorWithSubclass.next()); 1470 1471 setWithSubclass.add(EnumWithInnerClass.e); 1472 assertTrue("Should return true", iteratorWithSubclass.hasNext()); 1473 assertSame("Should be same", EnumWithInnerClass.f, iteratorWithSubclass.next()); 1474 1475 set = EnumSet.noneOf(EnumFoo.class); 1476 iterator = set.iterator(); 1477 try { 1478 iterator.next(); 1479 fail("Should throw NoSuchElementException"); 1480 } catch (NoSuchElementException e) { 1481 // expected 1482 } 1483 1484 set.add(EnumFoo.a); 1485 iterator = set.iterator(); 1486 assertEquals("Should return EnumFoo.a", EnumFoo.a, iterator.next()); 1487 assertEquals("Size of set should be 1", 1, set.size()); 1488 iterator.remove(); 1489 assertEquals("Size of set should be 0", 0, set.size()); 1490 assertFalse("Should return false", set.contains(EnumFoo.a)); 1491 1492 set.add(EnumFoo.a); 1493 set.add(EnumFoo.b); 1494 iterator = set.iterator(); 1495 assertEquals("Should be equals", EnumFoo.a, iterator.next()); 1496 iterator.remove(); 1497 try { 1498 iterator.remove(); 1499 fail("Should throw IllegalStateException"); 1500 } catch(IllegalStateException e) { 1501 // expected 1502 } 1503 1504 assertTrue("Should have next element", iterator.hasNext()); 1505 try { 1506 iterator.remove(); 1507 fail("Should throw IllegalStateException"); 1508 } catch (IllegalStateException e) { 1509 // expected 1510 } 1511 assertEquals("Size of set should be 1", 1, set.size()); 1512 assertTrue("Should have next element", iterator.hasNext()); 1513 assertEquals("Should return EnumFoo.b", EnumFoo.b, iterator.next()); 1514 set.remove(EnumFoo.b); 1515 assertEquals("Size of set should be 0", 0, set.size()); 1516 iterator.remove(); 1517 assertFalse("Should return false", set.contains(EnumFoo.a)); 1518 1519 // RI's bug, EnumFoo.b should not exist at the moment. 1520 if (!disableRIBugs) { 1521 assertFalse("Should return false", set.contains(EnumFoo.b)); 1522 } 1523 1524 // test enum type with more than 64 elements 1525 Set<HugeEnum> hugeSet = EnumSet.noneOf(HugeEnum.class); 1526 hugeSet.add(HugeEnum.a); 1527 hugeSet.add(HugeEnum.b); 1528 1529 Iterator<HugeEnum> hIterator = hugeSet.iterator(); 1530 Iterator<HugeEnum> anotherHugeIterator = hugeSet.iterator(); 1531 assertNotSame(hIterator, anotherHugeIterator); 1532 try { 1533 hIterator.remove(); 1534 fail("Should throw IllegalStateException"); 1535 } catch (IllegalStateException e) { 1536 // expectedd 1537 } 1538 1539 assertTrue(hIterator.hasNext()); 1540 assertSame(HugeEnum.a, hIterator.next()); 1541 hIterator.remove(); 1542 assertTrue(hIterator.hasNext()); 1543 assertSame(HugeEnum.b, hIterator.next()); 1544 assertFalse(hIterator.hasNext()); 1545 assertFalse(hIterator.hasNext()); 1546 1547 assertEquals(1, hugeSet.size()); 1548 1549 try { 1550 hIterator.next(); 1551 fail("Should throw NoSuchElementException"); 1552 } catch (NoSuchElementException e) { 1553 // expected 1554 } 1555 1556 Set<HugeEnumWithInnerClass> hugeSetWithSubclass = EnumSet 1557 .allOf(HugeEnumWithInnerClass.class); 1558 hugeSetWithSubclass.remove(HugeEnumWithInnerClass.e); 1559 Iterator<HugeEnumWithInnerClass> hugeIteratorWithSubclass = hugeSetWithSubclass 1560 .iterator(); 1561 assertSame(HugeEnumWithInnerClass.a, hugeIteratorWithSubclass.next()); 1562 1563 assertTrue(hugeIteratorWithSubclass.hasNext()); 1564 assertSame(HugeEnumWithInnerClass.b, hugeIteratorWithSubclass.next()); 1565 1566 setWithSubclass.remove(HugeEnumWithInnerClass.c); 1567 assertTrue(hugeIteratorWithSubclass.hasNext()); 1568 assertSame(HugeEnumWithInnerClass.c, hugeIteratorWithSubclass.next()); 1569 1570 assertTrue(hugeIteratorWithSubclass.hasNext()); 1571 assertSame(HugeEnumWithInnerClass.d, hugeIteratorWithSubclass.next()); 1572 1573 hugeSetWithSubclass.add(HugeEnumWithInnerClass.e); 1574 assertTrue(hugeIteratorWithSubclass.hasNext()); 1575 assertSame(HugeEnumWithInnerClass.f, hugeIteratorWithSubclass.next()); 1576 1577 hugeSet = EnumSet.noneOf(HugeEnum.class); 1578 hIterator = hugeSet.iterator(); 1579 try { 1580 hIterator.next(); 1581 fail("Should throw NoSuchElementException"); 1582 } catch (NoSuchElementException e) { 1583 // expected 1584 } 1585 1586 hugeSet.add(HugeEnum.a); 1587 hIterator = hugeSet.iterator(); 1588 assertEquals(HugeEnum.a, hIterator.next()); 1589 assertEquals(1, hugeSet.size()); 1590 hIterator.remove(); 1591 assertEquals(0, hugeSet.size()); 1592 assertFalse(hugeSet.contains(HugeEnum.a)); 1593 1594 hugeSet.add(HugeEnum.a); 1595 hugeSet.add(HugeEnum.b); 1596 hIterator = hugeSet.iterator(); 1597 hIterator.next(); 1598 hIterator.remove(); 1599 1600 assertTrue(hIterator.hasNext()); 1601 try { 1602 hIterator.remove(); 1603 fail("Should throw IllegalStateException"); 1604 } catch (IllegalStateException e) { 1605 // expected 1606 } 1607 assertEquals(1, hugeSet.size()); 1608 assertTrue(hIterator.hasNext()); 1609 assertEquals(HugeEnum.b, hIterator.next()); 1610 hugeSet.remove(HugeEnum.b); 1611 assertEquals(0, hugeSet.size()); 1612 hIterator.remove(); 1613 assertFalse(hugeSet.contains(HugeEnum.a)); 1614 // RI's bug, EnumFoo.b should not exist at the moment. 1615 if(!disableRIBugs) { 1616 assertFalse("Should return false", set.contains(EnumFoo.b)); 1617 } 1618 } 1619 1620 /** 1621 * java.util.EnumSet#of(E) 1622 */ 1623 public void test_Of_E() { 1624 EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a); 1625 assertEquals("enumSet should have length 1:", 1, enumSet.size()); 1626 1627 assertTrue("enumSet should contain EnumWithSubclass.a:", 1628 enumSet.contains(EnumWithInnerClass.a)); 1629 1630 try { 1631 EnumSet.of((EnumWithInnerClass) null); 1632 fail("Should throw NullPointerException"); 1633 } catch (NullPointerException npe) { 1634 // expected 1635 } 1636 1637 // test enum type with more than 64 elements 1638 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a); 1639 assertEquals(1, hugeEnumSet.size()); 1640 1641 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a)); 1642 } 1643 1644 /** 1645 * java.util.EnumSet#of(E, E) 1646 */ 1647 public void test_Of_EE() { 1648 EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a, 1649 EnumWithInnerClass.b); 1650 assertEquals("enumSet should have length 2:", 2, enumSet.size()); 1651 1652 assertTrue("enumSet should contain EnumWithSubclass.a:", 1653 enumSet.contains(EnumWithInnerClass.a)); 1654 assertTrue("enumSet should contain EnumWithSubclass.b:", 1655 enumSet.contains(EnumWithInnerClass.b)); 1656 1657 try { 1658 EnumSet.of((EnumWithInnerClass) null, EnumWithInnerClass.a); 1659 fail("Should throw NullPointerException"); 1660 } catch (NullPointerException npe) { 1661 // expected 1662 } 1663 1664 try { 1665 EnumSet.of( EnumWithInnerClass.a, (EnumWithInnerClass) null); 1666 fail("Should throw NullPointerException"); 1667 } catch (NullPointerException npe) { 1668 // expected 1669 } 1670 1671 try { 1672 EnumSet.of( (EnumWithInnerClass) null, (EnumWithInnerClass) null); 1673 fail("Should throw NullPointerException"); 1674 } catch (NullPointerException npe) { 1675 // expected 1676 } 1677 1678 enumSet = EnumSet.of(EnumWithInnerClass.a, EnumWithInnerClass.a); 1679 assertEquals("Size of enumSet should be 1", 1680 1, enumSet.size()); 1681 1682 // test enum type with more than 64 elements 1683 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, 1684 HugeEnumWithInnerClass.b); 1685 assertEquals(2, hugeEnumSet.size()); 1686 1687 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a)); 1688 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.b)); 1689 1690 try { 1691 EnumSet.of((HugeEnumWithInnerClass) null, HugeEnumWithInnerClass.a); 1692 fail("Should throw NullPointerException"); 1693 } catch (NullPointerException npe) { 1694 // expected 1695 } 1696 1697 try { 1698 EnumSet.of( HugeEnumWithInnerClass.a, (HugeEnumWithInnerClass) null); 1699 fail("Should throw NullPointerException"); 1700 } catch (NullPointerException npe) { 1701 // expected 1702 } 1703 1704 try { 1705 EnumSet.of( (HugeEnumWithInnerClass) null, (HugeEnumWithInnerClass) null); 1706 fail("Should throw NullPointerException"); 1707 } catch (NullPointerException npe) { 1708 // expected 1709 } 1710 1711 hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.a); 1712 assertEquals(1, hugeEnumSet.size()); 1713 } 1714 1715 /** 1716 * java.util.EnumSet#of(E, E, E) 1717 */ 1718 public void test_Of_EEE() { 1719 EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a, 1720 EnumWithInnerClass.b, EnumWithInnerClass.c); 1721 assertEquals("Size of enumSet should be 3:", 3, enumSet.size()); 1722 1723 assertTrue( 1724 "enumSet should contain EnumWithSubclass.a:", enumSet.contains(EnumWithInnerClass.a)); 1725 assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.c)); 1726 1727 try { 1728 EnumSet.of((EnumWithInnerClass) null, null, null); 1729 fail("Should throw NullPointerException"); 1730 } catch (NullPointerException npe) { 1731 // expected 1732 } 1733 1734 enumSet = EnumSet.of(EnumWithInnerClass.a, EnumWithInnerClass.b, 1735 EnumWithInnerClass.b); 1736 assertEquals("enumSet should contain 2 elements:", 2, enumSet.size()); 1737 1738 // test enum type with more than 64 elements 1739 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, 1740 HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.c); 1741 assertEquals(3, hugeEnumSet.size()); 1742 1743 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a)); 1744 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.c)); 1745 1746 try { 1747 EnumSet.of((HugeEnumWithInnerClass) null, null, null); 1748 fail("Should throw NullPointerException"); 1749 } catch (NullPointerException npe) { 1750 // expected 1751 } 1752 1753 hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.b, 1754 HugeEnumWithInnerClass.b); 1755 assertEquals(2, hugeEnumSet.size()); 1756 } 1757 1758 /** 1759 * java.util.EnumSet#of(E, E, E, E) 1760 */ 1761 public void test_Of_EEEE() { 1762 EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a, 1763 EnumWithInnerClass.b, EnumWithInnerClass.c, 1764 EnumWithInnerClass.d); 1765 assertEquals("Size of enumSet should be 4", 4, enumSet.size()); 1766 1767 assertTrue( 1768 "enumSet should contain EnumWithSubclass.a:", enumSet.contains(EnumWithInnerClass.a)); 1769 assertTrue("enumSet should contain EnumWithSubclass.d:", enumSet 1770 .contains(EnumWithInnerClass.d)); 1771 1772 try { 1773 EnumSet.of((EnumWithInnerClass) null, null, null, null); 1774 fail("Should throw NullPointerException"); 1775 } catch (NullPointerException npe) { 1776 // expected 1777 } 1778 1779 // test enum type with more than 64 elements 1780 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, 1781 HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.c, 1782 HugeEnumWithInnerClass.d); 1783 assertEquals(4, hugeEnumSet.size()); 1784 1785 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a)); 1786 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.d)); 1787 1788 try { 1789 EnumSet.of((HugeEnumWithInnerClass) null, null, null, null); 1790 fail("Should throw NullPointerException"); 1791 } catch (NullPointerException npe) { 1792 // expected 1793 } 1794 } 1795 1796 /** 1797 * java.util.EnumSet#of(E, E, E, E, E) 1798 */ 1799 public void test_Of_EEEEE() { 1800 EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a, 1801 EnumWithInnerClass.b, EnumWithInnerClass.c, 1802 EnumWithInnerClass.d, EnumWithInnerClass.e); 1803 assertEquals("Size of enumSet should be 5:", 5, enumSet.size()); 1804 1805 assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.a)); 1806 assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.e)); 1807 1808 try { 1809 EnumSet.of((EnumWithInnerClass) null, null, null, null, null); 1810 fail("Should throw NullPointerException"); 1811 } catch (NullPointerException npe) { 1812 // expected 1813 } 1814 1815 // test enum with more than 64 elements 1816 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, 1817 HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.c, 1818 HugeEnumWithInnerClass.d, HugeEnumWithInnerClass.e); 1819 assertEquals(5, hugeEnumSet.size()); 1820 1821 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a)); 1822 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.e)); 1823 1824 try { 1825 EnumSet.of((HugeEnumWithInnerClass) null, null, null, null, null); 1826 fail("Should throw NullPointerException"); 1827 } catch (NullPointerException npe) { 1828 // expected 1829 } 1830 } 1831 1832 /** 1833 * java.util.EnumSet#of(E, E...) 1834 */ 1835 public void test_Of_EEArray() { 1836 EnumWithInnerClass[] enumArray = new EnumWithInnerClass[] { 1837 EnumWithInnerClass.b, EnumWithInnerClass.c }; 1838 EnumSet<EnumWithInnerClass> enumSet = EnumSet.of(EnumWithInnerClass.a, 1839 enumArray); 1840 assertEquals("Should be equal", 3, enumSet.size()); 1841 1842 assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.a)); 1843 assertTrue("Should return true", enumSet.contains(EnumWithInnerClass.c)); 1844 1845 try { 1846 EnumSet.of(EnumWithInnerClass.a, (EnumWithInnerClass[])null); 1847 fail("Should throw NullPointerException"); 1848 } catch (NullPointerException npe) { 1849 // expected 1850 } 1851 1852 EnumFoo[] foos = {EnumFoo.a, EnumFoo.c, EnumFoo.d}; 1853 EnumSet<EnumFoo> set = EnumSet.of(EnumFoo.c, foos); 1854 assertEquals("size of set should be 1", 3, set.size()); 1855 assertTrue("Should contain EnumFoo.a", set.contains(EnumFoo.a)); 1856 assertTrue("Should contain EnumFoo.c", set.contains(EnumFoo.c)); 1857 assertTrue("Should contain EnumFoo.d", set.contains(EnumFoo.d)); 1858 1859 // test enum type with more than 64 elements 1860 HugeEnumWithInnerClass[] hugeEnumArray = new HugeEnumWithInnerClass[] { 1861 HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.c }; 1862 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.of(HugeEnumWithInnerClass.a, 1863 hugeEnumArray); 1864 assertEquals(3, hugeEnumSet.size()); 1865 1866 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.a)); 1867 assertTrue(hugeEnumSet.contains(HugeEnumWithInnerClass.c)); 1868 1869 try { 1870 EnumSet.of(HugeEnumWithInnerClass.a, (HugeEnumWithInnerClass[])null); 1871 fail("Should throw NullPointerException"); 1872 } catch (NullPointerException npe) { 1873 // expected 1874 } 1875 1876 HugeEnumWithInnerClass[] huges = {HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.c, HugeEnumWithInnerClass.d}; 1877 EnumSet<HugeEnumWithInnerClass> hugeSet = EnumSet.of(HugeEnumWithInnerClass.c, huges); 1878 assertEquals(3, hugeSet.size()); 1879 assertTrue(hugeSet.contains(HugeEnumWithInnerClass.a)); 1880 assertTrue(hugeSet.contains(HugeEnumWithInnerClass.c)); 1881 assertTrue(hugeSet.contains(HugeEnumWithInnerClass.d)); 1882 } 1883 1884 /** 1885 * java.util.EnumSet#range(E, E) 1886 */ 1887 public void test_Range_EE() { 1888 try { 1889 EnumSet.range(EnumWithInnerClass.c, null); 1890 fail("Should throw NullPointerException"); 1891 } catch (NullPointerException e) { 1892 // expected 1893 } 1894 1895 try { 1896 EnumSet.range(null, EnumWithInnerClass.c); 1897 fail("Should throw NullPointerException"); 1898 } catch (NullPointerException e) { 1899 // expected 1900 } 1901 1902 try { 1903 EnumSet.range(null, (EnumWithInnerClass) null); 1904 fail("Should throw NullPointerException"); 1905 } catch (NullPointerException e) { 1906 // expected 1907 } 1908 1909 try { 1910 EnumSet.range(EnumWithInnerClass.b, EnumWithInnerClass.a); 1911 fail("Should throw IllegalArgumentException"); 1912 } catch (IllegalArgumentException e) { 1913 // expected 1914 } 1915 1916 EnumSet<EnumWithInnerClass> enumSet = EnumSet.range( 1917 EnumWithInnerClass.a, EnumWithInnerClass.a); 1918 assertEquals("Size of enumSet should be 1", 1, enumSet.size()); 1919 1920 enumSet = EnumSet.range( 1921 EnumWithInnerClass.a, EnumWithInnerClass.c); 1922 assertEquals("Size of enumSet should be 3", 3, enumSet.size()); 1923 1924 // test enum with more than 64 elements 1925 try { 1926 EnumSet.range(HugeEnumWithInnerClass.c, null); 1927 fail("Should throw NullPointerException"); 1928 } catch (NullPointerException e) { 1929 // expected 1930 } 1931 1932 try { 1933 EnumSet.range(null, HugeEnumWithInnerClass.c); 1934 fail("Should throw NullPointerException"); 1935 } catch (NullPointerException e) { 1936 // expected 1937 } 1938 1939 try { 1940 EnumSet.range(null, (HugeEnumWithInnerClass) null); 1941 fail("Should throw NullPointerException"); 1942 } catch (NullPointerException e) { 1943 // expected 1944 } 1945 1946 try { 1947 EnumSet.range(HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.a); 1948 fail("Should throw IllegalArgumentException"); 1949 } catch (IllegalArgumentException e) { 1950 // expected 1951 } 1952 1953 EnumSet<HugeEnumWithInnerClass> hugeEnumSet = EnumSet.range( 1954 HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.a); 1955 assertEquals(1, hugeEnumSet.size()); 1956 1957 hugeEnumSet = EnumSet.range( 1958 HugeEnumWithInnerClass.c, HugeEnumWithInnerClass.aa); 1959 assertEquals(51, hugeEnumSet.size()); 1960 1961 hugeEnumSet = EnumSet.range( 1962 HugeEnumWithInnerClass.a, HugeEnumWithInnerClass.mm); 1963 assertEquals(65, hugeEnumSet.size()); 1964 1965 hugeEnumSet = EnumSet.range( 1966 HugeEnumWithInnerClass.b, HugeEnumWithInnerClass.mm); 1967 assertEquals(64, hugeEnumSet.size()); 1968 } 1969 1970 /** 1971 * java.util.EnumSet#clone() 1972 */ 1973 public void test_Clone() { 1974 EnumSet<EnumFoo> enumSet = EnumSet.allOf(EnumFoo.class); 1975 EnumSet<EnumFoo> clonedEnumSet = enumSet.clone(); 1976 assertEquals(enumSet, clonedEnumSet); 1977 assertNotSame(enumSet, clonedEnumSet); 1978 assertTrue(clonedEnumSet.contains(EnumFoo.a)); 1979 assertTrue(clonedEnumSet.contains(EnumFoo.b)); 1980 assertEquals(64, clonedEnumSet.size()); 1981 1982 // test enum type with more than 64 elements 1983 EnumSet<HugeEnum> hugeEnumSet = EnumSet.allOf(HugeEnum.class); 1984 EnumSet<HugeEnum> hugeClonedEnumSet = hugeEnumSet.clone(); 1985 assertEquals(hugeEnumSet, hugeClonedEnumSet); 1986 assertNotSame(hugeEnumSet, hugeClonedEnumSet); 1987 assertTrue(hugeClonedEnumSet.contains(HugeEnum.a)); 1988 assertTrue(hugeClonedEnumSet.contains(HugeEnum.b)); 1989 assertEquals(65, hugeClonedEnumSet.size()); 1990 1991 hugeClonedEnumSet.remove(HugeEnum.a); 1992 assertEquals(64, hugeClonedEnumSet.size()); 1993 assertFalse(hugeClonedEnumSet.contains(HugeEnum.a)); 1994 assertEquals(65, hugeEnumSet.size()); 1995 assertTrue(hugeEnumSet.contains(HugeEnum.a)); 1996 } 1997 1998 /** 1999 * java.util.EnumSet#Serialization() 2000 */ 2001 public void test_serialization() throws Exception { 2002 EnumSet<EnumFoo> set = EnumSet.allOf(EnumFoo.class); 2003 SerializationTest.verifySelf(set); 2004 } 2005 2006 /** 2007 * serialization/deserialization compatibility with RI. 2008 */ 2009 @SuppressWarnings( { "unchecked", "boxing" }) 2010 public void testSerializationCompatibility() throws Exception { 2011 EnumSet<EnumFoo> set = EnumSet.allOf(EnumFoo.class); 2012 SerializationTest.verifyGolden(this, set); 2013 } 2014 } 2015