Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/beans/XMLDecoder/spec/TestArray.java
38821 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Tests <array> element26* @author Sergey Malenkov27*/2829import java.beans.XMLDecoder;30import java.lang.reflect.Array;3132public final class TestArray extends AbstractTest {33public static final String XML34= "<java>\n"35+ " <array class=\"java.lang.Number\">\n"36+ " <byte>-111</byte>\n"37+ " <long>1111</long>\n"38+ " </array>\n"39+ " <array length=\"3\">\n"40+ " <void index=\"1\">\n"41+ " <string>Hello, world!</string>\n"42+ " </void>\n"43+ " </array>\n"44+ "</java>";4546public static void main(String[] args) {47new TestArray().test(true);48}4950@Override51protected void validate(XMLDecoder decoder) {52Number[] numbers = getArray(Number.class, 2, decoder.readObject());53if (!numbers[0].equals(Byte.valueOf("-111"))) { // NON-NLS: hardcoded in XML54throw new Error("unexpected byte value");55}56if (!numbers[1].equals(Long.valueOf("1111"))) { // NON-NLS: hardcoded in XML57throw new Error("unexpected long value");58}5960Object[] objects = getArray(Object.class, 3, decoder.readObject());61if (objects[0] != null) {62throw new Error("unexpected first value");63}64if (!objects[1].equals("Hello, world!")) { // NON-NLS: hardcoded in XML65throw new Error("unexpected string value");66}67if (objects[2] != null) {68throw new Error("unexpected last value");69}70}7172private static <T> T[] getArray(Class<T> component, int length, Object object) {73Class type = object.getClass();74if (!type.isArray()) {75throw new Error("array expected");76}77if (!type.getComponentType().equals(component)) {78throw new Error("unexpected component type");79}80if (length != Array.getLength(object)) {81throw new Error("unexpected array length");82}83return (T[]) object;84}85}868788