Search in sources :

Example 1 with CloudSpannerResultSetMetaData

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

the class UpdateModeTest method mockConnection.

private static void mockConnection(CloudSpannerConnection connection) throws SQLException {
    CloudSpannerPreparedStatement insertStatement = Mockito.mock(CloudSpannerPreparedStatement.class);
    CloudSpannerPreparedStatement selectStatement = Mockito.mock(CloudSpannerPreparedStatement.class);
    CloudSpannerPreparedStatement countStatement = Mockito.mock(CloudSpannerPreparedStatement.class);
    CloudSpannerResultSet selectResult = Mockito.mock(CloudSpannerResultSet.class);
    CloudSpannerResultSet countResult = Mockito.mock(CloudSpannerResultSet.class);
    CloudSpannerResultSetMetaData selectMetadata = Mockito.mock(CloudSpannerResultSetMetaData.class);
    Mockito.when(selectMetadata.getColumnCount()).thenReturn(3);
    Mockito.when(connection.prepareStatement(Mockito.startsWith("INSERT"))).thenReturn(insertStatement);
    Mockito.when(connection.prepareStatement(Mockito.startsWith("SELECT `FOO`"))).thenReturn(selectStatement);
    Mockito.when(connection.prepareStatement(Mockito.startsWith("SELECT COUNT(*)"))).thenReturn(countStatement);
    Mockito.when(selectStatement.executeQuery()).thenReturn(selectResult);
    Mockito.when(selectResult.next()).thenReturn(true, true, false);
    Mockito.when(selectResult.getObject(1)).thenReturn(1L, 2L);
    Mockito.when(selectResult.getObject(2)).thenReturn("One", "Two");
    Mockito.when(selectResult.getObject(3)).thenReturn("En", "To");
    Mockito.when(selectResult.getMetaData()).thenReturn(selectMetadata);
    Mockito.when(countStatement.executeQuery()).thenReturn(countResult);
    Mockito.when(countResult.next()).thenReturn(true, false);
    Mockito.when(countResult.getLong(1)).thenReturn(2L);
    Mockito.when(insertStatement.executeUpdate()).thenReturn(1, 1);
}
Also used : CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) CloudSpannerResultSetMetaData(nl.topicus.jdbc.resultset.CloudSpannerResultSetMetaData)

Example 2 with CloudSpannerResultSetMetaData

use of nl.topicus.jdbc.resultset.CloudSpannerResultSetMetaData 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 + ") 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)

Aggregations

CloudSpannerResultSet (nl.topicus.jdbc.resultset.CloudSpannerResultSet)2 CloudSpannerResultSetMetaData (nl.topicus.jdbc.resultset.CloudSpannerResultSetMetaData)2 SQLException (java.sql.SQLException)1 CloudSpannerConnection (nl.topicus.jdbc.CloudSpannerConnection)1 Returns (org.mockito.internal.stubbing.answers.Returns)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1