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 18 package org.apache.harmony.tests.java.lang.reflect; 19 20 import java.lang.reflect.Array; 21 22 public class ArrayTest extends junit.framework.TestCase { 23 24 /** 25 * java.lang.reflect.Array#get(java.lang.Object, int) 26 */ 27 public void test_getLjava_lang_ObjectI() { 28 // Test for method java.lang.Object 29 // java.lang.reflect.Array.get(java.lang.Object, int) 30 31 int[] x = { 1 }; 32 Object ret = null; 33 boolean thrown = false; 34 try { 35 ret = Array.get(x, 0); 36 } catch (Exception e) { 37 fail("Exception during get test : " + e.getMessage()); 38 } 39 assertEquals("Get returned incorrect value", 40 1, ((Integer) ret).intValue()); 41 try { 42 ret = Array.get(new Object(), 0); 43 } catch (IllegalArgumentException e) { 44 // Correct behaviour 45 thrown = true; 46 } 47 if (!thrown) { 48 fail("Passing non-array failed to throw exception"); 49 } 50 thrown = false; 51 try { 52 ret = Array.get(x, 4); 53 } catch (ArrayIndexOutOfBoundsException e) { 54 // Correct behaviour 55 thrown = true; 56 } 57 if (!thrown) { 58 fail("Invalid index failed to throw exception"); 59 } 60 61 //same test with non primitive component type 62 Integer[] y = new Integer[]{ 1 }; 63 ret = null; 64 thrown = false; 65 try { 66 ret = Array.get(y, 0); 67 } catch (Exception e) { 68 fail("Exception during get test : " + e.getMessage()); 69 } 70 assertEquals("Get returned incorrect value", 71 1, ((Integer) ret).intValue()); 72 try { 73 ret = Array.get(new Object(), 0); 74 } catch (IllegalArgumentException e) { 75 // Correct behaviour 76 thrown = true; 77 } 78 if (!thrown) { 79 fail("Passing non-array failed to throw exception"); 80 } 81 thrown = false; 82 try { 83 ret = Array.get(y, 4); 84 } catch (ArrayIndexOutOfBoundsException e) { 85 // Correct behaviour 86 thrown = true; 87 } 88 if (!thrown) { 89 fail("Invalid index failed to throw exception"); 90 } 91 } 92 93 /** 94 * java.lang.reflect.Array#getBoolean(java.lang.Object, int) 95 */ 96 public void test_getBooleanLjava_lang_ObjectI() { 97 // Test for method boolean 98 // java.lang.reflect.Array.getBoolean(java.lang.Object, int) 99 boolean[] x = { true }; 100 boolean ret = false; 101 boolean thrown = false; 102 try { 103 ret = Array.getBoolean(x, 0); 104 } catch (Exception e) { 105 fail("Exception during get test : " + e.getMessage()); 106 } 107 assertTrue("Get returned incorrect value", ret); 108 try { 109 ret = Array.getBoolean(new Object(), 0); 110 } catch (IllegalArgumentException e) { 111 // Correct behaviour 112 thrown = true; 113 } 114 if (!thrown) { 115 fail("Passing non-array failed to throw exception"); 116 } 117 thrown = false; 118 try { 119 ret = Array.getBoolean(x, 4); 120 } catch (ArrayIndexOutOfBoundsException e) { 121 // Correct behaviour 122 thrown = true; 123 } 124 if (!thrown) { 125 fail("Invalid index failed to throw exception"); 126 } 127 thrown = false; 128 try { 129 ret = Array.getBoolean(null, 0); 130 } catch (NullPointerException e) { 131 // Correct behaviour 132 thrown = true; 133 } 134 if (!thrown) { 135 fail("Null argument failed to throw NPE"); 136 } 137 } 138 139 /** 140 * java.lang.reflect.Array#getByte(java.lang.Object, int) 141 */ 142 public void test_getByteLjava_lang_ObjectI() { 143 // Test for method byte 144 // java.lang.reflect.Array.getByte(java.lang.Object, int) 145 byte[] x = { 1 }; 146 byte ret = 0; 147 boolean thrown = false; 148 try { 149 ret = Array.getByte(x, 0); 150 } catch (Exception e) { 151 fail("Exception during get test : " + e.getMessage()); 152 } 153 assertEquals("Get returned incorrect value", 1, ret); 154 try { 155 ret = Array.getByte(new Object(), 0); 156 } catch (IllegalArgumentException e) { 157 // Correct behaviour 158 thrown = true; 159 } 160 if (!thrown) { 161 fail("Passing non-array failed to throw exception"); 162 } 163 thrown = false; 164 try { 165 ret = Array.getByte(x, 4); 166 } catch (ArrayIndexOutOfBoundsException e) { 167 // Correct behaviour 168 thrown = true; 169 } 170 if (!thrown) { 171 fail("Invalid index failed to throw exception"); 172 } 173 thrown = false; 174 try { 175 ret = Array.getByte(null, 0); 176 } catch (NullPointerException e) { 177 // Correct behaviour 178 thrown = true; 179 } 180 if (!thrown) { 181 fail("Null argument failed to throw NPE"); 182 } 183 } 184 185 /** 186 * java.lang.reflect.Array#getChar(java.lang.Object, int) 187 */ 188 public void test_getCharLjava_lang_ObjectI() { 189 // Test for method char 190 // java.lang.reflect.Array.getChar(java.lang.Object, int) 191 char[] x = { 1 }; 192 char ret = 0; 193 boolean thrown = false; 194 try { 195 ret = Array.getChar(x, 0); 196 } catch (Exception e) { 197 fail("Exception during get test : " + e.getMessage()); 198 } 199 assertEquals("Get returned incorrect value", 1, ret); 200 try { 201 ret = Array.getChar(new Object(), 0); 202 } catch (IllegalArgumentException e) { 203 // Correct behaviour 204 thrown = true; 205 } 206 if (!thrown) { 207 fail("Passing non-array failed to throw exception"); 208 } 209 thrown = false; 210 try { 211 ret = Array.getChar(x, 4); 212 } catch (ArrayIndexOutOfBoundsException e) { 213 // Correct behaviour 214 thrown = true; 215 } 216 if (!thrown) { 217 fail("Invalid index failed to throw exception"); 218 } 219 thrown = false; 220 try { 221 ret = Array.getChar(null, 0); 222 } catch (NullPointerException e) { 223 // Correct behaviour 224 thrown = true; 225 } 226 if (!thrown) { 227 fail("Null argument failed to throw NPE"); 228 } 229 } 230 231 /** 232 * java.lang.reflect.Array#getDouble(java.lang.Object, int) 233 */ 234 public void test_getDoubleLjava_lang_ObjectI() { 235 // Test for method double 236 // java.lang.reflect.Array.getDouble(java.lang.Object, int) 237 double[] x = { 1 }; 238 double ret = 0; 239 boolean thrown = false; 240 try { 241 ret = Array.getDouble(x, 0); 242 } catch (Exception e) { 243 fail("Exception during get test : " + e.getMessage()); 244 } 245 assertEquals("Get returned incorrect value", 1, ret, 0.0); 246 try { 247 ret = Array.getDouble(new Object(), 0); 248 } catch (IllegalArgumentException e) { 249 // Correct behaviour 250 thrown = true; 251 } 252 253 if (!thrown) { 254 fail("Passing non-array failed to throw exception"); 255 } 256 thrown = false; 257 try { 258 ret = Array.getDouble(x, 4); 259 } catch (ArrayIndexOutOfBoundsException e) { 260 // Correct behaviour 261 thrown = true; 262 } 263 if (!thrown) { 264 fail("Invalid index failed to throw exception"); 265 } 266 thrown = false; 267 try { 268 ret = Array.getDouble(null, 0); 269 } catch (NullPointerException e) { 270 // Correct behaviour 271 thrown = true; 272 } 273 if (!thrown) { 274 fail("Null argument failed to throw NPE"); 275 } 276 } 277 278 /** 279 * java.lang.reflect.Array#getFloat(java.lang.Object, int) 280 */ 281 public void test_getFloatLjava_lang_ObjectI() { 282 // Test for method float 283 // java.lang.reflect.Array.getFloat(java.lang.Object, int) 284 float[] x = { 1 }; 285 float ret = 0; 286 boolean thrown = false; 287 try { 288 ret = Array.getFloat(x, 0); 289 } catch (Exception e) { 290 fail("Exception during get test : " + e.getMessage()); 291 } 292 assertEquals("Get returned incorrect value", 1, ret, 0.0); 293 try { 294 ret = Array.getFloat(new Object(), 0); 295 } catch (IllegalArgumentException e) { 296 // Correct behaviour 297 thrown = true; 298 } 299 if (!thrown) { 300 fail("Passing non-array failed to throw exception"); 301 } 302 thrown = false; 303 try { 304 ret = Array.getFloat(x, 4); 305 } catch (ArrayIndexOutOfBoundsException e) { 306 // Correct behaviour 307 thrown = true; 308 } 309 if (!thrown) { 310 fail("Invalid index failed to throw exception"); 311 } 312 thrown = false; 313 try { 314 ret = Array.getFloat(null, 0); 315 } catch (NullPointerException e) { 316 // Correct behaviour 317 thrown = true; 318 } 319 if (!thrown) { 320 fail("Null argument failed to throw NPE"); 321 } 322 } 323 324 /** 325 * java.lang.reflect.Array#getInt(java.lang.Object, int) 326 */ 327 public void test_getIntLjava_lang_ObjectI() { 328 // Test for method int java.lang.reflect.Array.getInt(java.lang.Object, 329 // int) 330 int[] x = { 1 }; 331 int ret = 0; 332 boolean thrown = false; 333 try { 334 ret = Array.getInt(x, 0); 335 } catch (Exception e) { 336 fail("Exception during get test : " + e.getMessage()); 337 } 338 assertEquals("Get returned incorrect value", 1, ret); 339 try { 340 ret = Array.getInt(new Object(), 0); 341 } catch (IllegalArgumentException e) { 342 // Correct behaviour 343 thrown = true; 344 } 345 if (!thrown) { 346 fail("Passing non-array failed to throw exception"); 347 } 348 thrown = false; 349 try { 350 ret = Array.getInt(x, 4); 351 } catch (ArrayIndexOutOfBoundsException e) { 352 // Correct behaviour 353 thrown = true; 354 } 355 if (!thrown) { 356 fail("Invalid index failed to throw exception"); 357 } 358 thrown = false; 359 try { 360 ret = Array.getInt(null, 0); 361 } catch (NullPointerException e) { 362 // Correct behaviour 363 thrown = true; 364 } 365 if (!thrown) { 366 fail("Null argument failed to throw NPE"); 367 } 368 } 369 370 /** 371 * java.lang.reflect.Array#getLength(java.lang.Object) 372 */ 373 public void test_getLengthLjava_lang_Object() { 374 // Test for method int 375 // java.lang.reflect.Array.getLength(java.lang.Object) 376 long[] x = { 1 }; 377 378 assertEquals("Returned incorrect length", 1, Array.getLength(x)); 379 assertEquals("Returned incorrect length", 10000, Array 380 .getLength(new Object[10000])); 381 try { 382 Array.getLength(new Object()); 383 } catch (IllegalArgumentException e) { 384 // Correct 385 return; 386 } 387 fail("Failed to throw exception when passed non-array"); 388 } 389 390 /** 391 * java.lang.reflect.Array#getLong(java.lang.Object, int) 392 */ 393 public void test_getLongLjava_lang_ObjectI() { 394 // Test for method long 395 // java.lang.reflect.Array.getLong(java.lang.Object, int) 396 long[] x = { 1 }; 397 long ret = 0; 398 boolean thrown = false; 399 try { 400 ret = Array.getLong(x, 0); 401 } catch (Exception e) { 402 fail("Exception during get test : " + e.getMessage()); 403 } 404 assertEquals("Get returned incorrect value", 1, ret); 405 try { 406 ret = Array.getLong(new Object(), 0); 407 } catch (IllegalArgumentException e) { 408 // Correct behaviour 409 thrown = true; 410 } 411 if (!thrown) { 412 fail("Passing non-array failed to throw exception"); 413 } 414 thrown = false; 415 try { 416 ret = Array.getLong(x, 4); 417 } catch (ArrayIndexOutOfBoundsException e) { 418 // Correct behaviour 419 thrown = true; 420 } 421 if (!thrown) { 422 fail("Invalid index failed to throw exception"); 423 } 424 thrown = false; 425 try { 426 ret = Array.getLong(null, 0); 427 } catch (NullPointerException e) { 428 // Correct behaviour 429 thrown = true; 430 } 431 if (!thrown) { 432 fail("Null argument failed to throw NPE"); 433 } 434 } 435 436 /** 437 * java.lang.reflect.Array#getShort(java.lang.Object, int) 438 */ 439 public void test_getShortLjava_lang_ObjectI() { 440 // Test for method short 441 // java.lang.reflect.Array.getShort(java.lang.Object, int) 442 short[] x = { 1 }; 443 short ret = 0; 444 boolean thrown = false; 445 try { 446 ret = Array.getShort(x, 0); 447 } catch (Exception e) { 448 fail("Exception during get test : " + e.getMessage()); 449 } 450 assertEquals("Get returned incorrect value", 1, ret); 451 try { 452 ret = Array.getShort(new Object(), 0); 453 } catch (IllegalArgumentException e) { 454 // Correct behaviour 455 thrown = true; 456 } 457 if (!thrown) { 458 fail("Passing non-array failed to throw exception"); 459 } 460 thrown = false; 461 try { 462 ret = Array.getShort(x, 4); 463 } catch (ArrayIndexOutOfBoundsException e) { 464 // Correct behaviour 465 thrown = true; 466 } 467 if (!thrown) { 468 fail("Invalid index failed to throw exception"); 469 } 470 thrown = false; 471 try { 472 ret = Array.getShort(null, 0); 473 } catch (NullPointerException e) { 474 // Correct behaviour 475 thrown = true; 476 } 477 if (!thrown) { 478 fail("Null argument failed to throw NPE"); 479 } 480 } 481 482 /** 483 * java.lang.reflect.Array#newInstance(java.lang.Class, int[]) 484 */ 485 public void test_newInstanceLjava_lang_Class$I() { 486 // Test for method java.lang.Object 487 // java.lang.reflect.Array.newInstance(java.lang.Class, int []) 488 int[][] x; 489 int[] y = { 2 }; 490 491 x = (int[][]) Array.newInstance(int[].class, y); 492 assertEquals("Failed to instantiate array properly", 2, x.length); 493 494 boolean thrown = false; 495 try { 496 x = (int[][]) Array.newInstance(null, y); 497 } catch (NullPointerException e) { 498 // Correct behaviour 499 thrown = true; 500 } 501 if (!thrown) { 502 fail("Null argument failed to throw NPE"); 503 } 504 505 thrown = false; 506 try { 507 Array.newInstance(int[].class, new int[]{1,-1}); 508 } catch (NegativeArraySizeException e) { 509 // Correct behaviour 510 thrown = true; 511 } 512 if (!thrown) { 513 fail("Negative array size failed to throw NegativeArraySizeException"); 514 } 515 516 thrown = false; 517 try { 518 Array.newInstance(int[].class, new int[]{}); 519 } catch (IllegalArgumentException e) { 520 // Correct behaviour 521 thrown = true; 522 } 523 if (!thrown) { 524 fail("Zero array size failed to throw IllegalArgumentException"); 525 } 526 } 527 528 /** 529 * java.lang.reflect.Array#newInstance(java.lang.Class, int) 530 */ 531 public void test_newInstanceLjava_lang_ClassI() { 532 // Test for method java.lang.Object 533 // java.lang.reflect.Array.newInstance(java.lang.Class, int) 534 int[] x; 535 536 x = (int[]) Array.newInstance(int.class, 100); 537 assertEquals("Failed to instantiate array properly", 100, x.length); 538 539 boolean thrown = false; 540 try { 541 Array.newInstance(null, 100); 542 } catch (NullPointerException e) { 543 // Correct behaviour 544 thrown = true; 545 } 546 if (!thrown) { 547 fail("Null argument failed to throw NPE"); 548 } 549 550 thrown = false; 551 try { 552 Array.newInstance(int[].class, -1); 553 } catch (NegativeArraySizeException e) { 554 // Correct behaviour 555 thrown = true; 556 } 557 if (!thrown) { 558 fail("Negative array size failed to throw NegativeArraySizeException"); 559 } 560 } 561 562 /** 563 * java.lang.reflect.Array#set(java.lang.Object, int, 564 * java.lang.Object) 565 */ 566 public void test_setLjava_lang_ObjectILjava_lang_Object() { 567 // Test for method void java.lang.reflect.Array.set(java.lang.Object, 568 // int, java.lang.Object) 569 int[] x = { 0 }; 570 boolean thrown = false; 571 try { 572 Array.set(x, 0, new Integer(1)); 573 } catch (Exception e) { 574 fail("Exception during get test : " + e.getMessage()); 575 } 576 assertEquals("Get returned incorrect value", 1, ((Integer) Array.get(x, 0)) 577 .intValue()); 578 try { 579 Array.set(new Object(), 0, new Object()); 580 } catch (IllegalArgumentException e) { 581 // Correct behaviour 582 thrown = true; 583 } 584 if (!thrown) { 585 fail("Passing non-array failed to throw exception"); 586 } 587 thrown = false; 588 try { 589 Array.set(x, 4, new Integer(1)); 590 } catch (ArrayIndexOutOfBoundsException e) { 591 // Correct behaviour 592 thrown = true; 593 } 594 if (!thrown) { 595 fail("Invalid index failed to throw exception"); 596 } 597 598 // trying to put null in a primitive array causes 599 // a IllegalArgumentException in 5.0 600 boolean exception = false; 601 try { 602 Array.set(new int[1], 0, null); 603 } catch (IllegalArgumentException e) { 604 exception = true; 605 } 606 assertTrue("expected exception not thrown", exception); 607 608 thrown = false; 609 try { 610 Array.set(null, 0, 2); 611 } catch (NullPointerException e) { 612 // Correct behaviour 613 thrown = true; 614 } 615 if (!thrown) { 616 fail("Null argument failed to throw NPE"); 617 } 618 } 619 620 /** 621 * java.lang.reflect.Array#setBoolean(java.lang.Object, int, boolean) 622 */ 623 public void test_setBooleanLjava_lang_ObjectIZ() { 624 // Test for method void 625 // java.lang.reflect.Array.setBoolean(java.lang.Object, int, boolean) 626 boolean[] x = { false }; 627 boolean thrown = false; 628 try { 629 Array.setBoolean(x, 0, true); 630 } catch (Exception e) { 631 fail("Exception during get test : " + e.getMessage()); 632 } 633 assertTrue("Failed to set correct value", Array.getBoolean(x, 0)); 634 try { 635 Array.setBoolean(new Object(), 0, false); 636 } catch (IllegalArgumentException e) { 637 // Correct behaviour 638 thrown = true; 639 } 640 if (!thrown){ 641 fail("Passing non-array failed to throw exception"); 642 } 643 thrown = false; 644 try { 645 Array.setBoolean(x, 4, false); 646 } catch (ArrayIndexOutOfBoundsException e) { 647 // Correct behaviour 648 thrown = true; 649 } 650 if (!thrown) { 651 fail("Invalid index failed to throw exception"); 652 } 653 654 thrown = false; 655 try { 656 Array.setBoolean(null, 0, true); 657 } catch (NullPointerException e) { 658 // Correct behaviour 659 thrown = true; 660 } 661 if (!thrown) { 662 fail("Null argument failed to throw NPE"); 663 } 664 } 665 666 /** 667 * java.lang.reflect.Array#setByte(java.lang.Object, int, byte) 668 */ 669 public void test_setByteLjava_lang_ObjectIB() { 670 // Test for method void 671 // java.lang.reflect.Array.setByte(java.lang.Object, int, byte) 672 byte[] x = { 0 }; 673 boolean thrown = false; 674 try { 675 Array.setByte(x, 0, (byte) 1); 676 } catch (Exception e) { 677 fail("Exception during get test : " + e.getMessage()); 678 } 679 assertEquals("Get returned incorrect value", 1, Array.getByte(x, 0)); 680 try { 681 Array.setByte(new Object(), 0, (byte) 9); 682 } catch (IllegalArgumentException e) { 683 // Correct behaviour 684 thrown = true; 685 } 686 if (!thrown) { 687 fail("Passing non-array failed to throw exception"); 688 } 689 thrown = false; 690 try { 691 Array.setByte(x, 4, (byte) 9); 692 } catch (ArrayIndexOutOfBoundsException e) { 693 // Correct behaviour 694 thrown = true; 695 } 696 if (!thrown) { 697 fail("Invalid index failed to throw exception"); 698 } 699 700 thrown = false; 701 try { 702 Array.setByte(null, 0, (byte)0); 703 } catch (NullPointerException e) { 704 // Correct behaviour 705 thrown = true; 706 } 707 if (!thrown) { 708 fail("Null argument failed to throw NPE"); 709 } 710 } 711 712 /** 713 * java.lang.reflect.Array#setChar(java.lang.Object, int, char) 714 */ 715 public void test_setCharLjava_lang_ObjectIC() { 716 // Test for method void 717 // java.lang.reflect.Array.setChar(java.lang.Object, int, char) 718 char[] x = { 0 }; 719 boolean thrown = false; 720 try { 721 Array.setChar(x, 0, (char) 1); 722 } catch (Exception e) { 723 fail("Exception during get test : " + e.getMessage()); 724 } 725 assertEquals("Get returned incorrect value", 1, Array.getChar(x, 0)); 726 try { 727 Array.setChar(new Object(), 0, (char) 9); 728 } catch (IllegalArgumentException e) { 729 // Correct behaviour 730 thrown = true; 731 } 732 if (!thrown) { 733 fail("Passing non-array failed to throw exception"); 734 } 735 thrown = false; 736 try { 737 Array.setChar(x, 4, (char) 9); 738 } catch (ArrayIndexOutOfBoundsException e) { 739 // Correct behaviour 740 thrown = true; 741 } 742 if (!thrown) { 743 fail("Invalid index failed to throw exception"); 744 } 745 746 thrown = false; 747 try { 748 Array.setChar(null, 0, (char)0); 749 } catch (NullPointerException e) { 750 // Correct behaviour 751 thrown = true; 752 } 753 if (!thrown) { 754 fail("Null argument failed to throw NPE"); 755 } 756 } 757 758 /** 759 * java.lang.reflect.Array#setDouble(java.lang.Object, int, double) 760 */ 761 public void test_setDoubleLjava_lang_ObjectID() { 762 // Test for method void 763 // java.lang.reflect.Array.setDouble(java.lang.Object, int, double) 764 double[] x = { 0 }; 765 boolean thrown = false; 766 try { 767 Array.setDouble(x, 0, 1); 768 } catch (Exception e) { 769 fail("Exception during get test : " + e.getMessage()); 770 } 771 assertEquals("Get returned incorrect value", 1, Array.getDouble(x, 0), 0.0); 772 try { 773 Array.setDouble(new Object(), 0, 9); 774 } catch (IllegalArgumentException e) { 775 // Correct behaviour 776 thrown = true; 777 } 778 if (!thrown) { 779 fail("Passing non-array failed to throw exception"); 780 } 781 thrown = false; 782 try { 783 Array.setDouble(x, 4, 9); 784 } catch (ArrayIndexOutOfBoundsException e) { 785 // Correct behaviour 786 thrown = true; 787 } 788 if (!thrown) { 789 fail("Invalid index failed to throw exception"); 790 } 791 792 thrown = false; 793 try { 794 Array.setDouble(null, 0, 0); 795 } catch (NullPointerException e) { 796 // Correct behaviour 797 thrown = true; 798 } 799 if (!thrown) { 800 fail("Null argument failed to throw NPE"); 801 } 802 } 803 804 /** 805 * java.lang.reflect.Array#setFloat(java.lang.Object, int, float) 806 */ 807 public void test_setFloatLjava_lang_ObjectIF() { 808 // Test for method void 809 // java.lang.reflect.Array.setFloat(java.lang.Object, int, float) 810 float[] x = { 0.0f }; 811 boolean thrown = false; 812 try { 813 Array.setFloat(x, 0, (float) 1); 814 } catch (Exception e) { 815 fail("Exception during get test : " + e.getMessage()); 816 } 817 assertEquals("Get returned incorrect value", 1, Array.getFloat(x, 0), 0.0); 818 try { 819 Array.setFloat(new Object(), 0, (float) 9); 820 } catch (IllegalArgumentException e) { 821 // Correct behaviour 822 thrown = true; 823 } 824 if (!thrown) { 825 fail("Passing non-array failed to throw exception"); 826 } 827 thrown = false; 828 try { 829 Array.setFloat(x, 4, (float) 9); 830 } catch (ArrayIndexOutOfBoundsException e) { 831 // Correct behaviour 832 thrown = true; 833 } 834 if (!thrown) { 835 fail("Invalid index failed to throw exception"); 836 } 837 838 thrown = false; 839 try { 840 Array.setFloat(null, 0, 0); 841 } catch (NullPointerException e) { 842 // Correct behaviour 843 thrown = true; 844 } 845 if (!thrown) { 846 fail("Null argument failed to throw NPE"); 847 } 848 } 849 850 /** 851 * java.lang.reflect.Array#setInt(java.lang.Object, int, int) 852 */ 853 public void test_setIntLjava_lang_ObjectII() { 854 // Test for method void java.lang.reflect.Array.setInt(java.lang.Object, 855 // int, int) 856 int[] x = { 0 }; 857 boolean thrown = false; 858 try { 859 Array.setInt(x, 0, (int) 1); 860 } catch (Exception e) { 861 fail("Exception during get test : " + e.getMessage()); 862 } 863 assertEquals("Get returned incorrect value", 1, Array.getInt(x, 0)); 864 try { 865 Array.setInt(new Object(), 0, (int) 9); 866 } catch (IllegalArgumentException e) { 867 // Correct behaviour 868 thrown = true; 869 } 870 if (!thrown) { 871 fail("Passing non-array failed to throw exception"); 872 } 873 thrown = false; 874 try { 875 Array.setInt(x, 4, (int) 9); 876 } catch (ArrayIndexOutOfBoundsException e) { 877 // Correct behaviour 878 thrown = true; 879 } 880 if (!thrown) { 881 fail("Invalid index failed to throw exception"); 882 } 883 884 thrown = false; 885 try { 886 Array.setInt(null, 0, 0); 887 } catch (NullPointerException e) { 888 // Correct behaviour 889 thrown = true; 890 } 891 if (!thrown) { 892 fail("Null argument failed to throw NPE"); 893 } 894 } 895 896 /** 897 * java.lang.reflect.Array#setLong(java.lang.Object, int, long) 898 */ 899 public void test_setLongLjava_lang_ObjectIJ() { 900 // Test for method void 901 // java.lang.reflect.Array.setLong(java.lang.Object, int, long) 902 long[] x = { 0 }; 903 boolean thrown = false; 904 try { 905 Array.setLong(x, 0, 1); 906 } catch (Exception e) { 907 fail("Exception during get test : " + e.getMessage()); 908 } 909 assertEquals("Get returned incorrect value", 1, Array.getLong(x, 0)); 910 try { 911 Array.setLong(new Object(), 0, 9); 912 } catch (IllegalArgumentException e) { 913 // Correct behaviour 914 thrown = true; 915 } 916 if (!thrown) { 917 fail("Passing non-array failed to throw exception"); 918 } 919 thrown = false; 920 try { 921 Array.setLong(x, 4, 9); 922 } catch (ArrayIndexOutOfBoundsException e) { 923 // Correct behaviour 924 thrown = true; 925 } 926 if (!thrown) { 927 fail("Invalid index failed to throw exception"); 928 } 929 930 thrown = false; 931 try { 932 Array.setLong(null, 0, 0); 933 } catch (NullPointerException e) { 934 // Correct behaviour 935 thrown = true; 936 } 937 if (!thrown) { 938 fail("Null argument failed to throw NPE"); 939 } 940 } 941 942 /** 943 * java.lang.reflect.Array#setShort(java.lang.Object, int, short) 944 */ 945 public void test_setShortLjava_lang_ObjectIS() { 946 // Test for method void 947 // java.lang.reflect.Array.setShort(java.lang.Object, int, short) 948 short[] x = { 0 }; 949 boolean thrown = false; 950 try { 951 Array.setShort(x, 0, (short) 1); 952 } catch (Exception e) { 953 fail("Exception during get test : " + e.getMessage()); 954 } 955 assertEquals("Get returned incorrect value", 1, Array.getShort(x, 0)); 956 try { 957 Array.setShort(new Object(), 0, (short) 9); 958 } catch (IllegalArgumentException e) { 959 // Correct behaviour 960 thrown = true; 961 } 962 if (!thrown) { 963 fail("Passing non-array failed to throw exception"); 964 } 965 thrown = false; 966 try { 967 Array.setShort(x, 4, (short) 9); 968 } catch (ArrayIndexOutOfBoundsException e) { 969 // Correct behaviour 970 thrown = true; 971 } 972 if (!thrown) { 973 fail("Invalid index failed to throw exception"); 974 } 975 976 thrown = false; 977 try { 978 Array.setShort(null, 0, (short)0); 979 } catch (NullPointerException e) { 980 // Correct behaviour 981 thrown = true; 982 } 983 if (!thrown) { 984 fail("Null argument failed to throw NPE"); 985 } 986 } 987 988 /** 989 * Sets up the fixture, for example, open a network connection. This method 990 * is called before a test is executed. 991 */ 992 protected void setUp() { 993 } 994 995 /** 996 * Tears down the fixture, for example, close a network connection. This 997 * method is called after a test is executed. 998 */ 999 protected void tearDown() { 1000 } 1001 } 1002