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();
}
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);
}
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);
}
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();
}
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);
}
Aggregations