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 that NegativeArraySizeException reports the wrong size.
  28  * @library /test/lib
  29  * @compile NegativeArraySizeExceptionTest.java
  30  * @run main NegativeArraySizeExceptionTest
  31  * @run main/othervm -Xcomp -XX:-TieredCompilation  NegativeArraySizeExceptionTest
  32  * @run main/othervm -Xcomp -XX:TieredStopAtLevel=1 NegativeArraySizeExceptionTest
  33  */
  34 
  35 import java.lang.reflect.Array;
  36 
  37 import jdk.test.lib.Asserts;
  38 
  39 public class NegativeArraySizeExceptionTest {
  40 
  41     private static void fail () throws Exception {
  42         throw new RuntimeException("Array allocation with negative size expected to fail!");
  43     }
  44 
  45     public static void main(String[] args) throws Exception {
  46         int minusOne = -1;
  47         Object r = null;
  48 
  49         try {
  50             r = new byte[minusOne];
  51             fail();
  52         } catch (NegativeArraySizeException e) {
  53             Asserts.assertEQ("-1", e.getMessage());
  54         }
  55 
  56         try {
  57             r = new byte[Integer.MIN_VALUE];
  58             fail();
  59         } catch (NegativeArraySizeException e) {
  60             Asserts.assertEQ("-2147483648", e.getMessage());
  61         }
  62 
  63         try {
  64             r = new Object[minusOne];
  65             fail();
  66         } catch (NegativeArraySizeException e) {
  67             Asserts.assertEQ("-1", e.getMessage());
  68         }
  69 
  70         try {
  71             r = new Object[Integer.MIN_VALUE];
  72             fail();
  73         } catch (NegativeArraySizeException e) {
  74             Asserts.assertEQ("-2147483648", e.getMessage());
  75         }
  76 
  77         try {
  78             r = new byte[3][minusOne];
  79             fail();
  80         } catch (NegativeArraySizeException e) {
  81             Asserts.assertEQ("-1", e.getMessage());
  82         }
  83 
  84         try {
  85             r = new byte[3][Integer.MIN_VALUE];
  86             fail();
  87         } catch (NegativeArraySizeException e) {
  88             Asserts.assertEQ("-2147483648", e.getMessage());
  89         }
  90 
  91         try {
  92             r = new Object[3][minusOne];
  93             fail();
  94         } catch (NegativeArraySizeException e) {
  95             Asserts.assertEQ("-1", e.getMessage());
  96         }
  97 
  98         try {
  99             r = new Object[3][Integer.MIN_VALUE];
 100             fail();
 101         } catch (NegativeArraySizeException e) {
 102             Asserts.assertEQ("-2147483648", e.getMessage());
 103         }
 104 
 105         try {
 106             r = new byte[minusOne][3];
 107             fail();
 108         } catch (NegativeArraySizeException e) {
 109             Asserts.assertEQ("-1", e.getMessage());
 110         }
 111 
 112         try {
 113             r = new byte[Integer.MIN_VALUE][3];
 114             fail();
 115         } catch (NegativeArraySizeException e) {
 116             Asserts.assertEQ("-2147483648", e.getMessage());
 117         }
 118 
 119         try {
 120             r = new Object[minusOne][3];
 121             fail();
 122         } catch (NegativeArraySizeException e) {
 123             Asserts.assertEQ("-1", e.getMessage());
 124         }
 125 
 126         try {
 127             r = new Object[Integer.MIN_VALUE][3];
 128             fail();
 129         } catch (NegativeArraySizeException e) {
 130             Asserts.assertEQ("-2147483648", e.getMessage());
 131         }
 132 
 133         try {
 134             Array.newInstance(byte.class, minusOne);
 135             fail();
 136         } catch (NegativeArraySizeException e) {
 137             Asserts.assertEQ("-1", e.getMessage());
 138         }
 139 
 140         try {
 141             Array.newInstance(byte.class, Integer.MIN_VALUE);
 142             fail();
 143         } catch (NegativeArraySizeException e) {
 144             Asserts.assertEQ("-2147483648", e.getMessage());
 145         }
 146 
 147         try {
 148             Array.newInstance(NegativeArraySizeException.class, minusOne);
 149             fail();
 150         } catch (NegativeArraySizeException e) {
 151             Asserts.assertEQ("-1", e.getMessage());
 152         }
 153 
 154         try {
 155             Array.newInstance(NegativeArraySizeException.class, Integer.MIN_VALUE);
 156             fail();
 157         } catch (NegativeArraySizeException e) {
 158             Asserts.assertEQ("-2147483648", e.getMessage());
 159         }
 160 
 161         try {
 162             Array.newInstance(byte.class, new int[] {3, minusOne});
 163             fail();
 164         } catch (NegativeArraySizeException e) {
 165             Asserts.assertEQ("-1", e.getMessage());
 166         }
 167 
 168         try {
 169             Array.newInstance(byte.class, new int[] {3, Integer.MIN_VALUE});
 170             fail();
 171         } catch (NegativeArraySizeException e) {
 172             Asserts.assertEQ("-2147483648", e.getMessage());
 173         }
 174 
 175         try {
 176             Array.newInstance(NegativeArraySizeException.class, new int[] {3, minusOne});
 177             fail();
 178         } catch (NegativeArraySizeException e) {
 179             Asserts.assertEQ("-1", e.getMessage());
 180         }
 181 
 182         try {
 183             Array.newInstance(NegativeArraySizeException.class, new int[] {3, Integer.MIN_VALUE});
 184             fail();
 185         } catch (NegativeArraySizeException e) {
 186             Asserts.assertEQ("-2147483648", e.getMessage());
 187         }
 188 
 189         try {
 190             Array.newInstance(byte.class, new int[] {minusOne, 3});
 191             fail();
 192         } catch (NegativeArraySizeException e) {
 193             Asserts.assertEQ("-1", e.getMessage());
 194         }
 195 
 196         try {
 197             Array.newInstance(byte.class, new int[] {Integer.MIN_VALUE, 3});
 198             fail();
 199         } catch (NegativeArraySizeException e) {
 200             Asserts.assertEQ("-2147483648", e.getMessage());
 201         }
 202 
 203         try {
 204             Array.newInstance(NegativeArraySizeException.class, new int[] {minusOne, 3});
 205             fail();
 206         } catch (NegativeArraySizeException e) {
 207             Asserts.assertEQ("-1", e.getMessage());
 208         }
 209 
 210         try {
 211             Array.newInstance(NegativeArraySizeException.class, new int[] {Integer.MIN_VALUE, 3});
 212             fail();
 213         } catch (NegativeArraySizeException e) {
 214             Asserts.assertEQ("-2147483648", e.getMessage());
 215         }
 216 
 217         Asserts.assertEQ(r, null, "Expected all tries to allocate negative array to fail.");
 218     }
 219 }