Example usage for org.springframework.dao UncategorizedDataAccessException getCause

List of usage examples for org.springframework.dao UncategorizedDataAccessException getCause

Introduction

In this page you can find the example usage for org.springframework.dao UncategorizedDataAccessException 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

public void testBogusUpdate() throws Exception {
    final String sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
    final int idParam = 6666;

    // It's because Integers aren't canonical
    SQLException sex = new SQLException("bad update");

    MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement.class);
    PreparedStatement mockPreparedStatement = (PreparedStatement) ctrlPreparedStatement.getMock();
    mockPreparedStatement.setInt(1, idParam);
    ctrlPreparedStatement.setVoidCallable();
    mockPreparedStatement.executeUpdate();
    ctrlPreparedStatement.setThrowable(sex);
    mockPreparedStatement.close();/*  w ww  .  j  ava2  s.c o m*/
    ctrlPreparedStatement.setVoidCallable();

    mockConnection.prepareStatement(sql);
    ctrlConnection.setReturnValue(mockPreparedStatement);

    ctrlPreparedStatement.replay();
    replay();

    Dispatcher d = new Dispatcher(idParam, sql);
    JdbcTemplate template = new JdbcTemplate(mockDataSource);

    try {
        template.update(d);
        fail("Bogus update should throw exception");
    } catch (UncategorizedDataAccessException ex) {
        // pass
        assertTrue("Correct exception", ex instanceof UncategorizedSQLException);
        assertTrue("Root cause is correct", ex.getCause() == sex);
        //assertTrue("no update occurred", !je.getDataWasUpdated());
    }

    ctrlPreparedStatement.verify();
}