Path: blob/master/test/jdk/java/awt/Container/ContainerAIOOBE/ContainerAIOOBE.java
41153 views
/*1* Copyright (c) 2014, 2016, 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*/2223import java.awt.Button;24import java.awt.Component;25import java.awt.Container;26import java.io.ByteArrayInputStream;27import java.io.ByteArrayOutputStream;28import java.io.ObjectInputStream;29import java.io.ObjectOutputStream;3031/**32* @test33* @key headful34* @bug 805959035* @summary ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized.36* @author Alexey Ivanov37* @run main ContainerAIOOBE38*/39public class ContainerAIOOBE {4041public static void main(final String[] args) throws Exception {42ZContainer z = new ZContainer();43z.add(new Button());4445ByteArrayOutputStream baos = new ByteArrayOutputStream();46ObjectOutputStream oos = new ObjectOutputStream(baos);47oos.writeObject(z);48oos.flush();49oos.close();5051byte[] array = baos.toByteArray();52ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(array));5354// Reading the object must not throw ArrayIndexOutOfBoundsException55ZContainer zz = (ZContainer) ois.readObject();5657if (zz.getComponentCount() != 1) {58throw new Exception("deserialized object must have 1 component");59}60if (!(zz.getComponent(0) instanceof Button)) {61throw new Exception("deserialized object must contain Button component");62}63if (zz.getComponents().length != 0) {64throw new Exception("deserialized object returns non-empty array");65}66System.out.println("Test passed");67}6869static class ZContainer extends Container {70public Component[] getComponents() {71return new Component[0];72}73}74}757677