From: Robert Varga Date: Mon, 23 Sep 2019 06:23:29 +0000 (+0200) Subject: Add more serialization assertions X-Git-Tag: release/magnesium~83 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=b394690b52324ae91124735c4ec19acc3389d4ec Add more serialization assertions This adds a few more assertions to ensure our serialization format does not move without us knowing. Change-Id: Ieed326e9e57fb15ea46cd7a088d713222963e2e5 Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/AbstractIdentifierTest.java b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/AbstractIdentifierTest.java index 080e1e46b4..8fcc9fa1f5 100644 --- a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/AbstractIdentifierTest.java +++ b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/AbstractIdentifierTest.java @@ -26,6 +26,8 @@ public abstract class AbstractIdentifierTest { abstract T equalObject(); + abstract int expectedSize(); + @Test public final void testEquals() { assertTrue(object().equals(object())); @@ -40,22 +42,26 @@ public abstract class AbstractIdentifierTest { 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 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()))); - } } diff --git a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/ClientIdentifierTest.java b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/ClientIdentifierTest.java index 4871def180..0908659487 100644 --- a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/ClientIdentifierTest.java +++ b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/ClientIdentifierTest.java @@ -29,4 +29,9 @@ public class ClientIdentifierTest extends AbstractIdentifierTest { return FrontendType.forName("type-1"); } + @Override + int expectedSize() { + return 104; + } + @Test public void testWriteToReadFrom() throws Exception { final FrontendType type = FrontendType.forName("type"); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final DataOutputStream dos = new DataOutputStream(baos); type.writeTo(dos); - final FrontendType read = - FrontendType.readFrom(new DataInputStream(new ByteArrayInputStream(baos.toByteArray()))); + + final byte[] bytes = baos.toByteArray(); + assertEquals(8, bytes.length); + final FrontendType read = FrontendType.readFrom(new DataInputStream(new ByteArrayInputStream(bytes))); Assert.assertEquals(type, read); } diff --git a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/LocalHistoryIdentifierTest.java b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/LocalHistoryIdentifierTest.java index 159896b9f4..161370deff 100644 --- a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/LocalHistoryIdentifierTest.java +++ b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/LocalHistoryIdentifierTest.java @@ -30,4 +30,9 @@ public class LocalHistoryIdentifierTest extends AbstractIdentifierTest { return EQUAL_OBJECT; } + @Override + int expectedSize() { + return 101; + } @Test public void testCompareTo() { diff --git a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/TransactionIdentifierTest.java b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/TransactionIdentifierTest.java index e1c281ac2b..b33b61c49b 100644 --- a/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/TransactionIdentifierTest.java +++ b/opendaylight/md-sal/cds-access-api/src/test/java/org/opendaylight/controller/cluster/access/concepts/TransactionIdentifierTest.java @@ -31,4 +31,9 @@ public class TransactionIdentifierTest extends AbstractIdentifierTest T copy(final T obj) throws IOException, ClassNotFoundException { + private static T copy(final T obj, final int expectedSize) 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(); } } diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/ShardDataTreeSnapshotTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/ShardDataTreeSnapshotTest.java index 9e05cf4860..9327f6262f 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/ShardDataTreeSnapshotTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/persisted/ShardDataTreeSnapshotTest.java @@ -47,8 +47,11 @@ public class ShardDataTreeSnapshotTest { snapshot.serialize(out); } + final byte[] bytes = bos.toByteArray(); + assertEquals(242, bytes.length); + ShardDataTreeSnapshot deserialized; - try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) { + try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) { deserialized = ShardDataTreeSnapshot.deserialize(in).getSnapshot(); } @@ -73,8 +76,11 @@ public class ShardDataTreeSnapshotTest { snapshot.serialize(out); } + final byte[] bytes = bos.toByteArray(); + assertEquals(390, bytes.length); + ShardDataTreeSnapshot deserialized; - try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray()))) { + try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes))) { deserialized = ShardDataTreeSnapshot.deserialize(in).getSnapshot(); }