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