1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2018 SAP SE. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /**
  26  * @test
  27  * @summary Test extended ArrayIndexOutOfBoundsException message. The
  28  *   message lists information about the array and the indexes involved.
  29  * @compile ArrayIndexOutOfBoundsExceptionTest.java
  30  * @run testng ArrayIndexOutOfBoundsExceptionTest
  31  * @run testng/othervm -Xcomp -XX:-TieredCompilation  ArrayIndexOutOfBoundsExceptionTest
  32  * @run testng/othervm -Xcomp -XX:TieredStopAtLevel=1 ArrayIndexOutOfBoundsExceptionTest
  33  */
  34 
  35 import java.io.ByteArrayInputStream;
  36 import java.io.ByteArrayOutputStream;
  37 import java.io.ObjectInputStream;
  38 import java.io.ObjectOutputStream;
  39 import java.util.ArrayList;
  40 
  41 import org.testng.annotations.Test;
  42 
  43 import static org.testng.Assert.assertEquals;
  44 import static org.testng.Assert.assertNull;
  45 import static org.testng.Assert.assertNotNull;
  46 import static org.testng.Assert.assertTrue;
  47 import static org.testng.Assert.fail;
  48 
  49 /**
  50  * Tests the detailed messages of the ArrayIndexOutOfBoundsException.
  51  */
  52 public class ArrayIndexOutOfBoundsExceptionTest {
  53 
  54     // Some fields used in the test.
  55     static int[] staticArray = new int[0];
  56     static long[][] staticLongArray = new long[0][0];
  57     ArrayList<String> names = new ArrayList<>();
  58     ArrayList<String> curr;
  59 
  60     public static void main(String[] args) {
  61         ArrayIndexOutOfBoundsExceptionTest t = new ArrayIndexOutOfBoundsExceptionTest();
  62         try {
  63             t.testAIOOBMessages();
  64         } catch (Exception e) {}
  65     }
  66 
  67     /**
  68      *
  69      */
  70     public static class ArrayGenerator {
  71 
  72         /**
  73          * @param dummy1
  74          * @return Object Array
  75          */
  76         public static Object[] arrayReturner(boolean dummy1) {
  77             return new Object[0];
  78         }
  79 
  80         /**
  81          * @param dummy1
  82          * @param dummy2
  83          * @param dummy3
  84          * @return Object Array
  85          */
  86         public Object[] returnMyArray(double dummy1, long dummy2, short dummy3) {
  87             return new Object[0];
  88         }
  89     }
  90 
  91     /**
  92      *
  93      */
  94     @Test
  95     public void testAIOOBMessages() {
  96         boolean[] za1 = new boolean[0];
  97         byte[]    ba1 = new byte[0];
  98         short[]   sa1 = new short[0];
  99         char[]    ca1 = new char[0];
 100         int[]     ia1 = new int[0];
 101         long[]    la1 = new long[0];
 102         float[]   fa1 = new float[0];
 103         double[]  da1 = new double[0];
 104         Object[]  oa1 = new Object[10];
 105         Object[]  oa2 = new Object[5];
 106 
 107         boolean[] za2 = new boolean[10];
 108         boolean[] za3 = new boolean[5];
 109         byte[]    ba2 = new byte[10];
 110         byte[]    ba3 = new byte[5];
 111         short[]   sa2 = new short[10];
 112         short[]   sa3 = new short[5];
 113         char[]    ca2 = new char[10];
 114         char[]    ca3 = new char[5];
 115         int[]     ia2 = new int[10];
 116         int[]     ia3 = new int[5];
 117         long[]    la2 = new long[10];
 118         long[]    la3 = new long[5];
 119         float[]   fa2 = new float[10];
 120         float[]   fa3 = new float[5];
 121         double[]  da2 = new double[10];
 122         double[]  da3 = new double[5];
 123 
 124         try {
 125             System.out.println(za1[-5]);
 126             fail();
 127         }
 128         catch (ArrayIndexOutOfBoundsException e) {
 129             assertEquals(e.getMessage(),
 130                 "Index -5 out of bounds for length 0");
 131         }
 132 
 133         try {
 134             System.out.println(ba1[0]);
 135             fail();
 136         }
 137         catch (ArrayIndexOutOfBoundsException e) {
 138             assertEquals(e.getMessage(),
 139                 "Index 0 out of bounds for length 0");
 140         }
 141 
 142         try {
 143             System.out.println(sa1[0]);
 144             fail();
 145         }
 146         catch (ArrayIndexOutOfBoundsException e) {
 147             assertEquals(e.getMessage(),
 148                 "Index 0 out of bounds for length 0");
 149         }
 150 
 151         try {
 152             System.out.println(ca1[0]);
 153             fail();
 154         }
 155         catch (ArrayIndexOutOfBoundsException e) {
 156             assertEquals(e.getMessage(),
 157                 "Index 0 out of bounds for length 0");
 158         }
 159 
 160         try {
 161             System.out.println(ia1[0]);
 162             fail();
 163         }
 164         catch (ArrayIndexOutOfBoundsException e) {
 165             assertEquals(e.getMessage(),
 166                 "Index 0 out of bounds for length 0");
 167         }
 168 
 169         try {
 170             System.out.println(la1[0]);
 171             fail();
 172         }
 173         catch (ArrayIndexOutOfBoundsException e) {
 174             assertEquals(e.getMessage(),
 175                 "Index 0 out of bounds for length 0");
 176         }
 177 
 178         try {
 179             System.out.println(fa1[0]);
 180             fail();
 181         }
 182         catch (ArrayIndexOutOfBoundsException e) {
 183             assertEquals(e.getMessage(),
 184                 "Index 0 out of bounds for length 0");
 185         }
 186 
 187         try {
 188             System.out.println(da1[0]);
 189             fail();
 190         }
 191         catch (ArrayIndexOutOfBoundsException e) {
 192             assertEquals(e.getMessage(),
 193                 "Index 0 out of bounds for length 0");
 194         }
 195 
 196         try {
 197             System.out.println(oa1[12]);
 198             fail();
 199         }
 200         catch (ArrayIndexOutOfBoundsException e) {
 201             assertEquals(e.getMessage(),
 202                 "Index 12 out of bounds for length 10");
 203         }
 204 
 205         try {
 206             System.out.println(za1[0] = false);
 207             fail();
 208         }
 209         catch (ArrayIndexOutOfBoundsException e) {
 210             assertEquals(e.getMessage(),
 211                 "Index 0 out of bounds for length 0");
 212         }
 213 
 214         try {
 215             System.out.println(ba1[0] = 0);
 216             fail();
 217         }
 218         catch (ArrayIndexOutOfBoundsException e) {
 219             assertEquals(e.getMessage(),
 220                 "Index 0 out of bounds for length 0");
 221         }
 222 
 223         try {
 224             System.out.println(sa1[0] = 0);
 225             fail();
 226         }
 227         catch (ArrayIndexOutOfBoundsException e) {
 228             assertEquals(e.getMessage(),
 229                 "Index 0 out of bounds for length 0");
 230         }
 231 
 232         try {
 233             System.out.println(ca1[0] = 0);
 234             fail();
 235         }
 236         catch (ArrayIndexOutOfBoundsException e) {
 237             assertEquals(e.getMessage(),
 238                 "Index 0 out of bounds for length 0");
 239         }
 240 
 241         try {
 242             System.out.println(ia1[0] = 0);
 243             fail();
 244         }
 245         catch (ArrayIndexOutOfBoundsException e) {
 246             assertEquals(e.getMessage(),
 247                 "Index 0 out of bounds for length 0");
 248         }
 249 
 250         try {
 251             System.out.println(la1[0] = 0);
 252             fail();
 253         }
 254         catch (ArrayIndexOutOfBoundsException e) {
 255             assertEquals(e.getMessage(),
 256                 "Index 0 out of bounds for length 0");
 257         }
 258 
 259         try {
 260             System.out.println(fa1[0] = 0);
 261             fail();
 262         }
 263         catch (ArrayIndexOutOfBoundsException e) {
 264             assertEquals(e.getMessage(),
 265                 "Index 0 out of bounds for length 0");
 266         }
 267 
 268         try {
 269             System.out.println(da1[0] = 0);
 270             fail();
 271         }
 272         catch (ArrayIndexOutOfBoundsException e) {
 273             assertEquals(e.getMessage(),
 274                 "Index 0 out of bounds for length 0");
 275         }
 276 
 277         try {
 278             System.out.println(oa1[-2] = null);
 279             fail();
 280         }
 281         catch (ArrayIndexOutOfBoundsException e) {
 282             assertEquals(e.getMessage(),
 283                 "Index -2 out of bounds for length 10");
 284         }
 285 
 286         try {
 287             assertTrue((ArrayGenerator.arrayReturner(false))[0] == null);
 288             fail();
 289         } catch (ArrayIndexOutOfBoundsException e) {
 290             assertEquals(e.getMessage(),
 291                 "Index 0 out of bounds for length 0");
 292         }
 293         try {
 294             staticArray[0] = 2;
 295             fail();
 296         } catch (ArrayIndexOutOfBoundsException e) {
 297             assertEquals(e.getMessage(),
 298                 "Index 0 out of bounds for length 0");
 299         }
 300 
 301         // Test all five possible messages of arraycopy exceptions thrown in ObjArrayKlass::copy_array().
 302 
 303         try {
 304             System.arraycopy(oa1, -17, oa2, 0, 5);
 305             fail();
 306         } catch (ArrayIndexOutOfBoundsException e) {
 307             assertEquals(e.getMessage(),
 308                 "arraycopy: source index -17 out of bounds for object array[10]");
 309         }
 310 
 311         try {
 312             System.arraycopy(oa1, 2, oa2, -18, 5);
 313             fail();
 314         } catch (ArrayIndexOutOfBoundsException e) {
 315             assertEquals(e.getMessage(),
 316                 "arraycopy: destination index -18 out of bounds for object array[5]");
 317         }
 318 
 319         try {
 320             System.arraycopy(oa1, 2, oa2, 0, -19);
 321             fail();
 322         } catch (ArrayIndexOutOfBoundsException e) {
 323             assertEquals(e.getMessage(),
 324                 "arraycopy: length -19 is negative");
 325         }
 326 
 327         try {
 328             System.arraycopy(oa1, 8, oa2, 0, 5);
 329             fail();
 330         } catch (ArrayIndexOutOfBoundsException e) {
 331             assertEquals(e.getMessage(),
 332                 "arraycopy: last source index 13 out of bounds for object array[10]");
 333         }
 334 
 335         try {
 336             System.arraycopy(oa1, 1, oa2, 0, 7);
 337             fail();
 338         } catch (ArrayIndexOutOfBoundsException e) {
 339             assertEquals(e.getMessage(),
 340                 "arraycopy: last destination index 7 out of bounds for object array[5]");
 341         }
 342 
 343         // Test all five possible messages of arraycopy exceptions thrown in TypeArrayKlass::copy_array().
 344 
 345         try {
 346             System.arraycopy(da2, -17, da3, 0, 5);
 347             fail();
 348         } catch (ArrayIndexOutOfBoundsException e) {
 349             assertEquals(e.getMessage(),
 350                 "arraycopy: source index -17 out of bounds for double[10]");
 351         }
 352 
 353         try {
 354             System.arraycopy(da2, 2, da3, -18, 5);
 355             fail();
 356         } catch (ArrayIndexOutOfBoundsException e) {
 357             assertEquals(e.getMessage(),
 358                 "arraycopy: destination index -18 out of bounds for double[5]");
 359         }
 360 
 361         try {
 362             System.arraycopy(da2, 2, da3, 0, -19);
 363             fail();
 364         } catch (ArrayIndexOutOfBoundsException e) {
 365             assertEquals(e.getMessage(),
 366                 "arraycopy: length -19 is negative");
 367         }
 368 
 369         try {
 370             System.arraycopy(da2, 8, da3, 0, 5);
 371             fail();
 372         } catch (ArrayIndexOutOfBoundsException e) {
 373             assertEquals(e.getMessage(),
 374                 "arraycopy: last source index 13 out of bounds for double[10]");
 375         }
 376 
 377         try {
 378             System.arraycopy(da2, 1, da3, 0, 7);
 379             fail();
 380         } catch (ArrayIndexOutOfBoundsException e) {
 381             assertEquals(e.getMessage(),
 382                 "arraycopy: last destination index 7 out of bounds for double[5]");
 383         }
 384 
 385         // Test all possible basic types in the messages of arraycopy exceptions thrown in TypeArrayKlass::copy_array().
 386 
 387         try {
 388             System.arraycopy(za2, -17, za3, 0, 5);
 389             fail();
 390         } catch (ArrayIndexOutOfBoundsException e) {
 391             assertEquals(e.getMessage(),
 392                 "arraycopy: source index -17 out of bounds for boolean[10]");
 393         }
 394 
 395         try {
 396             System.arraycopy(ba2, 2, ba3, -18, 5);
 397             fail();
 398         } catch (ArrayIndexOutOfBoundsException e) {
 399             assertEquals(e.getMessage(),
 400                 "arraycopy: destination index -18 out of bounds for byte[5]");
 401         }
 402 
 403         try {
 404             System.arraycopy(sa2, 2, sa3, 0, -19);
 405             fail();
 406         } catch (ArrayIndexOutOfBoundsException e) {
 407             assertEquals(e.getMessage(),
 408                 "arraycopy: length -19 is negative");
 409         }
 410 
 411         try {
 412             System.arraycopy(ca2, 8, ca3, 0, 5);
 413             fail();
 414         } catch (ArrayIndexOutOfBoundsException e) {
 415             assertEquals(e.getMessage(),
 416                 "arraycopy: last source index 13 out of bounds for char[10]");
 417         }
 418 
 419         try {
 420             System.arraycopy(ia2, 2, ia3, 0, -19);
 421             fail();
 422         } catch (ArrayIndexOutOfBoundsException e) {
 423             assertEquals(e.getMessage(),
 424                 "arraycopy: length -19 is negative");
 425         }
 426 
 427         try {
 428             System.arraycopy(la2, 1, la3, 0, 7);
 429             fail();
 430         } catch (ArrayIndexOutOfBoundsException e) {
 431             assertEquals(e.getMessage(),
 432                 "arraycopy: last destination index 7 out of bounds for long[5]");
 433         }
 434 
 435         try {
 436             System.arraycopy(fa2, 1, fa3, 0, 7);
 437             fail();
 438         } catch (ArrayIndexOutOfBoundsException e) {
 439             assertEquals(e.getMessage(),
 440                 "arraycopy: last destination index 7 out of bounds for float[5]");
 441         }
 442     }
 443 }