Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/beans/XMLDecoder/spec/TestArray.java
38821 views
1
/*
2
* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @summary Tests <array> element
27
* @author Sergey Malenkov
28
*/
29
30
import java.beans.XMLDecoder;
31
import java.lang.reflect.Array;
32
33
public final class TestArray extends AbstractTest {
34
public static final String XML
35
= "<java>\n"
36
+ " <array class=\"java.lang.Number\">\n"
37
+ " <byte>-111</byte>\n"
38
+ " <long>1111</long>\n"
39
+ " </array>\n"
40
+ " <array length=\"3\">\n"
41
+ " <void index=\"1\">\n"
42
+ " <string>Hello, world!</string>\n"
43
+ " </void>\n"
44
+ " </array>\n"
45
+ "</java>";
46
47
public static void main(String[] args) {
48
new TestArray().test(true);
49
}
50
51
@Override
52
protected void validate(XMLDecoder decoder) {
53
Number[] numbers = getArray(Number.class, 2, decoder.readObject());
54
if (!numbers[0].equals(Byte.valueOf("-111"))) { // NON-NLS: hardcoded in XML
55
throw new Error("unexpected byte value");
56
}
57
if (!numbers[1].equals(Long.valueOf("1111"))) { // NON-NLS: hardcoded in XML
58
throw new Error("unexpected long value");
59
}
60
61
Object[] objects = getArray(Object.class, 3, decoder.readObject());
62
if (objects[0] != null) {
63
throw new Error("unexpected first value");
64
}
65
if (!objects[1].equals("Hello, world!")) { // NON-NLS: hardcoded in XML
66
throw new Error("unexpected string value");
67
}
68
if (objects[2] != null) {
69
throw new Error("unexpected last value");
70
}
71
}
72
73
private static <T> T[] getArray(Class<T> component, int length, Object object) {
74
Class type = object.getClass();
75
if (!type.isArray()) {
76
throw new Error("array expected");
77
}
78
if (!type.getComponentType().equals(component)) {
79
throw new Error("unexpected component type");
80
}
81
if (length != Array.getLength(object)) {
82
throw new Error("unexpected array length");
83
}
84
return (T[]) object;
85
}
86
}
87
88