Search in sources :

Example 6 with ConnectionOptions

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();
    }
}
Also used : ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) ConnectionImplTest(com.google.cloud.spanner.connection.ConnectionImplTest) AbstractConnectionImplTest(com.google.cloud.spanner.connection.AbstractConnectionImplTest) Test(org.junit.Test)

Example 7 with ConnectionOptions

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());
    }
}
Also used : SQLException(java.sql.SQLException) ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) ConnectionImplTest(com.google.cloud.spanner.connection.ConnectionImplTest) AbstractConnectionImplTest(com.google.cloud.spanner.connection.AbstractConnectionImplTest) Test(org.junit.Test)

Example 8 with ConnectionOptions

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();
    }
}
Also used : Connection(java.sql.Connection) ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) ConnectionImplTest(com.google.cloud.spanner.connection.ConnectionImplTest) AbstractConnectionImplTest(com.google.cloud.spanner.connection.AbstractConnectionImplTest) Test(org.junit.Test)

Example 9 with ConnectionOptions

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"));
    }
}
Also used : ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) Properties(java.util.Properties) ConnectionImplTest(com.google.cloud.spanner.connection.ConnectionImplTest) AbstractConnectionImplTest(com.google.cloud.spanner.connection.AbstractConnectionImplTest) Test(org.junit.Test)

Example 10 with ConnectionOptions

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")));
}
Also used : ConnectionOptionsTest(com.google.cloud.spanner.connection.ConnectionOptionsTest) GoogleCredentials(com.google.auth.oauth2.GoogleCredentials) ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test) ConnectionOptionsTest(com.google.cloud.spanner.connection.ConnectionOptionsTest)

Aggregations

ConnectionOptions (com.google.cloud.spanner.connection.ConnectionOptions)19 Test (org.junit.Test)16 AbstractConnectionImplTest (com.google.cloud.spanner.connection.AbstractConnectionImplTest)15 ConnectionImplTest (com.google.cloud.spanner.connection.ConnectionImplTest)15 SQLException (java.sql.SQLException)7 Properties (java.util.Properties)3 SpannerException (com.google.cloud.spanner.SpannerException)2 JdbcSqlExceptionImpl (com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl)2 Connection (java.sql.Connection)2 ResultSet (java.sql.ResultSet)2 SQLWarning (java.sql.SQLWarning)2 Savepoint (java.sql.Savepoint)2 InternalApi (com.google.api.core.InternalApi)1 GoogleCredentials (com.google.auth.oauth2.GoogleCredentials)1 Statement (com.google.cloud.spanner.Statement)1 Connection (com.google.cloud.spanner.connection.Connection)1 ConnectionOptionsTest (com.google.cloud.spanner.connection.ConnectionOptionsTest)1 OptionsMetadata (com.google.cloud.spanner.pgadapter.metadata.OptionsMetadata)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 DatabaseMetaData (java.sql.DatabaseMetaData)1