Path: blob/master/test/jdk/java/beans/XMLDecoder/spec/TestArray.java
41155 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* @run main/othervm -Djava.security.manager=allow TestArray27* @author Sergey Malenkov28*/2930import java.beans.XMLDecoder;31import java.lang.reflect.Array;3233public final class TestArray extends AbstractTest {34public static final String XML35= "<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>";4647public static void main(String[] args) {48new TestArray().test(true);49}5051@Override52protected void validate(XMLDecoder decoder) {53Number[] numbers = getArray(Number.class, 2, decoder.readObject());54if (!numbers[0].equals(Byte.valueOf("-111"))) { // NON-NLS: hardcoded in XML55throw new Error("unexpected byte value");56}57if (!numbers[1].equals(Long.valueOf("1111"))) { // NON-NLS: hardcoded in XML58throw new Error("unexpected long value");59}6061Object[] objects = getArray(Object.class, 3, decoder.readObject());62if (objects[0] != null) {63throw new Error("unexpected first value");64}65if (!objects[1].equals("Hello, world!")) { // NON-NLS: hardcoded in XML66throw new Error("unexpected string value");67}68if (objects[2] != null) {69throw new Error("unexpected last value");70}71}7273private static <T> T[] getArray(Class<T> component, int length, Object object) {74Class type = object.getClass();75if (!type.isArray()) {76throw new Error("array expected");77}78if (!type.getComponentType().equals(component)) {79throw new Error("unexpected component type");80}81if (length != Array.getLength(object)) {82throw new Error("unexpected array length");83}84return (T[]) object;85}86}878889