Skip to content

Commit 83ddb08

Browse files
committed
Add equals and hashCode to exceptions. Add exceptions to serialization tests
1 parent 19e0c6e commit 83ddb08

7 files changed

Lines changed: 71 additions & 15 deletions

File tree

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryException.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.google.gcloud.RetryHelper.RetryInterruptedException;
2323

2424
import java.io.IOException;
25+
import java.util.Objects;
2526
import java.util.Set;
2627

2728
/**
@@ -73,6 +74,23 @@ protected Set<Error> retryableErrors() {
7374
return RETRYABLE_ERRORS;
7475
}
7576

77+
@Override
78+
public boolean equals(Object obj) {
79+
if (obj == this) {
80+
return true;
81+
}
82+
if (!(obj instanceof BigQueryException)) {
83+
return false;
84+
}
85+
BigQueryException other = (BigQueryException) obj;
86+
return super.equals(other) && Objects.equals(error, other.error);
87+
}
88+
89+
@Override
90+
public int hashCode() {
91+
return Objects.hash(super.hashCode(), error);
92+
}
93+
7694
/**
7795
* Translate RetryHelperException to the BigQueryException that caused the error. This method will
7896
* always throw an exception.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ public class SerializationTest extends BaseSerializationTest {
219219
new Dataset(BIGQUERY, new DatasetInfo.BuilderImpl(DATASET_INFO));
220220
private static final Table TABLE = new Table(BIGQUERY, new TableInfo.BuilderImpl(TABLE_INFO));
221221
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);
222224

223225
@Override
224226
protected Serializable[] serializableObjects() {
@@ -237,7 +239,7 @@ protected Serializable[] serializableObjects() {
237239
LOAD_STATISTICS, QUERY_STATISTICS, BIGQUERY_ERROR, JOB_STATUS, JOB_ID,
238240
COPY_JOB_CONFIGURATION, EXTRACT_JOB_CONFIGURATION, LOAD_CONFIGURATION,
239241
LOAD_JOB_CONFIGURATION, QUERY_JOB_CONFIGURATION, JOB_INFO, INSERT_ALL_REQUEST,
240-
INSERT_ALL_RESPONSE, FIELD_VALUE, QUERY_REQUEST, QUERY_RESPONSE,
242+
INSERT_ALL_RESPONSE, FIELD_VALUE, QUERY_REQUEST, QUERY_RESPONSE, BIG_QUERY_EXCEPTION,
241243
BigQuery.DatasetOption.fields(), BigQuery.DatasetDeleteOption.deleteContents(),
242244
BigQuery.DatasetListOption.all(), BigQuery.TableOption.fields(),
243245
BigQuery.TableListOption.pageSize(42L), BigQuery.JobOption.fields(),

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
*/
3333
public class BaseServiceException extends RuntimeException {
3434

35+
private static final long serialVersionUID = 759921776378760835L;
36+
public static final int UNKNOWN_CODE = 0;
37+
38+
private final int code;
39+
private final boolean retryable;
40+
private final String reason;
41+
private final boolean idempotent;
42+
private final String location;
43+
private final String debugInfo;
44+
3545
protected static final class Error implements Serializable {
3646

3747
private static final long serialVersionUID = -4019600198652965721L;
@@ -79,16 +89,6 @@ public int hashCode() {
7989
}
8090
}
8191

82-
private static final long serialVersionUID = 759921776378760835L;
83-
public static final int UNKNOWN_CODE = 0;
84-
85-
private final int code;
86-
private final boolean retryable;
87-
private final String reason;
88-
private final boolean idempotent;
89-
private final String location;
90-
private final String debugInfo;
91-
9292
public BaseServiceException(IOException exception, boolean idempotent) {
9393
super(message(exception), exception);
9494
int code = UNKNOWN_CODE;
@@ -198,6 +198,31 @@ protected String debugInfo() {
198198
return debugInfo;
199199
}
200200

201+
@Override
202+
public boolean equals(Object obj) {
203+
if (obj == this) {
204+
return true;
205+
}
206+
if (!(obj instanceof BaseServiceException)) {
207+
return false;
208+
}
209+
BaseServiceException other = (BaseServiceException) obj;
210+
return Objects.equals(getCause(), other.getCause())
211+
&& Objects.equals(getMessage(), other.getMessage())
212+
&& code == other.code
213+
&& retryable == other.retryable
214+
&& Objects.equals(reason, other.reason)
215+
&& idempotent == other.idempotent
216+
&& Objects.equals(location, other.location)
217+
&& Objects.equals(debugInfo, other.debugInfo);
218+
}
219+
220+
@Override
221+
public int hashCode() {
222+
return Objects.hash(getCause(), getMessage(), code, retryable, reason, idempotent, location,
223+
debugInfo);
224+
}
225+
201226
protected static String reason(GoogleJsonError error) {
202227
if (error.getErrors() != null && !error.getErrors().isEmpty()) {
203228
return error.getErrors().get(0).getReason();

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public Builder toBuilder() {
4646
}
4747
}
4848

49+
private static final BaseServiceException BASE_SERVICE_EXCEPTION =
50+
new BaseServiceException(42, "message", "reason", true);
4951
private static final ExceptionHandler EXCEPTION_HANDLER = ExceptionHandler.defaultInstance();
5052
private static final Identity IDENTITY = Identity.allAuthenticatedUsers();
5153
private static final PageImpl<String> PAGE =
@@ -81,7 +83,8 @@ public Builder toBuilder() {
8183

8284
@Override
8385
protected Serializable[] serializableObjects() {
84-
return new Serializable[]{EXCEPTION_HANDLER, IDENTITY, PAGE, RETRY_PARAMS, SOME_IAM_POLICY};
86+
return new Serializable[]{BASE_SERVICE_EXCEPTION, EXCEPTION_HANDLER, IDENTITY, PAGE,
87+
RETRY_PARAMS, SOME_IAM_POLICY};
8588
}
8689

8790
@Override

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public class SerializationTest extends BaseSerializationTest {
102102
.addValue(new NullValue())
103103
.build();
104104
private static final ProjectionEntity PROJECTION_ENTITY = ProjectionEntity.fromPb(ENTITY1.toPb());
105+
private static final DatastoreException DATASTORE_EXCEPTION =
106+
new DatastoreException(42, "message", "reason");
105107

106108
@Override
107109
protected java.io.Serializable[] serializableObjects() {
@@ -119,7 +121,8 @@ protected java.io.Serializable[] serializableObjects() {
119121
ENTITY2, ENTITY3, EMBEDDED_ENTITY, PROJECTION_ENTITY, DATE_TIME1, BLOB1, CURSOR1, GQL1,
120122
GQL2, QUERY1, QUERY2, QUERY3, NULL_VALUE, KEY_VALUE, STRING_VALUE, EMBEDDED_ENTITY_VALUE1,
121123
EMBEDDED_ENTITY_VALUE2, EMBEDDED_ENTITY_VALUE3, LIST_VALUE, LONG_VALUE, DOUBLE_VALUE,
122-
BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, options, otherOptions};
124+
BOOLEAN_VALUE, DATE_AND_TIME_VALUE, BLOB_VALUE, RAW_VALUE, DATASTORE_EXCEPTION, options,
125+
otherOptions};
123126
}
124127

125128
@Override

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class SerializationTest extends BaseSerializationTest {
4949
private static final Policy POLICY = Policy.builder()
5050
.addBinding(Policy.Role.viewer(), ImmutableSet.of(Identity.user("abc@gmail.com")))
5151
.build();
52+
private static final ResourceManagerException RESOURCE_MANAGER_EXCEPTION =
53+
new ResourceManagerException(42, "message");
5254

5355
@Override
5456
protected Serializable[] serializableObjects() {
@@ -57,7 +59,8 @@ protected Serializable[] serializableObjects() {
5759
.projectId("some-unnecessary-project-ID")
5860
.build();
5961
return new Serializable[]{PARTIAL_PROJECT_INFO, FULL_PROJECT_INFO, PROJECT, PAGE_RESULT,
60-
PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, options, otherOptions};
62+
PROJECT_GET_OPTION, PROJECT_LIST_OPTION, POLICY, RESOURCE_MANAGER_EXCEPTION, options,
63+
otherOptions};
6164
}
6265

6366
@Override

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class SerializationTest extends BaseSerializationTest {
5252
Collections.<BatchResponse.Result<Blob>>emptyList());
5353
private static final PageImpl<Blob> PAGE_RESULT =
5454
new PageImpl<>(null, "c", Collections.singletonList(BLOB));
55+
private static final StorageException STORAGE_EXCEPTION = new StorageException(42, "message");
5556
private static final Storage.BlobListOption BLOB_LIST_OPTIONS =
5657
Storage.BlobListOption.pageSize(100);
5758
private static final Storage.BlobSourceOption BLOB_SOURCE_OPTIONS =
@@ -79,7 +80,8 @@ protected Serializable[] serializableObjects() {
7980
return new Serializable[]{ACL_DOMAIN, ACL_GROUP, ACL_PROJECT_, ACL_USER, ACL_RAW, ACL,
8081
BLOB_INFO, BLOB, BUCKET_INFO, BUCKET, ORIGIN, CORS, BATCH_REQUEST, BATCH_RESPONSE,
8182
PAGE_RESULT, BLOB_LIST_OPTIONS, BLOB_SOURCE_OPTIONS, BLOB_TARGET_OPTIONS,
82-
BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS, options, otherOptions};
83+
BUCKET_LIST_OPTIONS, BUCKET_SOURCE_OPTIONS, BUCKET_TARGET_OPTIONS, STORAGE_EXCEPTION,
84+
options, otherOptions};
8385
}
8486

8587
@Override

0 commit comments

Comments
 (0)