use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testCatalog.
@Test
public void testCatalog() throws SQLException {
ConnectionOptions options = mockOptions();
when(options.getDatabaseName()).thenReturn("test");
try (JdbcConnection connection = createConnection(options)) {
// The connection should always return the empty string as the current catalog, as no other
// catalogs exist in the INFORMATION_SCHEMA.
assertThat(connection.getCatalog()).isEqualTo("");
// This should be allowed.
connection.setCatalog("");
try {
// This should cause an exception.
connection.setCatalog("other");
fail("missing expected exception");
} catch (JdbcSqlExceptionImpl e) {
assertThat(e.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
}
}
}
use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl in project java-spanner-jdbc by googleapis.
the class JdbcStatementTest method testExecuteUpdate.
@Test
public void testExecuteUpdate() throws SQLException {
Statement statement = createStatement();
assertThat(statement.executeUpdate(UPDATE)).isEqualTo(1);
try {
statement.executeUpdate(LARGE_UPDATE);
fail("missing expected exception");
} catch (JdbcSqlExceptionImpl e) {
assertThat(e.getCode()).isEqualTo(Code.OUT_OF_RANGE);
}
}
use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl in project java-spanner-jdbc by googleapis.
the class JdbcStatementTest method testExecuteWithUpdateStatement.
@Test
public void testExecuteWithUpdateStatement() throws SQLException {
Statement statement = createStatement();
boolean res = statement.execute(UPDATE);
assertThat(res).isFalse();
assertThat(statement.getResultSet()).isNull();
assertThat(statement.getUpdateCount()).isEqualTo(1);
assertThat(statement.execute(LARGE_UPDATE)).isFalse();
assertThat(statement.getResultSet()).isNull();
try {
statement.getUpdateCount();
fail("missing expected exception");
} catch (JdbcSqlExceptionImpl e) {
assertThat(e.getCode()).isEqualTo(Code.OUT_OF_RANGE);
}
assertThat(statement.getLargeUpdateCount()).isEqualTo(Integer.MAX_VALUE + 1L);
}
use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl in project java-spanner-jdbc by googleapis.
the class JdbcStatementTest method testInternalExecuteUpdate.
@Test
public void testInternalExecuteUpdate() throws SQLException {
JdbcConnection connection = mock(JdbcConnection.class);
when(connection.getDialect()).thenReturn(dialect);
Connection spannerConnection = mock(Connection.class);
when(connection.getSpannerConnection()).thenReturn(spannerConnection);
com.google.cloud.spanner.Statement updateStatement = com.google.cloud.spanner.Statement.of(UPDATE);
com.google.cloud.spanner.Statement largeUpdateStatement = com.google.cloud.spanner.Statement.of(LARGE_UPDATE);
when(spannerConnection.executeUpdate(updateStatement)).thenReturn(1L);
when(spannerConnection.executeUpdate(largeUpdateStatement)).thenReturn(Integer.MAX_VALUE + 1L);
try (JdbcStatement statement = new JdbcStatement(connection)) {
assertThat(statement.executeUpdate(updateStatement)).isEqualTo(1);
try {
statement.executeUpdate(largeUpdateStatement);
fail("missing expected exception");
} catch (JdbcSqlExceptionImpl e) {
assertThat(e.getCode()).isEqualTo(Code.OUT_OF_RANGE);
}
}
}
use of com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testIsValid.
@Test
public void testIsValid() throws SQLException {
// Setup.
ConnectionOptions options = mockOptions();
com.google.cloud.spanner.connection.Connection spannerConnection = mock(com.google.cloud.spanner.connection.Connection.class);
when(spannerConnection.getDialect()).thenReturn(dialect);
when(options.getConnection()).thenReturn(spannerConnection);
Statement statement = Statement.of(JdbcConnection.IS_VALID_QUERY);
// Verify that an opened connection that returns a result set is valid.
try (JdbcConnection connection = new JdbcConnection("url", options)) {
when(spannerConnection.executeQuery(statement)).thenReturn(createSelect1ResultSet());
assertThat(connection.isValid(1)).isTrue();
try {
// Invalid timeout value.
connection.isValid(-1);
fail("missing expected exception");
} catch (JdbcSqlExceptionImpl e) {
assertThat(e.getCode()).isEqualTo(Code.INVALID_ARGUMENT);
}
// Now let the query return an error. isValid should now return false.
when(spannerConnection.executeQuery(statement)).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.ABORTED, "the current transaction has been aborted"));
assertThat(connection.isValid(1)).isFalse();
}
}
Aggregations