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 6927486
  28  * @summary A serialized Hashtable can be de-serialized properly.
  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.Hashtable;
  40 
  41 public class SimpleSerialization {
  42     public static void main(final String[] args) throws Exception {
  43         Hashtable<String, String> h1 = new Hashtable<>();
  44 
  45         h1.put("key", "value");
  46 
  47         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  48         final ObjectOutputStream oos = new ObjectOutputStream(baos);
  49 
  50         oos.writeObject(h1);
  51         oos.close();
  52 
  53         final byte[] data = baos.toByteArray();
  54         final ByteArrayInputStream bais = new ByteArrayInputStream(data);
  55         final ObjectInputStream ois = new ObjectInputStream(bais);
  56 
  57         final Object deserializedObject = ois.readObject();
  58         ois.close();
  59 
  60         if (false == h1.equals(deserializedObject)) {
  61             throw new RuntimeException(getFailureText(h1, deserializedObject));
  62         }
  63     }
  64 
  65     private static String getFailureText(final Object orig, final Object copy) {
  66         final StringWriter sw = new StringWriter();
  67         final PrintWriter pw = new PrintWriter(sw);
  68 
  69         pw.println("Test FAILED: Deserialized object is not equal to the original object");
  70         pw.print("\tOriginal: ");
  71         printObject(pw, orig).println();
  72         pw.print("\tCopy:     ");
  73         printObject(pw, copy).println();
  74 
  75         pw.close();
  76         return sw.toString();
  77     }
  78 
  79     private static PrintWriter printObject(final PrintWriter pw, final Object o) {
  80         pw.printf("%s@%08x", o.getClass().getName(), System.identityHashCode(o));
  81         return pw;
  82     }
  83 }