Search in sources :

Example 1 with DataAccessException

use of cn.taketoday.dao.DataAccessException in project today-infrastructure by TAKETODAY.

the class EntityManagerFactoryUtilsTests method testTranslatesIllegalArgumentException.

@Test
public void testTranslatesIllegalArgumentException() {
    IllegalArgumentException iae = new IllegalArgumentException();
    DataAccessException dex = EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(iae);
    assertThat(dex.getCause()).isSameAs(iae);
    boolean condition = dex instanceof InvalidDataAccessApiUsageException;
    assertThat(condition).isTrue();
}
Also used : InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) EmptyResultDataAccessException(cn.taketoday.dao.EmptyResultDataAccessException) IncorrectResultSizeDataAccessException(cn.taketoday.dao.IncorrectResultSizeDataAccessException) DataAccessException(cn.taketoday.dao.DataAccessException) Test(org.junit.jupiter.api.Test)

Example 2 with DataAccessException

use of cn.taketoday.dao.DataAccessException in project today-infrastructure by TAKETODAY.

the class LocalContainerEntityManagerFactoryBeanTests method testExceptionTranslationWithNoDialect.

@Test
public void testExceptionTranslationWithNoDialect() throws Exception {
    LocalContainerEntityManagerFactoryBean cefb = parseValidPersistenceUnit();
    cefb.getObject();
    assertThat(cefb.getJpaDialect()).as("No dialect set").isNull();
    RuntimeException in1 = new RuntimeException("in1");
    PersistenceException in2 = new PersistenceException();
    assertThat(cefb.translateExceptionIfPossible(in1)).as("No translation here").isNull();
    DataAccessException dex = cefb.translateExceptionIfPossible(in2);
    assertThat(dex).isNotNull();
    assertThat(dex.getCause()).isSameAs(in2);
}
Also used : PersistenceException(jakarta.persistence.PersistenceException) DataAccessException(cn.taketoday.dao.DataAccessException) Test(org.junit.jupiter.api.Test)

Example 3 with DataAccessException

use of cn.taketoday.dao.DataAccessException in project today-infrastructure by TAKETODAY.

the class ContainerManagedEntityManagerIntegrationTests method doTestExceptionTranslationWithDialectFound.

protected void doTestExceptionTranslationWithDialectFound(PersistenceExceptionTranslator pet) throws Exception {
    RuntimeException in1 = new RuntimeException("in1");
    PersistenceException in2 = new PersistenceException();
    assertThat(pet.translateExceptionIfPossible(in1)).as("No translation here").isNull();
    DataAccessException dex = pet.translateExceptionIfPossible(in2);
    assertThat(dex).isNotNull();
    assertThat(dex.getCause()).isSameAs(in2);
}
Also used : PersistenceException(jakarta.persistence.PersistenceException) DataAccessException(cn.taketoday.dao.DataAccessException)

Example 4 with DataAccessException

use of cn.taketoday.dao.DataAccessException in project today-infrastructure by TAKETODAY.

the class CustomSQLExceptionTranslatorRegistrarTests method customErrorCodeTranslation.

@Test
@SuppressWarnings("resource")
public void customErrorCodeTranslation() {
    new ClassPathXmlApplicationContext("test-custom-translators-context.xml", CustomSQLExceptionTranslatorRegistrarTests.class);
    SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2");
    SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
    sext.setSqlErrorCodes(codes);
    DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000));
    assertThat(exFor4200).as("Should have been translated").isNotNull();
    assertThat(BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass())).as("Should have been instance of BadSqlGrammarException").isTrue();
    DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2));
    assertThat(exFor2).as("Should have been translated").isNotNull();
    assertThat(TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass())).as("Should have been instance of TransientDataAccessResourceException").isTrue();
    DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3));
    assertThat(exFor3).as("Should not have been translated").isNull();
}
Also used : BadSqlGrammarException(cn.taketoday.jdbc.BadSqlGrammarException) TransientDataAccessResourceException(cn.taketoday.dao.TransientDataAccessResourceException) ClassPathXmlApplicationContext(cn.taketoday.context.support.ClassPathXmlApplicationContext) SQLException(java.sql.SQLException) DataAccessException(cn.taketoday.dao.DataAccessException) Test(org.junit.jupiter.api.Test)

Example 5 with DataAccessException

use of cn.taketoday.dao.DataAccessException in project today-infrastructure by TAKETODAY.

the class SQLErrorCodeSQLExceptionTranslatorTests method customTranslateMethodTranslation.

@SuppressWarnings("serial")
@Test
public void customTranslateMethodTranslation() {
    final String TASK = "TASK";
    final String SQL = "SQL SELECT *";
    final DataAccessException customDex = new DataAccessException("") {
    };
    final SQLException badSqlEx = new SQLException("", "", 1);
    SQLException intVioEx = new SQLException("", "", 6);
    SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator() {

        @Override
        @Nullable
        protected DataAccessException customTranslate(String task, @Nullable String sql, SQLException sqlex) {
            assertThat(task).isEqualTo(TASK);
            assertThat(sql).isEqualTo(SQL);
            return (sqlex == badSqlEx) ? customDex : null;
        }
    };
    sext.setSqlErrorCodes(ERROR_CODES);
    // Shouldn't custom translate this
    assertThat(sext.translate(TASK, SQL, badSqlEx)).isEqualTo(customDex);
    DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, intVioEx);
    assertThat(diex.getCause()).isEqualTo(intVioEx);
}
Also used : SQLException(java.sql.SQLException) DeadlockLoserDataAccessException(cn.taketoday.dao.DeadlockLoserDataAccessException) DataAccessException(cn.taketoday.dao.DataAccessException) Nullable(cn.taketoday.lang.Nullable) DataIntegrityViolationException(cn.taketoday.dao.DataIntegrityViolationException) Test(org.junit.jupiter.api.Test)

Aggregations

DataAccessException (cn.taketoday.dao.DataAccessException)26 Test (org.junit.jupiter.api.Test)16 SQLException (java.sql.SQLException)14 DeadlockLoserDataAccessException (cn.taketoday.dao.DeadlockLoserDataAccessException)6 EmptyResultDataAccessException (cn.taketoday.dao.EmptyResultDataAccessException)6 IncorrectResultSizeDataAccessException (cn.taketoday.dao.IncorrectResultSizeDataAccessException)6 DataIntegrityViolationException (cn.taketoday.dao.DataIntegrityViolationException)4 InvalidDataAccessApiUsageException (cn.taketoday.dao.InvalidDataAccessApiUsageException)4 BadSqlGrammarException (cn.taketoday.jdbc.BadSqlGrammarException)4 Nullable (cn.taketoday.lang.Nullable)4 PersistenceException (jakarta.persistence.PersistenceException)4 ClassPathXmlApplicationContext (cn.taketoday.context.support.ClassPathXmlApplicationContext)2 TransientDataAccessResourceException (cn.taketoday.dao.TransientDataAccessResourceException)2 TypeMismatchDataAccessException (cn.taketoday.dao.TypeMismatchDataAccessException)2 InvalidResultSetAccessException (cn.taketoday.jdbc.InvalidResultSetAccessException)2 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)2