1 /*
   2  * Copyright 2010, 2011 IBM Corporation.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is licensed by Oracle to you under the terms of the GNU
   6  * General Public License version 2 only.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * @test
  27  * @bug 6934356
  28  * @summary A serialized Vector can be successfully de-serialized.
  29  * @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
  30  */
  31 
  32 import java.io.ByteArrayInputStream;
  33 import java.io.ByteArrayOutputStream;
  34 import java.io.IOException;
  35 import java.io.ObjectInputStream;
  36 import java.io.ObjectOutputStream;
  37 import java.io.PrintWriter;
  38 import java.io.StringWriter;
  39 import java.util.Vector;
  40 
  41 public class SimpleSerialization {
  42     public static void main(final String[] args) throws Exception {
  43         final Vector<String> v1 = new Vector<>();
  44 
  45         v1.add("entry1");
  46         v1.add("entry2");
  47 
  48         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  49         final ObjectOutputStream oos = new ObjectOutputStream(baos);
  50 
  51         oos.writeObject(v1);
  52         oos.close();
  53 
  54         final byte[] data = baos.toByteArray();
  55         final ByteArrayInputStream bais = new ByteArrayInputStream(data);
  56         final ObjectInputStream ois = new ObjectInputStream(bais);
  57 
  58         final Object deserializedObject = ois.readObject();
  59         ois.close();
  60 
  61         if (false == v1.equals(deserializedObject)) {
  62             throw new RuntimeException(getFailureText(v1, deserializedObject));
  63         }
  64     }
  65 
  66     private static String getFailureText(final Object orig, final Object copy) {
  67         final StringWriter sw = new StringWriter();
  68         final PrintWriter pw = new PrintWriter(sw);
  69 
  70         pw.println("Test FAILED: Deserialized object is not equal to the original object");
  71         pw.print("\tOriginal: ");
  72         printObject(pw, orig).println();
  73         pw.print("\tCopy:     ");
  74         printObject(pw, copy).println();
  75 
  76         pw.close();
  77         return sw.toString();
  78     }
  79 
  80     private static PrintWriter printObject(final PrintWriter pw, final Object o) {
  81         pw.printf("%s@%08x", o.getClass().getName(), System.identityHashCode(o));
  82         return pw;
  83     }
  84 }