Search in sources :

Example 6 with DataIntegrityViolationException

use of cn.taketoday.dao.DataIntegrityViolationException in project today-framework by TAKETODAY.

the class SQLExceptionSubclassTranslatorTests method errorCodeTranslation.

@Test
public void errorCodeTranslation() {
    SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
    SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 0);
    DataIntegrityViolationException divex = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx);
    assertThat(divex.getCause()).isEqualTo(dataIntegrityViolationEx);
    SQLException featureNotSupEx = SQLExceptionSubclassFactory.newSQLFeatureNotSupportedException("", "", 0);
    InvalidDataAccessApiUsageException idaex = (InvalidDataAccessApiUsageException) sext.translate("task", "SQL", featureNotSupEx);
    assertThat(idaex.getCause()).isEqualTo(featureNotSupEx);
    SQLException dataIntegrityViolationEx2 = SQLExceptionSubclassFactory.newSQLIntegrityConstraintViolationException("", "", 0);
    DataIntegrityViolationException divex2 = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx2);
    assertThat(divex2.getCause()).isEqualTo(dataIntegrityViolationEx2);
    SQLException permissionDeniedEx = SQLExceptionSubclassFactory.newSQLInvalidAuthorizationSpecException("", "", 0);
    PermissionDeniedDataAccessException pdaex = (PermissionDeniedDataAccessException) sext.translate("task", "SQL", permissionDeniedEx);
    assertThat(pdaex.getCause()).isEqualTo(permissionDeniedEx);
    SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLNonTransientConnectionException("", "", 0);
    DataAccessResourceFailureException darex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataAccessResourceEx);
    assertThat(darex.getCause()).isEqualTo(dataAccessResourceEx);
    SQLException badSqlEx2 = SQLExceptionSubclassFactory.newSQLSyntaxErrorException("", "", 0);
    BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", badSqlEx2);
    assertThat(bsgex2.getSql()).isEqualTo("SQL2");
    assertThat((Object) bsgex2.getSQLException()).isEqualTo(badSqlEx2);
    SQLException tranRollbackEx = SQLExceptionSubclassFactory.newSQLTransactionRollbackException("", "", 0);
    ConcurrencyFailureException cfex = (ConcurrencyFailureException) sext.translate("task", "SQL", tranRollbackEx);
    assertThat(cfex.getCause()).isEqualTo(tranRollbackEx);
    SQLException transientConnEx = SQLExceptionSubclassFactory.newSQLTransientConnectionException("", "", 0);
    TransientDataAccessResourceException tdarex = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx);
    assertThat(tdarex.getCause()).isEqualTo(transientConnEx);
    SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0);
    QueryTimeoutException tdarex2 = (QueryTimeoutException) sext.translate("task", "SQL", transientConnEx2);
    assertThat(tdarex2.getCause()).isEqualTo(transientConnEx2);
    SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0);
    RecoverableDataAccessException rdaex2 = (RecoverableDataAccessException) sext.translate("task", "SQL", recoverableEx);
    assertThat(rdaex2.getCause()).isEqualTo(recoverableEx);
    // Test classic error code translation. We should move there next if the exception we pass in is not one
    // of the new sub-classes.
    SQLException sexEct = new SQLException("", "", 1);
    BadSqlGrammarException bsgEct = (BadSqlGrammarException) sext.translate("task", "SQL-ECT", sexEct);
    assertThat(bsgEct.getSql()).isEqualTo("SQL-ECT");
    assertThat((Object) bsgEct.getSQLException()).isEqualTo(sexEct);
    // Test fallback. We assume that no database will ever return this error code,
    // but 07xxx will be bad grammar picked up by the fallback SQLState translator
    SQLException sexFbt = new SQLException("", "07xxx", 666666666);
    BadSqlGrammarException bsgFbt = (BadSqlGrammarException) sext.translate("task", "SQL-FBT", sexFbt);
    assertThat(bsgFbt.getSql()).isEqualTo("SQL-FBT");
    assertThat((Object) bsgFbt.getSQLException()).isEqualTo(sexFbt);
    // and 08xxx will be data resource failure (non-transient) picked up by the fallback SQLState translator
    SQLException sexFbt2 = new SQLException("", "08xxx", 666666666);
    DataAccessResourceFailureException darfFbt = (DataAccessResourceFailureException) sext.translate("task", "SQL-FBT2", sexFbt2);
    assertThat(darfFbt.getCause()).isEqualTo(sexFbt2);
}
Also used : BadSqlGrammarException(cn.taketoday.jdbc.BadSqlGrammarException) TransientDataAccessResourceException(cn.taketoday.dao.TransientDataAccessResourceException) SQLException(java.sql.SQLException) DataAccessResourceFailureException(cn.taketoday.dao.DataAccessResourceFailureException) PermissionDeniedDataAccessException(cn.taketoday.dao.PermissionDeniedDataAccessException) DataIntegrityViolationException(cn.taketoday.dao.DataIntegrityViolationException) QueryTimeoutException(cn.taketoday.dao.QueryTimeoutException) InvalidDataAccessApiUsageException(cn.taketoday.dao.InvalidDataAccessApiUsageException) ConcurrencyFailureException(cn.taketoday.dao.ConcurrencyFailureException) RecoverableDataAccessException(cn.taketoday.dao.RecoverableDataAccessException) Test(org.junit.jupiter.api.Test)

Example 7 with DataIntegrityViolationException

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

the class SQLErrorCodeSQLExceptionTranslatorTests method errorCodeTranslation.

@Test
public void errorCodeTranslation() {
    SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
    SQLException badSqlEx = new SQLException("", "", 1);
    BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", badSqlEx);
    assertThat(bsgex.getSql()).isEqualTo("SQL");
    assertThat((Object) bsgex.getSQLException()).isEqualTo(badSqlEx);
    SQLException invResEx = new SQLException("", "", 4);
    InvalidResultSetAccessException irsex = (InvalidResultSetAccessException) sext.translate("task", "SQL", invResEx);
    assertThat(irsex.getSql()).isEqualTo("SQL");
    assertThat((Object) irsex.getSQLException()).isEqualTo(invResEx);
    checkTranslation(sext, 5, DataAccessResourceFailureException.class);
    checkTranslation(sext, 6, DataIntegrityViolationException.class);
    checkTranslation(sext, 7, CannotAcquireLockException.class);
    checkTranslation(sext, 8, DeadlockLoserDataAccessException.class);
    checkTranslation(sext, 9, CannotSerializeTransactionException.class);
    checkTranslation(sext, 10, DuplicateKeyException.class);
    SQLException dupKeyEx = new SQLException("", "", 10);
    DataAccessException dksex = sext.translate("task", "SQL", dupKeyEx);
    assertThat(DataIntegrityViolationException.class.isInstance(dksex)).as("Not instance of DataIntegrityViolationException").isTrue();
    // Test fallback. We assume that no database will ever return this error code,
    // but 07xxx will be bad grammar picked up by the fallback SQLState translator
    SQLException sex = new SQLException("", "07xxx", 666666666);
    BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", sex);
    assertThat(bsgex2.getSql()).isEqualTo("SQL2");
    assertThat((Object) bsgex2.getSQLException()).isEqualTo(sex);
}
Also used : BadSqlGrammarException(cn.taketoday.jdbc.BadSqlGrammarException) SQLException(java.sql.SQLException) InvalidResultSetAccessException(cn.taketoday.jdbc.InvalidResultSetAccessException) DeadlockLoserDataAccessException(cn.taketoday.dao.DeadlockLoserDataAccessException) DataAccessException(cn.taketoday.dao.DataAccessException) DataIntegrityViolationException(cn.taketoday.dao.DataIntegrityViolationException) Test(org.junit.jupiter.api.Test)

Example 8 with DataIntegrityViolationException

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

the class SQLErrorCodeSQLExceptionTranslatorTests method customExceptionTranslation.

@Test
public void customExceptionTranslation() {
    final String TASK = "TASK";
    final String SQL = "SQL SELECT *";
    final SQLErrorCodes customErrorCodes = new SQLErrorCodes();
    final CustomSQLErrorCodesTranslation customTranslation = new CustomSQLErrorCodesTranslation();
    customErrorCodes.setBadSqlGrammarCodes("1", "2");
    customErrorCodes.setDataIntegrityViolationCodes("3", "4");
    customTranslation.setErrorCodes("1");
    customTranslation.setExceptionClass(CustomErrorCodeException.class);
    customErrorCodes.setCustomTranslations(customTranslation);
    SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(customErrorCodes);
    // Should custom translate this
    SQLException badSqlEx = new SQLException("", "", 1);
    assertThat(sext.translate(TASK, SQL, badSqlEx).getClass()).isEqualTo(CustomErrorCodeException.class);
    assertThat(sext.translate(TASK, SQL, badSqlEx).getCause()).isEqualTo(badSqlEx);
    // Shouldn't custom translate this
    SQLException invResEx = new SQLException("", "", 3);
    DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, invResEx);
    assertThat(diex.getCause()).isEqualTo(invResEx);
    // Shouldn't custom translate this - invalid class
    assertThatIllegalArgumentException().isThrownBy(() -> customTranslation.setExceptionClass(String.class));
}
Also used : SQLException(java.sql.SQLException) DataIntegrityViolationException(cn.taketoday.dao.DataIntegrityViolationException) Test(org.junit.jupiter.api.Test)

Aggregations

DataIntegrityViolationException (cn.taketoday.dao.DataIntegrityViolationException)8 SQLException (java.sql.SQLException)8 Test (org.junit.jupiter.api.Test)8 DataAccessException (cn.taketoday.dao.DataAccessException)4 DeadlockLoserDataAccessException (cn.taketoday.dao.DeadlockLoserDataAccessException)4 BadSqlGrammarException (cn.taketoday.jdbc.BadSqlGrammarException)4 ConcurrencyFailureException (cn.taketoday.dao.ConcurrencyFailureException)2 DataAccessResourceFailureException (cn.taketoday.dao.DataAccessResourceFailureException)2 InvalidDataAccessApiUsageException (cn.taketoday.dao.InvalidDataAccessApiUsageException)2 PermissionDeniedDataAccessException (cn.taketoday.dao.PermissionDeniedDataAccessException)2 QueryTimeoutException (cn.taketoday.dao.QueryTimeoutException)2 RecoverableDataAccessException (cn.taketoday.dao.RecoverableDataAccessException)2 TransientDataAccessResourceException (cn.taketoday.dao.TransientDataAccessResourceException)2 InvalidResultSetAccessException (cn.taketoday.jdbc.InvalidResultSetAccessException)2 Nullable (cn.taketoday.lang.Nullable)2