Example usage for org.springframework.jdbc.core RowCountCallbackHandler RowCountCallbackHandler

List of usage examples for org.springframework.jdbc.core RowCountCallbackHandler RowCountCallbackHandler

Introduction

In this page you can find the example usage for org.springframework.jdbc.core RowCountCallbackHandler RowCountCallbackHandler.

Prototype

RowCountCallbackHandler

Source Link

Usage

From source file:org.springframework.jdbc.core.JdbcTemplateTests.java

/**
 * Verify that afterPropertiesSet invokes exception translator.
 *///from ww w  . j a  v a 2  s  . c o m
public void testCouldntGetConnectionInOperationWithExceptionTranslatorInitialized() throws SQLException {
    SQLException sex = new SQLException("foo", "07xxx");

    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    //mockConnection.getMetaData();
    //ctrlConnection.setReturnValue(null, 1);
    //mockConnection.close();
    //ctrlConnection.setVoidCallable(1);
    ctrlConnection.replay();

    // Change behaviour in setUp() because we only expect one call to getConnection():
    // none is necessary to get metadata for exception translator
    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    // Upfront call for metadata - no longer the case
    //mockDataSource.getConnection();
    //ctrlDataSource.setReturnValue(mockConnection, 1);
    // One call for operation
    mockDataSource.getConnection();
    ctrlDataSource.setThrowable(sex, 2);
    ctrlDataSource.replay();

    try {
        JdbcTemplate template = new JdbcTemplate(mockDataSource, false);
        RowCountCallbackHandler rcch = new RowCountCallbackHandler();
        template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
        fail("Shouldn't have executed query without a connection");
    } catch (CannotGetJdbcConnectionException ex) {
        // pass
        assertTrue("Check root cause", ex.getCause() == sex);
    }

    ctrlDataSource.verify();
    ctrlConnection.verify();
}

From source file:org.springframework.jdbc.core.JdbcTemplateTests.java

/**
 * If beanProperty is true, initialize via exception translator bean property;
 * if false, use afterPropertiesSet()./*from w  w w  .j  a va  2  s  .c  o m*/
 */
private void doTestCouldntGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty)
        throws SQLException {

    SQLException sex = new SQLException("foo", "07xxx");

    ctrlConnection = MockControl.createControl(Connection.class);
    mockConnection = (Connection) ctrlConnection.getMock();
    //mockConnection.getMetaData();
    //ctrlConnection.setReturnValue(null, 1);
    //mockConnection.close();
    //ctrlConnection.setVoidCallable(1);
    ctrlConnection.replay();

    // Change behaviour in setUp() because we only expect one call to getConnection():
    // none is necessary to get metadata for exception translator
    ctrlDataSource = MockControl.createControl(DataSource.class);
    mockDataSource = (DataSource) ctrlDataSource.getMock();
    // Upfront call for metadata - no longer the case
    //mockDataSource.getConnection();
    //ctrlDataSource.setReturnValue(mockConnection, 1);
    // One call for operation
    mockDataSource.getConnection();
    ctrlDataSource.setThrowable(sex, 2);
    ctrlDataSource.replay();

    try {
        JdbcTemplate template2 = new JdbcTemplate();
        template2.setDataSource(mockDataSource);
        template2.setLazyInit(false);
        if (beanProperty) {
            // This will get a connection.
            template2.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(mockDataSource));
        } else {
            // This will cause creation of default SQL translator.
            // Note that only call should be effective.
            template2.afterPropertiesSet();
            template2.afterPropertiesSet();
        }
        RowCountCallbackHandler rcch = new RowCountCallbackHandler();
        template2.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);
        fail("Shouldn't have executed query without a connection");
    } catch (CannotGetJdbcConnectionException ex) {
        // pass
        assertTrue("Check root cause", ex.getCause() == sex);
    }

    ctrlDataSource.verify();
    ctrlConnection.verify();
}

From source file:org.springframework.jdbc.core.JdbcTemplateTests.java

public void testCouldntClose() throws Exception {
    MockControl ctrlStatement = MockControl.createControl(Statement.class);
    Statement mockStatement = (Statement) ctrlStatement.getMock();
    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockConnection.createStatement();/*from  www. ja  v  a2 s. c o m*/
    ctrlConnection.setReturnValue(mockStatement);
    String sql = "SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3";
    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    SQLException sex = new SQLException("bar");
    mockResultSet.close();
    ctrlResultSet.setThrowable(sex);
    if (debugEnabled) {
        mockStatement.getWarnings();
        ctrlStatement.setReturnValue(null);
    }
    mockStatement.close();
    ctrlStatement.setThrowable(sex);
    mockConnection.close();
    ctrlConnection.setThrowable(sex);

    ctrlStatement.replay();
    ctrlResultSet.replay();
    replay();

    JdbcTemplate template2 = new JdbcTemplate(mockDataSource);
    RowCountCallbackHandler rcch = new RowCountCallbackHandler();
    template2.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch);

    ctrlStatement.verify();
    ctrlResultSet.verify();
}