use of cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator in project today-infrastructure by TAKETODAY.
the class JdbcTemplateTests method testUseCustomSQLErrorCodeTranslator.
/**
* Test that we see an SQLException translated using Error Code.
* If we provide the SQLExceptionTranslator, we shouldn't use a connection
* to get the metadata
*/
@Test
public void testUseCustomSQLErrorCodeTranslator() throws Exception {
// Bad SQL state
final SQLException sqlException = new SQLException("I have a known problem", "07000", 1054);
final String sql = "SELECT ID FROM CUSTOMER";
given(this.resultSet.next()).willReturn(true);
given(this.connection.createStatement()).willReturn(this.preparedStatement);
JdbcTemplate template = new JdbcTemplate();
template.setDataSource(this.dataSource);
// Set custom exception translator
template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
template.afterPropertiesSet();
assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(() -> template.query(sql, (RowCallbackHandler) rs -> {
throw sqlException;
})).withCause(sqlException);
verify(this.resultSet).close();
verify(this.preparedStatement).close();
verify(this.connection).close();
}
use of cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator in project today-infrastructure by TAKETODAY.
the class RowMapperTests method setUp.
@BeforeEach
public void setUp() throws SQLException {
given(connection.createStatement()).willReturn(statement);
given(connection.prepareStatement(anyString())).willReturn(preparedStatement);
given(statement.executeQuery(anyString())).willReturn(resultSet);
given(preparedStatement.executeQuery()).willReturn(resultSet);
given(resultSet.next()).willReturn(true, true, false);
given(resultSet.getString(1)).willReturn("tb1", "tb2");
given(resultSet.getInt(2)).willReturn(1, 2);
template.setDataSource(new SingleConnectionDataSource(connection, false));
template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
template.afterPropertiesSet();
}
use of cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator in project today-infrastructure by TAKETODAY.
the class StoredProcedureTests method testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator.
/**
* Confirm no connection was used to get metadata. Does not use superclass replay
* mechanism.
*/
@Test
public void testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator() throws Exception {
given(callableStatement.execute()).willReturn(false);
given(callableStatement.getUpdateCount()).willReturn(-1);
given(callableStatement.getObject(2)).willReturn(5);
given(connection.prepareCall("{call " + StoredProcedureConfiguredViaJdbcTemplate.SQL + "(?, ?)}")).willReturn(callableStatement);
class TestJdbcTemplate extends JdbcTemplate {
int calls;
@Override
public Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters) throws DataAccessException {
calls++;
return super.call(csc, declaredParameters);
}
}
TestJdbcTemplate t = new TestJdbcTemplate();
t.setDataSource(dataSource);
// Will fail without the following, because we're not able to get a connection
// from the DataSource here if we need to create an ExceptionTranslator
t.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t);
assertThat(sp.execute(11)).isEqualTo(5);
assertThat(t.calls).isEqualTo(1);
verify(callableStatement).setObject(1, 11, Types.INTEGER);
verify(callableStatement).registerOutParameter(2, Types.INTEGER);
}
use of cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator in project today-framework by TAKETODAY.
the class JdbcTemplateTests method testUseCustomSQLErrorCodeTranslator.
/**
* Test that we see an SQLException translated using Error Code.
* If we provide the SQLExceptionTranslator, we shouldn't use a connection
* to get the metadata
*/
@Test
public void testUseCustomSQLErrorCodeTranslator() throws Exception {
// Bad SQL state
final SQLException sqlException = new SQLException("I have a known problem", "07000", 1054);
final String sql = "SELECT ID FROM CUSTOMER";
given(this.resultSet.next()).willReturn(true);
given(this.connection.createStatement()).willReturn(this.preparedStatement);
JdbcTemplate template = new JdbcTemplate();
template.setDataSource(this.dataSource);
// Set custom exception translator
template.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
template.afterPropertiesSet();
assertThatExceptionOfType(BadSqlGrammarException.class).isThrownBy(() -> template.query(sql, (RowCallbackHandler) rs -> {
throw sqlException;
})).withCause(sqlException);
verify(this.resultSet).close();
verify(this.preparedStatement).close();
verify(this.connection).close();
}
use of cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator in project today-framework by TAKETODAY.
the class StoredProcedureTests method testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator.
/**
* Confirm no connection was used to get metadata. Does not use superclass replay
* mechanism.
*/
@Test
public void testStoredProcedureConfiguredViaJdbcTemplateWithCustomExceptionTranslator() throws Exception {
given(callableStatement.execute()).willReturn(false);
given(callableStatement.getUpdateCount()).willReturn(-1);
given(callableStatement.getObject(2)).willReturn(5);
given(connection.prepareCall("{call " + StoredProcedureConfiguredViaJdbcTemplate.SQL + "(?, ?)}")).willReturn(callableStatement);
class TestJdbcTemplate extends JdbcTemplate {
int calls;
@Override
public Map<String, Object> call(CallableStatementCreator csc, List<SqlParameter> declaredParameters) throws DataAccessException {
calls++;
return super.call(csc, declaredParameters);
}
}
TestJdbcTemplate t = new TestJdbcTemplate();
t.setDataSource(dataSource);
// Will fail without the following, because we're not able to get a connection
// from the DataSource here if we need to create an ExceptionTranslator
t.setExceptionTranslator(new SQLStateSQLExceptionTranslator());
StoredProcedureConfiguredViaJdbcTemplate sp = new StoredProcedureConfiguredViaJdbcTemplate(t);
assertThat(sp.execute(11)).isEqualTo(5);
assertThat(t.calls).isEqualTo(1);
verify(callableStatement).setObject(1, 11, Types.INTEGER);
verify(callableStatement).registerOutParameter(2, Types.INTEGER);
}
Aggregations