1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.util; 19 20import dalvik.annotation.TestTargetNew; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetClass; 24 25import java.util.AbstractList; 26import java.util.ArrayList; 27import java.util.Collection; 28import java.util.ConcurrentModificationException; 29import java.util.List; 30import java.util.Vector; 31 32import junit.framework.TestCase; 33 34@TestTargetClass(AbstractList.class) 35public class ConcurrentModTest extends TestCase { 36 37 /* 38 * Test method for 'java.util.AbstractList.subList(int, int)' 39 */ 40 @TestTargetNew( 41 level = TestLevel.COMPLETE, 42 notes = "Doesn't verify IndexOutOfBoundsException.", 43 method = "get", 44 args = {int.class} 45 ) 46 public void testGet() { 47 AbstractList al = new ArrayList(); 48 Double one = new Double(1.0); 49 Double two = new Double(2.0); 50 Double three = new Double(3.0); 51 Double four = new Double(4.0); 52 al.add(one); 53 al.add(two); 54 al.add(three); 55 al.add(four); 56 List sub = al.subList(1, 3); 57 assertEquals(2, sub.size()); 58 // the sub.get(1) is 3.0 59 assertTrue(((Double) sub.get(1)).doubleValue() <= 3.0); 60 assertTrue(((Double) sub.get(1)).doubleValue() > 2.0); 61 62 al.remove(1); // remove the 2.0 63 64 try { 65 // illegal call the subList's method get(int). 66 sub.get(1); 67 fail("It should throws ConcurrentModificationException."); 68 } catch (ConcurrentModificationException e) { 69 return; 70 } 71 72 try { 73 al.get(-1); 74 fail("IndexOutOfBoundsException expected"); 75 } catch (IndexOutOfBoundsException ee) { 76 //expected 77 } 78 79 try { 80 al.get(al.size()+1); 81 fail("IndexOutOfBoundsException expected"); 82 } catch (IndexOutOfBoundsException ee) { 83 //expected 84 } 85 } 86 87 /* 88 * Test method for 'java.util.AbstractList.subList(int, int)' 89 */ 90 @TestTargetNew( 91 level = TestLevel.PARTIAL_COMPLETE, 92 notes = "Doesn't verify UnsupportedOperationException, ClassCastException, IllegalArgumentException, IndexOutOfBoundsException.", 93 method = "set", 94 args = {int.class, java.lang.Object.class} 95 ) 96 public void testSet() { 97 AbstractList al = new ArrayList(); 98 Double one = new Double(1.0); 99 Double two = new Double(2.0); 100 Double three = new Double(3.0); 101 Double four = new Double(4.0); 102 al.add(one); 103 al.add(two); 104 al.add(three); 105 al.add(four); 106 List sub = al.subList(1, 3); 107 assertEquals(2, sub.size()); 108 // the sub.get(1) is 3.0 109 assertTrue(((Double) sub.get(1)).doubleValue() <= 3.0); 110 assertTrue(((Double) sub.get(1)).doubleValue() > 2.0); 111 112 al.remove(1); // remove the 2.0 113 114 try { 115 // illegal call the subList's method set(int,Object). 116 sub.set(1, two); 117 fail("It should throws ConcurrentModificationException."); 118 } catch (ConcurrentModificationException e) { 119 return; 120 } 121 } 122 123 /* 124 * Test method for 'java.util.AbstractList.subList(int, int)' 125 */ 126 @TestTargetNew( 127 level = TestLevel.PARTIAL_COMPLETE, 128 notes = "Doesn't verify UnsupportedOperationException, ClassCastException, IllegalArgumentException, IndexOutOfBoundsException.", 129 method = "add", 130 args = {java.lang.Object.class} 131 ) 132 public void testAdd() { 133 AbstractList al = new ArrayList(); 134 Double one = new Double(1.0); 135 Double two = new Double(2.0); 136 Double three = new Double(3.0); 137 Double four = new Double(4.0); 138 al.add(one); 139 al.add(two); 140 al.add(three); 141 al.add(four); 142 List sub = al.subList(1, 3); 143 assertEquals(2, sub.size()); 144 // the sub.get(1) is 3.0 145 assertTrue(((Double) sub.get(1)).doubleValue() <= 3.0); 146 assertTrue(((Double) sub.get(1)).doubleValue() > 2.0); 147 148 al.remove(1); // remove the 2.0 149 150 try { 151 // illegal call the subList's method Add(int,Object). 152 sub.add(1, two); 153 fail("It should throws ConcurrentModificationException."); 154 } catch (ConcurrentModificationException e) { 155 return; 156 } 157 } 158 159 /* 160 * Test method for 'java.util.AbstractList.subList(int, int)' 161 */ 162 @TestTargetNew( 163 level = TestLevel.COMPLETE, 164 notes = "", 165 method = "remove", 166 args = {int.class} 167 ) 168 public void testRemove() { 169 AbstractList al = new ArrayList(); 170 Double one = new Double(1.0); 171 Double two = new Double(2.0); 172 Double three = new Double(3.0); 173 Double four = new Double(4.0); 174 al.add(one); 175 al.add(two); 176 al.add(three); 177 al.add(four); 178 List sub = al.subList(1, 3); 179 assertEquals(2, sub.size()); 180 // the sub.get(1) is 3.0 181 assertTrue(((Double) sub.get(1)).doubleValue() <= 3.0); 182 assertTrue(((Double) sub.get(1)).doubleValue() > 2.0); 183 184 al.remove(1); // remove the 2.0 185 186 try { 187 // illegal call the subList's method remove(int). 188 sub.remove(1); 189 fail("It should throws ConcurrentModificationException."); 190 } catch (ConcurrentModificationException e) { 191 return; 192 } 193 194 try { 195 sub.remove(-1); 196 fail("IndexOutOfBoundsException expected"); 197 } catch (IndexOutOfBoundsException ee) { 198 //expected 199 } 200 201 try { 202 sub.remove(sub.size() + 1); 203 fail("IndexOutOfBoundsException expected"); 204 } catch (IndexOutOfBoundsException ee) { 205 //expected 206 } 207 208 al = new AbstractList() { 209 210 @Override 211 public Object get(int index) { 212 // TODO Auto-generated method stub 213 return null; 214 } 215 216 @Override 217 public int size() { 218 // TODO Auto-generated method stub 219 return 0; 220 } 221 }; 222 223 try { 224 al.remove(1); 225 fail("UnsupportedOperationException expected"); 226 } catch (UnsupportedOperationException ee) { 227 //expected 228 } 229 } 230 231 /* 232 * Test method for 'java.util.AbstractList.subList(int, int)' 233 */ 234 @TestTargetNew( 235 level = TestLevel.PARTIAL_COMPLETE, 236 notes = "Doesn't verify UnsupportedOperationException, ClassCastException, IllegalArgumentException, NullPointerException.", 237 method = "addAll", 238 args = {int.class, java.util.Collection.class} 239 ) 240 public void testAddAll() { 241 AbstractList al = new ArrayList(); 242 Double one = new Double(1.0); 243 Double two = new Double(2.0); 244 Double three = new Double(3.0); 245 Double four = new Double(4.0); 246 al.add(one); 247 al.add(two); 248 al.add(three); 249 al.add(four); 250 List sub = al.subList(1, 3); 251 assertEquals(2, sub.size()); 252 // the sub.get(1) is 3.0 253 assertTrue(((Double) sub.get(1)).doubleValue() <= 3.0); 254 assertTrue(((Double) sub.get(1)).doubleValue() > 2.0); 255 256 al.remove(1); // remove the 2.0 257 258 try { 259 // illegal call the subList's method addAll(int,Collection). 260 Collection c = new Vector(); 261 Double five = new Double(5.0); 262 c.add(five); 263 sub.addAll(1, c); 264 fail("It should throws ConcurrentModificationException."); 265 } catch (ConcurrentModificationException e) { 266 return; 267 } 268 } 269 270 @TestTargetNew( 271 level = TestLevel.COMPLETE, 272 notes = "Class is abstract - test testing simplest implementation.", 273 method = "add", 274 args = {java.lang.Object.class} 275 ) 276 public void test_addLjava_lang_Object() { 277 AbstractList abstr = new AbstractList() { 278 279 @Override 280 public Object get(int arg0) { 281 return null; 282 } 283 284 @Override 285 public int size() { 286 return 0; 287 } 288 289 }; 290 291 try { 292 abstr.add(null); 293 fail("UnsupportedOperationException expected"); 294 } catch (UnsupportedOperationException e) { 295 //ecpected 296 } 297 abstr = new AbstractList<Double>() { 298 @Override 299 public boolean add(Double value) { 300 return true; 301 } 302 303 @Override 304 public Double get(int index) { 305 return null; 306 } 307 308 @Override 309 public int size() { 310 return 0; 311 } 312 }; 313 314 try { 315 abstr.add(1); 316 fail("ClassCastException expected"); 317 } catch (ClassCastException ee) { 318 //expected 319 } 320 321 abstr = new AbstractList<Integer>() { 322 final int forbiddenValue = 33; 323 @Override 324 public boolean add(Integer value) { 325 if (value == forbiddenValue) { 326 throw new IllegalArgumentException(); 327 } 328 return true; 329 } 330 331 @Override 332 public Integer get(int index) { 333 return null; 334 } 335 336 @Override 337 public int size() { 338 return 0; 339 } 340 }; 341 342 abstr.add(1); 343 try { 344 abstr.add(33); 345 fail("IllegalArgumentException expected"); 346 } catch (IllegalArgumentException ee) { 347 //expected 348 } 349 } 350 351 @TestTargets({ 352 @TestTargetNew( 353 level = TestLevel.COMPLETE, 354 notes = "", 355 method = "add", 356 args = {int.class, java.lang.Object.class} 357 ), 358 @TestTargetNew( 359 level = TestLevel.COMPLETE, 360 notes = "", 361 method = "AbstractList", 362 args = {} 363 ) 364 }) 365 public void test_addILjava_lang_Object() { 366 AbstractList abstr = new AbstractList() { 367 368 @Override 369 public Object get(int arg0) { 370 return null; 371 } 372 373 @Override 374 public int size() { 375 return 0; 376 } 377 378 }; 379 380 try { 381 abstr.add(1, null); 382 fail("UnsupportedOperationException expected"); 383 } catch (UnsupportedOperationException e) { 384 //ecpected 385 } 386 abstr = new AbstractList<Double>() { 387 @Override 388 public void add(int index, Double value) { 389 } 390 391 @Override 392 public Double get(int index) { 393 return null; 394 } 395 396 @Override 397 public int size() { 398 return 0; 399 } 400 }; 401 402 try { 403 abstr.add(1, 1); 404 fail("ClassCastException expected"); 405 } catch (ClassCastException ee) { 406 //expected 407 } 408 409 abstr = new AbstractList<Integer>() { 410 final int forbiddenValue = 33; 411 @Override 412 public void add(int index, Integer value) { 413 if (value == forbiddenValue) { 414 throw new IllegalArgumentException(); 415 } 416 } 417 418 @Override 419 public Integer get(int index) { 420 return null; 421 } 422 423 @Override 424 public int size() { 425 return 0; 426 } 427 }; 428 429 abstr.add(1, 1); 430 try { 431 abstr.add(1, 33); 432 fail("IllegalArgumentException expected"); 433 } catch (IllegalArgumentException ee) { 434 //expected 435 } 436 437 abstr = new ArrayList(); 438 439 abstr.add(0, "element"); 440 abstr.add(1, null); 441 abstr.add(2, 1); 442 abstr.add(3, new Double(33)); 443 444 try { 445 abstr.add(-3, new Double(33)); 446 fail("IndexOutOfBoundsException expected"); 447 } catch (IndexOutOfBoundsException ee) { 448 //expected 449 } 450 451 try { 452 abstr.add(abstr.size() + 1, new Double(33)); 453 fail("IndexOutOfBoundsException expected"); 454 } catch (IndexOutOfBoundsException ee) { 455 //expected 456 } 457 } 458 459 @TestTargetNew( 460 level = TestLevel.COMPLETE, 461 notes = "", 462 method = "addAll", 463 args = {int.class, java.util.Collection.class} 464 ) 465 public void test_addAllILjava_util_Collection() { 466 Collection c = new Vector(); 467 c.add(new Double(33)); 468 c.add(10); 469 c.add("String"); 470 471 AbstractList abstr = new AbstractList() { 472 @Override 473 public Object get(int arg0) { 474 return null; 475 } 476 477 @Override 478 public int size() { 479 return 0; 480 } 481 }; 482 483 try { 484 abstr.addAll(0, null); 485 fail("NullPointerException expected"); 486 } catch (NullPointerException ee) { 487 //expected 488 } 489 490 try { 491 abstr.addAll(0, c); 492 fail("UnsuportedOperationException expected"); 493 } catch (UnsupportedOperationException ee) { 494 //expected 495 } 496 497 abstr = new AbstractList<Double>() { 498 @Override 499 public void add(int index, Double value) { 500 } 501 502 @Override 503 public Double get(int arg0) { 504 return null; 505 } 506 507 @Override 508 public int size() { 509 return 0; 510 } 511 }; 512 513 try { 514 abstr.addAll(0, c); 515 fail("ClassCastException expected"); 516 } catch (ClassCastException ee) { 517 //expectd 518 } 519 520 abstr = new AbstractList<Integer>() { 521 final int forbiddenValue = 33; 522 @Override 523 public void add(int index, Integer value) { 524 if (value == forbiddenValue) { 525 throw new IllegalArgumentException(); 526 } 527 } 528 529 @Override 530 public Integer get(int arg0) { 531 return null; 532 } 533 534 @Override 535 public int size() { 536 return 0; 537 } 538 }; 539 c.clear(); 540 c.add(new Integer(1)); 541 c.add(new Integer(2)); 542 c.add(new Integer(3)); 543 c.add(new Integer(4)); 544 c.add(new Integer(5)); 545 546 abstr.addAll(0, c); 547 548 c.add(new Integer(33)); 549 550 try { 551 abstr.addAll(0, c); 552 fail("IllegalArgumentException expected"); 553 } catch (IllegalArgumentException ee) { 554 //expected 555 } 556 abstr = new ArrayList(); 557 abstr.addAll(0, c); 558 559 try { 560 abstr.addAll(-1, c); 561 fail("IndexOutOfBoundsException expected"); 562 } catch (IndexOutOfBoundsException ee) { 563 //expected 564 } 565 566 try { 567 abstr.addAll(abstr.size() + 1, c); 568 fail("IndexOutOfBoundsException expected"); 569 } catch (IndexOutOfBoundsException ee) { 570 //expected 571 } 572 } 573 574 @TestTargetNew( 575 level = TestLevel.COMPLETE, 576 notes = "", 577 method = "clear", 578 args = {} 579 ) 580 public void test_clear() { 581 AbstractList abstr = new ArrayList(); 582 583 assertEquals(0, abstr.size()); 584 abstr.add("String"); 585 abstr.add("1"); 586 abstr.add(2); 587 abstr.add(new Double(3)); 588 assertEquals(4, abstr.size()); 589 abstr.clear(); 590 assertEquals(0, abstr.size()); 591 } 592 593 @TestTargetNew( 594 level = TestLevel.COMPLETE, 595 notes = "", 596 method = "equals", 597 args = {java.lang.Object.class} 598 ) 599 public void test_equalsLjava_lang_Object() { 600 Collection c = new Vector(); 601 c.add(new Double(33)); 602 c.add(10); 603 c.add("String"); 604 605 AbstractList abstr = new ArrayList(); 606 AbstractList abstr1 = new ArrayList(); 607 608 assertFalse(abstr.equals(this)); 609 abstr.add(new Double(33)); 610 abstr.add(10); 611 abstr.add("String"); 612 assertTrue(abstr.equals(c)); 613 abstr1.addAll(c); 614 assertTrue(abstr.equals(abstr1)); 615 } 616 617 @TestTargetNew( 618 level = TestLevel.COMPLETE, 619 notes = "", 620 method = "set", 621 args = {int.class, java.lang.Object.class} 622 ) 623 public void test_setILjava_lang_Object() { 624 Collection c = new Vector(); 625 c.add(new Double(33)); 626 c.add(10); 627 c.add("String"); 628 629 AbstractList abstr1 = new ArrayList(); 630 AbstractList abstr2 = new ArrayList(); 631 632 abstr1.addAll(c); 633 abstr2.addAll(c); 634 assertTrue(abstr1.equals(abstr2)); 635 abstr1.set(1, 1); 636 assertFalse(abstr1.equals(abstr2)); 637 638 try { 639 abstr1.set(abstr1.size() + 1, 1); 640 fail("IndexOutOfBoundsException expected"); 641 } catch (IndexOutOfBoundsException ee) { 642 //expected 643 } 644 645 try { 646 abstr1.set(-1, 1); 647 fail("IndexOutOfBoundsException expected"); 648 } catch (IndexOutOfBoundsException ee) { 649 //expected 650 } 651 652 AbstractList abstr = new AbstractList() { 653 654 @Override 655 public Object get(int index) { 656 return null; 657 } 658 659 @Override 660 public int size() { 661 return 0; 662 } 663 664 }; 665 666 try { 667 abstr.set(0, null); 668 fail("UnsupportedOperationException expected"); 669 } catch (UnsupportedOperationException ee) { 670 //expected 671 } 672 673 abstr = new AbstractList<Double>() { 674 @Override 675 public Double set(int index, Double value) { 676 return value; 677 } 678 679 @Override 680 public Double get(int index) { 681 return null; 682 } 683 684 @Override 685 public int size() { 686 return 0; 687 } 688 }; 689 690 try { 691 abstr.set(0, 1); 692 fail("ClassCastException expected"); 693 } catch (ClassCastException ee) { 694 //expected 695 } 696 697 abstr = new AbstractList<Integer>() { 698 final int forbiddenValue = 33; 699 @Override 700 public Integer set(int index, Integer value) { 701 if (value == forbiddenValue) { 702 throw new IllegalArgumentException(); 703 } 704 return value; 705 } 706 707 @Override 708 public Integer get(int index) { 709 return null; 710 } 711 712 @Override 713 public int size() { 714 return 0; 715 } 716 }; 717 718 try { 719 abstr.set(0, 33); 720 fail("IllegalArgumentException expected"); 721 } catch (IllegalArgumentException ee) { 722 //expected 723 } 724 } 725 class Mock_ArrayList extends ArrayList { 726 @Override 727 public void removeRange(int fromIndex, int toIndex) { 728 super.removeRange(fromIndex, toIndex); 729 } 730 } 731 732 @TestTargetNew( 733 level = TestLevel.COMPLETE, 734 notes = "", 735 method = "removeRange", 736 args = {int.class, int.class} 737 ) 738 public void test_removeRangeII() { 739 Mock_ArrayList al1 = new Mock_ArrayList(); 740 al1.add(1); 741 al1.add(2); 742 al1.add(3); 743 al1.add(4); 744 al1.add(5); 745 Mock_ArrayList al2 = new Mock_ArrayList(); 746 747 al2.add(1); 748 al2.add(5); 749 assertNotSame(al1,al2); 750 al1.removeRange(1, 4); 751 assertEquals(al1,al2); 752 } 753} 754