Search in sources :

Example 1 with SQLStateSQLExceptionTranslator

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();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StringUtils(cn.taketoday.util.StringUtils) Connection(java.sql.Connection) BatchUpdateException(java.sql.BatchUpdateException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LinkedCaseInsensitiveMap(cn.taketoday.util.LinkedCaseInsensitiveMap) DatabaseMetaData(java.sql.DatabaseMetaData) DataAccessException(cn.taketoday.dao.DataAccessException) ArrayList(java.util.ArrayList) AbstractInterruptibleBatchPreparedStatementSetter(cn.taketoday.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter) ConnectionProxy(cn.taketoday.jdbc.datasource.ConnectionProxy) SQLException(java.sql.SQLException) BadSqlGrammarException(cn.taketoday.jdbc.BadSqlGrammarException) BDDMockito.given(org.mockito.BDDMockito.given) ResultSet(java.sql.ResultSet) Map(java.util.Map) DataSource(javax.sql.DataSource) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) SQLWarning(java.sql.SQLWarning) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) CannotGetJdbcConnectionException(cn.taketoday.jdbc.CannotGetJdbcConnectionException) SQLStateSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Mockito.times(org.mockito.Mockito.times) PreparedStatement(java.sql.PreparedStatement) SQLWarningException(cn.taketoday.jdbc.SQLWarningException) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) Mockito.never(org.mockito.Mockito.never) SingleConnectionDataSource(cn.taketoday.jdbc.datasource.SingleConnectionDataSource) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Collections(java.util.Collections) SQLErrorCodeSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLErrorCodeSQLExceptionTranslator) Mockito.reset(org.mockito.Mockito.reset) ResultSetMetaData(java.sql.ResultSetMetaData) Types(java.sql.Types) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SQLStateSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator) Test(org.junit.jupiter.api.Test)

Example 2 with SQLStateSQLExceptionTranslator

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();
}
Also used : SingleConnectionDataSource(cn.taketoday.jdbc.datasource.SingleConnectionDataSource) SQLStateSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with SQLStateSQLExceptionTranslator

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);
}
Also used : CallableStatementCreator(cn.taketoday.jdbc.core.CallableStatementCreator) List(java.util.List) SQLStateSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator) JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) Test(org.junit.jupiter.api.Test)

Example 4 with SQLStateSQLExceptionTranslator

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();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StringUtils(cn.taketoday.util.StringUtils) Connection(java.sql.Connection) BatchUpdateException(java.sql.BatchUpdateException) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) LinkedCaseInsensitiveMap(cn.taketoday.util.LinkedCaseInsensitiveMap) DatabaseMetaData(java.sql.DatabaseMetaData) DataAccessException(cn.taketoday.dao.DataAccessException) ArrayList(java.util.ArrayList) AbstractInterruptibleBatchPreparedStatementSetter(cn.taketoday.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter) ConnectionProxy(cn.taketoday.jdbc.datasource.ConnectionProxy) SQLException(java.sql.SQLException) BadSqlGrammarException(cn.taketoday.jdbc.BadSqlGrammarException) BDDMockito.given(org.mockito.BDDMockito.given) ResultSet(java.sql.ResultSet) Map(java.util.Map) DataSource(javax.sql.DataSource) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) SQLWarning(java.sql.SQLWarning) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) CannotGetJdbcConnectionException(cn.taketoday.jdbc.CannotGetJdbcConnectionException) SQLStateSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Mockito.times(org.mockito.Mockito.times) PreparedStatement(java.sql.PreparedStatement) SQLWarningException(cn.taketoday.jdbc.SQLWarningException) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) Mockito.never(org.mockito.Mockito.never) SingleConnectionDataSource(cn.taketoday.jdbc.datasource.SingleConnectionDataSource) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) Collections(java.util.Collections) SQLErrorCodeSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLErrorCodeSQLExceptionTranslator) Mockito.reset(org.mockito.Mockito.reset) ResultSetMetaData(java.sql.ResultSetMetaData) Types(java.sql.Types) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) SQLException(java.sql.SQLException) UncategorizedSQLException(cn.taketoday.jdbc.UncategorizedSQLException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SQLStateSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator) Test(org.junit.jupiter.api.Test)

Example 5 with SQLStateSQLExceptionTranslator

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);
}
Also used : CallableStatementCreator(cn.taketoday.jdbc.core.CallableStatementCreator) List(java.util.List) SQLStateSQLExceptionTranslator(cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator) JdbcTemplate(cn.taketoday.jdbc.core.JdbcTemplate) Test(org.junit.jupiter.api.Test)

Aggregations

SQLStateSQLExceptionTranslator (cn.taketoday.jdbc.support.SQLStateSQLExceptionTranslator)6 SingleConnectionDataSource (cn.taketoday.jdbc.datasource.SingleConnectionDataSource)4 List (java.util.List)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 Test (org.junit.jupiter.api.Test)4 DataAccessException (cn.taketoday.dao.DataAccessException)2 InvalidDataAccessApiUsageException (cn.taketoday.dao.InvalidDataAccessApiUsageException)2 BadSqlGrammarException (cn.taketoday.jdbc.BadSqlGrammarException)2 CannotGetJdbcConnectionException (cn.taketoday.jdbc.CannotGetJdbcConnectionException)2 SQLWarningException (cn.taketoday.jdbc.SQLWarningException)2 UncategorizedSQLException (cn.taketoday.jdbc.UncategorizedSQLException)2 CallableStatementCreator (cn.taketoday.jdbc.core.CallableStatementCreator)2 JdbcTemplate (cn.taketoday.jdbc.core.JdbcTemplate)2 AbstractInterruptibleBatchPreparedStatementSetter (cn.taketoday.jdbc.core.support.AbstractInterruptibleBatchPreparedStatementSetter)2 ConnectionProxy (cn.taketoday.jdbc.datasource.ConnectionProxy)2 SQLErrorCodeSQLExceptionTranslator (cn.taketoday.jdbc.support.SQLErrorCodeSQLExceptionTranslator)2 LinkedCaseInsensitiveMap (cn.taketoday.util.LinkedCaseInsensitiveMap)2 StringUtils (cn.taketoday.util.StringUtils)2 BatchUpdateException (java.sql.BatchUpdateException)2 CallableStatement (java.sql.CallableStatement)2