From 8ae8a1b160bbbd1e18bedf2a35d014449397ec3e Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Wed, 16 Mar 2016 09:35:09 +0100 Subject: [PATCH 1/7] Create base class for serialization tests --- gcloud-java-bigquery/pom.xml | 7 ++ .../gcloud/bigquery/SerializationTest.java | 47 ++-------- .../google/gcloud/BaseSerializationTest.java | 64 +++++++++++++ .../com/google/gcloud/SerializationTest.java | 33 +++++++ gcloud-java-datastore/pom.xml | 7 ++ .../gcloud/datastore/SerializationTest.java | 90 +++---------------- gcloud-java-resourcemanager/pom.xml | 7 ++ .../resourcemanager/SerializationTest.java | 50 ++--------- gcloud-java-storage/pom.xml | 7 ++ .../gcloud/storage/SerializationTest.java | 47 ++-------- pom.xml | 7 ++ 11 files changed, 163 insertions(+), 203 deletions(-) create mode 100644 gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java create mode 100644 gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java diff --git a/gcloud-java-bigquery/pom.xml b/gcloud-java-bigquery/pom.xml index 9a2137cb987d..5c79f150c722 100644 --- a/gcloud-java-bigquery/pom.xml +++ b/gcloud-java-bigquery/pom.xml @@ -39,6 +39,13 @@ + + ${project.groupId} + gcloud-java-core + ${project.version} + test-jar + test + junit junit diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index 254c8954bf30..f64946e062d7 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java @@ -17,11 +17,11 @@ package com.google.gcloud.bigquery; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.gcloud.AuthCredentials; +import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.RestorableState; import com.google.gcloud.RetryParams; import com.google.gcloud.WriteChannel; @@ -29,17 +29,13 @@ import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; import java.io.Serializable; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map; -public class SerializationTest { +public class SerializationTest extends BaseSerializationTest { private static final Acl DOMAIN_ACCESS = Acl.of(new Acl.Domain("domain"), Acl.Role.WRITER); @@ -231,27 +227,18 @@ public class SerializationTest { private static final Table TABLE = new Table(BIGQUERY, new TableInfo.BuilderImpl(TABLE_INFO)); private static final Job JOB = new Job(BIGQUERY, new JobInfo.BuilderImpl(JOB_INFO)); - @Test - public void testServiceOptions() throws Exception { + @Override + public Serializable[] serializableObjects() { BigQueryOptions options = BigQueryOptions.builder() .projectId("p1") .authCredentials(AuthCredentials.createForAppEngine()) .build(); - BigQueryOptions serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - - options = options.toBuilder() + BigQueryOptions otherOptions = options.toBuilder() .projectId("p2") .retryParams(RetryParams.defaultInstance()) .authCredentials(null) .build(); - serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - } - - @Test - public void testModelAndRequests() throws Exception { - Serializable[] objects = {DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID, + return new Serializable[]{DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID, DATASET_INFO, TABLE_ID, CSV_OPTIONS, STREAMING_BUFFER, TABLE_DEFINITION, EXTERNAL_TABLE_DEFINITION, VIEW_DEFINITION, TABLE_SCHEMA, TABLE_INFO, VIEW_INFO, EXTERNAL_TABLE_INFO, INLINE_FUNCTION, URI_FUNCTION, JOB_STATISTICS, EXTRACT_STATISTICS, @@ -262,14 +249,7 @@ public void testModelAndRequests() throws Exception { BigQuery.DatasetOption.fields(), BigQuery.DatasetDeleteOption.deleteContents(), BigQuery.DatasetListOption.all(), BigQuery.TableOption.fields(), BigQuery.TableListOption.pageSize(42L), BigQuery.JobOption.fields(), - BigQuery.JobListOption.allUsers(), DATASET, TABLE, JOB}; - for (Serializable obj : objects) { - Object copy = serializeAndDeserialize(obj); - assertEquals(obj, obj); - assertEquals(obj, copy); - assertNotSame(obj, copy); - assertEquals(copy, copy); - } + BigQuery.JobListOption.allUsers(), DATASET, TABLE, JOB, options, otherOptions}; } @Test @@ -288,17 +268,4 @@ public void testWriteChannelState() throws IOException, ClassNotFoundException { assertEquals(state.hashCode(), deserializedState.hashCode()); assertEquals(state.toString(), deserializedState.toString()); } - - @SuppressWarnings("unchecked") - private T serializeAndDeserialize(T obj) - throws IOException, ClassNotFoundException { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { - output.writeObject(obj); - } - try (ObjectInputStream input = - new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { - return (T) input.readObject(); - } - } } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java new file mode 100644 index 000000000000..533dfcde6ad8 --- /dev/null +++ b/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java @@ -0,0 +1,64 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; + +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +/** + * Base class for serialization tests. To use this class in your tests override the + * {@code serializableObjects()} method to return all objects that must be serializable. + */ +public abstract class BaseSerializationTest { + + public abstract Serializable[] serializableObjects(); + + @Test + public void testSerializableObjects() throws Exception { + for (Serializable obj : serializableObjects()) { + Object copy = serializeAndDeserialize(obj); + assertEquals(obj, obj); + assertEquals(obj, copy); + assertEquals(obj.hashCode(), copy.hashCode()); + assertEquals(obj.toString(), copy.toString()); + assertNotSame(obj, copy); + assertEquals(copy, copy); + } + } + + @SuppressWarnings("unchecked") + public T serializeAndDeserialize(T obj) + throws IOException, ClassNotFoundException { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { + output.writeObject(obj); + } + try (ObjectInputStream input = + new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { + return (T) input.readObject(); + } + } +} diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java new file mode 100644 index 000000000000..aec9158e1ee0 --- /dev/null +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java @@ -0,0 +1,33 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gcloud; + +import com.google.common.collect.ImmutableList; + +import java.io.Serializable; + +public class SerializationTest extends BaseSerializationTest { + + private static final PageImpl PAGE = + new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2")); + private static final RetryParams RETRY_PARAMS = RetryParams.defaultInstance(); + + @Override + public Serializable[] serializableObjects() { + return new Serializable[]{PAGE, RETRY_PARAMS}; + } +} diff --git a/gcloud-java-datastore/pom.xml b/gcloud-java-datastore/pom.xml index 977b6db22b14..f3b46e22b3c8 100644 --- a/gcloud-java-datastore/pom.xml +++ b/gcloud-java-datastore/pom.xml @@ -33,6 +33,13 @@ + + ${project.groupId} + gcloud-java-core + ${project.version} + test-jar + test + junit junit diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 3976be2cc383..7a6f065d6ca6 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -17,28 +17,17 @@ package com.google.gcloud.datastore; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; import com.google.api.services.datastore.DatastoreV1; -import com.google.common.collect.ImmutableMultimap; -import com.google.common.collect.Multimap; import com.google.gcloud.AuthCredentials; +import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.RetryParams; import com.google.gcloud.datastore.StructuredQuery.CompositeFilter; import com.google.gcloud.datastore.StructuredQuery.OrderBy; import com.google.gcloud.datastore.StructuredQuery.Projection; import com.google.gcloud.datastore.StructuredQuery.PropertyFilter; -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - -public class SerializationTest { +public class SerializationTest extends BaseSerializationTest { private static final IncompleteKey INCOMPLETE_KEY1 = IncompleteKey.builder("ds", "k").ancestors(PathElement.of("p", 1)).build(); @@ -114,82 +103,23 @@ public class SerializationTest { .build(); private static final ProjectionEntity PROJECTION_ENTITY = ProjectionEntity.fromPb(ENTITY1.toPb()); - @SuppressWarnings("rawtypes") - private static final Multimap TYPE_TO_VALUES = - ImmutableMultimap.builder() - .put(ValueType.NULL, NULL_VALUE) - .put(ValueType.KEY, KEY_VALUE) - .put(ValueType.STRING, STRING_VALUE) - .putAll(ValueType.ENTITY, EMBEDDED_ENTITY_VALUE1, EMBEDDED_ENTITY_VALUE2, - EMBEDDED_ENTITY_VALUE3) - .put(ValueType.LIST, LIST_VALUE) - .put(ValueType.LONG, LONG_VALUE) - .put(ValueType.DOUBLE, DOUBLE_VALUE) - .put(ValueType.BOOLEAN, BOOLEAN_VALUE) - .put(ValueType.DATE_TIME, DATE_AND_TIME_VALUE) - .put(ValueType.BLOB, BLOB_VALUE) - .put(ValueType.RAW_VALUE, RAW_VALUE) - .build(); - - @Test - public void testServiceOptions() throws Exception { + @Override + public java.io.Serializable[] serializableObjects() { DatastoreOptions options = DatastoreOptions.builder() .authCredentials(AuthCredentials.createForAppEngine()) .normalizeDataset(false) .projectId("ds1") .build(); - DatastoreOptions serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - - options = options.toBuilder() + DatastoreOptions otherOptions = options.toBuilder() .namespace("ns1") .retryParams(RetryParams.defaultInstance()) .authCredentials(null) .force(true) .build(); - serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - } - - @Test - public void testValues() throws Exception { - for (ValueType valueType : ValueType.values()) { - for (Value value : TYPE_TO_VALUES.get(valueType)) { - Value copy = serializeAndDeserialize(value); - assertEquals(value, value); - assertEquals(value, copy); - assertNotSame(value, copy); - assertEquals(copy, copy); - assertEquals(value.get(), copy.get()); - } - } - } - - @Test - public void testTypes() throws Exception { - Serializable[] types = { KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, ENTITY2, - ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, GQL2, - QUERY1, QUERY2, QUERY3}; - for (Serializable obj : types) { - Object copy = serializeAndDeserialize(obj); - assertEquals(obj, obj); - assertEquals(obj, copy); - assertNotSame(obj, copy); - assertEquals(copy, copy); - } - } - - private T serializeAndDeserialize(T obj) - throws IOException, ClassNotFoundException { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { - output.writeObject(obj); - } - try (ObjectInputStream input = - new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { - @SuppressWarnings("unchecked") - T result = (T) input.readObject(); - return result; - } + return new java.io.Serializable[]{KEY1, KEY2, INCOMPLETE_KEY1, INCOMPLETE_KEY2, ENTITY1, + ENTITY2, ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, + GQL2, QUERY1, QUERY2, QUERY3, NULL_VALUE, KEY_VALUE, STRING_VALUE, EMBEDDED_ENTITY_VALUE1, + EMBEDDED_ENTITY_VALUE2, EMBEDDED_ENTITY_VALUE3, LIST_VALUE, LONG_VALUE, DOUBLE_VALUE, + BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, options, otherOptions}; } } diff --git a/gcloud-java-resourcemanager/pom.xml b/gcloud-java-resourcemanager/pom.xml index c10691d3b07d..c0c48af48f1e 100644 --- a/gcloud-java-resourcemanager/pom.xml +++ b/gcloud-java-resourcemanager/pom.xml @@ -33,6 +33,13 @@ + + ${project.groupId} + gcloud-java-core + ${project.version} + test-jar + test + junit junit diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java index f71f5d7989d6..2dcd3f9d3e7b 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java @@ -16,26 +16,17 @@ package com.google.gcloud.resourcemanager; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; - import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.gcloud.Identity; +import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.PageImpl; import com.google.gcloud.RetryParams; -import org.junit.Test; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Collections; -public class SerializationTest { +public class SerializationTest extends BaseSerializationTest { private static final ResourceManager RESOURCE_MANAGER = ResourceManagerOptions.defaultInstance().service(); @@ -59,41 +50,14 @@ public class SerializationTest { .addBinding(Policy.Role.viewer(), ImmutableSet.of(Identity.user("abc@gmail.com"))) .build(); - @Test - public void testServiceOptions() throws Exception { + @Override + public Serializable[] serializableObjects() { ResourceManagerOptions options = ResourceManagerOptions.builder().build(); - ResourceManagerOptions serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - options = options.toBuilder() + ResourceManagerOptions otherOptions = options.toBuilder() .projectId("some-unnecessary-project-ID") .retryParams(RetryParams.defaultInstance()) .build(); - serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - } - - @Test - public void testModelAndRequests() throws Exception { - Serializable[] objects = {PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PROJECT, PAGE_RESULT, - PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY}; - for (Serializable obj : objects) { - Object copy = serializeAndDeserialize(obj); - assertEquals(obj, obj); - assertEquals(obj, copy); - assertNotSame(obj, copy); - assertEquals(copy, copy); - } - } - - @SuppressWarnings("unchecked") - private T serializeAndDeserialize(T obj) throws IOException, ClassNotFoundException { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { - output.writeObject(obj); - } - try (ObjectInputStream input = - new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { - return (T) input.readObject(); - } + return new Serializable[]{PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PROJECT, PAGE_RESULT, + PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, options, otherOptions}; } } diff --git a/gcloud-java-storage/pom.xml b/gcloud-java-storage/pom.xml index d5f0f6d98660..16427d50de3a 100644 --- a/gcloud-java-storage/pom.xml +++ b/gcloud-java-storage/pom.xml @@ -37,6 +37,13 @@ + + ${project.groupId} + gcloud-java-core + ${project.version} + test-jar + test + junit junit diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java index efa56d9e39b2..ee3d7c946312 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java @@ -17,10 +17,10 @@ package com.google.gcloud.storage; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotSame; import com.google.common.collect.ImmutableMap; import com.google.gcloud.AuthCredentials; +import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.PageImpl; import com.google.gcloud.ReadChannel; import com.google.gcloud.RestorableState; @@ -31,16 +31,12 @@ import org.junit.Test; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.Collections; import java.util.Map; -public class SerializationTest { +public class SerializationTest extends BaseSerializationTest { private static final Storage STORAGE = StorageOptions.builder().projectId("p").build().service(); private static final Acl.Domain ACL_DOMAIN = new Acl.Domain("domain"); @@ -77,37 +73,21 @@ public class SerializationTest { Storage.BucketTargetOption.metagenerationNotMatch(); private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); - @Test - public void testServiceOptions() throws Exception { + @Override + public Serializable[] serializableObjects() { StorageOptions options = StorageOptions.builder() .projectId("p1") .authCredentials(AuthCredentials.createForAppEngine()) .build(); - StorageOptions serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - - options = options.toBuilder() + StorageOptions otherOptions = options.toBuilder() .projectId("p2") .retryParams(RetryParams.defaultInstance()) .authCredentials(null) .build(); - serializedCopy = serializeAndDeserialize(options); - assertEquals(options, serializedCopy); - } - - @Test - public void testModelAndRequests() throws Exception { - Serializable[] objects = {ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL, + return new Serializable[]{ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL, BLOB_INFO, BLOB, BUCKET_INFO, BUCKET, ORIGIN, CORS, BATCH_REQUEST, BATCH_RESPONSE, PAGE_RESULT, BLOB_LIST_OPTIONS, BLOB_SOURCE_OPTIONS, BLOB_TARGET_OPTIONS, - BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS}; - for (Serializable obj : objects) { - Object copy = serializeAndDeserialize(obj); - assertEquals(obj, obj); - assertEquals(obj, copy); - assertNotSame(obj, copy); - assertEquals(copy, copy); - } + BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS, options, otherOptions}; } @Test @@ -142,17 +122,4 @@ public void testWriteChannelState() throws IOException, ClassNotFoundException { assertEquals(state.hashCode(), deserializedState.hashCode()); assertEquals(state.toString(), deserializedState.toString()); } - - @SuppressWarnings("unchecked") - private T serializeAndDeserialize(T obj) - throws IOException, ClassNotFoundException { - ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { - output.writeObject(obj); - } - try (ObjectInputStream input = - new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()))) { - return (T) input.readObject(); - } - } } diff --git a/pom.xml b/pom.xml index b7766ac0e5c6..1abfb094ffb8 100644 --- a/pom.xml +++ b/pom.xml @@ -215,6 +215,13 @@ + + + + test-jar + + + maven-compiler-plugin From 967c5c5a20f39ce232e415b371e7b3a038e453fa Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 17 Mar 2016 09:54:13 +0100 Subject: [PATCH 2/7] Add tests for restorable classes. Fix serialization issues --- .../gcloud/bigquery/SerializationTest.java | 17 +------ .../com/google/gcloud/AuthCredentials.java | 10 ++++ .../com/google/gcloud/ExceptionHandler.java | 22 +++++++++ .../google/gcloud/BaseSerializationTest.java | 19 +++++++- .../com/google/gcloud/SerializationTest.java | 46 ++++++++++++++++++- .../gcloud/datastore/SerializationTest.java | 2 - .../resourcemanager/SerializationTest.java | 4 +- .../gcloud/storage/SerializationTest.java | 29 ++---------- 8 files changed, 101 insertions(+), 48 deletions(-) diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index f64946e062d7..087d263b0fa4 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java @@ -16,15 +16,10 @@ package com.google.gcloud.bigquery; -import static org.junit.Assert.assertEquals; - import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.gcloud.AuthCredentials; import com.google.gcloud.BaseSerializationTest; -import com.google.gcloud.RestorableState; -import com.google.gcloud.RetryParams; -import com.google.gcloud.WriteChannel; import com.google.gcloud.bigquery.StandardTableDefinition.StreamingBuffer; import org.junit.Test; @@ -235,7 +230,6 @@ public Serializable[] serializableObjects() { .build(); BigQueryOptions otherOptions = options.toBuilder() .projectId("p2") - .retryParams(RetryParams.defaultInstance()) .authCredentials(null) .build(); return new Serializable[]{DOMAIN_ACCESS, GROUP_ACCESS, USER_ACCESS, VIEW_ACCESS, DATASET_ID, @@ -254,18 +248,11 @@ public Serializable[] serializableObjects() { @Test public void testWriteChannelState() throws IOException, ClassNotFoundException { - BigQueryOptions options = BigQueryOptions.builder() - .projectId("p2") - .retryParams(RetryParams.defaultInstance()) - .build(); + BigQueryOptions options = BigQueryOptions.builder().projectId("p2").build(); // avoid closing when you don't want partial writes upon failure @SuppressWarnings("resource") TableDataWriteChannel writer = new TableDataWriteChannel(options, LOAD_CONFIGURATION, "upload-id"); - RestorableState state = writer.capture(); - RestorableState deserializedState = serializeAndDeserialize(state); - assertEquals(state, deserializedState); - assertEquals(state.hashCode(), deserializedState.hashCode()); - assertEquals(state.toString(), deserializedState.toString()); + assertRestorable(writer); } } diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java index 6f9e09ca04bc..38265a26be2e 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java @@ -276,6 +276,16 @@ private static class NoAuthCredentialsState public AuthCredentials restore() { return INSTANCE; } + + @Override + public int hashCode() { + return getClass().getName().hashCode(); + } + + @Override + public boolean equals(Object obj) { + return obj instanceof NoAuthCredentialsState; + } } @Override diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java b/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java index 39d4c4e75a1a..0dbcbeb65378 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java @@ -26,6 +26,7 @@ import java.io.Serializable; import java.lang.reflect.Method; +import java.util.Objects; import java.util.Set; import java.util.concurrent.Callable; @@ -259,6 +260,27 @@ boolean shouldRetry(Exception ex) { return retryResult == Interceptor.RetryResult.RETRY; } + @Override + public int hashCode() { + return Objects.hash(interceptors, retriableExceptions, nonRetriableExceptions, retryInfo); + } + + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof ExceptionHandler)) { + return false; + } + ExceptionHandler other = (ExceptionHandler) obj; + return Objects.equals(interceptors, other.interceptors) + && Objects.equals(retriableExceptions, other.retriableExceptions) + && Objects.equals(nonRetriableExceptions, other.nonRetriableExceptions) + && Objects.equals(retryInfo, other.retryInfo); + + } + /** * Returns an instance which retry any checked exception and abort on any runtime exception. */ diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java index 533dfcde6ad8..a8136bfbf0cc 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java @@ -34,6 +34,9 @@ */ public abstract class BaseSerializationTest { + /** + * Returns all objects for which correct serialization must be tested. + */ public abstract Serializable[] serializableObjects(); @Test @@ -50,8 +53,7 @@ public void testSerializableObjects() throws Exception { } @SuppressWarnings("unchecked") - public T serializeAndDeserialize(T obj) - throws IOException, ClassNotFoundException { + public T serializeAndDeserialize(T obj) throws IOException, ClassNotFoundException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); try (ObjectOutputStream output = new ObjectOutputStream(bytes)) { output.writeObject(obj); @@ -61,4 +63,17 @@ public T serializeAndDeserialize(T obj) return (T) input.readObject(); } } + + /** + * Checks whether the state of a restorable object can correctly be captured, serialized and + * restored. + */ + public void assertRestorable(Restorable restorable) throws IOException, + ClassNotFoundException { + RestorableState state = restorable.capture(); + RestorableState deserializedState = serializeAndDeserialize(state); + assertEquals(state, deserializedState); + assertEquals(state.hashCode(), deserializedState.hashCode()); + assertEquals(state.toString(), deserializedState.toString()); + } } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java index aec9158e1ee0..46f1f73563f7 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java @@ -18,16 +18,60 @@ import com.google.common.collect.ImmutableList; +import org.junit.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; import java.io.Serializable; public class SerializationTest extends BaseSerializationTest { + private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.defaultInstance(); + private static final Identity IDENTITY = Identity.allAuthenticatedUsers(); private static final PageImpl PAGE = new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2")); private static final RetryParams RETRY_PARAMS = RetryParams.defaultInstance(); + private static final String JSON_KEY = "{\n" + + " \"private_key_id\": \"somekeyid\",\n" + + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" + + "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" + + "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" + + "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZakGMn8EE1uVa2" + + "LgczOjwWHGi99MFjxSer5m9\\n1tCa3/KEGKiS/YL71JvjwX3mb+cewlkcmweBKZHM2JPTk0ZednFSpVZMtycjkbLa" + + "\\ndYOS8V85AgMBewECggEBAKksaldajfDZDV6nGqbFjMiizAKJolr/M3OQw16K6o3/\\n0S31xIe3sSlgW0+UbYlF" + + "4U8KifhManD1apVSC3csafaspP4RZUHFhtBywLO9pR5c\\nr6S5aLp+gPWFyIp1pfXbWGvc5VY/v9x7ya1VEa6rXvL" + + "sKupSeWAW4tMj3eo/64ge\\nsdaceaLYw52KeBYiT6+vpsnYrEkAHO1fF/LavbLLOFJmFTMxmsNaG0tuiJHgjshB\\" + + "n82DpMCbXG9YcCgI/DbzuIjsdj2JC1cascSP//3PmefWysucBQe7Jryb6NQtASmnv\\nCdDw/0jmZTEjpe4S1lxfHp" + + "lAhHFtdgYTvyYtaLZiVVkCgYEA8eVpof2rceecw/I6\\n5ng1q3Hl2usdWV/4mZMvR0fOemacLLfocX6IYxT1zA1FF" + + "JlbXSRsJMf/Qq39mOR2\\nSpW+hr4jCoHeRVYLgsbggtrevGmILAlNoqCMpGZ6vDmJpq6ECV9olliDvpPgWOP+\\nm" + + "YPDreFBGxWvQrADNbRt2dmGsrsCgYEAyUHqB2wvJHFqdmeBsaacewzV8x9WgmeX\\ngUIi9REwXlGDW0Mz50dxpxcK" + + "CAYn65+7TCnY5O/jmL0VRxU1J2mSWyWTo1C+17L0\\n3fUqjxL1pkefwecxwecvC+gFFYdJ4CQ/MHHXU81Lwl1iWdF" + + "Cd2UoGddYaOF+KNeM\\nHC7cmqra+JsCgYEAlUNywzq8nUg7282E+uICfCB0LfwejuymR93CtsFgb7cRd6ak\\nECR" + + "8FGfCpH8ruWJINllbQfcHVCX47ndLZwqv3oVFKh6pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl" + + "HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa" + + "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi" + + "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG" + + "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk" + + "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" + + " \"client_email\": \"someclientid@developer.gserviceaccount.com\",\n" + + " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" + + " \"type\": \"service_account\"\n" + + "}"; @Override public Serializable[] serializableObjects() { - return new Serializable[]{PAGE, RETRY_PARAMS}; + return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS}; + } + + @Test + public void testAuthCredentialState() throws IOException, ClassNotFoundException { + AuthCredentials credentials = AuthCredentials.createApplicationDefaults(); + assertRestorable(credentials); + credentials = AuthCredentials.createForAppEngine(); + assertRestorable(credentials); + credentials = AuthCredentials.noAuth(); + assertRestorable(credentials); + credentials = AuthCredentials.createForJson(new ByteArrayInputStream(JSON_KEY.getBytes())); + assertRestorable(credentials); } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 7a6f065d6ca6..5cbf63b9624d 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -21,7 +21,6 @@ import com.google.api.services.datastore.DatastoreV1; import com.google.gcloud.AuthCredentials; import com.google.gcloud.BaseSerializationTest; -import com.google.gcloud.RetryParams; import com.google.gcloud.datastore.StructuredQuery.CompositeFilter; import com.google.gcloud.datastore.StructuredQuery.OrderBy; import com.google.gcloud.datastore.StructuredQuery.Projection; @@ -112,7 +111,6 @@ public java.io.Serializable[] serializableObjects() { .build(); DatastoreOptions otherOptions = options.toBuilder() .namespace("ns1") - .retryParams(RetryParams.defaultInstance()) .authCredentials(null) .force(true) .build(); diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java index 2dcd3f9d3e7b..786865e0f872 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java @@ -18,10 +18,9 @@ import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; -import com.google.gcloud.Identity; import com.google.gcloud.BaseSerializationTest; +import com.google.gcloud.Identity; import com.google.gcloud.PageImpl; -import com.google.gcloud.RetryParams; import java.io.Serializable; import java.util.Collections; @@ -55,7 +54,6 @@ public Serializable[] serializableObjects() { ResourceManagerOptions options = ResourceManagerOptions.builder().build(); ResourceManagerOptions otherOptions = options.toBuilder() .projectId("some-unnecessary-project-ID") - .retryParams(RetryParams.defaultInstance()) .build(); return new Serializable[]{PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PROJECT, PAGE_RESULT, PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, options, otherOptions}; diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java index ee3d7c946312..392636ccd100 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java @@ -16,16 +16,11 @@ package com.google.gcloud.storage; -import static org.junit.Assert.assertEquals; - import com.google.common.collect.ImmutableMap; import com.google.gcloud.AuthCredentials; import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.PageImpl; import com.google.gcloud.ReadChannel; -import com.google.gcloud.RestorableState; -import com.google.gcloud.RetryParams; -import com.google.gcloud.WriteChannel; import com.google.gcloud.storage.Acl.Project.ProjectRole; import com.google.gcloud.storage.spi.StorageRpc; @@ -81,7 +76,6 @@ public Serializable[] serializableObjects() { .build(); StorageOptions otherOptions = options.toBuilder() .projectId("p2") - .retryParams(RetryParams.defaultInstance()) .authCredentials(null) .build(); return new Serializable[]{ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL, @@ -92,34 +86,19 @@ public Serializable[] serializableObjects() { @Test public void testReadChannelState() throws IOException, ClassNotFoundException { - StorageOptions options = StorageOptions.builder() - .projectId("p2") - .retryParams(RetryParams.defaultInstance()) - .build(); + StorageOptions options = StorageOptions.builder().projectId("p2").build(); ReadChannel reader = new BlobReadChannel(options, BlobId.of("b", "n"), EMPTY_RPC_OPTIONS); - RestorableState state = reader.capture(); - RestorableState deserializedState = serializeAndDeserialize(state); - assertEquals(state, deserializedState); - assertEquals(state.hashCode(), deserializedState.hashCode()); - assertEquals(state.toString(), deserializedState.toString()); - reader.close(); + assertRestorable(reader); } @Test public void testWriteChannelState() throws IOException, ClassNotFoundException { - StorageOptions options = StorageOptions.builder() - .projectId("p2") - .retryParams(RetryParams.defaultInstance()) - .build(); + StorageOptions options = StorageOptions.builder().projectId("p2").build(); // avoid closing when you don't want partial writes to GCS upon failure @SuppressWarnings("resource") BlobWriteChannel writer = new BlobWriteChannel(options, BlobInfo.builder(BlobId.of("b", "n")).build(), "upload-id"); - RestorableState state = writer.capture(); - RestorableState deserializedState = serializeAndDeserialize(state); - assertEquals(state, deserializedState); - assertEquals(state.hashCode(), deserializedState.hashCode()); - assertEquals(state.toString(), deserializedState.toString()); + assertRestorable(writer); } } From 034432a4915439f8323cc898e38c47f5cd4f1788 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 17 Mar 2016 17:28:55 +0100 Subject: [PATCH 3/7] Remove serialization test for ApplicationDefaultAuthCredentials --- .../src/test/java/com/google/gcloud/SerializationTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java index 46f1f73563f7..a9526a42d7d7 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java @@ -65,9 +65,7 @@ public Serializable[] serializableObjects() { @Test public void testAuthCredentialState() throws IOException, ClassNotFoundException { - AuthCredentials credentials = AuthCredentials.createApplicationDefaults(); - assertRestorable(credentials); - credentials = AuthCredentials.createForAppEngine(); + AuthCredentials credentials = AuthCredentials.createForAppEngine(); assertRestorable(credentials); credentials = AuthCredentials.noAuth(); assertRestorable(credentials); From ce597930debaa6ab8ffc16af25547e1bfa7283a6 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 17 Mar 2016 18:59:46 +0100 Subject: [PATCH 4/7] Add restorableObjects method to BaseSerializationTest --- .../gcloud/bigquery/SerializationTest.java | 12 +++--- .../com/google/gcloud/ExceptionHandler.java | 1 - .../google/gcloud/BaseSerializationTest.java | 39 +++++++++++-------- .../com/google/gcloud/SerializationTest.java | 21 +++++----- .../gcloud/datastore/SerializationTest.java | 8 +++- .../resourcemanager/SerializationTest.java | 8 +++- .../gcloud/storage/SerializationTest.java | 18 +++------ 7 files changed, 57 insertions(+), 50 deletions(-) diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index 087d263b0fa4..74644216c109 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java @@ -20,11 +20,9 @@ import com.google.common.collect.ImmutableMap; import com.google.gcloud.AuthCredentials; import com.google.gcloud.BaseSerializationTest; +import com.google.gcloud.Restorable; import com.google.gcloud.bigquery.StandardTableDefinition.StreamingBuffer; -import org.junit.Test; - -import java.io.IOException; import java.io.Serializable; import java.nio.charset.StandardCharsets; import java.util.List; @@ -223,7 +221,7 @@ public class SerializationTest extends BaseSerializationTest { private static final Job JOB = new Job(BIGQUERY, new JobInfo.BuilderImpl(JOB_INFO)); @Override - public Serializable[] serializableObjects() { + protected Serializable[] serializableObjects() { BigQueryOptions options = BigQueryOptions.builder() .projectId("p1") .authCredentials(AuthCredentials.createForAppEngine()) @@ -246,13 +244,13 @@ public Serializable[] serializableObjects() { BigQuery.JobListOption.allUsers(), DATASET, TABLE, JOB, options, otherOptions}; } - @Test - public void testWriteChannelState() throws IOException, ClassNotFoundException { + @Override + protected Restorable[] restorableObjects() { BigQueryOptions options = BigQueryOptions.builder().projectId("p2").build(); // avoid closing when you don't want partial writes upon failure @SuppressWarnings("resource") TableDataWriteChannel writer = new TableDataWriteChannel(options, LOAD_CONFIGURATION, "upload-id"); - assertRestorable(writer); + return new Restorable[]{writer}; } } diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java b/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java index 0dbcbeb65378..0b3c923d1eb9 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/ExceptionHandler.java @@ -278,7 +278,6 @@ public boolean equals(Object obj) { && Objects.equals(retriableExceptions, other.retriableExceptions) && Objects.equals(nonRetriableExceptions, other.nonRetriableExceptions) && Objects.equals(retryInfo, other.retryInfo); - } /** diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java index a8136bfbf0cc..e9ab3d47984b 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/BaseSerializationTest.java @@ -16,6 +16,7 @@ package com.google.gcloud; +import static com.google.common.base.MoreObjects.firstNonNull; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotSame; @@ -30,18 +31,26 @@ /** * Base class for serialization tests. To use this class in your tests override the - * {@code serializableObjects()} method to return all objects that must be serializable. + * {@code serializableObjects()} method to return all objects that must be serializable. Also + * override {@code restorableObjects()} method to return all restorable objects whose state must be + * tested for proper serialization. Both methods can return {@code null} if no such object needs to + * be tested. */ public abstract class BaseSerializationTest { /** * Returns all objects for which correct serialization must be tested. */ - public abstract Serializable[] serializableObjects(); + protected abstract Serializable[] serializableObjects(); + + /** + * Returns all restorable objects whose state must be tested for proper serialization. + */ + protected abstract Restorable[] restorableObjects(); @Test public void testSerializableObjects() throws Exception { - for (Serializable obj : serializableObjects()) { + for (Serializable obj : firstNonNull(serializableObjects(), new Serializable[0])) { Object copy = serializeAndDeserialize(obj); assertEquals(obj, obj); assertEquals(obj, copy); @@ -52,6 +61,17 @@ public void testSerializableObjects() throws Exception { } } + @Test + public void testRestorableObjects() throws Exception { + for (Restorable restorable : firstNonNull(restorableObjects(), new Restorable[0])) { + RestorableState state = restorable.capture(); + RestorableState deserializedState = serializeAndDeserialize(state); + assertEquals(state, deserializedState); + assertEquals(state.hashCode(), deserializedState.hashCode()); + assertEquals(state.toString(), deserializedState.toString()); + } + } + @SuppressWarnings("unchecked") public T serializeAndDeserialize(T obj) throws IOException, ClassNotFoundException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); @@ -63,17 +83,4 @@ public T serializeAndDeserialize(T obj) throws IOException, ClassNotFoundExc return (T) input.readObject(); } } - - /** - * Checks whether the state of a restorable object can correctly be captured, serialized and - * restored. - */ - public void assertRestorable(Restorable restorable) throws IOException, - ClassNotFoundException { - RestorableState state = restorable.capture(); - RestorableState deserializedState = serializeAndDeserialize(state); - assertEquals(state, deserializedState); - assertEquals(state.hashCode(), deserializedState.hashCode()); - assertEquals(state.toString(), deserializedState.toString()); - } } diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java index a9526a42d7d7..c4bcfcc13b1c 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java @@ -18,8 +18,6 @@ import com.google.common.collect.ImmutableList; -import org.junit.Test; - import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.Serializable; @@ -59,17 +57,18 @@ public class SerializationTest extends BaseSerializationTest { + "}"; @Override - public Serializable[] serializableObjects() { + protected Serializable[] serializableObjects() { return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS}; } - @Test - public void testAuthCredentialState() throws IOException, ClassNotFoundException { - AuthCredentials credentials = AuthCredentials.createForAppEngine(); - assertRestorable(credentials); - credentials = AuthCredentials.noAuth(); - assertRestorable(credentials); - credentials = AuthCredentials.createForJson(new ByteArrayInputStream(JSON_KEY.getBytes())); - assertRestorable(credentials); + @Override + protected Restorable[] restorableObjects() { + try { + return new Restorable[]{AuthCredentials.createForAppEngine(), AuthCredentials.noAuth(), + AuthCredentials.createForJson(new ByteArrayInputStream(JSON_KEY.getBytes()))}; + } catch (IOException ex) { + // never reached + throw new RuntimeException(ex); + } } } diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 5cbf63b9624d..3ea74ef7c4d6 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -21,6 +21,7 @@ import com.google.api.services.datastore.DatastoreV1; import com.google.gcloud.AuthCredentials; import com.google.gcloud.BaseSerializationTest; +import com.google.gcloud.Restorable; import com.google.gcloud.datastore.StructuredQuery.CompositeFilter; import com.google.gcloud.datastore.StructuredQuery.OrderBy; import com.google.gcloud.datastore.StructuredQuery.Projection; @@ -103,7 +104,7 @@ public class SerializationTest extends BaseSerializationTest { private static final ProjectionEntity PROJECTION_ENTITY = ProjectionEntity.fromPb(ENTITY1.toPb()); @Override - public java.io.Serializable[] serializableObjects() { + protected java.io.Serializable[] serializableObjects() { DatastoreOptions options = DatastoreOptions.builder() .authCredentials(AuthCredentials.createForAppEngine()) .normalizeDataset(false) @@ -120,4 +121,9 @@ public java.io.Serializable[] serializableObjects() { EMBEDDED_ENTITY_VALUE2, EMBEDDED_ENTITY_VALUE3, LIST_VALUE, LONG_VALUE, DOUBLE_VALUE, BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, options, otherOptions}; } + + @Override + protected Restorable[] restorableObjects() { + return null; + } } diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java index 786865e0f872..5bad065003e0 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java @@ -21,6 +21,7 @@ import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.Identity; import com.google.gcloud.PageImpl; +import com.google.gcloud.Restorable; import java.io.Serializable; import java.util.Collections; @@ -50,7 +51,7 @@ public class SerializationTest extends BaseSerializationTest { .build(); @Override - public Serializable[] serializableObjects() { + protected Serializable[] serializableObjects() { ResourceManagerOptions options = ResourceManagerOptions.builder().build(); ResourceManagerOptions otherOptions = options.toBuilder() .projectId("some-unnecessary-project-ID") @@ -58,4 +59,9 @@ public Serializable[] serializableObjects() { return new Serializable[]{PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PROJECT, PAGE_RESULT, PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, options, otherOptions}; } + + @Override + protected Restorable[] restorableObjects() { + return null; + } } diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java index 392636ccd100..6aae7b1a0cb2 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java @@ -21,12 +21,10 @@ import com.google.gcloud.BaseSerializationTest; import com.google.gcloud.PageImpl; import com.google.gcloud.ReadChannel; +import com.google.gcloud.Restorable; import com.google.gcloud.storage.Acl.Project.ProjectRole; import com.google.gcloud.storage.spi.StorageRpc; -import org.junit.Test; - -import java.io.IOException; import java.io.Serializable; import java.util.Collections; import java.util.Map; @@ -69,7 +67,7 @@ public class SerializationTest extends BaseSerializationTest { private static final Map EMPTY_RPC_OPTIONS = ImmutableMap.of(); @Override - public Serializable[] serializableObjects() { + protected Serializable[] serializableObjects() { StorageOptions options = StorageOptions.builder() .projectId("p1") .authCredentials(AuthCredentials.createForAppEngine()) @@ -84,21 +82,15 @@ public Serializable[] serializableObjects() { BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS, options, otherOptions}; } - @Test - public void testReadChannelState() throws IOException, ClassNotFoundException { + @Override + protected Restorable[] restorableObjects() { StorageOptions options = StorageOptions.builder().projectId("p2").build(); ReadChannel reader = new BlobReadChannel(options, BlobId.of("b", "n"), EMPTY_RPC_OPTIONS); - assertRestorable(reader); - } - - @Test - public void testWriteChannelState() throws IOException, ClassNotFoundException { - StorageOptions options = StorageOptions.builder().projectId("p2").build(); // avoid closing when you don't want partial writes to GCS upon failure @SuppressWarnings("resource") BlobWriteChannel writer = new BlobWriteChannel(options, BlobInfo.builder(BlobId.of("b", "n")).build(), "upload-id"); - assertRestorable(writer); + return new Restorable[]{reader, writer}; } } From 5c4e2886f8257470932341191b699e1d621c890e Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 18 Mar 2016 16:01:58 +0100 Subject: [PATCH 5/7] Make NoAuthCredentials constructor private --- .../src/main/java/com/google/gcloud/AuthCredentials.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java index 38265a26be2e..27cafc181505 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/AuthCredentials.java @@ -288,6 +288,8 @@ public boolean equals(Object obj) { } } + private NoAuthCredentials() {} + @Override public GoogleCredentials credentials() { return null; From 19e0c6e9736bc13a26d6a8f811322ee97e477200 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 18 Mar 2016 16:03:46 +0100 Subject: [PATCH 6/7] Add IamPolicy to SerializationTest --- .../com/google/gcloud/SerializationTest.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java index c4bcfcc13b1c..194479ac365c 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java @@ -24,11 +24,34 @@ public class SerializationTest extends BaseSerializationTest { + private static class SomeIamPolicy extends IamPolicy { + + private static final long serialVersionUID = 271243551016958285L; + + private static class Builder extends IamPolicy.Builder { + + @Override + public SomeIamPolicy build() { + return new SomeIamPolicy(this); + } + } + + protected SomeIamPolicy(Builder builder) { + super(builder); + } + + @Override + public Builder toBuilder() { + return new Builder(); + } + } + private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.defaultInstance(); private static final Identity IDENTITY = Identity.allAuthenticatedUsers(); private static final PageImpl PAGE = new PageImpl<>(null, "cursor", ImmutableList.of("string1", "string2")); private static final RetryParams RETRY_PARAMS = RetryParams.defaultInstance(); + private static final SomeIamPolicy SOME_IAM_POLICY = new SomeIamPolicy.Builder().build(); private static final String JSON_KEY = "{\n" + " \"private_key_id\": \"somekeyid\",\n" + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" @@ -58,7 +81,7 @@ public class SerializationTest extends BaseSerializationTest { @Override protected Serializable[] serializableObjects() { - return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS}; + return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS, SOME_IAM_POLICY}; } @Override From 83ddb08d0e6aae40707ab8b9d071fa62854e8769 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Fri, 18 Mar 2016 16:08:37 +0100 Subject: [PATCH 7/7] Add equals and hashCode to exceptions. Add exceptions to serialization tests --- .../gcloud/bigquery/BigQueryException.java | 18 ++++++++ .../gcloud/bigquery/SerializationTest.java | 4 +- .../google/gcloud/BaseServiceException.java | 45 ++++++++++++++----- .../com/google/gcloud/SerializationTest.java | 5 ++- .../gcloud/datastore/SerializationTest.java | 5 ++- .../resourcemanager/SerializationTest.java | 5 ++- .../gcloud/storage/SerializationTest.java | 4 +- 7 files changed, 71 insertions(+), 15 deletions(-) diff --git a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java index a157afd25db2..e78734a2899e 100644 --- a/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java +++ b/gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java @@ -22,6 +22,7 @@ import com.google.gcloud.RetryHelper.RetryInterruptedException; import java.io.IOException; +import java.util.Objects; import java.util.Set; /** @@ -73,6 +74,23 @@ protected Set retryableErrors() { return RETRYABLE_ERRORS; } + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof BigQueryException)) { + return false; + } + BigQueryException other = (BigQueryException) obj; + return super.equals(other) && Objects.equals(error, other.error); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), error); + } + /** * Translate RetryHelperException to the BigQueryException that caused the error. This method will * always throw an exception. diff --git a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java index 74644216c109..111df074ffa2 100644 --- a/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java +++ b/gcloud-java-bigquery/src/test/java/com/google/gcloud/bigquery/SerializationTest.java @@ -219,6 +219,8 @@ public class SerializationTest extends BaseSerializationTest { new Dataset(BIGQUERY, new DatasetInfo.BuilderImpl(DATASET_INFO)); private static final Table TABLE = new Table(BIGQUERY, new TableInfo.BuilderImpl(TABLE_INFO)); private static final Job JOB = new Job(BIGQUERY, new JobInfo.BuilderImpl(JOB_INFO)); + private static final BigQueryException BIG_QUERY_EXCEPTION = + new BigQueryException(42, "message", BIGQUERY_ERROR); @Override protected Serializable[] serializableObjects() { @@ -237,7 +239,7 @@ protected Serializable[] serializableObjects() { LOAD_STATISTICS, QUERY_STATISTICS, BIGQUERY_ERROR, JOB_STATUS, JOB_ID, COPY_JOB_CONFIGURATION, EXTRACT_JOB_CONFIGURATION, LOAD_CONFIGURATION, LOAD_JOB_CONFIGURATION, QUERY_JOB_CONFIGURATION, JOB_INFO, INSERT_ALL_REQUEST, - INSERT_ALL_RESPONSE, FIELD_VALUE, QUERY_REQUEST, QUERY_RESPONSE, + INSERT_ALL_RESPONSE, FIELD_VALUE, QUERY_REQUEST, QUERY_RESPONSE, BIG_QUERY_EXCEPTION, BigQuery.DatasetOption.fields(), BigQuery.DatasetDeleteOption.deleteContents(), BigQuery.DatasetListOption.all(), BigQuery.TableOption.fields(), BigQuery.TableListOption.pageSize(42L), BigQuery.JobOption.fields(), diff --git a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java index 365243904436..4e0d03e0073a 100644 --- a/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java +++ b/gcloud-java-core/src/main/java/com/google/gcloud/BaseServiceException.java @@ -32,6 +32,16 @@ */ public class BaseServiceException extends RuntimeException { + private static final long serialVersionUID = 759921776378760835L; + public static final int UNKNOWN_CODE = 0; + + private final int code; + private final boolean retryable; + private final String reason; + private final boolean idempotent; + private final String location; + private final String debugInfo; + protected static final class Error implements Serializable { private static final long serialVersionUID = -4019600198652965721L; @@ -79,16 +89,6 @@ public int hashCode() { } } - private static final long serialVersionUID = 759921776378760835L; - public static final int UNKNOWN_CODE = 0; - - private final int code; - private final boolean retryable; - private final String reason; - private final boolean idempotent; - private final String location; - private final String debugInfo; - public BaseServiceException(IOException exception, boolean idempotent) { super(message(exception), exception); int code = UNKNOWN_CODE; @@ -198,6 +198,31 @@ protected String debugInfo() { return debugInfo; } + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof BaseServiceException)) { + return false; + } + BaseServiceException other = (BaseServiceException) obj; + return Objects.equals(getCause(), other.getCause()) + && Objects.equals(getMessage(), other.getMessage()) + && code == other.code + && retryable == other.retryable + && Objects.equals(reason, other.reason) + && idempotent == other.idempotent + && Objects.equals(location, other.location) + && Objects.equals(debugInfo, other.debugInfo); + } + + @Override + public int hashCode() { + return Objects.hash(getCause(), getMessage(), code, retryable, reason, idempotent, location, + debugInfo); + } + protected static String reason(GoogleJsonError error) { if (error.getErrors() != null && !error.getErrors().isEmpty()) { return error.getErrors().get(0).getReason(); diff --git a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java index 194479ac365c..3255a17333aa 100644 --- a/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java +++ b/gcloud-java-core/src/test/java/com/google/gcloud/SerializationTest.java @@ -46,6 +46,8 @@ public Builder toBuilder() { } } + private static final BaseServiceException BASE_SERVICE_EXCEPTION = + new BaseServiceException(42, "message", "reason", true); private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.defaultInstance(); private static final Identity IDENTITY = Identity.allAuthenticatedUsers(); private static final PageImpl PAGE = @@ -81,7 +83,8 @@ public Builder toBuilder() { @Override protected Serializable[] serializableObjects() { - return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS, SOME_IAM_POLICY}; + return new Serializable[]{BASE_SERVICE_EXCEPTION, EXCEPTION_HANDLER, IDENTITY, PAGE, + RETRY_PARAMS, SOME_IAM_POLICY}; } @Override diff --git a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java index 3ea74ef7c4d6..b9e78800ffab 100644 --- a/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java +++ b/gcloud-java-datastore/src/test/java/com/google/gcloud/datastore/SerializationTest.java @@ -102,6 +102,8 @@ public class SerializationTest extends BaseSerializationTest { .addValue(new NullValue()) .build(); private static final ProjectionEntity PROJECTION_ENTITY = ProjectionEntity.fromPb(ENTITY1.toPb()); + private static final DatastoreException DATASTORE_EXCEPTION = + new DatastoreException(42, "message", "reason"); @Override protected java.io.Serializable[] serializableObjects() { @@ -119,7 +121,8 @@ protected java.io.Serializable[] serializableObjects() { ENTITY2, ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1, GQL2, QUERY1, QUERY2, QUERY3, NULL_VALUE, KEY_VALUE, STRING_VALUE, EMBEDDED_ENTITY_VALUE1, EMBEDDED_ENTITY_VALUE2, EMBEDDED_ENTITY_VALUE3, LIST_VALUE, LONG_VALUE, DOUBLE_VALUE, - BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, options, otherOptions}; + BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, DATASTORE_EXCEPTION, options, + otherOptions}; } @Override diff --git a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java index 5bad065003e0..c6e907da16a0 100644 --- a/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java +++ b/gcloud-java-resourcemanager/src/test/java/com/google/gcloud/resourcemanager/SerializationTest.java @@ -49,6 +49,8 @@ public class SerializationTest extends BaseSerializationTest { private static final Policy POLICY = Policy.builder() .addBinding(Policy.Role.viewer(), ImmutableSet.of(Identity.user("abc@gmail.com"))) .build(); + private static final ResourceManagerException RESOURCE_MANAGER_EXCEPTION = + new ResourceManagerException(42, "message"); @Override protected Serializable[] serializableObjects() { @@ -57,7 +59,8 @@ protected Serializable[] serializableObjects() { .projectId("some-unnecessary-project-ID") .build(); return new Serializable[]{PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PROJECT, PAGE_RESULT, - PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, options, otherOptions}; + PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, RESOURCE_MANAGER_EXCEPTION, options, + otherOptions}; } @Override diff --git a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java index 6aae7b1a0cb2..613cb81c3549 100644 --- a/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java +++ b/gcloud-java-storage/src/test/java/com/google/gcloud/storage/SerializationTest.java @@ -52,6 +52,7 @@ public class SerializationTest extends BaseSerializationTest { Collections.>emptyList()); private static final PageImpl PAGE_RESULT = new PageImpl<>(null, "c", Collections.singletonList(BLOB)); + private static final StorageException STORAGE_EXCEPTION = new StorageException(42, "message"); private static final Storage.BlobListOption BLOB_LIST_OPTIONS = Storage.BlobListOption.pageSize(100); private static final Storage.BlobSourceOption BLOB_SOURCE_OPTIONS = @@ -79,7 +80,8 @@ protected Serializable[] serializableObjects() { return new Serializable[]{ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL, BLOB_INFO, BLOB, BUCKET_INFO, BUCKET, ORIGIN, CORS, BATCH_REQUEST, BATCH_RESPONSE, PAGE_RESULT, BLOB_LIST_OPTIONS, BLOB_SOURCE_OPTIONS, BLOB_TARGET_OPTIONS, - BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS, options, otherOptions}; + BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS, STORAGE_EXCEPTION, + options, otherOptions}; } @Override