test/java/util/Hashtable/SimpleSerialization.java

Print this page
rev 5382 : 7126277: Alternative hashing implementation


  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  * Portions Copyright (c) 2010, 2011 IBM Corporation
  26  */
  27 
  28 /*
  29  * @test
  30  * @bug 6927486
  31  * @summary A serialized Hashtable can be de-serialized properly.
  32  * @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
  33  */
  34 
  35 import java.io.ByteArrayInputStream;
  36 import java.io.ByteArrayOutputStream;
  37 import java.io.IOException;
  38 import java.io.ObjectInputStream;
  39 import java.io.ObjectOutputStream;
  40 import java.io.PrintWriter;
  41 import java.io.StringWriter;
  42 import java.util.Hashtable;

  43 
  44 public class SimpleSerialization {
  45     public static void main(final String[] args) throws Exception {
  46         Hashtable<String, String> h1 = new Hashtable<>();
  47 


  48         h1.put("key", "value");
  49 
  50         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  51         final ObjectOutputStream oos = new ObjectOutputStream(baos);
  52 
  53         oos.writeObject(h1);
  54         oos.close();
  55 
  56         final byte[] data = baos.toByteArray();
  57         final ByteArrayInputStream bais = new ByteArrayInputStream(data);
  58         final ObjectInputStream ois = new ObjectInputStream(bais);



  59 
  60         final Object deserializedObject = ois.readObject();
  61         ois.close();

  62 
  63         if (false == h1.equals(deserializedObject)) {







  64             throw new RuntimeException(getFailureText(h1, deserializedObject));
  65         }
  66     }
  67 
  68     private static String getFailureText(final Object orig, final Object copy) {
  69         final StringWriter sw = new StringWriter();
  70         final PrintWriter pw = new PrintWriter(sw);
  71 
  72         pw.println("Test FAILED: Deserialized object is not equal to the original object");
  73         pw.print("\tOriginal: ");
  74         printObject(pw, orig).println();
  75         pw.print("\tCopy:     ");
  76         printObject(pw, copy).println();
  77 
  78         pw.close();
  79         return sw.toString();
  80     }
  81 
  82     private static PrintWriter printObject(final PrintWriter pw, final Object o) {
  83         pw.printf("%s@%08x", o.getClass().getName(), System.identityHashCode(o));
  84         return pw;
  85     }
  86 }


  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  * Portions Copyright (c) 2010, 2011 IBM Corporation
  26  */
  27 
  28 /*
  29  * @test
  30  * @bug 6927486
  31  * @summary A serialized Hashtable can be de-serialized properly.
  32  * @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
  33  */
  34 
  35 import java.io.ByteArrayInputStream;
  36 import java.io.ByteArrayOutputStream;

  37 import java.io.ObjectInputStream;
  38 import java.io.ObjectOutputStream;
  39 import java.io.PrintWriter;
  40 import java.io.StringWriter;
  41 import java.util.Hashtable;
  42 import java.util.Map;
  43 
  44 public class SimpleSerialization {
  45     public static void main(final String[] args) throws Exception {
  46         Hashtable<String, String> h1 = new Hashtable<>();
  47 
  48         System.err.println("*** BEGIN TEST ***");
  49 
  50         h1.put("key", "value");
  51 
  52         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  53         try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {

  54             oos.writeObject(h1);
  55         }
  56 
  57         final byte[] data = baos.toByteArray();
  58         final ByteArrayInputStream bais = new ByteArrayInputStream(data);
  59         final Object deserializedObject;
  60         try (ObjectInputStream ois = new ObjectInputStream(bais)) {
  61             deserializedObject = ois.readObject();
  62         }
  63 
  64         if(!h1.getClass().isInstance(deserializedObject)) {
  65              throw new RuntimeException("Result not assignable to type of h1");
  66         }
  67 
  68         if (false == h1.equals(deserializedObject)) {
  69              Hashtable<String, String> d1 = (Hashtable<String, String>) h1;
  70             for(Map.Entry entry: h1.entrySet()) {
  71                 System.err.println("h1.key::" + entry.getKey() + " d1.containsKey()::" + d1.containsKey((String) entry.getKey()));
  72                 System.err.println("h1.value::" + entry.getValue() + " d1.contains()::" + d1.contains(entry.getValue()));
  73                 System.err.println("h1.value == d1.value " + entry.getValue().equals(d1.get((String) entry.getKey())));
  74             }
  75 
  76             throw new RuntimeException(getFailureText(h1, deserializedObject));
  77         }
  78     }
  79 
  80     private static String getFailureText(final Object orig, final Object copy) {
  81         final StringWriter sw = new StringWriter();
  82         try (PrintWriter pw = new PrintWriter(sw)) {

  83             pw.println("Test FAILED: Deserialized object is not equal to the original object");
  84             pw.print("\tOriginal: ");
  85             printObject(pw, orig).println();
  86             pw.print("\tCopy:     ");
  87             printObject(pw, copy).println();
  88         }

  89         return sw.toString();
  90     }
  91 
  92     private static PrintWriter printObject(final PrintWriter pw, final Object o) {
  93         pw.printf("%s@%08x", o.getClass().getName(), System.identityHashCode(o));
  94         return pw;
  95     }
  96 }