Skip to content

Commit

Permalink
fix: fix deprecation warnings in JDBC (test) files (googleapis#81)
Browse files Browse the repository at this point in the history
* fix: fix deprecation warnings in JDBC files

* fix: fix typo in test case

* fix: remove commented line of code
  • Loading branch information
olavloite committed Mar 10, 2020
1 parent 9e770f2 commit a5e031d
Show file tree
Hide file tree
Showing 11 changed files with 392 additions and 320 deletions.
Expand Up @@ -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();
Expand Down
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Object[]> data() {
List<Object[]> params = new ArrayList<>();
Expand Down Expand Up @@ -175,23 +167,25 @@ 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);
}
}

@Test
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();
}
}

Expand All @@ -203,25 +197,27 @@ 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();
try (java.sql.Statement statement = connection.createStatement()) {
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();
}
}
}
Expand All @@ -233,38 +229,35 @@ 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);
}
}
}
}

@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.
Expand All @@ -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(
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -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(
Expand All @@ -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 {
Expand All @@ -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();
}
}
}

0 comments on commit a5e031d

Please sign in to comment.