Skip to content

Commit ce59793

Browse files
committed
Add restorableObjects method to BaseSerializationTest
1 parent 034432a commit ce59793

7 files changed

Lines changed: 57 additions & 50 deletions

File tree

gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@
2020
import com.google.common.collect.ImmutableMap;
2121
import com.google.gcloud.AuthCredentials;
2222
import com.google.gcloud.BaseSerializationTest;
23+
import com.google.gcloud.Restorable;
2324
import com.google.gcloud.bigquery.StandardTableDefinition.StreamingBuffer;
2425

25-
import org.junit.Test;
26-
27-
import java.io.IOException;
2826
import java.io.Serializable;
2927
import java.nio.charset.StandardCharsets;
3028
import java.util.List;
@@ -223,7 +221,7 @@ public class SerializationTest extends BaseSerializationTest {
223221
private static final Job JOB = new Job(BIGQUERY, new JobInfo.BuilderImpl(JOB_INFO));
224222

225223
@Override
226-
public Serializable[] serializableObjects() {
224+
protected Serializable[] serializableObjects() {
227225
BigQueryOptions options = BigQueryOptions.builder()
228226
.projectId("p1")
229227
.authCredentials(AuthCredentials.createForAppEngine())
@@ -246,13 +244,13 @@ public Serializable[] serializableObjects() {
246244
BigQuery.JobListOption.allUsers(), DATASET, TABLE, JOB, options, otherOptions};
247245
}
248246

249-
@Test
250-
public void testWriteChannelState() throws IOException, ClassNotFoundException {
247+
@Override
248+
protected Restorable<?>[] restorableObjects() {
251249
BigQueryOptions options = BigQueryOptions.builder().projectId("p2").build();
252250
// avoid closing when you don't want partial writes upon failure
253251
@SuppressWarnings("resource")
254252
TableDataWriteChannel writer =
255253
new TableDataWriteChannel(options, LOAD_CONFIGURATION, "upload-id");
256-
assertRestorable(writer);
254+
return new Restorable<?>[]{writer};
257255
}
258256
}

gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ public boolean equals(Object obj) {
278278
&& Objects.equals(retriableExceptions, other.retriableExceptions)
279279
&& Objects.equals(nonRetriableExceptions, other.nonRetriableExceptions)
280280
&& Objects.equals(retryInfo, other.retryInfo);
281-
282281
}
283282

284283
/**

gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.gcloud;
1818

19+
import static com.google.common.base.MoreObjects.firstNonNull;
1920
import static org.junit.Assert.assertEquals;
2021
import static org.junit.Assert.assertNotSame;
2122

@@ -30,18 +31,26 @@
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
*/
3539
public 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
}

gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import com.google.common.collect.ImmutableList;
2020

21-
import org.junit.Test;
22-
2321
import java.io.ByteArrayInputStream;
2422
import java.io.IOException;
2523
import java.io.Serializable;
@@ -59,17 +57,18 @@ public class SerializationTest extends BaseSerializationTest {
5957
+ "}";
6058

6159
@Override
62-
public Serializable[] serializableObjects() {
60+
protected Serializable[] serializableObjects() {
6361
return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS};
6462
}
6563

66-
@Test
67-
public void testAuthCredentialState() throws IOException, ClassNotFoundException {
68-
AuthCredentials credentials = AuthCredentials.createForAppEngine();
69-
assertRestorable(credentials);
70-
credentials = AuthCredentials.noAuth();
71-
assertRestorable(credentials);
72-
credentials = AuthCredentials.createForJson(new ByteArrayInputStream(JSON_KEY.getBytes()));
73-
assertRestorable(credentials);
64+
@Override
65+
protected Restorable<?>[] restorableObjects() {
66+
try {
67+
return new Restorable<?>[]{AuthCredentials.createForAppEngine(), AuthCredentials.noAuth(),
68+
AuthCredentials.createForJson(new ByteArrayInputStream(JSON_KEY.getBytes()))};
69+
} catch (IOException ex) {
70+
// never reached
71+
throw new RuntimeException(ex);
72+
}
7473
}
7574
}

gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.api.services.datastore.DatastoreV1;
2222
import com.google.gcloud.AuthCredentials;
2323
import com.google.gcloud.BaseSerializationTest;
24+
import com.google.gcloud.Restorable;
2425
import com.google.gcloud.datastore.StructuredQuery.CompositeFilter;
2526
import com.google.gcloud.datastore.StructuredQuery.OrderBy;
2627
import com.google.gcloud.datastore.StructuredQuery.Projection;
@@ -103,7 +104,7 @@ public class SerializationTest extends BaseSerializationTest {
103104
private static final ProjectionEntity PROJECTION_ENTITY = ProjectionEntity.fromPb(ENTITY1.toPb());
104105

105106
@Override
106-
public java.io.Serializable[] serializableObjects() {
107+
protected java.io.Serializable[] serializableObjects() {
107108
DatastoreOptions options = DatastoreOptions.builder()
108109
.authCredentials(AuthCredentials.createForAppEngine())
109110
.normalizeDataset(false)
@@ -120,4 +121,9 @@ public java.io.Serializable[] serializableObjects() {
120121
EMBEDDED_ENTITY_VALUE2, EMBEDDED_ENTITY_VALUE3, LIST_VALUE, LONG_VALUE, DOUBLE_VALUE,
121122
BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, options, otherOptions};
122123
}
124+
125+
@Override
126+
protected Restorable<?>[] restorableObjects() {
127+
return null;
128+
}
123129
}

gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.google.gcloud.BaseSerializationTest;
2222
import com.google.gcloud.Identity;
2323
import com.google.gcloud.PageImpl;
24+
import com.google.gcloud.Restorable;
2425

2526
import java.io.Serializable;
2627
import java.util.Collections;
@@ -50,12 +51,17 @@ public class SerializationTest extends BaseSerializationTest {
5051
.build();
5152

5253
@Override
53-
public Serializable[] serializableObjects() {
54+
protected Serializable[] serializableObjects() {
5455
ResourceManagerOptions options = ResourceManagerOptions.builder().build();
5556
ResourceManagerOptions otherOptions = options.toBuilder()
5657
.projectId("some-unnecessary-project-ID")
5758
.build();
5859
return new Serializable[]{PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PROJECT, PAGE_RESULT,
5960
PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, options, otherOptions};
6061
}
62+
63+
@Override
64+
protected Restorable<?>[] restorableObjects() {
65+
return null;
66+
}
6167
}

gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
import com.google.gcloud.BaseSerializationTest;
2222
import com.google.gcloud.PageImpl;
2323
import com.google.gcloud.ReadChannel;
24+
import com.google.gcloud.Restorable;
2425
import com.google.gcloud.storage.Acl.Project.ProjectRole;
2526
import com.google.gcloud.storage.spi.StorageRpc;
2627

27-
import org.junit.Test;
28-
29-
import java.io.IOException;
3028
import java.io.Serializable;
3129
import java.util.Collections;
3230
import java.util.Map;
@@ -69,7 +67,7 @@ public class SerializationTest extends BaseSerializationTest {
6967
private static final Map<StorageRpc.Option, ?> EMPTY_RPC_OPTIONS = ImmutableMap.of();
7068

7169
@Override
72-
public Serializable[] serializableObjects() {
70+
protected Serializable[] serializableObjects() {
7371
StorageOptions options = StorageOptions.builder()
7472
.projectId("p1")
7573
.authCredentials(AuthCredentials.createForAppEngine())
@@ -84,21 +82,15 @@ public Serializable[] serializableObjects() {
8482
BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS, options, otherOptions};
8583
}
8684

87-
@Test
88-
public void testReadChannelState() throws IOException, ClassNotFoundException {
85+
@Override
86+
protected Restorable<?>[] restorableObjects() {
8987
StorageOptions options = StorageOptions.builder().projectId("p2").build();
9088
ReadChannel reader =
9189
new BlobReadChannel(options, BlobId.of("b", "n"), EMPTY_RPC_OPTIONS);
92-
assertRestorable(reader);
93-
}
94-
95-
@Test
96-
public void testWriteChannelState() throws IOException, ClassNotFoundException {
97-
StorageOptions options = StorageOptions.builder().projectId("p2").build();
9890
// avoid closing when you don't want partial writes to GCS upon failure
9991
@SuppressWarnings("resource")
10092
BlobWriteChannel writer =
10193
new BlobWriteChannel(options, BlobInfo.builder(BlobId.of("b", "n")).build(), "upload-id");
102-
assertRestorable(writer);
94+
return new Restorable<?>[]{reader, writer};
10395
}
10496
}

0 commit comments

Comments
 (0)