Search in sources :

Example 21 with CloudSpannerConnection

use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.

the class CloudSpannerIT method performJdbcTests.

private void performJdbcTests() throws Exception {
    // Get a JDBC connection
    try (Connection connection = createConnection()) {
        connection.setAutoCommit(false);
        // Test connection validity
        assertTrue(connection.isValid(0));
        assertTrue(connection.isValid(1));
        assertTrue(connection.isValid(1000));
        // Check node count
        assertEquals(1, ((CloudSpannerConnection) connection).getNodeCount());
        // Test connection pooling
        ConnectionPoolingTester poolingTester = new ConnectionPoolingTester();
        poolingTester.testPooling((CloudSpannerConnection) connection);
        // Test Table DDL statements
        TableDDLTester tableDDLTester = new TableDDLTester(connection);
        tableDDLTester.runCreateTests();
        // Test DML statements
        DMLTester dmlTester = new DMLTester(connection);
        dmlTester.runDMLTests();
        // Test meta data functions
        MetaDataTester metaDataTester = new MetaDataTester((CloudSpannerConnection) connection);
        metaDataTester.runMetaDataTests();
        // Test transaction functions
        TransactionTester txTester = new TransactionTester(connection);
        txTester.runTransactionTests();
        // Test select statements
        SelectStatementsTester selectTester = new SelectStatementsTester(connection);
        selectTester.runSelectTests();
        // Test XA transactions
        XATester xaTester = new XATester();
        xaTester.testXA(getProject(), instanceId, DATABASE_ID, credentialsPath);
        // Test drop statements
        tableDDLTester.runDropTests();
    } catch (SQLException | PropertyVetoException | AssertionError e) {
        log.log(Level.WARNING, "Error during JDBC tests", e);
        throw e;
    }
}
Also used : XATester(nl.topicus.jdbc.test.integration.xa.XATester) PropertyVetoException(java.beans.PropertyVetoException) MetaDataTester(nl.topicus.jdbc.test.integration.ddl.MetaDataTester) SQLException(java.sql.SQLException) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) Connection(java.sql.Connection) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) TableDDLTester(nl.topicus.jdbc.test.integration.ddl.TableDDLTester) DMLTester(nl.topicus.jdbc.test.integration.dml.DMLTester)

Example 22 with CloudSpannerConnection

use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.

the class InsertWorkerTest method createMocks.

private void createMocks(CloudSpannerConnection connection, String selectSQL, long count, String updateSQL, boolean throwExceptionOnUpdate) throws SQLException {
    when(connection.createCopyConnection()).thenAnswer(new Answer<CloudSpannerConnection>() {

        @Override
        public CloudSpannerConnection answer(InvocationOnMock invocation) throws Throwable {
            CloudSpannerConnection copy = CloudSpannerTestObjects.createConnection();
            createMocks(copy, selectSQL, count, updateSQL);
            return copy;
        }
    });
    CloudSpannerPreparedStatement countStatement = mock(CloudSpannerPreparedStatement.class);
    CloudSpannerResultSet countResultSet = mock(CloudSpannerResultSet.class);
    when(countResultSet.next()).thenReturn(true, false);
    when(countResultSet.getLong(1)).thenReturn(count);
    when(countStatement.executeQuery()).thenReturn(countResultSet);
    when(connection.prepareStatement("SELECT COUNT(*) AS C FROM ((" + selectSQL + ") LIMIT 5000) Q")).thenReturn(countStatement);
    CloudSpannerPreparedStatement selectStatement = mock(CloudSpannerPreparedStatement.class);
    CloudSpannerResultSet selectResultSet = mock(CloudSpannerResultSet.class);
    CloudSpannerResultSetMetaData metadata = mock(CloudSpannerResultSetMetaData.class);
    when(metadata.getColumnCount()).thenReturn(3);
    when(selectResultSet.next()).then(new Answer<Boolean>() {

        private long called = 0;

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            called++;
            if (called <= count)
                return true;
            return false;
        }
    });
    when(selectResultSet.getObject(1)).then(new Returns(1L));
    when(selectResultSet.getObject(2)).then(new Returns("TWO"));
    when(selectResultSet.getObject(3)).then(new Returns("TO"));
    when(selectResultSet.getMetaData()).thenReturn(metadata);
    when(selectStatement.executeQuery()).thenReturn(selectResultSet);
    when(connection.prepareStatement(selectSQL)).thenReturn(selectStatement);
    CloudSpannerPreparedStatement updateStatement = mock(CloudSpannerPreparedStatement.class);
    if (throwExceptionOnUpdate)
        when(updateStatement.executeUpdate()).thenThrow(SQLException.class);
    else
        when(updateStatement.executeUpdate()).thenReturn(1);
    when(connection.prepareStatement(updateSQL)).thenReturn(updateStatement);
}
Also used : SQLException(java.sql.SQLException) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) Returns(org.mockito.internal.stubbing.answers.Returns) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) CloudSpannerResultSetMetaData(nl.topicus.jdbc.resultset.CloudSpannerResultSetMetaData)

Example 23 with CloudSpannerConnection

use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.

the class InsertWorkerTest method prepareAutoCommitSimpleInsert.

private CloudSpannerPreparedStatement prepareAutoCommitSimpleInsert(boolean exception) throws SQLException {
    CloudSpannerPreparedStatement statement = prepareSimpleInsert(exception);
    CloudSpannerConnection connection = statement.getConnection();
    assertFalse(connection.getAutoCommit());
    connection.setAutoCommit(true);
    assertTrue(connection.getAutoCommit());
    return statement;
}
Also used : CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection)

Example 24 with CloudSpannerConnection

use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.

the class ExtendedModeIT method test1_ExtendedModeUpdate.

@Test
public void test1_ExtendedModeUpdate() throws SQLException {
    ((CloudSpannerConnection) getConnection()).setAllowExtendedMode(true);
    try (ResultSet rs = getConnection().createStatement().executeQuery("select * from test order by id")) {
        int i = 0;
        while (rs.next()) {
            assertEquals(String.valueOf(i), rs.getString("name"));
            i++;
        }
    }
    PreparedStatement ps = getConnection().prepareStatement("update test set name=concat('one', name) where id < ?");
    ps.setLong(1, 10L);
    ps.executeUpdate();
    getConnection().commit();
    try (ResultSet rs = getConnection().createStatement().executeQuery("select * from test order by id")) {
        int i = 0;
        while (rs.next()) {
            if (i < 10) {
                assertEquals("one" + String.valueOf(i), rs.getString("name"));
            } else {
                assertEquals(String.valueOf(i), rs.getString("name"));
            }
            i++;
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) PreparedStatement(java.sql.PreparedStatement) IntegrationTest(nl.topicus.jdbc.test.category.IntegrationTest) Test(org.junit.Test)

Example 25 with CloudSpannerConnection

use of nl.topicus.jdbc.CloudSpannerConnection in project spanner-jdbc by olavloite.

the class ExtendedModeIT method test5_ExtendedModeUpdateWithScaleOrLength.

@Test
public void test5_ExtendedModeUpdateWithScaleOrLength() throws SQLException {
    ((CloudSpannerConnection) getConnection()).setAllowExtendedMode(true);
    try (ResultSet rs = getConnection().createStatement().executeQuery("select count(*) from test where id=40 and name like '4%'")) {
        while (rs.next()) {
            assertEquals(1L, rs.getLong(1));
        }
    }
    PreparedStatement ps = getConnection().prepareStatement("delete from test where id=? and name like coalesce(?, '5%')");
    ps.setLong(1, 40L);
    ps.setObject(2, "4%", Types.NVARCHAR, 100);
    ps.executeUpdate();
    getConnection().commit();
    try (ResultSet rs = getConnection().createStatement().executeQuery("select count(*) from test where id=40 and name like '4%'")) {
        while (rs.next()) {
            assertEquals(0L, rs.getLong(1));
        }
    }
}
Also used : ResultSet(java.sql.ResultSet) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) PreparedStatement(java.sql.PreparedStatement) IntegrationTest(nl.topicus.jdbc.test.category.IntegrationTest) Test(org.junit.Test)

Aggregations

CloudSpannerConnection (nl.topicus.jdbc.CloudSpannerConnection)37 Test (org.junit.Test)22 UnitTest (nl.topicus.jdbc.test.category.UnitTest)17 ResultSet (java.sql.ResultSet)11 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 IntegrationTest (nl.topicus.jdbc.test.category.IntegrationTest)5 SQLException (java.sql.SQLException)4 Returns (org.mockito.internal.stubbing.answers.Returns)4 TableKeyMetaData (nl.topicus.jdbc.MetaDataStore.TableKeyMetaData)3 CloudSpannerResultSet (nl.topicus.jdbc.resultset.CloudSpannerResultSet)3 Select (net.sf.jsqlparser.statement.select.Select)2 CloudSpannerDataSourceTest (nl.topicus.jdbc.CloudSpannerDataSourceTest)2 CloudSpannerDatabaseMetaData (nl.topicus.jdbc.CloudSpannerDatabaseMetaData)2 CloudSpannerXADataSource (nl.topicus.jdbc.CloudSpannerXADataSource)2 CloudSpannerSQLException (nl.topicus.jdbc.exception.CloudSpannerSQLException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Code (com.google.rpc.Code)1 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)1 PropertyVetoException (java.beans.PropertyVetoException)1