Example usage for org.springframework.jdbc SQLWarningException getCause

List of usage examples for org.springframework.jdbc SQLWarningException getCause

Introduction

In this page you can find the example usage for org.springframework.jdbc SQLWarningException getCause.

Prototype

public synchronized Throwable getCause() 

Source Link

Document

Returns the cause of this throwable or null if the cause is nonexistent or unknown.

Usage

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

/**
 * Mock objects allow us to produce warnings at will
 *//*from ww  w.  j  a va  2  s  . c o  m*/
public void testFatalWarning() throws Exception {
    String sql = "SELECT forename from custmr";
    SQLWarning warnings = new SQLWarning("My warning");

    MockControl ctrlResultSet = MockControl.createControl(ResultSet.class);
    ResultSet mockResultSet = (ResultSet) ctrlResultSet.getMock();
    mockResultSet.next();
    ctrlResultSet.setReturnValue(false);
    mockResultSet.close();
    ctrlResultSet.setVoidCallable();

    MockControl ctrlStatement = MockControl.createControl(PreparedStatement.class);
    PreparedStatement mockStatement = (PreparedStatement) ctrlStatement.getMock();
    mockStatement.executeQuery(sql);
    ctrlStatement.setReturnValue(mockResultSet);
    mockStatement.getWarnings();
    ctrlStatement.setReturnValue(warnings);
    mockStatement.close();
    ctrlStatement.setVoidCallable();

    mockConnection.createStatement();
    ctrlConnection.setReturnValue(mockStatement);

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

    JdbcTemplate t = new JdbcTemplate(mockDataSource);
    t.setIgnoreWarnings(false);
    try {
        t.query(sql, new RowCallbackHandler() {
            public void processRow(ResultSet rs) throws SQLException {
                rs.getByte(1);
            }
        });
        fail("Should have thrown exception on warning");
    } catch (SQLWarningException ex) {
        // Pass
        assertTrue("Root cause of warning was correct", ex.getCause() == warnings);
    }

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