1 /*
   2  * Copyright (c) 2010, 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 
  25 /* @test
  26  * @bug 6990094
  27  * @summary Verify ObjectInputStream.cloneArray works on many kinds of arrays
  28  * @author Stuart Marks, Joseph D. Darcy
  29  */
  30 
  31 import java.io.ByteArrayInputStream;
  32 import java.io.ByteArrayOutputStream;
  33 import java.io.IOException;
  34 import java.io.ObjectInputStream;
  35 import java.io.ObjectOutputStream;
  36 import java.io.ObjectStreamException;
  37 import java.io.Serializable;
  38 
  39 public class TestObjectInputStreamCloneArray {
  40     static class Resolver implements Serializable {
  41         static Object replacement = null;
  42 
  43         public static void setReplacement(Object o) {
  44             replacement = o;
  45         }
  46         private Object readResolve() throws ObjectStreamException {
  47             return replacement;
  48         }
  49     }
  50 
  51     private static void test(Object replacement)
  52         throws IOException, ClassNotFoundException {
  53         Resolver.setReplacement(replacement);
  54 
  55         try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
  56             try(ObjectOutputStream oos = new ObjectOutputStream(baos)) {
  57                 oos.writeObject(new Resolver());
  58                 oos.writeObject(new Resolver());
  59             }
  60 
  61             Object o1;
  62             Object o2;
  63             try(ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  64                 ObjectInputStream ois = new ObjectInputStream(bais)) {
  65                 o1 = ois.readUnshared();
  66                 o2 = ois.readUnshared();
  67             }
  68 
  69             if (o1 == o2)
  70                 throw new AssertionError("o1 and o2 must not be identical");
  71         }
  72     }
  73     
  74     public static void main(String[] args)
  75         throws IOException, ClassNotFoundException {
  76         Object[] replacements = {
  77             new byte[]    {1},
  78             new char[]    {'2'},
  79             new short[]   {3},
  80             new int[]     {4},
  81             new long[]    {5},
  82             new float[]   {6.0f},
  83             new double[]  {7.0},
  84             new boolean[] {true},
  85             new Object[] {"A string."}
  86         };
  87 
  88         for(Object replacement : replacements) {
  89             test(replacement);
  90         }
  91     }
  92 }