Add more serialization assertions
[controller.git] / opendaylight / md-sal / cds-access-api / src / test / java / org / opendaylight / controller / cluster / access / concepts / AbstractIdentifierTest.java
index 34ce489793a1f2f1190944ed18610bbfd71abcb8..8fcc9fa1f5ffe1f7eabf3326f65982ac56ed6b64 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.controller.cluster.access.concepts;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -20,9 +21,13 @@ import org.opendaylight.yangtools.concepts.Identifier;
 
 public abstract class AbstractIdentifierTest<T extends Identifier> {
     abstract T object();
+
     abstract T differentObject();
+
     abstract T equalObject();
 
+    abstract int expectedSize();
+
     @Test
     public final void testEquals() {
         assertTrue(object().equals(object()));
@@ -37,22 +42,26 @@ public abstract class AbstractIdentifierTest<T extends Identifier> {
         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 o) 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(o);
+            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())));
-    }
 }