1616
1717package com .google .gcloud ;
1818
19+ import static com .google .common .base .MoreObjects .firstNonNull ;
1920import static org .junit .Assert .assertEquals ;
2021import static org .junit .Assert .assertNotSame ;
2122
3031
3132/**
3233 * Base class for serialization tests. To use this class in your tests override the
33- * {@code serializableObjects()} method to return all objects that must be serializable.
34+ * {@code serializableObjects()} method to return all objects that must be serializable. Also
35+ * override {@code restorableObjects()} method to return all restorable objects whose state must be
36+ * tested for proper serialization. Both methods can return {@code null} if no such object needs to
37+ * be tested.
3438 */
3539public abstract class BaseSerializationTest {
3640
3741 /**
3842 * Returns all objects for which correct serialization must be tested.
3943 */
40- public abstract Serializable [] serializableObjects ();
44+ protected abstract Serializable [] serializableObjects ();
45+
46+ /**
47+ * Returns all restorable objects whose state must be tested for proper serialization.
48+ */
49+ protected abstract Restorable <?>[] restorableObjects ();
4150
4251 @ Test
4352 public void testSerializableObjects () throws Exception {
44- for (Serializable obj : serializableObjects ()) {
53+ for (Serializable obj : firstNonNull ( serializableObjects (), new Serializable [ 0 ] )) {
4554 Object copy = serializeAndDeserialize (obj );
4655 assertEquals (obj , obj );
4756 assertEquals (obj , copy );
@@ -52,6 +61,17 @@ public void testSerializableObjects() throws Exception {
5261 }
5362 }
5463
64+ @ Test
65+ public void testRestorableObjects () throws Exception {
66+ for (Restorable restorable : firstNonNull (restorableObjects (), new Restorable [0 ])) {
67+ RestorableState <?> state = restorable .capture ();
68+ RestorableState <?> deserializedState = serializeAndDeserialize (state );
69+ assertEquals (state , deserializedState );
70+ assertEquals (state .hashCode (), deserializedState .hashCode ());
71+ assertEquals (state .toString (), deserializedState .toString ());
72+ }
73+ }
74+
5575 @ SuppressWarnings ("unchecked" )
5676 public <T > T serializeAndDeserialize (T obj ) throws IOException , ClassNotFoundException {
5777 ByteArrayOutputStream bytes = new ByteArrayOutputStream ();
@@ -63,17 +83,4 @@ public <T> T serializeAndDeserialize(T obj) throws IOException, ClassNotFoundExc
6383 return (T ) input .readObject ();
6484 }
6585 }
66-
67- /**
68- * Checks whether the state of a restorable object can correctly be captured, serialized and
69- * restored.
70- */
71- public void assertRestorable (Restorable <?> restorable ) throws IOException ,
72- ClassNotFoundException {
73- RestorableState <?> state = restorable .capture ();
74- RestorableState <?> deserializedState = serializeAndDeserialize (state );
75- assertEquals (state , deserializedState );
76- assertEquals (state .hashCode (), deserializedState .hashCode ());
77- assertEquals (state .toString (), deserializedState .toString ());
78- }
7986}
0 commit comments