use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testCommit.
@Test
public void testCommit() throws SQLException {
ConnectionOptions options = mockOptions();
try (JdbcConnection connection = createConnection(options)) {
// verify that there is no transaction started
assertThat(connection.getSpannerConnection().isTransactionStarted()).isFalse();
// start a transaction
connection.createStatement().execute(AbstractConnectionImplTest.SELECT);
// verify that we did start a transaction
assertThat(connection.getSpannerConnection().isTransactionStarted()).isTrue();
// do a commit
connection.commit();
// verify that there is no transaction started anymore
assertThat(connection.getSpannerConnection().isTransactionStarted()).isFalse();
// verify that there is a commit timestamp
assertThat(connection.getSpannerConnection().getCommitTimestamp()).isNotNull();
}
}
use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testIsReturnCommitStats_throwsSqlException.
@Test
public void testIsReturnCommitStats_throwsSqlException() {
ConnectionOptions options = mockOptions();
com.google.cloud.spanner.connection.Connection spannerConnection = mock(com.google.cloud.spanner.connection.Connection.class);
when(options.getConnection()).thenReturn(spannerConnection);
when(spannerConnection.isReturnCommitStats()).thenThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION, "test exception"));
try (JdbcConnection connection = new JdbcConnection("jdbc:cloudspanner://localhost/projects/project/instances/instance/databases/database;credentialsUrl=url", options)) {
connection.isReturnCommitStats();
fail("missing expected exception");
} catch (SQLException e) {
assertTrue(e instanceof JdbcSqlException);
assertEquals(Code.FAILED_PRECONDITION, ((JdbcSqlException) e).getCode());
}
}
use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testAutoCommit.
@Test
public void testAutoCommit() throws SQLException {
ConnectionOptions options = mockOptions();
when(options.isAutocommit()).thenReturn(true);
try (Connection connection = createConnection(options)) {
assertThat(connection.getAutoCommit()).isTrue();
connection.setAutoCommit(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()).isTrue();
}
}
use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcConnectionTest method testSetInvalidClientInfo.
@Test
public void testSetInvalidClientInfo() throws SQLException {
ConnectionOptions options = mockOptions();
try (JdbcConnection connection = createConnection(options)) {
assertThat((Object) connection.getWarnings()).isNull();
connection.setClientInfo("test", "foo");
assertThat((Object) connection.getWarnings()).isNotNull();
assertThat(connection.getWarnings().getMessage()).isEqualTo(String.format(AbstractJdbcConnection.CLIENT_INFO_NOT_SUPPORTED, "TEST"));
connection.clearWarnings();
assertThat((Object) connection.getWarnings()).isNull();
Properties props = new Properties();
props.setProperty("test", "foo");
connection.setClientInfo(props);
assertThat((Object) connection.getWarnings()).isNotNull();
assertThat(connection.getWarnings().getMessage()).isEqualTo(String.format(AbstractJdbcConnection.CLIENT_INFO_NOT_SUPPORTED, "TEST"));
}
}
use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.
the class JdbcDatabaseMetaDataTest method testGetUserName.
@Test
public void testGetUserName() throws SQLException, IOException {
GoogleCredentials credentials = GoogleCredentials.fromStream(Objects.requireNonNull(ConnectionOptionsTest.class.getResource("test-key.json")).openStream());
JdbcConnection connection = mock(JdbcConnection.class);
ConnectionOptions options = mock(ConnectionOptions.class);
when(options.getCredentials()).thenReturn(credentials);
when(connection.getConnectionOptions()).thenReturn(options);
DatabaseMetaData meta = new JdbcDatabaseMetaData(connection);
assertThat(meta.getUserName(), is(equalTo("test@test-project.iam.gserviceaccount.com")));
}
Aggregations