Search in sources :

Example 1 with ConnectionOptions

use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.

the class JdbcConnectionTest method testInvokeMethodOnClosedConnection.

private void testInvokeMethodOnClosedConnection(Method method, Object... args) throws SQLException, IllegalAccessException, IllegalArgumentException {
    ConnectionOptions options = mockOptions();
    JdbcConnection connection = createConnection(options);
    connection.close();
    boolean valid = false;
    try {
        method.invoke(connection, args);
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof JdbcSqlException && ((JdbcSqlException) e.getTargetException()).getCode() == Code.FAILED_PRECONDITION && ((JdbcSqlException) e.getTargetException()).getMessage().endsWith("has been closed")) {
            // this is the expected exception
            valid = true;
        }
    }
    assertWithMessage("Method did not throw exception on closed connection: " + method.getName()).that(valid).isTrue();
}
Also used : ConnectionOptions(com.google.cloud.spanner.connection.ConnectionOptions) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ConnectionOptions

use of com.google.cloud.spanner.connection.ConnectionOptions 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);
        }
    }
}
Also used : JdbcSqlExceptionImpl(com.google.cloud.spanner.jdbc.JdbcSqlExceptionFactory.JdbcSqlExceptionImpl) 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 3 with ConnectionOptions

use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.

the class JdbcConnectionTest method testSetClientInfo.

@Test
public void testSetClientInfo() throws SQLException {
    ConnectionOptions options = mockOptions();
    try (JdbcConnection connection = createConnection(options)) {
        try (ResultSet validProperties = connection.getMetaData().getClientInfoProperties()) {
            while (validProperties.next()) {
                assertThat((Object) connection.getWarnings()).isNull();
                String name = validProperties.getString("NAME");
                connection.setClientInfo(name, "new-client-info-value");
                assertThat((Object) connection.getWarnings()).isNull();
                assertThat(connection.getClientInfo(name)).isEqualTo("new-client-info-value");
                Properties props = new Properties();
                props.setProperty(name.toLowerCase(), "some-other-value");
                connection.setClientInfo(props);
                assertThat((Object) connection.getWarnings()).isNull();
                assertThat(connection.getClientInfo(name)).isEqualTo("some-other-value");
                assertThat(connection.getClientInfo().keySet()).hasSize(1);
                for (String key : connection.getClientInfo().stringPropertyNames()) {
                    if (key.equals(name)) {
                        assertThat(connection.getClientInfo().getProperty(key)).isEqualTo("some-other-value");
                    } else {
                        assertThat(connection.getClientInfo().getProperty(key)).isEqualTo("");
                    }
                }
            }
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) 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 4 with ConnectionOptions

use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.

the class JdbcConnectionTest method testGetCommitResponse_throwsSqlException.

@Test
public void testGetCommitResponse_throwsSqlException() {
    ConnectionOptions options = mockOptions();
    com.google.cloud.spanner.connection.Connection spannerConnection = mock(com.google.cloud.spanner.connection.Connection.class);
    when(options.getConnection()).thenReturn(spannerConnection);
    Mockito.doThrow(SpannerExceptionFactory.newSpannerException(ErrorCode.FAILED_PRECONDITION, "test exception")).when(spannerConnection).setReturnCommitStats(any(boolean.class));
    try (JdbcConnection connection = new JdbcConnection("jdbc:cloudspanner://localhost/projects/project/instances/instance/databases/database;credentialsUrl=url", options)) {
        connection.setReturnCommitStats(true);
        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 5 with ConnectionOptions

use of com.google.cloud.spanner.connection.ConnectionOptions in project java-spanner-jdbc by googleapis.

the class JdbcConnectionTest method testWarnings.

@Test
public void testWarnings() throws SQLException {
    ConnectionOptions options = mockOptions();
    try (JdbcConnection connection = createConnection(options)) {
        assertThat((Object) connection.getWarnings()).isNull();
        // Push one warning and get it twice.
        connection.pushWarning(new SQLWarning("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()).isEqualTo("test 1");
        assertThat(connection.getWarnings().getMessage()).isEqualTo("test 1");
        assertThat(connection.getWarnings().getNextWarning().getMessage()).isEqualTo("test 2");
        // Clear warnings.
        connection.clearWarnings();
        assertThat((Object) connection.getWarnings()).isNull();
    }
}
Also used : SQLWarning(java.sql.SQLWarning) 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)

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