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     public static void main(String[] args) throws Exception {
  67         try {
  68             boolean[]    za1 = new boolean[3];
  69             byte[]       ba1 = new byte[3];
  70             short[]      sa1 = new short[3];
  71             char[]       ca1 = new char[3];
  72             int[]        ia1 = new int[3];
  73             long[]       la1 = new long[3];
  74             float[]      fa1 = new float[3];
  75             double[]     da1 = new double[3];
  76             Object[]     oa1 = new Object[3];
  77 
  78             boolean[]    za2 = new boolean[9];
  79             byte[]       ba2 = new byte[9];
  80             short[]      sa2 = new short[9];
  81             char[]       ca2 = new char[9];
  82             int[]        ia2 = new int[9];
  83             long[]       la2 = new long[9];
  84             float[]      fa2 = new float[9];
  85             double[]     da2 = new double[9];
  86             Object[]     oa2 = new Object[9];
  87 
  88             boolean[][]  za3 = new boolean[9][9];
  89             byte[][]     ba3 = new byte[9][9];
  90             short[][]    sa3 = new short[9][9];
  91             char[][]     ca3 = new char[9][9];
  92             int[][]      ia3 = new int[9][9];
  93             long[][]     la3 = new long[9][9];
  94             float[][]    fa3 = new float[9][9];
  95             double[][]   da3 = new double[9][9];
  96             Object[][]   oa3 = new Object[9][9];
  97 
  98             int[][][]    ia4 = new int[9][9][9];
  99             Object[][][] oa4 = new Object[9][9][9];
 100 
 101 
 102             testASMessages(za1, ba2, "arraycopy: type mismatch: can not copy boolean[] into byte[]");
 103             testASMessages(ba1, sa2, "arraycopy: type mismatch: can not copy byte[] into short[]");
 104             testASMessages(sa1, ca2, "arraycopy: type mismatch: can not copy short[] into char[]");
 105             testASMessages(ca1, ia2, "arraycopy: type mismatch: can not copy char[] into int[]");
 106             testASMessages(ia1, la2, "arraycopy: type mismatch: can not copy int[] into long[]");
 107             testASMessages(la1, fa2, "arraycopy: type mismatch: can not copy long[] into float[]");
 108             testASMessages(fa1, da2, "arraycopy: type mismatch: can not copy float[] into double[]");
 109             testASMessages(da1, oa2, "arraycopy: type mismatch: can not copy double[] into object[]");
 110             testASMessages(oa1, za2, "arraycopy: type mismatch: can not copy object[] into boolean[]");
 111 
 112             testASMessages(za1, oa2, "arraycopy: type mismatch: can not copy boolean[] into object[]");
 113             testASMessages(ba1, za2, "arraycopy: type mismatch: can not copy byte[] into boolean[]");
 114             testASMessages(sa1, ba2, "arraycopy: type mismatch: can not copy short[] into byte[]");
 115             testASMessages(ca1, sa2, "arraycopy: type mismatch: can not copy char[] into short[]");
 116             testASMessages(ia1, ca2, "arraycopy: type mismatch: can not copy int[] into char[]");
 117             testASMessages(la1, ia2, "arraycopy: type mismatch: can not copy long[] into int[]");
 118             testASMessages(fa1, la2, "arraycopy: type mismatch: can not copy float[] into long[]");
 119             testASMessages(da1, fa2, "arraycopy: type mismatch: can not copy double[] into float[]");
 120             testASMessages(oa1, da2, "arraycopy: type mismatch: can not copy object[] into double[]");
 121 
 122             testASMessages(za3, ba2, "arraycopy: type mismatch: can not copy object[] into byte[]");
 123             testASMessages(ba3, sa2, "arraycopy: type mismatch: can not copy object[] into short[]");
 124             testASMessages(sa3, ca2, "arraycopy: type mismatch: can not copy object[] into char[]");
 125             testASMessages(ca3, ia2, "arraycopy: type mismatch: can not copy object[] into int[]");
 126             testASMessages(ia3, la2, "arraycopy: type mismatch: can not copy object[] into long[]");
 127             testASMessages(la3, fa2, "arraycopy: type mismatch: can not copy object[] into float[]");
 128             testASMessages(fa3, da2, "arraycopy: type mismatch: can not copy object[] into double[]");
 129             testASMessages(oa3, za2, "arraycopy: type mismatch: can not copy object[] into boolean[]");
 130 
 131             testASMessages(za1, oa3, "arraycopy: type mismatch: can not copy boolean[] into object[]");
 132             testASMessages(ba1, za3, "arraycopy: type mismatch: can not copy byte[] into object[]");
 133             testASMessages(sa1, ba3, "arraycopy: type mismatch: can not copy short[] into object[]");
 134             testASMessages(ca1, sa3, "arraycopy: type mismatch: can not copy char[] into object[]");
 135             testASMessages(ia1, ca3, "arraycopy: type mismatch: can not copy int[] into object[]");
 136             testASMessages(la1, ia3, "arraycopy: type mismatch: can not copy long[] into object[]");
 137             testASMessages(fa1, la3, "arraycopy: type mismatch: can not copy float[] into object[]");
 138             testASMessages(da1, fa3, "arraycopy: type mismatch: can not copy double[] into object[]");
 139 
 140             String[] Sa1 = new String[3];
 141             Date[]   Da1 = new Date[3];
 142             String[] Sa2 = new String[9];
 143             Date[]   Da2 = new Date[9];
 144 
 145             for (int i = 0; i < 3; i++) {
 146                 Sa1[i] = "" + i;
 147                 oa1[i] = "" + i;
 148             }
 149             testASMessages(Sa1, Da2,
 150                            "arraycopy: type mismatch: can not copy java.lang.String[] " +
 151                            "into java.util.Date[]");
 152             testASMessages(oa1, Da2,
 153                            "arraycopy: element type mismatch: can not cast one of the " +
 154                            "elements of java.lang.Object[] to the type of the destination " +
 155                            "array, java.util.Date");
 156 
 157             // These should succeed.
 158             doNativeArrayStore(Sa1, "This is a string", 0);
 159             doNativeArrayStore(oa1, "This is a string", 2);
 160 
 161             testNativeASMessages(Da1, "This is not a date", 0,
 162                                  "type mismatch: can not store java.lang.String to java.util.Date[0]");
 163             testNativeASMessages(Da1, "This is not a date", 2,
 164                                  "type mismatch: can not store java.lang.String to java.util.Date[2]");
 165             testNativeASMessages(oa3, "This is not a date", 2,
 166                                  "type mismatch: can not store java.lang.String to java.lang.Object[2][]");
 167             testNativeASMessages(oa4, "This is not a date", 1,
 168                                  "type mismatch: can not store java.lang.String to java.lang.Object[1][][]");
 169             testNativeASMessages(ia3, "This is not a date", 1,
 170                                  "type mismatch: can not store java.lang.String to int[1][]");
 171             testNativeASMessages(ia4, "This is not a date", 2,
 172                                  "type mismatch: can not store java.lang.String to int[2][][]");
 173 
 174         } catch (Exception e) {
 175             e.printStackTrace();
 176             Asserts.fail("Wrong exception thrown: " + e);
 177         }
 178     }
 179 }