Search in sources :

Example 1 with CloudSpannerDatabaseMetaData

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

the class CloudSpannerResultSetMetaDataTest method setup.

@Before
public void setup() throws SQLException {
    connection = mock(CloudSpannerConnection.class);
    CloudSpannerStatement statement = mock(CloudSpannerStatement.class);
    CloudSpannerResultSet resultSet = getFooTestResultSet(statement);
    CloudSpannerDatabaseMetaData metadata = mock(CloudSpannerDatabaseMetaData.class);
    when(metadata.getColumns(eq(""), eq(""), eq("FOO"), any())).then(new Answer<CloudSpannerResultSet>() {

        @Override
        public CloudSpannerResultSet answer(InvocationOnMock invocation) throws Throwable {
            return createFooColumnsResultSet(statement, invocation.getArgument(3));
        }
    });
    when(metadata.getPrimaryKeys("", "", "FOO")).then(new Answer<CloudSpannerResultSet>() {

        @Override
        public CloudSpannerResultSet answer(InvocationOnMock invocation) throws Throwable {
            return createFooPrimaryKeysResultSet(statement);
        }
    });
    when(connection.getMetaData()).then(new Returns(metadata));
    when(connection.isReportDefaultSchemaAsNull()).thenCallRealMethod();
    when(connection.setReportDefaultSchemaAsNull(anyBoolean())).thenCallRealMethod();
    when(connection.getSchema()).thenCallRealMethod();
    when(connection.getCatalog()).thenCallRealMethod();
    when(statement.getConnection()).then(new Returns(connection));
    subject = resultSet.getMetaData();
}
Also used : CloudSpannerStatement(nl.topicus.jdbc.statement.CloudSpannerStatement) CloudSpannerDatabaseMetaData(nl.topicus.jdbc.CloudSpannerDatabaseMetaData) Returns(org.mockito.internal.stubbing.answers.Returns) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) Before(org.junit.Before)

Example 2 with CloudSpannerDatabaseMetaData

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

the class CloudSpannerParameterMetaDataTest method createStatement.

private CloudSpannerPreparedStatement createStatement(String sql) throws SQLException {
    CloudSpannerConnection connection = mock(CloudSpannerConnection.class);
    CloudSpannerDatabaseMetaData metadata = mock(CloudSpannerDatabaseMetaData.class);
    CloudSpannerResultSet columns = mock(CloudSpannerResultSet.class);
    TableKeyMetaData table = mock(TableKeyMetaData.class);
    when(table.getKeyColumns()).thenReturn(Arrays.asList("COL1"));
    when(connection.getTable("FOO")).thenReturn(table);
    when(connection.getMetaData()).thenAnswer(new Returns(metadata));
    when(metadata.getColumns(null, null, "FOO", null)).thenReturn(columns);
    final ColumnsNextAnswer next = new ColumnsNextAnswer();
    when(columns.next()).thenAnswer(next);
    when(columns.getString("COLUMN_NAME")).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return Arrays.asList("COL1", "COL2", "COL3").get(next.nextCalled - 1);
        }
    });
    when(columns.getInt("COLUMN_SIZE")).thenAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            return Arrays.asList(8, 50, 100).get(next.nextCalled - 1);
        }
    });
    when(columns.getInt("DATA_TYPE")).thenAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            return Arrays.asList(Types.BIGINT, Types.NVARCHAR, Types.NVARCHAR).get(next.nextCalled - 1);
        }
    });
    when(columns.getInt("NULLABLE")).thenAnswer(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocation) throws Throwable {
            return Arrays.asList(ParameterMetaData.parameterNoNulls, ParameterMetaData.parameterNoNulls, ParameterMetaData.parameterNullable).get(next.nextCalled - 1);
        }
    });
    return new CloudSpannerPreparedStatement(sql, connection, null);
}
Also used : CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) TableKeyMetaData(nl.topicus.jdbc.MetaDataStore.TableKeyMetaData) CloudSpannerDatabaseMetaData(nl.topicus.jdbc.CloudSpannerDatabaseMetaData) Returns(org.mockito.internal.stubbing.answers.Returns) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 3 with CloudSpannerDatabaseMetaData

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

the class CloudSpannerTestObjects method createMetaData.

private static CloudSpannerDatabaseMetaData createMetaData() throws SQLException {
    CloudSpannerDatabaseMetaData metadata = Mockito.mock(CloudSpannerDatabaseMetaData.class);
    Mockito.when(metadata.getPrimaryKeys(Mockito.any(), Mockito.any(), Mockito.matches(Pattern.compile("FOO", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)))).thenAnswer(new Answer<ResultSet>() {

        @Override
        public ResultSet answer(InvocationOnMock invocation) throws Throwable {
            CloudSpannerResultSet primaryKeyFoo = Mockito.mock(CloudSpannerResultSet.class);
            Mockito.when(primaryKeyFoo.next()).thenReturn(true, false);
            Mockito.when(primaryKeyFoo.getString("COLUMN_NAME")).thenReturn("ID");
            return primaryKeyFoo;
        }
    });
    Mockito.when(metadata.getPrimaryKeys(Mockito.any(), Mockito.any(), Mockito.matches(Pattern.compile("BAR", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)))).thenAnswer(new Answer<ResultSet>() {

        @Override
        public ResultSet answer(InvocationOnMock invocation) throws Throwable {
            CloudSpannerResultSet primaryKeyBar = Mockito.mock(CloudSpannerResultSet.class);
            Mockito.when(primaryKeyBar.next()).thenReturn(true, true, false);
            Mockito.when(primaryKeyBar.getString("COLUMN_NAME")).thenReturn("ID1", "ID2");
            return primaryKeyBar;
        }
    });
    Mockito.when(metadata.getColumns(Mockito.any(), Mockito.any(), Mockito.matches(Pattern.compile("FOO", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)), Mockito.any())).thenAnswer(new Answer<ResultSet>() {

        @Override
        public ResultSet answer(InvocationOnMock invocation) throws Throwable {
            CloudSpannerResultSet fooColumns = Mockito.mock(CloudSpannerResultSet.class);
            Mockito.when(fooColumns.next()).thenReturn(true, true, true, false);
            Mockito.when(fooColumns.getString("COLUMN_NAME")).thenReturn("ID", "COL1", "COL2");
            Mockito.when(fooColumns.getInt("COLUMN_SIZE")).thenReturn(8, 50, 100);
            Mockito.when(fooColumns.getInt("DATA_TYPE")).thenReturn(Types.BIGINT, Types.NVARCHAR, Types.NVARCHAR);
            Mockito.when(fooColumns.getInt("NULLABLE")).thenReturn(ResultSetMetaData.columnNoNulls, ResultSetMetaData.columnNoNulls, ResultSetMetaData.columnNullable);
            return fooColumns;
        }
    });
    Mockito.when(metadata.getIndexInfo(Mockito.any(), Mockito.any(), Mockito.matches(Pattern.compile("FOO", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)), Mockito.anyBoolean(), Mockito.anyBoolean())).thenAnswer(new Answer<ResultSet>() {

        @Override
        public ResultSet answer(InvocationOnMock invocation) throws Throwable {
            CloudSpannerResultSet indices = Mockito.mock(CloudSpannerResultSet.class);
            Mockito.when(indices.next()).thenReturn(true, false);
            return indices;
        }
    });
    return metadata;
}
Also used : CloudSpannerDatabaseMetaData(nl.topicus.jdbc.CloudSpannerDatabaseMetaData) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) ResultSet(java.sql.ResultSet) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

Example 4 with CloudSpannerDatabaseMetaData

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

the class CloudSpannerTestObjects method createConnection.

public static CloudSpannerConnection createConnection() throws SQLException {
    CloudSpannerConnection connection = Mockito.mock(CloudSpannerConnection.class);
    Mockito.doCallRealMethod().when(connection).setAutoCommit(Mockito.anyBoolean());
    Mockito.when(connection.getAutoCommit()).thenCallRealMethod();
    connection.setAutoCommit(false);
    Mockito.doCallRealMethod().when(connection).setBatchReadOnly(Mockito.anyBoolean());
    Mockito.when(connection.isBatchReadOnly()).thenCallRealMethod();
    Mockito.doCallRealMethod().when(connection).setReadOnly(Mockito.anyBoolean());
    Mockito.when(connection.isReadOnly()).thenCallRealMethod();
    Mockito.when(connection.isAllowExtendedMode()).thenAnswer(new Returns(true));
    Mockito.when(connection.createArrayOf(Mockito.anyString(), Mockito.any())).thenCallRealMethod();
    CloudSpannerDatabaseMetaData metadata = createMetaData();
    Mockito.when(connection.getMetaData()).thenReturn(metadata);
    CloudSpannerTransaction transaction = Mockito.mock(CloudSpannerTransaction.class);
    Mockito.when(transaction.executeQuery(Mockito.any())).thenReturn(Mockito.mock(com.google.cloud.spanner.ResultSet.class));
    Mockito.when(transaction.partitionQuery(Mockito.any(), Mockito.any())).thenReturn(Arrays.asList(mock(Partition.class), mock(Partition.class), mock(Partition.class)));
    Mockito.when(connection.getTransaction()).thenReturn(transaction);
    TableKeyMetaData tableFoo = Mockito.mock(TableKeyMetaData.class);
    Mockito.when(tableFoo.getKeyColumns()).thenAnswer(new Returns(Arrays.asList("ID")));
    Mockito.when(connection.getTable(Mockito.matches(Pattern.compile("FOO", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)))).thenAnswer(new Returns(tableFoo));
    TableKeyMetaData tableBar = Mockito.mock(TableKeyMetaData.class);
    Mockito.when(tableBar.getKeyColumns()).thenAnswer(new Returns(Arrays.asList("ID1", "ID2")));
    Mockito.when(connection.getTable(Mockito.matches(Pattern.compile("BAR", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)))).thenAnswer(new Returns(tableBar));
    Mockito.when(connection.getLogger()).thenAnswer(new Returns(new Logger()));
    mockXAMethods(connection);
    return connection;
}
Also used : TableKeyMetaData(nl.topicus.jdbc.MetaDataStore.TableKeyMetaData) Returns(org.mockito.internal.stubbing.answers.Returns) CloudSpannerDatabaseMetaData(nl.topicus.jdbc.CloudSpannerDatabaseMetaData) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) ResultSet(java.sql.ResultSet) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) CloudSpannerTransaction(nl.topicus.jdbc.transaction.CloudSpannerTransaction) Logger(nl.topicus.jdbc.Logger)

Aggregations

CloudSpannerDatabaseMetaData (nl.topicus.jdbc.CloudSpannerDatabaseMetaData)4 CloudSpannerConnection (nl.topicus.jdbc.CloudSpannerConnection)3 CloudSpannerResultSet (nl.topicus.jdbc.resultset.CloudSpannerResultSet)3 Returns (org.mockito.internal.stubbing.answers.Returns)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 ResultSet (java.sql.ResultSet)2 TableKeyMetaData (nl.topicus.jdbc.MetaDataStore.TableKeyMetaData)2 Logger (nl.topicus.jdbc.Logger)1 CloudSpannerStatement (nl.topicus.jdbc.statement.CloudSpannerStatement)1 CloudSpannerTransaction (nl.topicus.jdbc.transaction.CloudSpannerTransaction)1 Before (org.junit.Before)1