use of cn.taketoday.jdbc.support.SQLErrorCodeSQLExceptionTranslator in project today-framework by TAKETODAY.
the class JdbcTemplateTests method doTestCouldNotGetConnectionInOperationWithExceptionTranslatorInitialized.
/**
* If beanProperty is true, initialize via exception translator bean property;
* if false, use afterPropertiesSet().
*/
private void doTestCouldNotGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty) throws SQLException {
SQLException sqlException = new SQLException("foo", "07xxx");
this.dataSource = mock(DataSource.class);
given(this.dataSource.getConnection()).willThrow(sqlException);
this.template = new JdbcTemplate();
this.template.setDataSource(this.dataSource);
this.template.setLazyInit(false);
if (beanProperty) {
// This will get a connection.
this.template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(this.dataSource));
} else {
// This will cause creation of default SQL translator.
this.template.afterPropertiesSet();
}
RowCountCallbackHandler rcch = new RowCountCallbackHandler();
assertThatExceptionOfType(CannotGetJdbcConnectionException.class).isThrownBy(() -> this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch)).withCause(sqlException);
}
use of cn.taketoday.jdbc.support.SQLErrorCodeSQLExceptionTranslator in project today-infrastructure by TAKETODAY.
the class JdbcTemplateTests method doTestCouldNotGetConnectionInOperationWithExceptionTranslatorInitialized.
/**
* If beanProperty is true, initialize via exception translator bean property;
* if false, use afterPropertiesSet().
*/
private void doTestCouldNotGetConnectionInOperationWithExceptionTranslatorInitialized(boolean beanProperty) throws SQLException {
SQLException sqlException = new SQLException("foo", "07xxx");
this.dataSource = mock(DataSource.class);
given(this.dataSource.getConnection()).willThrow(sqlException);
this.template = new JdbcTemplate();
this.template.setDataSource(this.dataSource);
this.template.setLazyInit(false);
if (beanProperty) {
// This will get a connection.
this.template.setExceptionTranslator(new SQLErrorCodeSQLExceptionTranslator(this.dataSource));
} else {
// This will cause creation of default SQL translator.
this.template.afterPropertiesSet();
}
RowCountCallbackHandler rcch = new RowCountCallbackHandler();
assertThatExceptionOfType(CannotGetJdbcConnectionException.class).isThrownBy(() -> this.template.query("SELECT ID, FORENAME FROM CUSTMR WHERE ID < 3", rcch)).withCause(sqlException);
}
Aggregations