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 ArrayStoreException message. The message lists
  28  *   information about the array types involved.
  29  * @library /test/lib
  30  * @run main ArrayStoreExceptionTest
  31  */
  32 
  33 import java.util.Date;
  34 import jdk.test.lib.Asserts;
  35 
  36 /**
  37  * Tests the detailed messages of the ArrayStoreException.
  38  */
  39 public class ArrayStoreExceptionTest {
  40 
  41     static {
  42         System.loadLibrary("ArrayStoreExceptionTest");
  43     }
  44 
  45     static void testASMessages(Object from, Object to, String message) throws Exception {
  46         try {
  47             System.arraycopy(from, 1, to, 3, 2);
  48             Asserts.fail("Expected ArrayStoreException not thrown");
  49         } catch (ArrayStoreException e) {
  50             Asserts.assertEquals(e.getMessage(), message);
  51         }
  52     }
  53 
  54     static native void doNativeArrayStore(Object[] src, Object dst, int index);
  55 
  56     static void testNativeASMessages(Object[] array, Object elem, int index, String message)
  57         throws Exception {
  58         try {
  59             doNativeArrayStore(array, elem, index);
  60             Asserts.fail("Expected ArrayStoreException not thrown");
  61         } catch (ArrayStoreException e) {
  62             Asserts.assertEquals(e.getMessage(), message);
  63         }
  64     }
  65 
  66     static native void doNativeArrayStore2(Object src, Object dst, int index);
  67 
  68     static void testNativeASMessages2(Object array, Object elem, int index, String message)
  69         throws Exception {
  70         try {
  71             doNativeArrayStore2(array, elem, index);
  72             Asserts.fail("Expected ArrayStoreException not thrown");
  73         } catch (ArrayIndexOutOfBoundsException e) {
  74             Asserts.assertEquals(e.getMessage(), message);
  75         }
  76     }
  77 
  78     public static void main(String[] args) throws Exception {
  79         try {
  80             boolean[]    za1 = new boolean[3];
  81             byte[]       ba1 = new byte[3];
  82             short[]      sa1 = new short[3];
  83             char[]       ca1 = new char[3];
  84             int[]        ia1 = new int[3];
  85             long[]       la1 = new long[3];
  86             float[]      fa1 = new float[3];
  87             double[]     da1 = new double[3];
  88             Object[]     oa1 = new Object[3];
  89 
  90             boolean[]    za2 = new boolean[9];
  91             byte[]       ba2 = new byte[9];
  92             short[]      sa2 = new short[9];
  93             char[]       ca2 = new char[9];
  94             int[]        ia2 = new int[9];
  95             long[]       la2 = new long[9];
  96             float[]      fa2 = new float[9];
  97             double[]     da2 = new double[9];
  98             Object[]     oa2 = new Object[9];
  99 
 100             boolean[][]  za3 = new boolean[9][9];
 101             byte[][]     ba3 = new byte[9][9];
 102             short[][]    sa3 = new short[9][9];
 103             char[][]     ca3 = new char[9][9];
 104             int[][]      ia3 = new int[9][9];
 105             long[][]     la3 = new long[9][9];
 106             float[][]    fa3 = new float[9][9];
 107             double[][]   da3 = new double[9][9];
 108             Object[][]   oa3 = new Object[9][9];
 109 
 110             int[][][]    ia4 = new int[9][9][9];
 111             Object[][][] oa4 = new Object[9][9][9];
 112 
 113 
 114             testASMessages(za1, ba2, "arraycopy: type mismatch: can not copy boolean[] into byte[]");
 115             testASMessages(ba1, sa2, "arraycopy: type mismatch: can not copy byte[] into short[]");
 116             testASMessages(sa1, ca2, "arraycopy: type mismatch: can not copy short[] into char[]");
 117             testASMessages(ca1, ia2, "arraycopy: type mismatch: can not copy char[] into int[]");
 118             testASMessages(ia1, la2, "arraycopy: type mismatch: can not copy int[] into long[]");
 119             testASMessages(la1, fa2, "arraycopy: type mismatch: can not copy long[] into float[]");
 120             testASMessages(fa1, da2, "arraycopy: type mismatch: can not copy float[] into double[]");
 121             testASMessages(da1, oa2, "arraycopy: type mismatch: can not copy double[] into object array[]");
 122             testASMessages(oa1, za2, "arraycopy: type mismatch: can not copy object array[] into boolean[]");
 123 
 124             testASMessages(za1, oa2, "arraycopy: type mismatch: can not copy boolean[] into object array[]");
 125             testASMessages(ba1, za2, "arraycopy: type mismatch: can not copy byte[] into boolean[]");
 126             testASMessages(sa1, ba2, "arraycopy: type mismatch: can not copy short[] into byte[]");
 127             testASMessages(ca1, sa2, "arraycopy: type mismatch: can not copy char[] into short[]");
 128             testASMessages(ia1, ca2, "arraycopy: type mismatch: can not copy int[] into char[]");
 129             testASMessages(la1, ia2, "arraycopy: type mismatch: can not copy long[] into int[]");
 130             testASMessages(fa1, la2, "arraycopy: type mismatch: can not copy float[] into long[]");
 131             testASMessages(da1, fa2, "arraycopy: type mismatch: can not copy double[] into float[]");
 132             testASMessages(oa1, da2, "arraycopy: type mismatch: can not copy object array[] into double[]");
 133 
 134             testASMessages(za3, ba2, "arraycopy: type mismatch: can not copy object array[] into byte[]");
 135             testASMessages(ba3, sa2, "arraycopy: type mismatch: can not copy object array[] into short[]");
 136             testASMessages(sa3, ca2, "arraycopy: type mismatch: can not copy object array[] into char[]");
 137             testASMessages(ca3, ia2, "arraycopy: type mismatch: can not copy object array[] into int[]");
 138             testASMessages(ia3, la2, "arraycopy: type mismatch: can not copy object array[] into long[]");
 139             testASMessages(la3, fa2, "arraycopy: type mismatch: can not copy object array[] into float[]");
 140             testASMessages(fa3, da2, "arraycopy: type mismatch: can not copy object array[] into double[]");
 141             testASMessages(oa3, za2, "arraycopy: type mismatch: can not copy object array[] into boolean[]");
 142 
 143             testASMessages(za1, oa3, "arraycopy: type mismatch: can not copy boolean[] into object array[]");
 144             testASMessages(ba1, za3, "arraycopy: type mismatch: can not copy byte[] into object array[]");
 145             testASMessages(sa1, ba3, "arraycopy: type mismatch: can not copy short[] into object array[]");
 146             testASMessages(ca1, sa3, "arraycopy: type mismatch: can not copy char[] into object array[]");
 147             testASMessages(ia1, ca3, "arraycopy: type mismatch: can not copy int[] into object array[]");
 148             testASMessages(la1, ia3, "arraycopy: type mismatch: can not copy long[] into object array[]");
 149             testASMessages(fa1, la3, "arraycopy: type mismatch: can not copy float[] into object array[]");
 150             testASMessages(da1, fa3, "arraycopy: type mismatch: can not copy double[] into object array[]");
 151 
 152             //testASMessages(null, ba2,  "arraycopy: type mismatch: can not copy boolean[] into byte[]"); NPE
 153             //testASMessages(za1,  null, "arraycopy: type mismatch: can not copy boolean[] into byte[]"); NPE
 154             testASMessages("This is not an array", ia2, "arraycopy: source type java.lang.String is not an array");
 155             testASMessages(la1, "This is not an array", "arraycopy: destination type java.lang.String is not an array");
 156 
 157             //testASMessages(null, oa2,  "arraycopy: type mismatch: can not copy boolean[] into byte[]"); NPE
 158             //testASMessages(oa1,  null, "arraycopy: type mismatch: can not copy boolean[] into byte[]"); NPE
 159             testASMessages("This is not an array", oa2, "arraycopy: source type java.lang.String is not an array");
 160             testASMessages(oa1, "This is not an array", "arraycopy: destination type java.lang.String is not an array");
 161 
 162             String[] Sa1 = new String[3];
 163             Date[]   Da1 = new Date[3];
 164             String[] Sa2 = new String[9];
 165             Date[]   Da2 = new Date[9];
 166 
 167             for (int i = 0; i < 3; i++) {
 168                 Sa1[i] = "" + i;
 169                 oa1[i] = "" + i;
 170             }
 171             testASMessages(Sa1, Da2,
 172                            "arraycopy: type mismatch: can not copy java.lang.String[] " +
 173                            "into java.util.Date[]");
 174             testASMessages(oa1, Da2,
 175                            "arraycopy: element type mismatch: can not cast one of the " +
 176                            "elements of java.lang.Object[] to the type of the destination " +
 177                            "array, java.util.Date");
 178 
 179             // These should succeed.
 180             doNativeArrayStore(Sa1, "This is a string", 0);
 181             doNativeArrayStore(oa1, "This is a string", 2);
 182 
 183             testNativeASMessages(Da1, "This is not a date", 0,
 184                                  "type mismatch: can not store java.lang.String to java.util.Date[0]");
 185             testNativeASMessages(Da1, "This is not a date", 2,
 186                                  "type mismatch: can not store java.lang.String to java.util.Date[2]");
 187             testNativeASMessages(oa3, "This is not a date", 2,
 188                                  "type mismatch: can not store java.lang.String to java.lang.Object[2][]");
 189             testNativeASMessages(oa4, "This is not a date", 1,
 190                                  "type mismatch: can not store java.lang.String to java.lang.Object[1][][]");
 191             testNativeASMessages(ia3, "This is not a date", 1,
 192                                  "type mismatch: can not store java.lang.String to int[1][]");
 193             testNativeASMessages(ia4, "This is not a date", 2,
 194                                  "type mismatch: can not store java.lang.String to int[2][][]");
 195 
 196             testNativeASMessages2("This is not an array", "This is not a date", 2, "2");
 197 
 198         } catch (java.lang.RuntimeException e) {
 199             throw e;
 200         } catch (Exception e) {
 201             e.printStackTrace();
 202             Asserts.fail("Wrong exception thrown: " + e);
 203         }
 204     }
 205 }