50601
|
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 array[]");
|
|
110 |
testASMessages(oa1, za2, "arraycopy: type mismatch: can not copy object array[] into boolean[]");
|
|
111 |
|
|
112 |
testASMessages(za1, oa2, "arraycopy: type mismatch: can not copy boolean[] into object array[]");
|
|
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 array[] into double[]");
|
|
121 |
|
|
122 |
testASMessages(za3, ba2, "arraycopy: type mismatch: can not copy object array[] into byte[]");
|
|
123 |
testASMessages(ba3, sa2, "arraycopy: type mismatch: can not copy object array[] into short[]");
|
|
124 |
testASMessages(sa3, ca2, "arraycopy: type mismatch: can not copy object array[] into char[]");
|
|
125 |
testASMessages(ca3, ia2, "arraycopy: type mismatch: can not copy object array[] into int[]");
|
|
126 |
testASMessages(ia3, la2, "arraycopy: type mismatch: can not copy object array[] into long[]");
|
|
127 |
testASMessages(la3, fa2, "arraycopy: type mismatch: can not copy object array[] into float[]");
|
|
128 |
testASMessages(fa3, da2, "arraycopy: type mismatch: can not copy object array[] into double[]");
|
|
129 |
testASMessages(oa3, za2, "arraycopy: type mismatch: can not copy object array[] into boolean[]");
|
|
130 |
|
|
131 |
testASMessages(za1, oa3, "arraycopy: type mismatch: can not copy boolean[] into object array[]");
|
|
132 |
testASMessages(ba1, za3, "arraycopy: type mismatch: can not copy byte[] into object array[]");
|
|
133 |
testASMessages(sa1, ba3, "arraycopy: type mismatch: can not copy short[] into object array[]");
|
|
134 |
testASMessages(ca1, sa3, "arraycopy: type mismatch: can not copy char[] into object array[]");
|
|
135 |
testASMessages(ia1, ca3, "arraycopy: type mismatch: can not copy int[] into object array[]");
|
|
136 |
testASMessages(la1, ia3, "arraycopy: type mismatch: can not copy long[] into object array[]");
|
|
137 |
testASMessages(fa1, la3, "arraycopy: type mismatch: can not copy float[] into object array[]");
|
|
138 |
testASMessages(da1, fa3, "arraycopy: type mismatch: can not copy double[] into object array[]");
|
|
139 |
|
|
140 |
//testASMessages(null, ba2, "arraycopy: type mismatch: can not copy boolean[] into byte[]"); NPE
|
|
141 |
//testASMessages(za1, null, "arraycopy: type mismatch: can not copy boolean[] into byte[]"); NPE
|
|
142 |
testASMessages("This is not an array", ia2, "arraycopy: source type java.lang.String is not an array");
|
|
143 |
testASMessages(la1, "This is not an array", "arraycopy: destination type java.lang.String is not an array");
|
|
144 |
|
|
145 |
//testASMessages(null, oa2, "arraycopy: type mismatch: can not copy boolean[] into byte[]"); NPE
|
|
146 |
//testASMessages(oa1, null, "arraycopy: type mismatch: can not copy boolean[] into byte[]"); NPE
|
|
147 |
testASMessages("This is not an array", oa2, "arraycopy: source type java.lang.String is not an array");
|
|
148 |
testASMessages(oa1, "This is not an array", "arraycopy: destination type java.lang.String is not an array");
|
|
149 |
|
|
150 |
String[] Sa1 = new String[3];
|
|
151 |
Date[] Da1 = new Date[3];
|
|
152 |
String[] Sa2 = new String[9];
|
|
153 |
Date[] Da2 = new Date[9];
|
|
154 |
|
|
155 |
for (int i = 0; i < 3; i++) {
|
|
156 |
Sa1[i] = "" + i;
|
|
157 |
oa1[i] = "" + i;
|
|
158 |
}
|
|
159 |
testASMessages(Sa1, Da2,
|
|
160 |
"arraycopy: type mismatch: can not copy java.lang.String[] " +
|
|
161 |
"into java.util.Date[]");
|
|
162 |
testASMessages(oa1, Da2,
|
|
163 |
"arraycopy: element type mismatch: can not cast one of the " +
|
|
164 |
"elements of java.lang.Object[] to the type of the destination " +
|
|
165 |
"array, java.util.Date");
|
|
166 |
|
|
167 |
// These should succeed.
|
|
168 |
doNativeArrayStore(Sa1, "This is a string", 0);
|
|
169 |
doNativeArrayStore(oa1, "This is a string", 2);
|
|
170 |
|
|
171 |
testNativeASMessages(Da1, "This is not a date", 0,
|
|
172 |
"type mismatch: can not store java.lang.String to java.util.Date[0]");
|
|
173 |
testNativeASMessages(Da1, "This is not a date", 2,
|
|
174 |
"type mismatch: can not store java.lang.String to java.util.Date[2]");
|
|
175 |
testNativeASMessages(oa3, "This is not a date", 2,
|
|
176 |
"type mismatch: can not store java.lang.String to java.lang.Object[2][]");
|
|
177 |
testNativeASMessages(oa4, "This is not a date", 1,
|
|
178 |
"type mismatch: can not store java.lang.String to java.lang.Object[1][][]");
|
|
179 |
testNativeASMessages(ia3, "This is not a date", 1,
|
|
180 |
"type mismatch: can not store java.lang.String to int[1][]");
|
|
181 |
testNativeASMessages(ia4, "This is not a date", 2,
|
|
182 |
"type mismatch: can not store java.lang.String to int[2][][]");
|
|
183 |
|
|
184 |
} catch (java.lang.RuntimeException e) {
|
|
185 |
throw e;
|
|
186 |
} catch (Exception e) {
|
|
187 |
e.printStackTrace();
|
|
188 |
Asserts.fail("Wrong exception thrown: " + e);
|
|
189 |
}
|
|
190 |
}
|
|
191 |
}
|