Skip to content

Commit

Permalink
Add equals and hashCode to exceptions. Add exceptions to serializatio…
Browse files Browse the repository at this point in the history
…n tests
  • Loading branch information
mziccard committed Mar 18, 2016
1 parent 19e0c6e commit 83ddb08
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.gcloud.RetryHelper.RetryInterruptedException;

import java.io.IOException;
import java.util.Objects;
import java.util.Set;

/**
Expand Down Expand Up @@ -73,6 +74,23 @@ protected Set<Error> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> PAGE =
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class SerializationTest extends BaseSerializationTest {
Collections.<BatchResponse.Result<Blob>>emptyList());
private static final PageImpl<Blob> 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 =
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 83ddb08

Please sign in to comment.