/ Published in: Java
Checks whether a Java instance really is serializable and measures how big it is. Writes an object to an ObjectInputStream and then tries to read it again and assert that the original and the deserialized object are equal. On the way it outputs the size of the byte array by product so you get a rough estimate of your object's size.
Expand |
Embed | Plain Text
package com.nothingspecial.test.util; import org.junit.Assert; import java.io.*; public class SerializationTestUtil { private SerializationTestUtil() { } @SuppressWarnings({"unchecked"}) public static <T> T bothWays(T vo) { final byte[] bytes = serilaze(vo); return (T) deserialize(bytes); } if (bytes.length == 0) { } try { result = objectInputStream.readObject(); Assert.fail(format("Could not deserialize message: {0} type {1}: ", e.getMessage(), e.getClass().getSimpleName())); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } } } return result; } try { outputStream.writeObject(instance); outputStream.flush(); System.out.println(format("Serialized form size for [{0}] instance: {1}", instance.getClass().getName(), byteArrayOutputStream.size())); return byteArrayOutputStream.toByteArray(); Assert.fail(format("Could not serialize {2} msg {0} type {1}", e.getMessage(), e.getClass().getSimpleName(), instance)); } finally { if (outputStream != null) { try { outputStream.close(); } } } return null; } }
You need to login to post a comment.
