diff --git a/src/main/java/com/google/cloud/spanner/jdbc/AbstractJdbcPreparedStatement.java b/src/main/java/com/google/cloud/spanner/jdbc/AbstractJdbcPreparedStatement.java index ef0c5af4b9..52ac32c9cf 100644 --- a/src/main/java/com/google/cloud/spanner/jdbc/AbstractJdbcPreparedStatement.java +++ b/src/main/java/com/google/cloud/spanner/jdbc/AbstractJdbcPreparedStatement.java @@ -169,6 +169,7 @@ public void setAsciiStream(int parameterIndex, InputStream value, int length) } @Override + @Deprecated public void setUnicodeStream(int parameterIndex, InputStream value, int length) throws SQLException { checkClosed(); diff --git a/src/main/java/com/google/cloud/spanner/jdbc/JdbcResultSet.java b/src/main/java/com/google/cloud/spanner/jdbc/JdbcResultSet.java index 00c6361ff4..e9532554d6 100644 --- a/src/main/java/com/google/cloud/spanner/jdbc/JdbcResultSet.java +++ b/src/main/java/com/google/cloud/spanner/jdbc/JdbcResultSet.java @@ -215,6 +215,7 @@ public InputStream getAsciiStream(int columnIndex) throws SQLException { } @Override + @Deprecated public InputStream getUnicodeStream(int columnIndex) throws SQLException { checkClosedAndValidRow(); return getInputStream(getString(columnIndex), StandardCharsets.UTF_16LE); @@ -314,6 +315,7 @@ public InputStream getAsciiStream(String columnLabel) throws SQLException { } @Override + @Deprecated public InputStream getUnicodeStream(String columnLabel) throws SQLException { checkClosedAndValidRow(); return getInputStream(getString(columnLabel), StandardCharsets.UTF_16LE); @@ -406,12 +408,14 @@ public BigDecimal getBigDecimal(String columnLabel) throws SQLException { } @Override + @Deprecated public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException { checkClosedAndValidRow(); return getBigDecimal(columnIndex, true, scale); } @Override + @Deprecated public BigDecimal getBigDecimal(String columnLabel, int scale) throws SQLException { checkClosedAndValidRow(); return getBigDecimal(spanner.getColumnIndex(columnLabel) + 1, true, scale); diff --git a/src/test/java/com/google/cloud/spanner/jdbc/JdbcAbortedTransactionTest.java b/src/test/java/com/google/cloud/spanner/jdbc/JdbcAbortedTransactionTest.java index 7b9c4d0503..681f605281 100644 --- a/src/test/java/com/google/cloud/spanner/jdbc/JdbcAbortedTransactionTest.java +++ b/src/test/java/com/google/cloud/spanner/jdbc/JdbcAbortedTransactionTest.java @@ -16,11 +16,7 @@ package com.google.cloud.spanner.jdbc; -import static org.hamcrest.CoreMatchers.endsWith; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; import com.google.cloud.Timestamp; @@ -53,9 +49,7 @@ import java.util.List; import org.junit.AfterClass; import org.junit.BeforeClass; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; @@ -110,8 +104,6 @@ public void retryFinished( @Parameter(0) public boolean retryAbortsInternally; - @Rule public ExpectedException expected = ExpectedException.none(); - @Parameters(name = "retryAbortsInternally = {0}") public static Collection data() { List params = new ArrayList<>(); @@ -175,7 +167,7 @@ public void testAutocommitUpdateAborted() throws SQLException { try (java.sql.Connection connection = createConnection()) { mockSpanner.abortNextStatement(); int updateCount = connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); - assertThat(updateCount, is(equalTo(UPDATE_COUNT))); + assertThat(updateCount).isEqualTo(UPDATE_COUNT); } } @@ -183,15 +175,17 @@ public void testAutocommitUpdateAborted() throws SQLException { public void testTransactionalUpdateAborted() throws SQLException { // Updates in transactional mode are automatically retried by default, but this can be switched // off. - if (!retryAbortsInternally) { - expected.expect(JdbcAbortedException.class); - } try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); mockSpanner.abortNextStatement(); int updateCount = connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); - assertThat(updateCount, is(equalTo(UPDATE_COUNT))); - assertThat(getRetryCount(connection), is(equalTo(1))); + if (!retryAbortsInternally) { + fail("missing expected exception"); + } + assertThat(updateCount).isEqualTo(UPDATE_COUNT); + assertThat(getRetryCount(connection)).isEqualTo(1); + } catch (JdbcAbortedException e) { + assertThat(retryAbortsInternally).isFalse(); } } @@ -203,16 +197,13 @@ public void testAutocommitBatchUpdateAborted() throws SQLException { statement.addBatch(UPDATE_STATEMENT.getSql()); statement.addBatch(UPDATE_STATEMENT.getSql()); int[] updateCounts = statement.executeBatch(); - assertThat(updateCounts, is(equalTo(new int[] {UPDATE_COUNT, UPDATE_COUNT}))); + assertThat(updateCounts).asList().containsExactly(UPDATE_COUNT, UPDATE_COUNT); } } } @Test public void testTransactionalBatchUpdateAborted() throws SQLException { - if (!retryAbortsInternally) { - expected.expect(JdbcAbortedException.class); - } try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); mockSpanner.abortNextStatement(); @@ -220,8 +211,13 @@ public void testTransactionalBatchUpdateAborted() throws SQLException { statement.addBatch(UPDATE_STATEMENT.getSql()); statement.addBatch(UPDATE_STATEMENT.getSql()); int[] updateCounts = statement.executeBatch(); - assertThat(updateCounts, is(equalTo(new int[] {UPDATE_COUNT, UPDATE_COUNT}))); - assertThat(getRetryCount(connection), is(equalTo(1))); + if (!retryAbortsInternally) { + fail("missing expected exception"); + } + assertThat(updateCounts).asList().containsExactly(UPDATE_COUNT, UPDATE_COUNT); + assertThat(getRetryCount(connection)).isEqualTo(1); + } catch (JdbcAbortedException e) { + assertThat(retryAbortsInternally).isFalse(); } } } @@ -233,7 +229,7 @@ public void testAutocommitSelectAborted() throws SQLException { mockSpanner.abortNextStatement(); try (ResultSet rs = connection.createStatement().executeQuery(SELECT1.getSql())) { while (rs.next()) { - assertThat(rs.getLong(1), is(equalTo(1L))); + assertThat(rs.getLong(1)).isEqualTo(1L); } } } @@ -241,30 +237,27 @@ public void testAutocommitSelectAborted() throws SQLException { @Test public void testTransactionalSelectAborted() throws SQLException { - if (!retryAbortsInternally) { - expected.expect(JdbcAbortedException.class); - } try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); mockSpanner.abortNextStatement(); try (ResultSet rs = connection.createStatement().executeQuery(SELECT1.getSql())) { while (rs.next()) { - assertThat(rs.getLong(1), is(equalTo(1L))); + if (!retryAbortsInternally) { + fail("missing expected exception"); + } + assertThat(rs.getLong(1)).isEqualTo(1L); } } - assertThat(getRetryCount(connection), is(equalTo(1))); + assertThat(getRetryCount(connection)).isEqualTo(1); + } catch (JdbcAbortedException e) { + assertThat(retryAbortsInternally).isFalse(); } } @Test public void testTransactionalUpdateWithConcurrentModificationsAborted() throws SQLException { - if (retryAbortsInternally) { - // As the transaction does a random select, the retry will always see different data than the - // original attempt. - expected.expect(JdbcAbortedDueToConcurrentModificationException.class); - } else { - expected.expect(JdbcAbortedException.class); - } + // As the transaction does a random select, the retry will always see different data than the + // original attempt. try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); // Set a random answer. @@ -281,14 +274,15 @@ public void testTransactionalUpdateWithConcurrentModificationsAborted() throws S // This will abort and start an internal retry. connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); fail("missing expected aborted exception"); + } catch (JdbcAbortedDueToConcurrentModificationException e) { + assertThat(retryAbortsInternally).isTrue(); + } catch (JdbcAbortedException e) { + assertThat(retryAbortsInternally).isFalse(); } } @Test public void testTransactionalUpdateWithErrorOnOriginalAndRetry() throws SQLException { - if (!retryAbortsInternally) { - expected.expect(JdbcAbortedException.class); - } final String sql = "UPDATE SOMETHING SET OTHER=1"; mockSpanner.putStatementResult( StatementResult.exception( @@ -298,7 +292,7 @@ public void testTransactionalUpdateWithErrorOnOriginalAndRetry() throws SQLExcep connection.setAutoCommit(false); try (ResultSet rs = connection.createStatement().executeQuery(SELECT1.getSql())) { while (rs.next()) { - assertThat(rs.getLong(1), is(equalTo(1L))); + assertThat(rs.getLong(1)).isEqualTo(1L); } } try { @@ -309,16 +303,16 @@ public void testTransactionalUpdateWithErrorOnOriginalAndRetry() throws SQLExcep } mockSpanner.abortNextStatement(); connection.commit(); + if (!retryAbortsInternally) { + fail("missing expected exception"); + } + } catch (JdbcAbortedException e) { + assertThat(retryAbortsInternally).isFalse(); } } @Test public void testTransactionalUpdateWithErrorOnRetryAndNotOnOriginal() throws SQLException { - if (retryAbortsInternally) { - expected.expect(JdbcAbortedDueToConcurrentModificationException.class); - } else { - expected.expect(JdbcAbortedException.class); - } final String sql = "UPDATE SOMETHING SET OTHER=1"; try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); @@ -335,20 +329,17 @@ public void testTransactionalUpdateWithErrorOnRetryAndNotOnOriginal() throws SQL connection.commit(); fail("missing expected aborted exception"); } catch (JdbcAbortedDueToConcurrentModificationException e) { - assertThat( - e.getDatabaseErrorDuringRetry().getErrorCode(), is(equalTo(ErrorCode.INVALID_ARGUMENT))); - assertThat(e.getDatabaseErrorDuringRetry().getMessage(), endsWith("test")); - throw e; + assertThat(retryAbortsInternally).isTrue(); + assertThat(e.getDatabaseErrorDuringRetry().getErrorCode()) + .isEqualTo(ErrorCode.INVALID_ARGUMENT); + assertThat(e.getDatabaseErrorDuringRetry().getMessage()).endsWith("test"); + } catch (JdbcAbortedException e) { + assertThat(retryAbortsInternally).isFalse(); } } @Test public void testTransactionalUpdateWithErrorOnOriginalAndNotOnRetry() throws SQLException { - if (retryAbortsInternally) { - expected.expect(JdbcAbortedDueToConcurrentModificationException.class); - } else { - expected.expect(JdbcAbortedException.class); - } final String sql = "UPDATE SOMETHING SET OTHER=1"; mockSpanner.putStatementResult( StatementResult.exception( @@ -358,7 +349,7 @@ public void testTransactionalUpdateWithErrorOnOriginalAndNotOnRetry() throws SQL connection.setAutoCommit(false); try (ResultSet rs = connection.createStatement().executeQuery(SELECT1.getSql())) { while (rs.next()) { - assertThat(rs.getLong(1), is(equalTo(1L))); + assertThat(rs.getLong(1)).isEqualTo(1L); } } try { @@ -373,8 +364,10 @@ public void testTransactionalUpdateWithErrorOnOriginalAndNotOnRetry() throws SQL connection.commit(); fail("missing expected aborted exception"); } catch (JdbcAbortedDueToConcurrentModificationException e) { - assertThat(e.getDatabaseErrorDuringRetry(), is(nullValue())); - throw e; + assertThat(retryAbortsInternally).isTrue(); + assertThat(e.getDatabaseErrorDuringRetry()).isNull(); + } catch (JdbcAbortedException e) { + assertThat(retryAbortsInternally).isFalse(); } } } diff --git a/src/test/java/com/google/cloud/spanner/jdbc/JdbcConnectionTest.java b/src/test/java/com/google/cloud/spanner/jdbc/JdbcConnectionTest.java index 3d9c77caf1..8d49440a5e 100644 --- a/src/test/java/com/google/cloud/spanner/jdbc/JdbcConnectionTest.java +++ b/src/test/java/com/google/cloud/spanner/jdbc/JdbcConnectionTest.java @@ -16,11 +16,8 @@ package com.google.cloud.spanner.jdbc; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -48,15 +45,12 @@ import java.util.Map; import java.util.Properties; import java.util.concurrent.Executor; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class JdbcConnectionTest { - @Rule public final ExpectedException exception = ExpectedException.none(); private static final com.google.cloud.spanner.ResultSet SELECT1_RESULTSET = ResultSets.forRows( Type.struct(StructField.of("", Type.int64())), @@ -76,14 +70,14 @@ public void testAutoCommit() throws SQLException { ConnectionOptions options = mock(ConnectionOptions.class); when(options.isAutocommit()).thenReturn(true); try (Connection connection = createConnection(options)) { - assertThat(connection.getAutoCommit(), is(true)); + assertThat(connection.getAutoCommit()).isTrue(); connection.setAutoCommit(false); - assertThat(connection.getAutoCommit(), is(false)); + assertThat(connection.getAutoCommit()).isFalse(); // execute a query that will start a transaction connection.createStatement().executeQuery(AbstractConnectionImplTest.SELECT); // setting autocommit will automatically commit the transaction connection.setAutoCommit(true); - assertThat(connection.getAutoCommit(), is(true)); + assertThat(connection.getAutoCommit()).isTrue(); } } @@ -93,14 +87,16 @@ public void testReadOnly() throws SQLException { when(options.isAutocommit()).thenReturn(true); when(options.isReadOnly()).thenReturn(true); try (Connection connection = createConnection(options)) { - assertThat(connection.isReadOnly(), is(true)); + assertThat(connection.isReadOnly()).isTrue(); connection.setReadOnly(false); - assertThat(connection.isReadOnly(), is(false)); + assertThat(connection.isReadOnly()).isFalse(); // start a transaction connection.createStatement().execute("begin transaction"); // setting readonly should no longer be allowed - exception.expect(JdbcExceptionMatcher.matchCode(Code.FAILED_PRECONDITION)); connection.setReadOnly(true); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(JdbcExceptionMatcher.matchCode(Code.FAILED_PRECONDITION).matches(e)).isTrue(); } } @@ -109,17 +105,17 @@ public void testCommit() throws SQLException { ConnectionOptions options = mock(ConnectionOptions.class); try (JdbcConnection connection = createConnection(options)) { // verify that there is no transaction started - assertThat(connection.getSpannerConnection().isTransactionStarted(), is(false)); + assertThat(connection.getSpannerConnection().isTransactionStarted()).isFalse(); // start a transaction connection.createStatement().execute(AbstractConnectionImplTest.SELECT); // verify that we did start a transaction - assertThat(connection.getSpannerConnection().isTransactionStarted(), is(true)); + assertThat(connection.getSpannerConnection().isTransactionStarted()).isTrue(); // do a commit connection.commit(); // verify that there is no transaction started anymore - assertThat(connection.getSpannerConnection().isTransactionStarted(), is(false)); + assertThat(connection.getSpannerConnection().isTransactionStarted()).isFalse(); // verify that there is a commit timestamp - assertThat(connection.getSpannerConnection().getCommitTimestamp(), is(notNullValue())); + assertThat(connection.getSpannerConnection().getCommitTimestamp()).isNotNull(); } } @@ -128,20 +124,20 @@ public void testRollback() throws SQLException { ConnectionOptions options = mock(ConnectionOptions.class); try (JdbcConnection connection = createConnection(options)) { // verify that there is no transaction started - assertThat(connection.getSpannerConnection().isTransactionStarted(), is(false)); + assertThat(connection.getSpannerConnection().isTransactionStarted()).isFalse(); // start a transaction connection.createStatement().execute(AbstractConnectionImplTest.SELECT); // verify that we did start a transaction - assertThat(connection.getSpannerConnection().isTransactionStarted(), is(true)); + assertThat(connection.getSpannerConnection().isTransactionStarted()).isTrue(); // do a rollback connection.rollback(); // verify that there is no transaction started anymore - assertThat(connection.getSpannerConnection().isTransactionStarted(), is(false)); + assertThat(connection.getSpannerConnection().isTransactionStarted()).isFalse(); // verify that there is no commit timestamp try (ResultSet rs = connection.createStatement().executeQuery("show variable commit_timestamp")) { - assertThat(rs.next(), is(true)); - assertThat(rs.getTimestamp("COMMIT_TIMESTAMP"), is(nullValue())); + assertThat(rs.next()).isTrue(); + assertThat(rs.getTimestamp("COMMIT_TIMESTAMP")).isNull(); } } } @@ -310,18 +306,17 @@ private void testInvokeMethodOnClosedConnection(Method method, Object... args) valid = true; } } - assertThat( - "Method did not throw exception on closed connection: " + method.getName(), - valid, - is(true)); + assertWithMessage("Method did not throw exception on closed connection: " + method.getName()) + .that(valid) + .isTrue(); } @Test public void testTransactionIsolation() throws SQLException { ConnectionOptions options = mock(ConnectionOptions.class); try (JdbcConnection connection = createConnection(options)) { - assertThat( - connection.getTransactionIsolation(), is(equalTo(Connection.TRANSACTION_SERIALIZABLE))); + assertThat(connection.getTransactionIsolation()) + .isEqualTo(Connection.TRANSACTION_SERIALIZABLE); // assert that setting it to this value is ok. connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); // assert that setting it to something else is not ok. @@ -347,7 +342,7 @@ public void testTransactionIsolation() throws SQLException { && ((JdbcSqlException) e).getCode() == Code.UNIMPLEMENTED); } } - assertThat(exception, is(true)); + assertThat(exception).isTrue(); } } } @@ -356,7 +351,7 @@ public void testTransactionIsolation() throws SQLException { public void testHoldability() throws SQLException { ConnectionOptions options = mock(ConnectionOptions.class); try (JdbcConnection connection = createConnection(options)) { - assertThat(connection.getHoldability(), is(equalTo(ResultSet.CLOSE_CURSORS_AT_COMMIT))); + assertThat(connection.getHoldability()).isEqualTo(ResultSet.CLOSE_CURSORS_AT_COMMIT); // assert that setting it to this value is ok. connection.setHoldability(ResultSet.CLOSE_CURSORS_AT_COMMIT); // assert that setting it to something else is not ok. @@ -376,7 +371,7 @@ public void testHoldability() throws SQLException { && ((JdbcSqlException) e).getCode() == Code.UNIMPLEMENTED); } } - assertThat(exception, is(true)); + assertThat(exception).isTrue(); } } } @@ -385,24 +380,24 @@ public void testHoldability() throws SQLException { public void testWarnings() throws SQLException { ConnectionOptions options = mock(ConnectionOptions.class); try (JdbcConnection connection = createConnection(options)) { - assertThat(connection.getWarnings(), is(nullValue())); + assertThat((Object) connection.getWarnings()).isNull(); // Push one warning and get it twice. connection.pushWarning(new SQLWarning("test")); - assertThat(connection.getWarnings().getMessage(), is(equalTo("test"))); - assertThat(connection.getWarnings().getMessage(), is(equalTo("test"))); + assertThat(connection.getWarnings().getMessage()).isEqualTo("test"); + assertThat(connection.getWarnings().getMessage()).isEqualTo("test"); // Clear warnings and push two warnings and get them both. connection.clearWarnings(); connection.pushWarning(new SQLWarning("test 1")); connection.pushWarning(new SQLWarning("test 2")); - assertThat(connection.getWarnings().getMessage(), is(equalTo("test 1"))); - assertThat(connection.getWarnings().getMessage(), is(equalTo("test 1"))); - assertThat(connection.getWarnings().getNextWarning().getMessage(), is(equalTo("test 2"))); + assertThat(connection.getWarnings().getMessage()).isEqualTo("test 1"); + assertThat(connection.getWarnings().getMessage()).isEqualTo("test 1"); + assertThat(connection.getWarnings().getNextWarning().getMessage()).isEqualTo("test 2"); // Clear warnings. connection.clearWarnings(); - assertThat(connection.getWarnings(), is(nullValue())); + assertThat((Object) connection.getWarnings()).isNull(); } } @@ -410,23 +405,21 @@ public void testWarnings() throws SQLException { public void testSetClientInfo() throws SQLException { ConnectionOptions options = mock(ConnectionOptions.class); try (JdbcConnection connection = createConnection(options)) { - assertThat(connection.getWarnings(), is(nullValue())); + assertThat((Object) connection.getWarnings()).isNull(); connection.setClientInfo("test", "foo"); - assertThat(connection.getWarnings(), is(notNullValue())); - assertThat( - connection.getWarnings().getMessage(), - is(equalTo(AbstractJdbcConnection.CLIENT_INFO_NOT_SUPPORTED))); + assertThat((Object) connection.getWarnings()).isNotNull(); + assertThat(connection.getWarnings().getMessage()) + .isEqualTo(AbstractJdbcConnection.CLIENT_INFO_NOT_SUPPORTED); connection.clearWarnings(); - assertThat(connection.getWarnings(), is(nullValue())); + assertThat((Object) connection.getWarnings()).isNull(); Properties props = new Properties(); props.setProperty("test", "foo"); connection.setClientInfo(props); - assertThat(connection.getWarnings(), is(notNullValue())); - assertThat( - connection.getWarnings().getMessage(), - is(equalTo(AbstractJdbcConnection.CLIENT_INFO_NOT_SUPPORTED))); + assertThat((Object) connection.getWarnings()).isNotNull(); + assertThat(connection.getWarnings().getMessage()) + .isEqualTo(AbstractJdbcConnection.CLIENT_INFO_NOT_SUPPORTED); } } @@ -442,13 +435,13 @@ public void testIsValid() throws SQLException { // Verify that an opened connection that returns a result set is valid. try (JdbcConnection connection = new JdbcConnection("url", options)) { when(spannerConnection.executeQuery(statement)).thenReturn(SELECT1_RESULTSET); - assertThat(connection.isValid(1), is(true)); + assertThat(connection.isValid(1)).isTrue(); try { // Invalid timeout value. connection.isValid(-1); fail("missing expected exception"); } catch (JdbcSqlExceptionImpl e) { - assertThat(e.getCode(), is(equalTo(Code.INVALID_ARGUMENT))); + assertThat(e.getCode()).isEqualTo(Code.INVALID_ARGUMENT); } // Now let the query return an error. isValid should now return false. @@ -456,7 +449,7 @@ public void testIsValid() throws SQLException { .thenThrow( SpannerExceptionFactory.newSpannerException( ErrorCode.ABORTED, "the current transaction has been aborted")); - assertThat(connection.isValid(1), is(false)); + assertThat(connection.isValid(1)).isFalse(); } } @@ -464,7 +457,7 @@ public void testIsValid() throws SQLException { public void testIsValidOnClosedConnection() throws SQLException { Connection connection = createConnection(mock(ConnectionOptions.class)); connection.close(); - assertThat(connection.isValid(1), is(false)); + assertThat(connection.isValid(1)).isFalse(); } @Test @@ -483,8 +476,8 @@ public void testCreateStatement() throws SQLException { { java.sql.Statement statement = connection.createStatement(resultSetType, resultSetConcurrency); - assertThat(statement.getResultSetType(), is(equalTo(resultSetType))); - assertThat(statement.getResultSetConcurrency(), is(equalTo(resultSetConcurrency))); + assertThat(statement.getResultSetType()).isEqualTo(resultSetType); + assertThat(statement.getResultSetConcurrency()).isEqualTo(resultSetConcurrency); } else { assertCreateStatementFails(connection, resultSetType, resultSetConcurrency); } @@ -499,9 +492,9 @@ public void testCreateStatement() throws SQLException { java.sql.Statement statement = connection.createStatement( resultSetType, resultSetConcurrency, resultSetHoldability); - assertThat(statement.getResultSetType(), is(equalTo(resultSetType))); - assertThat(statement.getResultSetConcurrency(), is(equalTo(resultSetConcurrency))); - assertThat(statement.getResultSetHoldability(), is(equalTo(resultSetHoldability))); + assertThat(statement.getResultSetType()).isEqualTo(resultSetType); + assertThat(statement.getResultSetConcurrency()).isEqualTo(resultSetConcurrency); + assertThat(statement.getResultSetHoldability()).isEqualTo(resultSetHoldability); } else { assertCreateStatementFails( connection, resultSetType, resultSetConcurrency, resultSetHoldability); @@ -557,8 +550,8 @@ public void testPrepareStatement() throws SQLException { { PreparedStatement ps = connection.prepareStatement("SELECT 1", resultSetType, resultSetConcurrency); - assertThat(ps.getResultSetType(), is(equalTo(resultSetType))); - assertThat(ps.getResultSetConcurrency(), is(equalTo(resultSetConcurrency))); + assertThat(ps.getResultSetType()).isEqualTo(resultSetType); + assertThat(ps.getResultSetConcurrency()).isEqualTo(resultSetConcurrency); } else { assertPrepareStatementFails(connection, resultSetType, resultSetConcurrency); } @@ -573,9 +566,9 @@ public void testPrepareStatement() throws SQLException { PreparedStatement ps = connection.prepareStatement( "SELECT 1", resultSetType, resultSetConcurrency, resultSetHoldability); - assertThat(ps.getResultSetType(), is(equalTo(resultSetType))); - assertThat(ps.getResultSetConcurrency(), is(equalTo(resultSetConcurrency))); - assertThat(ps.getResultSetHoldability(), is(equalTo(resultSetHoldability))); + assertThat(ps.getResultSetType()).isEqualTo(resultSetType); + assertThat(ps.getResultSetConcurrency()).isEqualTo(resultSetConcurrency); + assertThat(ps.getResultSetHoldability()).isEqualTo(resultSetHoldability); } else { assertPrepareStatementFails( connection, resultSetType, resultSetConcurrency, resultSetHoldability); @@ -623,7 +616,7 @@ public void testPrepareStatementWithAutoGeneratedKeys() throws SQLException { PreparedStatement statement = connection.prepareStatement(sql, java.sql.Statement.NO_GENERATED_KEYS); ResultSet rs = statement.getGeneratedKeys(); - assertThat(rs.next(), is(false)); + assertThat(rs.next()).isFalse(); try { statement = connection.prepareStatement(sql, java.sql.Statement.RETURN_GENERATED_KEYS); fail("missing expected SQLFeatureNotSupportedException"); @@ -638,7 +631,7 @@ public void testCatalog() throws SQLException { ConnectionOptions options = mock(ConnectionOptions.class); when(options.getDatabaseName()).thenReturn("test"); try (JdbcConnection connection = createConnection(options)) { - assertThat(connection.getCatalog(), is(equalTo("test"))); + assertThat(connection.getCatalog()).isEqualTo("test"); // This should be allowed. connection.setCatalog(""); try { @@ -646,7 +639,7 @@ public void testCatalog() throws SQLException { connection.setCatalog("other"); fail("missing expected exception"); } catch (JdbcSqlExceptionImpl e) { - assertThat(e.getCode(), is(equalTo(Code.INVALID_ARGUMENT))); + assertThat(e.getCode()).isEqualTo(Code.INVALID_ARGUMENT); } } } @@ -654,7 +647,7 @@ public void testCatalog() throws SQLException { @Test public void testSchema() throws SQLException { try (JdbcConnection connection = createConnection(mock(ConnectionOptions.class))) { - assertThat(connection.getSchema(), is(equalTo(""))); + assertThat(connection.getSchema()).isEqualTo(""); // This should be allowed. connection.setSchema(""); try { @@ -662,7 +655,7 @@ public void testSchema() throws SQLException { connection.setSchema("other"); fail("missing expected exception"); } catch (JdbcSqlExceptionImpl e) { - assertThat(e.getCode(), is(equalTo(Code.INVALID_ARGUMENT))); + assertThat(e.getCode()).isEqualTo(Code.INVALID_ARGUMENT); } } } diff --git a/src/test/java/com/google/cloud/spanner/jdbc/JdbcGrpcErrorTest.java b/src/test/java/com/google/cloud/spanner/jdbc/JdbcGrpcErrorTest.java index 06ea2a343a..a902207b8a 100644 --- a/src/test/java/com/google/cloud/spanner/jdbc/JdbcGrpcErrorTest.java +++ b/src/test/java/com/google/cloud/spanner/jdbc/JdbcGrpcErrorTest.java @@ -16,6 +16,9 @@ package com.google.cloud.spanner.jdbc; +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; + import com.google.cloud.spanner.MockSpannerServiceImpl; import com.google.cloud.spanner.MockSpannerServiceImpl.SimulatedExecutionTime; import com.google.cloud.spanner.MockSpannerServiceImpl.StatementResult; @@ -43,9 +46,7 @@ import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Ignore; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -84,7 +85,6 @@ public class JdbcGrpcErrorTest { private static Server server; private static InetSocketAddress address; - @Rule public ExpectedException expected = ExpectedException.none(); // FAILED_PRECONDITION is chosen as the test error code as it should never be retryable. private final Exception serverException = Status.FAILED_PRECONDITION.withDescription("test exception").asRuntimeException(); @@ -143,87 +143,100 @@ private Connection createConnection() throws SQLException { @Ignore( "This can only be guaranteed with MinSessions=0. Re-enable when MinSessions is configurable for JDBC.") @Test - public void autocommitBeginTransaction() throws SQLException { - expected.expect(testExceptionMatcher); + public void autocommitBeginTransaction() { mockSpanner.setBeginTransactionExecutionTime( SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Ignore( "This can only be guaranteed with MinSessions=0. Re-enable when MinSessions is configurable for JDBC.") @Test - public void autocommitBeginPDMLTransaction() throws SQLException { - expected.expect(testExceptionMatcher); + public void autocommitBeginPDMLTransaction() { mockSpanner.setBeginTransactionExecutionTime( SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.createStatement().execute("SET AUTOCOMMIT_DML_MODE='PARTITIONED_NON_ATOMIC'"); connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Ignore( "This can only be guaranteed with MinSessions=0. Re-enable when MinSessions is configurable for JDBC.") @Test - public void transactionalBeginTransaction() throws SQLException { - expected.expect(testExceptionMatcher); + public void transactionalBeginTransaction() { mockSpanner.setBeginTransactionExecutionTime( SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Ignore( "This can only be guaranteed with MinSessions=0. Re-enable when MinSessions is configurable for JDBC.") @Test - public void readOnlyBeginTransaction() throws SQLException { - expected.expect(testExceptionMatcher); + public void readOnlyBeginTransaction() { mockSpanner.setBeginTransactionExecutionTime( SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); connection.setReadOnly(true); connection.createStatement().executeQuery(SELECT1.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Test - public void autocommitExecuteSql() throws SQLException { - expected.expect(testExceptionMatcher); + public void autocommitExecuteSql() { mockSpanner.setExecuteSqlExecutionTime(SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Test - public void autocommitPDMLExecuteSql() throws SQLException { - expected.expect(testExceptionMatcher); + public void autocommitPDMLExecuteSql() { mockSpanner.setExecuteSqlExecutionTime(SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.createStatement().execute("SET AUTOCOMMIT_DML_MODE='PARTITIONED_NON_ATOMIC'"); connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Test - public void transactionalExecuteSql() throws SQLException { - expected.expect(testExceptionMatcher); + public void transactionalExecuteSql() { mockSpanner.setExecuteSqlExecutionTime(SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Test public void autocommitExecuteBatchDml() throws SQLException { - expected.expect(testExceptionMatcher); mockSpanner.setExecuteBatchDmlExecutionTime( SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { @@ -231,13 +244,15 @@ public void autocommitExecuteBatchDml() throws SQLException { statement.addBatch(UPDATE_STATEMENT.getSql()); statement.addBatch(UPDATE_STATEMENT.getSql()); statement.executeBatch(); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } } @Test public void transactionalExecuteBatchDml() throws SQLException { - expected.expect(testExceptionMatcher); mockSpanner.setExecuteBatchDmlExecutionTime( SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { @@ -246,40 +261,51 @@ public void transactionalExecuteBatchDml() throws SQLException { statement.addBatch(UPDATE_STATEMENT.getSql()); statement.addBatch(UPDATE_STATEMENT.getSql()); statement.executeBatch(); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } } @Test - public void autocommitCommit() throws SQLException { - expected.expect(testExceptionMatcher); + public void autocommitCommit() { mockSpanner.setCommitExecutionTime(SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Test - public void transactionalCommit() throws SQLException { - expected.expect(testExceptionMatcher); + public void transactionalCommit() { mockSpanner.setCommitExecutionTime(SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); connection.commit(); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Test - public void autocommitRollback() throws SQLException { + public void autocommitRollback() { // The JDBC driver should throw the exception of the SQL statement and ignore any errors from // the rollback() method. - expected.expect( - SpannerJdbcExceptionMatcher.matchCodeAndMessage( - JdbcSqlExceptionImpl.class, Code.NOT_FOUND, "Unknown table name")); mockSpanner.setRollbackExecutionTime(SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.createStatement().executeUpdate(INVALID_UPDATE_STATEMENT.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat( + SpannerJdbcExceptionMatcher.matchCodeAndMessage( + JdbcSqlExceptionImpl.class, Code.NOT_FOUND, "Unknown table name") + .matches(e)) + .isTrue(); } } @@ -296,74 +322,86 @@ public void transactionalRollback() throws SQLException { } @Test - public void autocommitExecuteStreamingSql() throws SQLException { - expected.expect(testExceptionMatcher); + public void autocommitExecuteStreamingSql() { mockSpanner.setExecuteStreamingSqlExecutionTime( SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.createStatement().executeQuery(SELECT1.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Test - public void transactionalExecuteStreamingSql() throws SQLException { - expected.expect(testExceptionMatcher); + public void transactionalExecuteStreamingSql() { mockSpanner.setExecuteStreamingSqlExecutionTime( SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); connection.createStatement().executeQuery(SELECT1.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Test - public void readOnlyExecuteStreamingSql() throws SQLException { - expected.expect(testExceptionMatcher); + public void readOnlyExecuteStreamingSql() { mockSpanner.setExecuteStreamingSqlExecutionTime( SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); connection.setReadOnly(true); connection.createStatement().executeQuery(SELECT1.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Ignore( "This can only be guaranteed with MinSessions=0. Re-enable when MinSessions is configurable for JDBC.") @Test - public void autocommitCreateSession() throws SQLException { - expected.expect(testExceptionMatcher); + public void autocommitCreateSession() { mockSpanner.setBatchCreateSessionsExecutionTime( SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Ignore( "This can only be guaranteed with MinSessions=0. Re-enable when MinSessions is configurable for JDBC.") @Test - public void transactionalCreateSession() throws SQLException { - expected.expect(testExceptionMatcher); + public void transactionalCreateSession() { mockSpanner.setBatchCreateSessionsExecutionTime( SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); connection.createStatement().executeUpdate(UPDATE_STATEMENT.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } @Ignore( "This can only be guaranteed with MinSessions=0. Re-enable when MinSessions is configurable for JDBC.") @Test - public void readOnlyCreateSession() throws SQLException { - expected.expect(testExceptionMatcher); + public void readOnlyCreateSession() { mockSpanner.setBatchCreateSessionsExecutionTime( SimulatedExecutionTime.ofException(serverException)); try (java.sql.Connection connection = createConnection()) { connection.setAutoCommit(false); connection.setReadOnly(true); connection.createStatement().executeQuery(SELECT1.getSql()); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(testExceptionMatcher.matches(e)).isTrue(); } } } diff --git a/src/test/java/com/google/cloud/spanner/jdbc/JdbcPreparedStatementTest.java b/src/test/java/com/google/cloud/spanner/jdbc/JdbcPreparedStatementTest.java index c72db1ec41..9b829e54e0 100644 --- a/src/test/java/com/google/cloud/spanner/jdbc/JdbcPreparedStatementTest.java +++ b/src/test/java/com/google/cloud/spanner/jdbc/JdbcPreparedStatementTest.java @@ -50,16 +50,12 @@ import java.util.Calendar; import java.util.TimeZone; import org.junit.Assert; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @RunWith(JUnit4.class) public class JdbcPreparedStatementTest { - @Rule public final ExpectedException thrown = ExpectedException.none(); - private String generateSqlWithParameters(int numberOfParams) { StringBuilder sql = new StringBuilder("INSERT INTO FOO ("); boolean first = true; @@ -100,6 +96,7 @@ private JdbcConnection createMockConnection(Connection spanner) throws SQLExcept return connection; } + @SuppressWarnings("deprecation") @Test public void testParameters() throws SQLException, MalformedURLException { final int numberOfParams = 48; diff --git a/src/test/java/com/google/cloud/spanner/jdbc/JdbcResultSetTest.java b/src/test/java/com/google/cloud/spanner/jdbc/JdbcResultSetTest.java index d564ad0f2a..44eefa9701 100644 --- a/src/test/java/com/google/cloud/spanner/jdbc/JdbcResultSetTest.java +++ b/src/test/java/com/google/cloud/spanner/jdbc/JdbcResultSetTest.java @@ -16,12 +16,14 @@ package com.google.cloud.spanner.jdbc; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import com.google.cloud.ByteArray; @@ -33,6 +35,7 @@ import com.google.cloud.spanner.Type; import com.google.cloud.spanner.Type.StructField; import com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl; +import io.grpc.Status.Code; import java.io.IOException; import java.io.InputStream; import java.io.Reader; @@ -50,9 +53,7 @@ import java.util.HashMap; import java.util.Map; import java.util.TimeZone; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -111,8 +112,6 @@ public class JdbcResultSetTest { private static final int URL_COLINDEX_NULL = 19; private static final int URL_COLINDEX_NOTNULL = 20; - @Rule public ExpectedException thrown = ExpectedException.none(); - private JdbcResultSet subject; static ResultSet getMockResultSet() { @@ -262,10 +261,19 @@ public void testGetURLIndex() throws SQLException, MalformedURLException { } @Test - public void testGetURLIndexInvalid() throws SQLException, MalformedURLException { - thrown.expect(JdbcSqlExceptionImpl.class); - thrown.expectMessage("Invalid URL"); - assertNotNull(subject.getURL(STRING_COLINDEX_NOTNULL)); + public void testGetURLIndexInvalid() throws SQLException { + try { + subject.getURL(STRING_COLINDEX_NOTNULL); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat( + SpannerJdbcExceptionMatcher.matchCodeAndMessage( + JdbcSqlExceptionImpl.class, + Code.INVALID_ARGUMENT, + "Invalid URL: " + subject.getString(STRING_COLINDEX_NOTNULL)) + .matches(e)) + .isTrue(); + } } @Test @@ -294,6 +302,7 @@ public void testGetDoubleIndex() throws SQLException { assertTrue(subject.wasNull()); } + @SuppressWarnings("deprecation") @Test public void testGetBigDecimalIndexAndScale() throws SQLException { assertNotNull(subject.getBigDecimal(DOUBLE_COLINDEX_NOTNULL, 2)); @@ -376,9 +385,18 @@ public void testGetURLLabel() throws SQLException { @Test public void testGetURLLabelInvalid() throws SQLException { - thrown.expect(JdbcSqlExceptionImpl.class); - thrown.expectMessage("Invalid URL"); - assertNotNull(subject.getURL(STRING_COL_NOT_NULL)); + try { + subject.getURL(STRING_COL_NOT_NULL); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat( + SpannerJdbcExceptionMatcher.matchCodeAndMessage( + JdbcSqlExceptionImpl.class, + Code.INVALID_ARGUMENT, + "Invalid URL: " + subject.getString(STRING_COL_NOT_NULL)) + .matches(e)) + .isTrue(); + } } @Test @@ -407,6 +425,7 @@ public void testGetDoubleLabel() throws SQLException { assertTrue(subject.wasNull()); } + @SuppressWarnings("deprecation") @Test public void testGetBigDecimalLabelAndScale() throws SQLException { assertNotNull(subject.getBigDecimal(DOUBLE_COL_NOT_NULL, 2)); @@ -839,6 +858,7 @@ public void testGetAsciiStreamIndex() throws SQLException, IOException { assertTrue(subject.wasNull()); } + @SuppressWarnings("deprecation") @Test public void testGetUnicodeStreamIndex() throws SQLException, IOException { assertNotNull(subject.getUnicodeStream(STRING_COLINDEX_NOTNULL)); @@ -861,7 +881,7 @@ public void testGetBinaryStreamIndex() throws SQLException, IOException { assertArrayEquals(BYTES_VALUE.toByteArray(), cbuf); assertEquals(3, len); assertFalse(subject.wasNull()); - assertNull(subject.getUnicodeStream(BYTES_COLINDEX_NULL)); + assertNull(subject.getBinaryStream(BYTES_COLINDEX_NULL)); assertTrue(subject.wasNull()); } @@ -878,6 +898,7 @@ public void testGetAsciiStreamLabel() throws SQLException, IOException { assertTrue(subject.wasNull()); } + @SuppressWarnings("deprecation") @Test public void testGetUnicodeStreamLabel() throws SQLException, IOException { assertNotNull(subject.getUnicodeStream(STRING_COL_NOT_NULL)); @@ -900,39 +921,59 @@ public void testGetBinaryStreamLabel() throws SQLException, IOException { assertArrayEquals(ByteArray.copyFrom("FOO").toByteArray(), cbuf); assertEquals(3, len); assertFalse(subject.wasNull()); - assertNull(subject.getUnicodeStream(BYTES_COL_NULL)); + assertNull(subject.getBinaryStream(BYTES_COL_NULL)); assertTrue(subject.wasNull()); } @Test - public void testGetBeforeNext() throws SQLException { + public void testGetBeforeNext() { try (JdbcResultSet rs = JdbcResultSet.of(mock(Statement.class), getMockResultSet())) { - thrown.expect(SQLException.class); - thrown.expectMessage( - "FAILED_PRECONDITION: ResultSet is before first row. Call next() first."); rs.getBigDecimal(LONG_COLINDEX_NOTNULL); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat( + SpannerJdbcExceptionMatcher.matchCodeAndMessage( + JdbcSqlExceptionImpl.class, + Code.FAILED_PRECONDITION, + "ResultSet is before first row. Call next() first.") + .matches(e)) + .isTrue(); } } @Test - public void testGetAfterLast() throws SQLException { + public void testGetAfterLast() { try (JdbcResultSet rs = JdbcResultSet.of(mock(Statement.class), getMockResultSet())) { while (rs.next()) { // do nothing } - thrown.expect(SQLException.class); - thrown.expectMessage( - "FAILED_PRECONDITION: ResultSet is after last row. There is no more data available."); rs.getBigDecimal(LONG_COLINDEX_NOTNULL); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat( + SpannerJdbcExceptionMatcher.matchCodeAndMessage( + JdbcSqlExceptionImpl.class, + Code.FAILED_PRECONDITION, + "ResultSet is after last row. There is no more data available.") + .matches(e)) + .isTrue(); } } @Test public void testFindIllegalColumnName() throws SQLException { - thrown.expect(SQLException.class); - thrown.expectMessage("INVALID_ARGUMENT: no column with label " + UNKNOWN_COLUMN + " found"); - int index = subject.findColumn(UNKNOWN_COLUMN); - assertEquals(0, index); + try { + subject.findColumn(UNKNOWN_COLUMN); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat( + SpannerJdbcExceptionMatcher.matchCodeAndMessage( + JdbcSqlExceptionImpl.class, + Code.INVALID_ARGUMENT, + "no column with label " + UNKNOWN_COLUMN + " found") + .matches(e)) + .isTrue(); + } } @Test diff --git a/src/test/java/com/google/cloud/spanner/jdbc/JdbcStatementTest.java b/src/test/java/com/google/cloud/spanner/jdbc/JdbcStatementTest.java index 28bb25849e..12e0921e36 100644 --- a/src/test/java/com/google/cloud/spanner/jdbc/JdbcStatementTest.java +++ b/src/test/java/com/google/cloud/spanner/jdbc/JdbcStatementTest.java @@ -16,11 +16,7 @@ package com.google.cloud.spanner.jdbc; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.CoreMatchers.nullValue; -import static org.hamcrest.MatcherAssert.assertThat; +import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -39,9 +35,7 @@ import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.mockito.Matchers; @@ -50,14 +44,11 @@ @RunWith(JUnit4.class) public class JdbcStatementTest { - @Rule public final ExpectedException thrown = ExpectedException.none(); private static final String SELECT = "SELECT 1"; private static final String UPDATE = "UPDATE FOO SET BAR=1 WHERE BAZ=2"; private static final String LARGE_UPDATE = "UPDATE FOO SET BAR=1 WHERE 1=1"; private static final String DDL = "CREATE INDEX FOO ON BAR(ID)"; - @Rule public final ExpectedException expected = ExpectedException.none(); - private JdbcStatement createStatement() { Connection spanner = mock(Connection.class); @@ -140,13 +131,13 @@ public void testQueryTimeout() throws SQLException { when(result.getResultSet()).thenReturn(mock(com.google.cloud.spanner.ResultSet.class)); when(spanner.execute(com.google.cloud.spanner.Statement.of(select))).thenReturn(result); try (Statement statement = new JdbcStatement(connection)) { - assertThat(statement.getQueryTimeout(), is(equalTo(0))); + assertThat(statement.getQueryTimeout()).isEqualTo(0); statement.setQueryTimeout(1); - assertThat(statement.getQueryTimeout(), is(equalTo(1))); + assertThat(statement.getQueryTimeout()).isEqualTo(1); statement.setQueryTimeout(99); - assertThat(statement.getQueryTimeout(), is(equalTo(99))); + assertThat(statement.getQueryTimeout()).isEqualTo(99); statement.setQueryTimeout(0); - assertThat(statement.getQueryTimeout(), is(equalTo(0))); + assertThat(statement.getQueryTimeout()).isEqualTo(0); } when(spanner.getStatementTimeout(TimeUnit.SECONDS)).thenReturn(1L); @@ -155,7 +146,7 @@ public void testQueryTimeout() throws SQLException { when(spanner.getStatementTimeout(TimeUnit.NANOSECONDS)).thenReturn(1000000000L); when(spanner.hasStatementTimeout()).thenReturn(true); try (Statement statement = new JdbcStatement(connection)) { - assertThat(statement.getQueryTimeout(), is(equalTo(0))); + assertThat(statement.getQueryTimeout()).isEqualTo(0); statement.execute(select); // statement has no timeout, so it should also not be set on the connection verify(spanner, never()).setStatementTimeout(1L, TimeUnit.SECONDS); @@ -175,12 +166,12 @@ public void testQueryTimeout() throws SQLException { public void testExecuteWithSelectStatement() throws SQLException { Statement statement = createStatement(); boolean res = statement.execute(SELECT); - assertThat(res, is(true)); - assertThat(statement.getUpdateCount(), is(equalTo(JdbcConstants.STATEMENT_RESULT_SET))); + assertThat(res).isTrue(); + assertThat(statement.getUpdateCount()).isEqualTo(JdbcConstants.STATEMENT_RESULT_SET); try (ResultSet rs = statement.getResultSet()) { - assertThat(rs, is(notNullValue())); - assertThat(rs.next(), is(true)); - assertThat(rs.getLong(1), is(equalTo(1L))); + assertThat(rs).isNotNull(); + assertThat(rs.next()).isTrue(); + assertThat(rs.getLong(1)).isEqualTo(1L); } } @@ -188,16 +179,16 @@ public void testExecuteWithSelectStatement() throws SQLException { public void testExecuteWithUpdateStatement() throws SQLException { Statement statement = createStatement(); boolean res = statement.execute(UPDATE); - assertThat(res, is(false)); - assertThat(statement.getResultSet(), is(nullValue())); - assertThat(statement.getUpdateCount(), is(equalTo(1))); + assertThat(res).isFalse(); + assertThat(statement.getResultSet()).isNull(); + assertThat(statement.getUpdateCount()).isEqualTo(1); try { - assertThat(statement.execute(LARGE_UPDATE), is(false)); - assertThat(statement.getResultSet(), is(nullValue())); + assertThat(statement.execute(LARGE_UPDATE)).isFalse(); + assertThat(statement.getResultSet()).isNull(); statement.getUpdateCount(); fail("missing expected exception"); } catch (JdbcSqlExceptionImpl e) { - assertThat(e.getCode(), is(equalTo(Code.OUT_OF_RANGE))); + assertThat(e.getCode()).isEqualTo(Code.OUT_OF_RANGE); } } @@ -205,17 +196,17 @@ public void testExecuteWithUpdateStatement() throws SQLException { public void testExecuteWithDdlStatement() throws SQLException { Statement statement = createStatement(); boolean res = statement.execute(DDL); - assertThat(res, is(false)); - assertThat(statement.getResultSet(), is(nullValue())); - assertThat(statement.getUpdateCount(), is(equalTo(JdbcConstants.STATEMENT_NO_RESULT))); + assertThat(res).isFalse(); + assertThat(statement.getResultSet()).isNull(); + assertThat(statement.getUpdateCount()).isEqualTo(JdbcConstants.STATEMENT_NO_RESULT); } @Test public void testExecuteWithGeneratedKeys() throws SQLException { Statement statement = createStatement(); - assertThat(statement.execute(UPDATE, Statement.NO_GENERATED_KEYS), is(false)); + assertThat(statement.execute(UPDATE, Statement.NO_GENERATED_KEYS)).isFalse(); ResultSet keys = statement.getGeneratedKeys(); - assertThat(keys.next(), is(false)); + assertThat(keys.next()).isFalse(); try { statement.execute(UPDATE, Statement.RETURN_GENERATED_KEYS); fail("missing expected exception"); @@ -228,59 +219,79 @@ public void testExecuteWithGeneratedKeys() throws SQLException { public void testExecuteQuery() throws SQLException { Statement statement = createStatement(); try (ResultSet rs = statement.executeQuery(SELECT)) { - assertThat(rs, is(notNullValue())); - assertThat(rs.next(), is(true)); - assertThat(rs.getLong(1), is(equalTo(1L))); + assertThat(rs).isNotNull(); + assertThat(rs.next()).isTrue(); + assertThat(rs.getLong(1)).isEqualTo(1L); } } @Test - public void testExecuteQueryWithUpdateStatement() throws SQLException { + public void testExecuteQueryWithUpdateStatement() { Statement statement = createStatement(); - expected.expect(JdbcExceptionMatcher.matchCodeAndMessage(Code.INVALID_ARGUMENT, "not a query")); - statement.executeQuery(UPDATE); + try { + statement.executeQuery(UPDATE); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat( + JdbcExceptionMatcher.matchCodeAndMessage(Code.INVALID_ARGUMENT, "not a query") + .matches(e)) + .isTrue(); + } } @Test - public void testExecuteQueryWithDdlStatement() throws SQLException { + public void testExecuteQueryWithDdlStatement() { Statement statement = createStatement(); - expected.expect(JdbcExceptionMatcher.matchCodeAndMessage(Code.INVALID_ARGUMENT, "not a query")); - statement.executeQuery(DDL); + try { + statement.executeQuery(DDL); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat( + JdbcExceptionMatcher.matchCodeAndMessage(Code.INVALID_ARGUMENT, "not a query") + .matches(e)) + .isTrue(); + } } @Test public void testExecuteUpdate() throws SQLException { Statement statement = createStatement(); - assertThat(statement.executeUpdate(UPDATE), is(equalTo(1))); + assertThat(statement.executeUpdate(UPDATE)).isEqualTo(1); try { statement.executeUpdate(LARGE_UPDATE); fail("missing expected exception"); } catch (JdbcSqlExceptionImpl e) { - assertThat(e.getCode(), is(equalTo(Code.OUT_OF_RANGE))); + assertThat(e.getCode()).isEqualTo(Code.OUT_OF_RANGE); } } @Test public void testExecuteUpdateWithSelectStatement() throws SQLException { Statement statement = createStatement(); - expected.expect( - JdbcExceptionMatcher.matchCodeAndMessage( - Code.INVALID_ARGUMENT, "The statement is not an update or DDL statement")); - statement.executeUpdate(SELECT); + try { + statement.executeUpdate(SELECT); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat( + JdbcExceptionMatcher.matchCodeAndMessage( + Code.INVALID_ARGUMENT, "The statement is not an update or DDL statement") + .matches(e)) + .isTrue(); + } } @Test public void testExecuteUpdateWithDdlStatement() throws SQLException { Statement statement = createStatement(); - assertThat(statement.executeUpdate(DDL), is(equalTo(0))); + assertThat(statement.executeUpdate(DDL)).isEqualTo(0); } @Test public void testExecuteUpdateWithGeneratedKeys() throws SQLException { Statement statement = createStatement(); - assertThat(statement.executeUpdate(UPDATE, Statement.NO_GENERATED_KEYS), is(equalTo(1))); + assertThat(statement.executeUpdate(UPDATE, Statement.NO_GENERATED_KEYS)).isEqualTo(1); ResultSet keys = statement.getGeneratedKeys(); - assertThat(keys.next(), is(false)); + assertThat(keys.next()).isFalse(); try { statement.executeUpdate(UPDATE, Statement.RETURN_GENERATED_KEYS); fail("missing expected exception"); @@ -292,36 +303,46 @@ public void testExecuteUpdateWithGeneratedKeys() throws SQLException { @Test public void testMoreResults() throws SQLException { Statement statement = createStatement(); - assertThat(statement.execute(SELECT), is(true)); + assertThat(statement.execute(SELECT)).isTrue(); ResultSet rs = statement.getResultSet(); - assertThat(statement.getMoreResults(), is(false)); - assertThat(statement.getResultSet(), is(nullValue())); - assertThat(rs.isClosed(), is(true)); + assertThat(statement.getMoreResults()).isFalse(); + assertThat(statement.getResultSet()).isNull(); + assertThat(rs.isClosed()).isTrue(); - assertThat(statement.execute(SELECT), is(true)); + assertThat(statement.execute(SELECT)).isTrue(); rs = statement.getResultSet(); - assertThat(statement.getMoreResults(Statement.KEEP_CURRENT_RESULT), is(false)); - assertThat(statement.getResultSet(), is(nullValue())); - assertThat(rs.isClosed(), is(false)); + assertThat(statement.getMoreResults(Statement.KEEP_CURRENT_RESULT)).isFalse(); + assertThat(statement.getResultSet()).isNull(); + assertThat(rs.isClosed()).isFalse(); } @Test - public void testNoBatchMixing() throws SQLException { - thrown.expect(SQLException.class); - thrown.expectMessage("Mixing DML and DDL statements in a batch is not allowed."); + public void testNoBatchMixing() { try (Statement statement = createStatement()) { statement.addBatch("INSERT INTO FOO (ID, NAME) VALUES (1, 'FOO')"); statement.addBatch("CREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat( + JdbcExceptionMatcher.matchCodeAndMessage( + Code.INVALID_ARGUMENT, + "Mixing DML and DDL statements in a batch is not allowed.") + .matches(e)) + .isTrue(); } } @Test - public void testNoBatchQuery() throws SQLException { - thrown.expect(SQLException.class); - thrown.expectMessage( - "The statement is not suitable for batching. Only DML and DDL statements are allowed for batching."); + public void testNoBatchQuery() { try (Statement statement = createStatement()) { statement.addBatch("SELECT * FROM FOO"); + } catch (SQLException e) { + assertThat( + JdbcExceptionMatcher.matchCodeAndMessage( + Code.INVALID_ARGUMENT, + "The statement is not suitable for batching. Only DML and DDL statements are allowed for batching.") + .matches(e)) + .isTrue(); } } @@ -333,7 +354,7 @@ public void testDmlBatch() throws SQLException { statement.addBatch("INSERT INTO FOO (ID, NAME) VALUES (1, 'TEST')"); statement.addBatch("INSERT INTO FOO (ID, NAME) VALUES (2, 'TEST')"); statement.addBatch("INSERT INTO FOO (ID, NAME) VALUES (3, 'TEST')"); - assertThat(statement.executeBatch(), is(equalTo(new int[] {1, 1, 1}))); + assertThat(statement.executeBatch()).asList().containsExactly(1, 1, 1); } } } @@ -342,12 +363,14 @@ public void testDmlBatch() throws SQLException { public void testConvertUpdateCounts() throws SQLException { try (JdbcStatement statement = new JdbcStatement(mock(JdbcConnection.class))) { int[] updateCounts = statement.convertUpdateCounts(new long[] {1L, 2L, 3L}); - assertThat(updateCounts, is(equalTo(new int[] {1, 2, 3}))); + assertThat(updateCounts).asList().containsExactly(1, 2, 3); updateCounts = statement.convertUpdateCounts(new long[] {0L, 0L, 0L}); - assertThat(updateCounts, is(equalTo(new int[] {0, 0, 0}))); + assertThat(updateCounts).asList().containsExactly(0, 0, 0); - expected.expect(JdbcExceptionMatcher.matchCode(Code.OUT_OF_RANGE)); statement.convertUpdateCounts(new long[] {1L, Integer.MAX_VALUE + 1L}); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(JdbcExceptionMatcher.matchCode(Code.OUT_OF_RANGE).matches(e)).isTrue(); } } @@ -356,35 +379,28 @@ public void testConvertUpdateCountsToSuccessNoInfo() throws SQLException { try (JdbcStatement statement = new JdbcStatement(mock(JdbcConnection.class))) { int[] updateCounts = new int[3]; statement.convertUpdateCountsToSuccessNoInfo(new long[] {1L, 2L, 3L}, updateCounts); - assertThat( - updateCounts, - is( - equalTo( - new int[] { - Statement.SUCCESS_NO_INFO, Statement.SUCCESS_NO_INFO, Statement.SUCCESS_NO_INFO - }))); + assertThat(updateCounts) + .asList() + .containsExactly( + Statement.SUCCESS_NO_INFO, Statement.SUCCESS_NO_INFO, Statement.SUCCESS_NO_INFO); statement.convertUpdateCountsToSuccessNoInfo(new long[] {0L, 0L, 0L}, updateCounts); - assertThat( - updateCounts, - is( - equalTo( - new int[] { - Statement.EXECUTE_FAILED, Statement.EXECUTE_FAILED, Statement.EXECUTE_FAILED - }))); + assertThat(updateCounts) + .asList() + .containsExactly( + Statement.EXECUTE_FAILED, Statement.EXECUTE_FAILED, Statement.EXECUTE_FAILED); statement.convertUpdateCountsToSuccessNoInfo(new long[] {1L, 0L, 2L}, updateCounts); - assertThat( - updateCounts, - is( - equalTo( - new int[] { - Statement.SUCCESS_NO_INFO, Statement.EXECUTE_FAILED, Statement.SUCCESS_NO_INFO - }))); - - expected.expect(JdbcExceptionMatcher.matchCode(Code.OUT_OF_RANGE)); + assertThat(updateCounts) + .asList() + .containsExactly( + Statement.SUCCESS_NO_INFO, Statement.EXECUTE_FAILED, Statement.SUCCESS_NO_INFO); + statement.convertUpdateCountsToSuccessNoInfo( new long[] {1L, Integer.MAX_VALUE + 1L}, updateCounts); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(JdbcExceptionMatcher.matchCode(Code.OUT_OF_RANGE).matches(e)).isTrue(); } } } diff --git a/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcReadOnlyTest.java b/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcReadOnlyTest.java index 8e083f592e..061f2e8de9 100644 --- a/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcReadOnlyTest.java +++ b/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcReadOnlyTest.java @@ -29,10 +29,8 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -42,8 +40,6 @@ public class ITJdbcReadOnlyTest extends ITAbstractJdbcTest { private static final long TEST_ROWS_COUNT = 1000L; - @Rule public ExpectedException exception = ExpectedException.none(); - @Override protected void appendConnectionUri(StringBuilder url) { url.append(";readOnly=true"); diff --git a/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcReadWriteAutocommitTest.java b/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcReadWriteAutocommitTest.java index dba6764fc1..fdc8bb890d 100644 --- a/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcReadWriteAutocommitTest.java +++ b/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcReadWriteAutocommitTest.java @@ -27,10 +27,8 @@ import com.google.cloud.spanner.jdbc.JdbcSqlScriptVerifier; import com.google.cloud.spanner.jdbc.SqlScriptVerifier; import org.junit.FixMethodOrder; -import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.junit.runners.MethodSorters; @@ -40,8 +38,6 @@ @FixMethodOrder(MethodSorters.NAME_ASCENDING) public class ITJdbcReadWriteAutocommitTest extends ITAbstractJdbcTest { - @Rule public ExpectedException exception = ExpectedException.none(); - @Override protected void appendConnectionUri(StringBuilder uri) { uri.append(";autocommit=true"); diff --git a/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcSimpleStatementsTest.java b/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcSimpleStatementsTest.java index a1130f2b6e..15f25b919e 100644 --- a/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcSimpleStatementsTest.java +++ b/src/test/java/com/google/cloud/spanner/jdbc/it/ITJdbcSimpleStatementsTest.java @@ -16,9 +16,8 @@ package com.google.cloud.spanner.jdbc.it; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; +import static com.google.common.truth.Truth.assertThat; +import static org.junit.Assert.fail; import com.google.cloud.spanner.IntegrationTest; import com.google.cloud.spanner.jdbc.ITAbstractJdbcTest; @@ -27,10 +26,8 @@ import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; @@ -38,15 +35,13 @@ @RunWith(JUnit4.class) @Category(IntegrationTest.class) public class ITJdbcSimpleStatementsTest extends ITAbstractJdbcTest { - @Rule public final ExpectedException expected = ExpectedException.none(); - @Test public void testSelect1() throws SQLException { try (Connection connection = createConnection()) { try (ResultSet rs = connection.createStatement().executeQuery("select 1")) { - assertThat(rs.next(), is(true)); - assertThat(rs.getInt(1), is(equalTo(1))); - assertThat(rs.next(), is(false)); + assertThat(rs.next()).isTrue(); + assertThat(rs.getInt(1)).isEqualTo(1); + assertThat(rs.next()).isFalse(); } } } @@ -56,9 +51,9 @@ public void testSelect1PreparedStatement() throws SQLException { try (Connection connection = createConnection()) { try (PreparedStatement ps = connection.prepareStatement("select 1")) { try (ResultSet rs = ps.executeQuery()) { - assertThat(rs.next(), is(true)); - assertThat(rs.getInt(1), is(equalTo(1))); - assertThat(rs.next(), is(false)); + assertThat(rs.next()).isTrue(); + assertThat(rs.getInt(1)).isEqualTo(1); + assertThat(rs.next()).isFalse(); } } } @@ -73,9 +68,9 @@ public void testPreparedStatement() throws SQLException { for (int i = 1; i <= 3; i++) { ps.setInt(1, i); try (ResultSet rs = ps.executeQuery()) { - assertThat(rs.next(), is(true)); - assertThat(rs.getInt(1), is(equalTo(i))); - assertThat(rs.next(), is(false)); + assertThat(rs.next()).isTrue(); + assertThat(rs.getInt(1)).isEqualTo(i); + assertThat(rs.next()).isFalse(); } } } @@ -92,17 +87,17 @@ public void testBatchedDdlStatements() throws SQLException { statement.addBatch( "CREATE TABLE FOO2 (ID INT64 NOT NULL, NAME STRING(100)) PRIMARY KEY (ID)"); int[] updateCounts = statement.executeBatch(); - assertThat( - updateCounts, - is(equalTo(new int[] {Statement.SUCCESS_NO_INFO, Statement.SUCCESS_NO_INFO}))); + assertThat(updateCounts) + .asList() + .containsExactly(Statement.SUCCESS_NO_INFO, Statement.SUCCESS_NO_INFO); } try (ResultSet rs = connection .createStatement() .executeQuery( "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='' AND TABLE_NAME LIKE 'FOO%'")) { - assertThat(rs.next(), is(true)); - assertThat(rs.getLong(1), is(equalTo(2L))); + assertThat(rs.next()).isTrue(); + assertThat(rs.getLong(1)).isEqualTo(2L); } } // Execute a batch of DDL statements that contains a statement that will fail. @@ -121,35 +116,37 @@ public void testBatchedDdlStatements() throws SQLException { } catch (SQLException e) { gotExpectedException = true; } - assertThat(gotExpectedException, is(true)); + assertThat(gotExpectedException).isTrue(); // The table should have been created, the index should not. try (ResultSet rs = connection .createStatement() .executeQuery( "SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='' AND TABLE_NAME LIKE 'FOO%'")) { - assertThat(rs.next(), is(true)); - assertThat(rs.getLong(1), is(equalTo(3L))); + assertThat(rs.next()).isTrue(); + assertThat(rs.getLong(1)).isEqualTo(3L); } try (ResultSet rs = connection .createStatement() .executeQuery( "SELECT COUNT(*) FROM INFORMATION_SCHEMA.INDEXES WHERE TABLE_SCHEMA='' AND INDEX_NAME='IDX_FOO1_UNIQUE'")) { - assertThat(rs.next(), is(true)); - assertThat(rs.getLong(1), is(equalTo(0L))); + assertThat(rs.next()).isTrue(); + assertThat(rs.getLong(1)).isEqualTo(0L); } } } @Test - public void testAddBatchWhenAlreadyInBatch() throws SQLException { - expected.expect(SQLException.class); - expected.expectMessage( - "Calling addBatch() is not allowed when a DML or DDL batch has been started on the connection."); + public void testAddBatchWhenAlreadyInBatch() { try (Connection connection = createConnection()) { connection.createStatement().execute("START BATCH DML"); connection.createStatement().addBatch("INSERT INTO Singers (SingerId) VALUES (-1)"); + fail("missing expected exception"); + } catch (SQLException e) { + assertThat(e.getMessage()) + .contains( + "Calling addBatch() is not allowed when a DML or DDL batch has been started on the connection."); } } }