Skip to content

Commit

Permalink
chore: use assertThrows with lambda instead of try-catch (#1139)
Browse files Browse the repository at this point in the history
Fixes #1124
  • Loading branch information
olavloite authored May 6, 2021
1 parent 699a426 commit b3ebfcf
Show file tree
Hide file tree
Showing 41 changed files with 1,121 additions and 1,761 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;
import static org.junit.runners.Parameterized.Parameter;

import com.google.cloud.ByteArray;
Expand Down Expand Up @@ -408,46 +408,40 @@ public void getterForIncorrectType() {
// Skip allowed getters.
continue;
}
try {
getterByIndex(method.getName(), columnIndex);
fail("Expected " + IllegalStateException.class.getSimpleName() + " for " + method);
} catch (IllegalStateException e) {
assertWithMessage("Exception for " + method).that(e.getMessage()).contains("was " + type);
assertWithMessage("Exception for " + method)
.that(e.getMessage())
.contains("Column " + columnIndex);
}
try {
getterByName(method.getName(), "F1");
fail("Expected ISE for " + method);
} catch (IllegalStateException e) {
assertWithMessage("Exception for " + method).that(e.getMessage()).contains("was " + type);
assertWithMessage("Exception for " + method).that(e.getMessage()).contains("Column F1");
}
IllegalStateException getterByIndexException =
assertThrows(
IllegalStateException.class, () -> getterByIndex(method.getName(), columnIndex));
assertWithMessage("Exception for " + method)
.that(getterByIndexException.getMessage())
.contains("was " + type);
assertWithMessage("Exception for " + method)
.that(getterByIndexException.getMessage())
.contains("Column " + columnIndex);

IllegalStateException getterByNameException =
assertThrows(IllegalStateException.class, () -> getterByName(method.getName(), "F1"));
assertWithMessage("Exception for " + method)
.that(getterByNameException.getMessage())
.contains("was " + type);
assertWithMessage("Exception for " + method)
.that(getterByNameException.getMessage())
.contains("Column F1");
}
}

@Test
public void getterWhenNull() {
Mockito.when(reader.getType()).thenReturn(Type.struct(StructField.of("F1", type)));
Mockito.when(reader.isNull(0)).thenReturn(true);
try {
getterByIndex(0);
fail("Expected exception");
} catch (NullPointerException ex) {
assertNotNull(ex.getMessage());
}
NullPointerException ex = assertThrows(NullPointerException.class, () -> getterByIndex(0));
assertNotNull(ex.getMessage());
}

@Test
public void getterByNameWhenNull() {
Mockito.when(reader.getType()).thenReturn(Type.struct(StructField.of("F1", type)));
Mockito.when(reader.isNull(0)).thenReturn(true);
try {
getterByName("F1");
fail("Expected exception");
} catch (NullPointerException ex) {
assertNotNull(ex.getMessage());
}
NullPointerException ex = assertThrows(NullPointerException.class, () -> getterByName("F1"));
assertNotNull(ex.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.google.cloud.spanner;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -68,24 +68,13 @@ public void close() {
rs.close();

// The following methods are not allowed to call after closing the result set.
try {
rs.setCallback(mock(Executor.class), mock(ReadyCallback.class));
fail("missing expected exception");
} catch (IllegalStateException e) {
// Expected exception
}
try {
rs.toList(mock(Function.class));
fail("missing expected exception");
} catch (IllegalStateException e) {
// Expected exception
}
try {
rs.toListAsync(mock(Function.class), mock(Executor.class));
fail("missing expected exception");
} catch (IllegalStateException e) {
// Expected exception
}
assertThrows(
IllegalStateException.class,
() -> rs.setCallback(mock(Executor.class), mock(ReadyCallback.class)));
assertThrows(IllegalStateException.class, () -> rs.toList(mock(Function.class)));
assertThrows(
IllegalStateException.class,
() -> rs.toListAsync(mock(Function.class), mock(Executor.class)));

// The following methods are allowed on a closed result set.
AsyncResultSetImpl rs2 =
Expand All @@ -103,13 +92,8 @@ public void tryNextNotAllowed() {
new AsyncResultSetImpl(
mockedProvider, mock(ResultSet.class), AsyncResultSetImpl.DEFAULT_BUFFER_SIZE)) {
rs.setCallback(mock(Executor.class), mock(ReadyCallback.class));
try {
rs.tryNext();
fail("missing expected exception");
} catch (IllegalStateException e) {
assertThat(e.getMessage())
.contains("tryNext may only be called from a DataReady callback.");
}
IllegalStateException e = assertThrows(IllegalStateException.class, () -> rs.tryNext());
assertThat(e.getMessage()).contains("tryNext may only be called from a DataReady callback.");
}
}

Expand All @@ -134,9 +118,8 @@ public void toListPropagatesError() {
ErrorCode.INVALID_ARGUMENT, "invalid query"));
try (AsyncResultSetImpl rs =
new AsyncResultSetImpl(simpleProvider, delegate, AsyncResultSetImpl.DEFAULT_BUFFER_SIZE)) {
rs.toList(ignored -> new Object());
fail("missing expected exception");
} catch (SpannerException e) {
SpannerException e =
assertThrows(SpannerException.class, () -> rs.toList(ignored -> new Object()));
assertThat(e.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
assertThat(e.getMessage()).contains("invalid query");
}
Expand Down Expand Up @@ -166,9 +149,10 @@ public void toListAsyncPropagatesError() throws InterruptedException {
ErrorCode.INVALID_ARGUMENT, "invalid query"));
try (AsyncResultSetImpl rs =
new AsyncResultSetImpl(simpleProvider, delegate, AsyncResultSetImpl.DEFAULT_BUFFER_SIZE)) {
rs.toListAsync(ignored -> new Object(), executor).get();
fail("missing expected exception");
} catch (ExecutionException e) {
ExecutionException e =
assertThrows(
ExecutionException.class,
() -> rs.toListAsync(ignored -> new Object(), executor).get());
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
Expand Down Expand Up @@ -386,9 +370,7 @@ public void callbackReturnsError() throws InterruptedException {
callbackCounter.incrementAndGet();
throw new RuntimeException("async test");
});
rs.getResult().get();
fail("missing expected exception");
} catch (ExecutionException e) {
ExecutionException e = assertThrows(ExecutionException.class, () -> rs.getResult().get());
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.UNKNOWN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import static com.google.cloud.spanner.SpannerApiFutures.get;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -68,24 +68,18 @@ public void testAsyncRunReturnsResultAndCommitResponse() {
public void testGetCommitTimestampReturnsErrorBeforeRun() {
TransactionRunnerImpl delegate = mock(TransactionRunnerImpl.class);
AsyncRunnerImpl runner = new AsyncRunnerImpl(delegate);
try {
runner.getCommitTimestamp();
fail("missing expected exception");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
}
IllegalStateException e =
assertThrows(IllegalStateException.class, () -> runner.getCommitTimestamp());
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
}

@Test
public void testGetCommitResponseReturnsErrorBeforeRun() {
TransactionRunnerImpl delegate = mock(TransactionRunnerImpl.class);
AsyncRunnerImpl runner = new AsyncRunnerImpl(delegate);
try {
runner.getCommitResponse();
fail("missing expected exception");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
}
IllegalStateException e =
assertThrows(IllegalStateException.class, () -> runner.getCommitResponse());
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
}

@Test
Expand All @@ -99,12 +93,9 @@ public void testGetCommitResponseReturnsErrorIfRunFails() {
AsyncRunnerImpl runner = new AsyncRunnerImpl(delegate);
runner.runAsync(txn -> ApiFutures.immediateFailedFuture(expectedException), executor);

try {
get(runner.getCommitResponse());
fail("missing expected exception");
} catch (SpannerException e) {
assertSame(expectedException, e);
}
SpannerException e =
assertThrows(SpannerException.class, () -> get(runner.getCommitResponse()));
assertSame(expectedException, e);
}

@SuppressWarnings("unchecked")
Expand All @@ -117,11 +108,10 @@ public void testRunAsyncFailsIfCalledMultipleTimes() {
AsyncRunnerImpl runner = new AsyncRunnerImpl(delegate);
runner.runAsync(txn -> ApiFutures.immediateFuture(result), executor);

try {
runner.runAsync(txn -> ApiFutures.immediateFuture(null), executor);
fail("missing expected exception");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("runAsync() can only be called once"));
}
IllegalStateException e =
assertThrows(
IllegalStateException.class,
() -> runner.runAsync(txn -> ApiFutures.immediateFuture(null), executor));
assertTrue(e.getMessage().contains("runAsync() can only be called once"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

import static com.google.cloud.spanner.MockSpannerTestUtil.*;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutures;
Expand Down Expand Up @@ -54,23 +54,17 @@ public class AsyncRunnerTest extends AbstractAsyncTransactionTest {
@Test
public void testAsyncRunner_doesNotReturnCommitTimestampBeforeCommit() {
AsyncRunner runner = client().runAsync();
try {
runner.getCommitTimestamp();
fail("missing expected exception");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
}
IllegalStateException e =
assertThrows(IllegalStateException.class, () -> runner.getCommitTimestamp());
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
}

@Test
public void testAsyncRunner_doesNotReturnCommitResponseBeforeCommit() {
AsyncRunner runner = client().runAsync();
try {
runner.getCommitResponse();
fail("missing expected exception");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
}
IllegalStateException e =
assertThrows(IllegalStateException.class, () -> runner.getCommitResponse());
assertTrue(e.getMessage().contains("runAsync() has not yet been called"));
}

@Test
Expand Down Expand Up @@ -103,15 +97,11 @@ public void asyncRunnerInvalidUpdate() throws Exception {
AsyncRunner runner = client().runAsync();
ApiFuture<Long> updateCount =
runner.runAsync(txn -> txn.executeUpdateAsync(INVALID_UPDATE_STATEMENT), executor);
try {
updateCount.get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
assertThat(se.getMessage()).contains("invalid statement");
}
ExecutionException e = assertThrows(ExecutionException.class, () -> updateCount.get());
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
assertThat(se.getMessage()).contains("invalid statement");
}

@Test
Expand Down Expand Up @@ -233,15 +223,11 @@ public void asyncRunnerCommitFails() throws Exception {
return txn.executeUpdateAsync(UPDATE_STATEMENT);
},
executor);
try {
updateCount.get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.RESOURCE_EXHAUSTED);
assertThat(se.getMessage()).contains("mutation limit exceeded");
}
ExecutionException e = assertThrows(ExecutionException.class, () -> updateCount.get());
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.RESOURCE_EXHAUSTED);
assertThat(se.getMessage()).contains("mutation limit exceeded");
}

@Test
Expand Down Expand Up @@ -295,15 +281,11 @@ public void asyncRunnerInvalidBatchUpdate() throws Exception {
txn ->
txn.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, INVALID_UPDATE_STATEMENT)),
executor);
try {
updateCount.get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
assertThat(se.getMessage()).contains("invalid statement");
}
ExecutionException e = assertThrows(ExecutionException.class, () -> updateCount.get());
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.INVALID_ARGUMENT);
assertThat(se.getMessage()).contains("invalid statement");
}

@Test
Expand Down Expand Up @@ -423,15 +405,11 @@ public void asyncRunnerWithBatchUpdateCommitFails() throws Exception {
return txn.batchUpdateAsync(ImmutableList.of(UPDATE_STATEMENT, UPDATE_STATEMENT));
},
executor);
try {
updateCount.get();
fail("missing expected exception");
} catch (ExecutionException e) {
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.RESOURCE_EXHAUSTED);
assertThat(se.getMessage()).contains("mutation limit exceeded");
}
ExecutionException e = assertThrows(ExecutionException.class, () -> updateCount.get());
assertThat(e.getCause()).isInstanceOf(SpannerException.class);
SpannerException se = (SpannerException) e.getCause();
assertThat(se.getErrorCode()).isEqualTo(ErrorCode.RESOURCE_EXHAUSTED);
assertThat(se.getMessage()).contains("mutation limit exceeded");
}

@Test
Expand Down
Loading

0 comments on commit b3ebfcf

Please sign in to comment.