abstract T equalObject();
+ abstract int expectedSize();
+
@Test
public final void testEquals() {
assertTrue(object().equals(object()));
assertEquals(object().hashCode(), equalObject().hashCode());
}
+ @Test
+ public final void testSerialization() throws Exception {
+ assertTrue(object().equals(copy(object())));
+ assertTrue(object().equals(copy(equalObject())));
+ assertFalse(differentObject().equals(copy(object())));
+ }
+
@SuppressWarnings("unchecked")
- private static <T> T copy(T obj) throws IOException, ClassNotFoundException {
+ private T copy(final T obj) throws IOException, ClassNotFoundException {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
oos.writeObject(obj);
}
- try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) {
+ final byte[] bytes = bos.toByteArray();
+ assertEquals(expectedSize(), bytes.length);
+
+ try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
return (T) ois.readObject();
}
}
-
- @Test
- public final void testSerialization() throws Exception {
- assertTrue(object().equals(copy(object())));
- assertTrue(object().equals(copy(equalObject())));
- assertFalse(differentObject().equals(copy(object())));
- }
}