private static void testWriteAndRead(ComplexClass cc1)
     throws IOException, ClassNotFoundException {
   ByteArrayOutputStream bout = new ByteArrayOutputStream();
   ObjectOutputStream out = new ObjectOutputStream(bout);
   out.writeObject(cc1);
   out.close();
   ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(bout.toByteArray()));
   ComplexClass cc2 = (ComplexClass) oin.readObject();
   assertEquals(cc1, cc2);
 }
Example #2
0
  /** Serialise an object o then read it back in. Used for testing serialisation. */
  public static Object rewrite(Object o) throws IOException, ClassNotFoundException {
    // Open output stream
    ObjectOutputStream oStream = new ObjectOutputStream(new FileOutputStream("temp.out"));

    // Write object
    oStream.writeObject(o);
    oStream.close();

    // reread object
    ObjectInputStream iStream = new ObjectInputStream(new FileInputStream("temp.out"));
    return iStream.readObject();
  }
  /** A deserialized map equals original */
  public void testSerialization() throws Exception {
    ConcurrentNavigableMap q = map5();

    ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
    ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
    out.writeObject(q);
    out.close();

    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
    ConcurrentNavigableMap r = (ConcurrentNavigableMap) in.readObject();
    assertEquals(q.size(), r.size());
    assertTrue(q.equals(r));
    assertTrue(r.equals(q));
  }
  /** a deserialized serialized atomic holds same value */
  public void testSerialization() {
    AtomicLong l = new AtomicLong();

    try {
      l.set(-22);
      ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
      ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
      out.writeObject(l);
      out.close();

      ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
      ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
      AtomicLong r = (AtomicLong) in.readObject();
      assertEquals(l.get(), r.get());
    } catch (Exception e) {
      unexpectedException();
    }
  }