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())));
- }
}
ClientIdentifier equalObject() {
return EQUAL_OBJECT;
}
+
+ @Override
+ int expectedSize() {
+ return 114;
+ }
}
FrontendIdentifier equalObject() {
return EQUAL_OBJECT;
}
+
+ @Override
+ int expectedSize() {
+ return 115;
+ }
}
*/
package org.opendaylight.controller.cluster.access.concepts;
+import static org.junit.Assert.assertEquals;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
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);
}
LocalHistoryIdentifier equalObject() {
return EQUAL_OBJECT;
}
+
+ @Override
+ int expectedSize() {
+ return 121;
+ }
}
return EQUAL_OBJECT;
}
+ @Override
+ int expectedSize() {
+ return 101;
+ }
@Test
public void testCompareTo() {
TransactionIdentifier equalObject() {
return EQUAL_OBJECT;
}
+
+ @Override
+ int expectedSize() {
+ return 121;
+ }
}
@Test
public final void testCreateMetadataSnapshotEmptyInput() throws Exception {
final FrontendShardDataTreeSnapshotMetadata emptyOrigSnapshot = createEmptyMetadataSnapshot();
- final FrontendShardDataTreeSnapshotMetadata emptyCopySnapshot = copy(emptyOrigSnapshot);
+ final FrontendShardDataTreeSnapshotMetadata emptyCopySnapshot = copy(emptyOrigSnapshot, 127);
testMetadataSnapshotEqual(emptyOrigSnapshot, emptyCopySnapshot);
}
@Test
public final void testSerializeMetadataSnapshotWithOneClient() throws Exception {
final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(1);
- final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot);
+ final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot, 162);
testMetadataSnapshotEqual(origSnapshot, copySnapshot);
}
@Test
public final void testSerializeMetadataSnapshotWithMoreClients() throws Exception {
final FrontendShardDataTreeSnapshotMetadata origSnapshot = createMetadataSnapshot(5);
- final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot);
+ final FrontendShardDataTreeSnapshotMetadata copySnapshot = copy(origSnapshot, 314);
testMetadataSnapshotEqual(origSnapshot, copySnapshot);
}
}
@SuppressWarnings("unchecked")
- private static <T> T copy(final T obj) throws IOException, ClassNotFoundException {
+ private static <T> 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();
}
}
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();
}
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();
}