Skip to content

Commit

Permalink
Make exception constructors package scoped, better test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mziccard committed Apr 20, 2016
1 parent 7847bea commit 4b8f3ea
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public BigQueryException(int code, String message) {
this(code, message, (Throwable) null);
}

private BigQueryException(int code, String message, Throwable cause) {
BigQueryException(int code, String message, Throwable cause) {
super(code, message, null, true, cause);
this.error = null;
}

public BigQueryException(int code, String message, BigQueryError error) {
BigQueryException(int code, String message, BigQueryError error) {
super(code, message, error != null ? error.reason() : null, true);
this.error = error;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import com.google.cloud.BaseServiceException;
Expand Down Expand Up @@ -86,13 +87,24 @@ public void testBigqueryException() {
assertTrue(exception.retryable());
assertTrue(exception.idempotent());

IOException cause = new SocketTimeoutException();
IOException cause = new SocketTimeoutException("socketTimeoutMessage");
exception = new BigQueryException(cause);
assertEquals(BigQueryException.UNKNOWN_CODE, exception.code());
assertNull(exception.reason());
assertNull(exception.getMessage());
assertEquals("socketTimeoutMessage", exception.getMessage());
assertEquals(cause, exception.getCause());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());
assertEquals(cause, exception.getCause());
assertSame(cause, exception.getCause());

exception = new BigQueryException(504, "message", cause);
assertEquals(504, exception.code());
assertEquals("message", exception.getMessage());
assertNull(exception.reason());
assertNull(exception.error());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());
assertSame(cause, exception.getCause());
}

@Test
Expand Down Expand Up @@ -123,7 +135,7 @@ public void testTranslateAndThrow() throws Exception {
assertEquals("message", ex.getMessage());
assertFalse(ex.retryable());
assertTrue(ex.idempotent());
assertEquals(cause, ex.getCause());
assertSame(cause, ex.getCause());
} finally {
verify(exceptionMock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class DatastoreException extends BaseServiceException {
new Error(10, "ABORTED"), new Error(4, "DEADLINE_EXCEEDED"), new Error(14, "UNAVAILABLE"));
private static final long serialVersionUID = 2663750991205874435L;

public DatastoreException(int code, String message, String reason) {
DatastoreException(int code, String message, String reason) {
this(code, message, reason, null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -66,12 +67,23 @@ public void testDatastoreException() throws Exception {
assertFalse(exception.retryable());
assertTrue(exception.idempotent());

IOException cause = new SocketTimeoutException();
IOException cause = new SocketTimeoutException("socketTimeoutMessage");
exception = new DatastoreException(cause);
assertEquals(DatastoreException.UNKNOWN_CODE, exception.code());
assertNull(exception.reason());
assertNull(exception.getMessage());
assertEquals("socketTimeoutMessage", exception.getMessage());
assertEquals(cause, exception.getCause());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());
assertSame(cause, exception.getCause());

exception = new DatastoreException(2, "message", "INTERNAL", cause);
assertEquals(2, exception.code());
assertEquals("INTERNAL", exception.reason());
assertEquals("message", exception.getMessage());
assertFalse(exception.retryable());
assertTrue(exception.idempotent());
assertSame(cause, exception.getCause());
}

@Test
Expand Down Expand Up @@ -103,7 +115,7 @@ public void testTranslateAndThrow() throws Exception {
assertEquals("message", ex.getMessage());
assertFalse(ex.retryable());
assertTrue(ex.idempotent());
assertEquals(cause, ex.getCause());
assertSame(cause, ex.getCause());
} finally {
verify(exceptionMock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,87 @@

package com.google.cloud.dns;

import com.google.cloud.BaseServiceException;
import com.google.cloud.RetryHelper.RetryHelperException;

import org.junit.Test;

import java.io.IOException;
import java.net.SocketTimeoutException;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.cloud.BaseServiceException;
import com.google.cloud.RetryHelper.RetryHelperException;

import org.junit.Test;

import java.io.IOException;
import java.net.SocketTimeoutException;

public class DnsExceptionTest {

@Test
public void testDnsException() throws Exception {
IOException cause = new SocketTimeoutException("message");
DnsException exception = new DnsException(cause, true);
assertEquals(DnsException.UNKNOWN_CODE, exception.code());
IOException cause = new SocketTimeoutException("socketTimeoutMessage");
DnsException exception = new DnsException(500, "message", cause);
assertEquals(500, exception.code());
assertEquals("message", exception.getMessage());
assertNull(exception.reason());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());
assertSame(cause, exception.getCause());

exception = new DnsException(502, "message", cause);
assertEquals(502, exception.code());
assertEquals("message", exception.getMessage());
assertNull(exception.reason());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());
assertSame(cause, exception.getCause());

exception = new DnsException(503, "message", cause);
assertEquals(503, exception.code());
assertEquals("message", exception.getMessage());
assertNull(exception.reason());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());
assertSame(cause, exception.getCause());

exception = new DnsException(429, "message", cause);
assertEquals(429, exception.code());
assertEquals("message", exception.getMessage());
assertNull(exception.reason());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());
assertSame(cause, exception.getCause());

exception = new DnsException(404, "message", cause);
assertEquals(404, exception.code());
assertEquals("message", exception.getMessage());
assertNull(exception.reason());
assertFalse(exception.retryable());
assertTrue(exception.idempotent());
assertSame(cause, exception.getCause());

exception = new DnsException(cause, true);
assertEquals(DnsException.UNKNOWN_CODE, exception.code());
assertNull(exception.reason());
assertEquals("socketTimeoutMessage", exception.getMessage());
assertEquals(cause, exception.getCause());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());
assertSame(cause, exception.getCause());

GoogleJsonError error = new GoogleJsonError();
error.setCode(503);
error.setMessage("message");
exception = new DnsException(error, true);
assertEquals(503, exception.code());
assertEquals("message", exception.getMessage());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());
}

@Test
Expand Down Expand Up @@ -78,7 +130,7 @@ public void testTranslateAndThrow() throws Exception {
assertEquals("message", ex.getMessage());
assertFalse(ex.retryable());
assertTrue(ex.idempotent());
assertEquals(cause, ex.getCause());
assertSame(cause, ex.getCause());
} finally {
verify(exceptionMock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ public class ResourceManagerException extends BaseServiceException {
new Error(403, "variableTermLimitExceeded"));
private static final long serialVersionUID = -9207194488966554136L;

public ResourceManagerException(int code, String message) {
ResourceManagerException(int code, String message) {
this(code, message, null);
}

private ResourceManagerException(int code, String message, Throwable cause) {
ResourceManagerException(int code, String message, Throwable cause) {
super(code, message, null, true, cause);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import com.google.cloud.BaseServiceException;
Expand Down Expand Up @@ -71,7 +72,15 @@ public void testResourceManagerException() {
assertNull(exception.getMessage());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());
assertEquals(cause, exception.getCause());
assertSame(cause, exception.getCause());

exception = new ResourceManagerException(404, "message", cause);
assertEquals(404, exception.code());
assertEquals("message", exception.getMessage());
assertNull(exception.reason());
assertFalse(exception.retryable());
assertTrue(exception.idempotent());
assertSame(cause, exception.getCause());
}

@Test
Expand Down Expand Up @@ -102,7 +111,7 @@ public void testTranslateAndThrow() throws Exception {
assertEquals("message", ex.getMessage());
assertFalse(ex.retryable());
assertTrue(ex.idempotent());
assertEquals(cause, ex.getCause());
assertSame(cause, ex.getCause());
} finally {
verify(exceptionMock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public StorageException(int code, String message) {
this(code, message, null);
}

private StorageException(int code, String message, Throwable cause) {
StorageException(int code, String message, Throwable cause) {
super(code, message, null, true, cause);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import com.google.api.client.googleapis.json.GoogleJsonError;
Expand Down Expand Up @@ -93,7 +94,7 @@ public void testStorageException() {
assertNull(exception.getMessage());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());
assertEquals(cause, exception.getCause());
assertSame(cause, exception.getCause());

GoogleJsonError error = new GoogleJsonError();
error.setCode(503);
Expand All @@ -103,6 +104,14 @@ public void testStorageException() {
assertEquals("message", exception.getMessage());
assertTrue(exception.retryable());
assertTrue(exception.idempotent());

exception = new StorageException(400, "message", cause);
assertEquals(400, exception.code());
assertEquals("message", exception.getMessage());
assertNull(exception.reason());
assertFalse(exception.retryable());
assertTrue(exception.idempotent());
assertSame(cause, exception.getCause());
}

@Test
Expand Down Expand Up @@ -133,7 +142,7 @@ public void testTranslateAndThrow() throws Exception {
assertEquals("message", ex.getMessage());
assertFalse(ex.retryable());
assertTrue(ex.idempotent());
assertEquals(cause, ex.getCause());
assertSame(cause, ex.getCause());
} finally {
verify(exceptionMock);
}
Expand Down

0 comments on commit 4b8f3ea

Please sign in to comment.