|
16 | 16 |
|
17 | 17 | package com.google.gcloud.bigquery; |
18 | 18 |
|
19 | | -import static org.junit.Assert.assertEquals; |
20 | | -import static org.junit.Assert.assertNotSame; |
21 | | - |
22 | 19 | import com.google.common.collect.ImmutableList; |
23 | 20 | import com.google.common.collect.ImmutableMap; |
24 | 21 | import com.google.gcloud.AuthCredentials; |
25 | | -import com.google.gcloud.RestorableState; |
26 | | -import com.google.gcloud.RetryParams; |
27 | | -import com.google.gcloud.WriteChannel; |
| 22 | +import com.google.gcloud.BaseSerializationTest; |
| 23 | +import com.google.gcloud.Restorable; |
28 | 24 | import com.google.gcloud.bigquery.StandardTableDefinition.StreamingBuffer; |
29 | 25 |
|
30 | | -import org.junit.Test; |
31 | | - |
32 | | -import java.io.ByteArrayInputStream; |
33 | | -import java.io.ByteArrayOutputStream; |
34 | | -import java.io.IOException; |
35 | | -import java.io.ObjectInputStream; |
36 | | -import java.io.ObjectOutputStream; |
37 | 26 | import java.io.Serializable; |
38 | 27 | import java.nio.charset.StandardCharsets; |
39 | 28 | import java.util.List; |
40 | 29 | import java.util.Map; |
41 | 30 |
|
42 | | -public class SerializationTest { |
| 31 | +public class SerializationTest extends BaseSerializationTest { |
43 | 32 |
|
44 | 33 | private static final Acl DOMAIN_ACCESS = |
45 | 34 | Acl.of(new Acl.Domain("domain"), Acl.Role.WRITER); |
@@ -230,75 +219,40 @@ public class SerializationTest { |
230 | 219 | new Dataset(BIGQUERY, new DatasetInfo.BuilderImpl(DATASET_INFO)); |
231 | 220 | private static final Table TABLE = new Table(BIGQUERY, new TableInfo.BuilderImpl(TABLE_INFO)); |
232 | 221 | private static final Job JOB = new Job(BIGQUERY, new JobInfo.BuilderImpl(JOB_INFO)); |
| 222 | + private static final BigQueryException BIG_QUERY_EXCEPTION = |
| 223 | + new BigQueryException(42, "message", BIGQUERY_ERROR); |
233 | 224 |
|
234 | | - @Test |
235 | | - public void testServiceOptions() throws Exception { |
| 225 | + @Override |
| 226 | + protected Serializable[] serializableObjects() { |
236 | 227 | BigQueryOptions options = BigQueryOptions.builder() |
237 | 228 | .projectId("p1") |
238 | 229 | .authCredentials(AuthCredentials.createForAppEngine()) |
239 | 230 | .build(); |
240 | | - BigQueryOptions serializedCopy = serializeAndDeserialize(options); |
241 | | - assertEquals(options, serializedCopy); |
242 | | - |
243 | | - options = options.toBuilder() |
| 231 | + BigQueryOptions otherOptions = options.toBuilder() |
244 | 232 | .projectId("p2") |
245 | | - .retryParams(RetryParams.defaultInstance()) |
246 | 233 | .authCredentials(null) |
247 | 234 | .build(); |
248 | | - serializedCopy = serializeAndDeserialize(options); |
249 | | - assertEquals(options, serializedCopy); |
250 | | - } |
251 | | - |
252 | | - @Test |
253 | | - public void testModelAndRequests() throws Exception { |
254 | | - Serializable[] objects = {DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID, |
| 235 | + return new Serializable[]{DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID, |
255 | 236 | DATASET_INFO, TABLE_ID, CSV_OPTIONS, STREAMING_BUFFER, TABLE_DEFINITION, |
256 | 237 | EXTERNAL_TABLE_DEFINITION, VIEW_DEFINITION, TABLE_SCHEMA, TABLE_INFO, VIEW_INFO, |
257 | 238 | EXTERNAL_TABLE_INFO, INLINE_FUNCTION, URI_FUNCTION, JOB_STATISTICS, EXTRACT_STATISTICS, |
258 | 239 | LOAD_STATISTICS, QUERY_STATISTICS, BIGQUERY_ERROR, JOB_STATUS, JOB_ID, |
259 | 240 | COPY_JOB_CONFIGURATION, EXTRACT_JOB_CONFIGURATION, LOAD_CONFIGURATION, |
260 | 241 | LOAD_JOB_CONFIGURATION, QUERY_JOB_CONFIGURATION, JOB_INFO, INSERT_ALL_REQUEST, |
261 | | - INSERT_ALL_RESPONSE, FIELD_VALUE, QUERY_REQUEST, QUERY_RESPONSE, |
| 242 | + INSERT_ALL_RESPONSE, FIELD_VALUE, QUERY_REQUEST, QUERY_RESPONSE, BIG_QUERY_EXCEPTION, |
262 | 243 | BigQuery.DatasetOption.fields(), BigQuery.DatasetDeleteOption.deleteContents(), |
263 | 244 | BigQuery.DatasetListOption.all(), BigQuery.TableOption.fields(), |
264 | 245 | BigQuery.TableListOption.pageSize(42L), BigQuery.JobOption.fields(), |
265 | | - BigQuery.JobListOption.allUsers(), DATASET, TABLE, JOB}; |
266 | | - for (Serializable obj : objects) { |
267 | | - Object copy = serializeAndDeserialize(obj); |
268 | | - assertEquals(obj, obj); |
269 | | - assertEquals(obj, copy); |
270 | | - assertNotSame(obj, copy); |
271 | | - assertEquals(copy, copy); |
272 | | - } |
| 246 | + BigQuery.JobListOption.allUsers(), DATASET, TABLE, JOB, options, otherOptions}; |
273 | 247 | } |
274 | 248 |
|
275 | | - @Test |
276 | | - public void testWriteChannelState() throws IOException, ClassNotFoundException { |
277 | | - BigQueryOptions options = BigQueryOptions.builder() |
278 | | - .projectId("p2") |
279 | | - .retryParams(RetryParams.defaultInstance()) |
280 | | - .build(); |
| 249 | + @Override |
| 250 | + protected Restorable<?>[] restorableObjects() { |
| 251 | + BigQueryOptions options = BigQueryOptions.builder().projectId("p2").build(); |
281 | 252 | // avoid closing when you don't want partial writes upon failure |
282 | 253 | @SuppressWarnings("resource") |
283 | 254 | TableDataWriteChannel writer = |
284 | 255 | new TableDataWriteChannel(options, LOAD_CONFIGURATION, "upload-id"); |
285 | | - RestorableState<WriteChannel> state = writer.capture(); |
286 | | - RestorableState<WriteChannel> deserializedState = serializeAndDeserialize(state); |
287 | | - assertEquals(state, deserializedState); |
288 | | - assertEquals(state.hashCode(), deserializedState.hashCode()); |
289 | | - assertEquals(state.toString(), deserializedState.toString()); |
290 | | - } |
291 | | - |
292 | | - @SuppressWarnings("unchecked") |
293 | | - private <T> T serializeAndDeserialize(T obj) |
294 | | - throws IOException, ClassNotFoundException { |
295 | | - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); |
296 | | - try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { |
297 | | - output.writeObject(obj); |
298 | | - } |
299 | | - try (ObjectInputStream input = |
300 | | - new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { |
301 | | - return (T) input.readObject(); |
302 | | - } |
| 256 | + return new Restorable<?>[]{writer}; |
303 | 257 | } |
304 | 258 | } |
0 commit comments