|
43 | 43 | import org.springframework.dao.TransientDataAccessResourceException; |
44 | 44 | import org.springframework.jdbc.BadSqlGrammarException; |
45 | 45 |
|
46 | | -import static org.assertj.core.api.Assertions.assertThat; |
| 46 | +import static org.springframework.jdbc.support.SQLStateSQLExceptionTranslatorTests.buildBatchUpdateException; |
47 | 47 |
|
48 | 48 | /** |
49 | 49 | * @author Thomas Risberg |
50 | 50 | * @author Juergen Hoeller |
51 | 51 | */ |
52 | 52 | class SQLExceptionSubclassTranslatorTests { |
53 | 53 |
|
| 54 | + private final SQLExceptionTranslator translator = new SQLExceptionSubclassTranslator(); |
| 55 | + |
| 56 | + |
54 | 57 | @Test |
55 | 58 | void exceptionClassTranslation() { |
56 | | - doTest(new SQLDataException("", "", 0), DataIntegrityViolationException.class); |
57 | | - doTest(new SQLFeatureNotSupportedException("", "", 0), InvalidDataAccessApiUsageException.class); |
58 | | - doTest(new SQLIntegrityConstraintViolationException("", "", 0), DataIntegrityViolationException.class); |
59 | | - doTest(new SQLIntegrityConstraintViolationException("", "23505", 0), DuplicateKeyException.class); |
60 | | - doTest(new SQLIntegrityConstraintViolationException("", "23000", 1), DuplicateKeyException.class); |
61 | | - doTest(new SQLIntegrityConstraintViolationException("", "23000", 1062), DuplicateKeyException.class); |
62 | | - doTest(new SQLIntegrityConstraintViolationException("", "23000", 2601), DuplicateKeyException.class); |
63 | | - doTest(new SQLIntegrityConstraintViolationException("", "23000", 2627), DuplicateKeyException.class); |
64 | | - doTest(new SQLInvalidAuthorizationSpecException("", "", 0), PermissionDeniedDataAccessException.class); |
65 | | - doTest(new SQLNonTransientConnectionException("", "", 0), DataAccessResourceFailureException.class); |
66 | | - doTest(new SQLRecoverableException("", "", 0), RecoverableDataAccessException.class); |
67 | | - doTest(new SQLSyntaxErrorException("", "", 0), BadSqlGrammarException.class); |
68 | | - doTest(new SQLTimeoutException("", "", 0), QueryTimeoutException.class); |
69 | | - doTest(new SQLTransactionRollbackException("", "", 0), PessimisticLockingFailureException.class); |
70 | | - doTest(new SQLTransactionRollbackException("", "40001", 0), CannotAcquireLockException.class); |
71 | | - doTest(new SQLTransientConnectionException("", "", 0), TransientDataAccessResourceException.class); |
| 59 | + assertTranslation(new SQLDataException("", "", 0), DataIntegrityViolationException.class); |
| 60 | + assertTranslation(new SQLFeatureNotSupportedException("", "", 0), InvalidDataAccessApiUsageException.class); |
| 61 | + assertTranslation(new SQLIntegrityConstraintViolationException("", "", 0), DataIntegrityViolationException.class); |
| 62 | + assertTranslation(new SQLIntegrityConstraintViolationException("", "23505", 0), DuplicateKeyException.class); |
| 63 | + assertTranslation(new SQLIntegrityConstraintViolationException("", "23000", 1), DuplicateKeyException.class); |
| 64 | + assertTranslation(new SQLIntegrityConstraintViolationException("", "23000", 1062), DuplicateKeyException.class); |
| 65 | + assertTranslation(new SQLIntegrityConstraintViolationException("", "23000", 2601), DuplicateKeyException.class); |
| 66 | + assertTranslation(new SQLIntegrityConstraintViolationException("", "23000", 2627), DuplicateKeyException.class); |
| 67 | + assertTranslation(new SQLInvalidAuthorizationSpecException("", "", 0), PermissionDeniedDataAccessException.class); |
| 68 | + assertTranslation(new SQLNonTransientConnectionException("", "", 0), DataAccessResourceFailureException.class); |
| 69 | + assertTranslation(new SQLRecoverableException("", "", 0), RecoverableDataAccessException.class); |
| 70 | + assertTranslation(new SQLSyntaxErrorException("", "", 0), BadSqlGrammarException.class); |
| 71 | + assertTranslation(new SQLTimeoutException("", "", 0), QueryTimeoutException.class); |
| 72 | + assertTranslation(new SQLTransactionRollbackException("", "", 0), PessimisticLockingFailureException.class); |
| 73 | + assertTranslation(new SQLTransactionRollbackException("", "40001", 0), CannotAcquireLockException.class); |
| 74 | + assertTranslation(new SQLTransientConnectionException("", "", 0), TransientDataAccessResourceException.class); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void batchExceptionTranslation() { |
| 79 | + assertTranslation(buildBatchUpdateException("JZ", new SQLIntegrityConstraintViolationException("", "23505", 0)), |
| 80 | + DuplicateKeyException.class); |
| 81 | + assertTranslation(buildBatchUpdateException(null, new SQLIntegrityConstraintViolationException("", "23505", 0)), |
| 82 | + DuplicateKeyException.class); |
72 | 83 | } |
73 | 84 |
|
74 | 85 | @Test |
75 | 86 | void fallbackStateTranslation() { |
76 | 87 | // Test fallback. We assume that no database will ever return this error code, |
77 | 88 | // but 07xxx will be bad grammar picked up by the fallback SQLState translator |
78 | | - doTest(new SQLException("", "07xxx", 666666666), BadSqlGrammarException.class); |
| 89 | + assertTranslation(new SQLException("", "07xxx", 666666666), BadSqlGrammarException.class); |
79 | 90 | // and 08xxx will be data resource failure (non-transient) picked up by the fallback SQLState translator |
80 | | - doTest(new SQLException("", "08xxx", 666666666), DataAccessResourceFailureException.class); |
| 91 | + assertTranslation(new SQLException("", "08xxx", 666666666), DataAccessResourceFailureException.class); |
81 | 92 | } |
82 | 93 |
|
83 | 94 |
|
84 | | - private void doTest(SQLException ex, Class<?> dataAccessExceptionType) { |
85 | | - SQLExceptionTranslator translator = new SQLExceptionSubclassTranslator(); |
86 | | - DataAccessException dax = translator.translate("task", "SQL", ex); |
87 | | - |
88 | | - assertThat(dax).as("Specific translation must not result in null").isNotNull(); |
89 | | - assertThat(dax).as("Wrong DataAccessException type returned").isExactlyInstanceOf(dataAccessExceptionType); |
90 | | - assertThat(dax.getCause()).as("The exact same original SQLException must be preserved").isSameAs(ex); |
| 95 | + private void assertTranslation(SQLException ex, Class<?> dataAccessExceptionType) { |
| 96 | + DataAccessException dae = translator.translate("task", "SQL", ex); |
| 97 | + SQLStateSQLExceptionTranslatorTests.assertTranslation(dae, ex, dataAccessExceptionType); |
91 | 98 | } |
92 | 99 |
|
93 | 100 | } |
0 commit comments