1 /*
   2  * Copyright (c) 2014, 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 import java.awt.Button;
  25 import java.awt.Component;
  26 import java.awt.Container;
  27 import java.io.ByteArrayInputStream;
  28 import java.io.ByteArrayOutputStream;
  29 import java.io.ObjectInputStream;
  30 import java.io.ObjectOutputStream;
  31 
  32 /**
  33  * @test
  34  * @bug 8059590
  35  * @summary ArrayIndexOutOfBoundsException occurs when Container with overridden getComponents() is deserialized.
  36  * @author Alexey Ivanov
  37  * @run main bug8059590
  38  */
  39 public class bug8059590 {
  40 
  41     public static void main(final String[] args) throws Exception {
  42         ZContainer z = new ZContainer();
  43         z.add(new Button());
  44 
  45         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  46         ObjectOutputStream oos = new ObjectOutputStream(baos);
  47         oos.writeObject(z);
  48         oos.flush();
  49         oos.close();
  50 
  51         byte[] array = baos.toByteArray();
  52         ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(array));
  53 
  54         // Reading the object must not throw ArrayIndexOutOfBoundsException
  55         ZContainer zz = (ZContainer) ois.readObject();
  56 
  57         if (zz.getComponentCount() != 1) {
  58             throw new Exception("deserialized object must have 1 component");
  59         }
  60         if (!(zz.getComponent(0) instanceof Button)) {
  61             throw new Exception("deserialized object must contain Button component");
  62         }
  63         if (zz.getComponents().length != 0) {
  64             throw new Exception("deserialized object returns non-empty array");
  65         }
  66         System.out.println("Test passed");
  67     }
  68 
  69     static class ZContainer extends Container {
  70         public Component[] getComponents() {
  71             return new Component[0];
  72         }
  73     }
  74 }