jdk/test/java/awt/Container/ContainerAIOOBE/ContainerAIOOBE.java
author goetz
Tue, 14 Jun 2016 10:44:59 +0200
changeset 39056 d99e63b6d962
parent 27279 a5914bdb193c
permissions -rw-r--r--
8159690: [TESTBUG] Mark headful tests with @key headful. Reviewed-by: simonis, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27279
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
     1
/*
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 27279
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
27279
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
     4
 *
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
     8
 *
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    13
 * accompanied this code).
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    14
 *
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    18
 *
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    21
 * questions.
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    22
 */
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    23
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    24
import java.awt.Button;
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    25
import java.awt.Component;
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    26
import java.awt.Container;
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    27
import java.io.ByteArrayInputStream;
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    28
import java.io.ByteArrayOutputStream;
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    29
import java.io.ObjectInputStream;
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    30
import java.io.ObjectOutputStream;
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    31
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    32
/**
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    33
 * @test
39056
d99e63b6d962 8159690: [TESTBUG] Mark headful tests with @key headful.
goetz
parents: 27279
diff changeset
    34
 * @key headful
27279
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    35
 * @bug 8059590
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    36
 * @summary ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized.
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    37
 * @author Alexey Ivanov
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    38
 * @run main ContainerAIOOBE
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    39
 */
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    40
public class ContainerAIOOBE {
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    41
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    42
    public static void main(final String[] args) throws Exception {
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    43
        ZContainer z = new ZContainer();
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    44
        z.add(new Button());
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    45
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    46
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    47
        ObjectOutputStream oos = new ObjectOutputStream(baos);
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    48
        oos.writeObject(z);
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    49
        oos.flush();
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    50
        oos.close();
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    51
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    52
        byte[] array = baos.toByteArray();
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    53
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(array));
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    54
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    55
        // Reading the object must not throw ArrayIndexOutOfBoundsException
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    56
        ZContainer zz = (ZContainer) ois.readObject();
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    57
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    58
        if (zz.getComponentCount() != 1) {
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    59
            throw new Exception("deserialized object must have 1 component");
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    60
        }
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    61
        if (!(zz.getComponent(0) instanceof Button)) {
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    62
            throw new Exception("deserialized object must contain Button component");
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    63
        }
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    64
        if (zz.getComponents().length != 0) {
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    65
            throw new Exception("deserialized object returns non-empty array");
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    66
        }
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    67
        System.out.println("Test passed");
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    68
    }
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    69
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    70
    static class ZContainer extends Container {
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    71
        public Component[] getComponents() {
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    72
            return new Component[0];
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    73
        }
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    74
    }
a5914bdb193c 8059590: ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized
aivanov
parents:
diff changeset
    75
}