|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.jdbc.support;
|
18 | 18 |
|
| 19 | +import java.sql.SQLDataException; |
19 | 20 | import java.sql.SQLException;
|
| 21 | +import java.sql.SQLFeatureNotSupportedException; |
| 22 | +import java.sql.SQLIntegrityConstraintViolationException; |
| 23 | +import java.sql.SQLInvalidAuthorizationSpecException; |
| 24 | +import java.sql.SQLNonTransientConnectionException; |
| 25 | +import java.sql.SQLRecoverableException; |
| 26 | +import java.sql.SQLSyntaxErrorException; |
| 27 | +import java.sql.SQLTimeoutException; |
| 28 | +import java.sql.SQLTransactionRollbackException; |
| 29 | +import java.sql.SQLTransientConnectionException; |
20 | 30 |
|
21 | 31 | import org.junit.jupiter.api.Test;
|
22 | 32 |
|
23 | 33 | import org.springframework.dao.ConcurrencyFailureException;
|
| 34 | +import org.springframework.dao.DataAccessException; |
24 | 35 | import org.springframework.dao.DataAccessResourceFailureException;
|
25 | 36 | import org.springframework.dao.DataIntegrityViolationException;
|
26 | 37 | import org.springframework.dao.InvalidDataAccessApiUsageException;
|
|
34 | 45 |
|
35 | 46 | /**
|
36 | 47 | * @author Thomas Risberg
|
| 48 | + * @author Juergen Hoeller |
37 | 49 | */
|
38 | 50 | public class SQLExceptionSubclassTranslatorTests {
|
39 | 51 |
|
40 |
| - private static SQLErrorCodes ERROR_CODES = new SQLErrorCodes(); |
41 |
| - |
42 |
| - static { |
43 |
| - ERROR_CODES.setBadSqlGrammarCodes("1"); |
| 52 | + @Test |
| 53 | + public void exceptionClassTranslation() { |
| 54 | + doTest(new SQLDataException("", "", 0), DataIntegrityViolationException.class); |
| 55 | + doTest(new SQLFeatureNotSupportedException("", "", 0), InvalidDataAccessApiUsageException.class); |
| 56 | + doTest(new SQLIntegrityConstraintViolationException("", "", 0), DataIntegrityViolationException.class); |
| 57 | + doTest(new SQLInvalidAuthorizationSpecException("", "", 0), PermissionDeniedDataAccessException.class); |
| 58 | + doTest(new SQLNonTransientConnectionException("", "", 0), DataAccessResourceFailureException.class); |
| 59 | + doTest(new SQLRecoverableException("", "", 0), RecoverableDataAccessException.class); |
| 60 | + doTest(new SQLSyntaxErrorException("", "", 0), BadSqlGrammarException.class); |
| 61 | + doTest(new SQLTimeoutException("", "", 0), QueryTimeoutException.class); |
| 62 | + doTest(new SQLTransactionRollbackException("", "", 0), ConcurrencyFailureException.class); |
| 63 | + doTest(new SQLTransientConnectionException("", "", 0), TransientDataAccessResourceException.class); |
44 | 64 | }
|
45 | 65 |
|
46 |
| - |
47 | 66 | @Test
|
48 |
| - public void errorCodeTranslation() { |
49 |
| - SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES); |
50 |
| - |
51 |
| - SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 0); |
52 |
| - DataIntegrityViolationException divex = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx); |
53 |
| - assertThat(divex.getCause()).isEqualTo(dataIntegrityViolationEx); |
54 |
| - |
55 |
| - SQLException featureNotSupEx = SQLExceptionSubclassFactory.newSQLFeatureNotSupportedException("", "", 0); |
56 |
| - InvalidDataAccessApiUsageException idaex = (InvalidDataAccessApiUsageException) sext.translate("task", "SQL", featureNotSupEx); |
57 |
| - assertThat(idaex.getCause()).isEqualTo(featureNotSupEx); |
58 |
| - |
59 |
| - SQLException dataIntegrityViolationEx2 = SQLExceptionSubclassFactory.newSQLIntegrityConstraintViolationException("", "", 0); |
60 |
| - DataIntegrityViolationException divex2 = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx2); |
61 |
| - assertThat(divex2.getCause()).isEqualTo(dataIntegrityViolationEx2); |
62 |
| - |
63 |
| - SQLException permissionDeniedEx = SQLExceptionSubclassFactory.newSQLInvalidAuthorizationSpecException("", "", 0); |
64 |
| - PermissionDeniedDataAccessException pdaex = (PermissionDeniedDataAccessException) sext.translate("task", "SQL", permissionDeniedEx); |
65 |
| - assertThat(pdaex.getCause()).isEqualTo(permissionDeniedEx); |
66 |
| - |
67 |
| - SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLNonTransientConnectionException("", "", 0); |
68 |
| - DataAccessResourceFailureException darex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataAccessResourceEx); |
69 |
| - assertThat(darex.getCause()).isEqualTo(dataAccessResourceEx); |
70 |
| - |
71 |
| - SQLException badSqlEx2 = SQLExceptionSubclassFactory.newSQLSyntaxErrorException("", "", 0); |
72 |
| - BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", badSqlEx2); |
73 |
| - assertThat(bsgex2.getSql()).isEqualTo("SQL2"); |
74 |
| - assertThat((Object) bsgex2.getSQLException()).isEqualTo(badSqlEx2); |
75 |
| - |
76 |
| - SQLException tranRollbackEx = SQLExceptionSubclassFactory.newSQLTransactionRollbackException("", "", 0); |
77 |
| - ConcurrencyFailureException cfex = (ConcurrencyFailureException) sext.translate("task", "SQL", tranRollbackEx); |
78 |
| - assertThat(cfex.getCause()).isEqualTo(tranRollbackEx); |
79 |
| - |
80 |
| - SQLException transientConnEx = SQLExceptionSubclassFactory.newSQLTransientConnectionException("", "", 0); |
81 |
| - TransientDataAccessResourceException tdarex = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx); |
82 |
| - assertThat(tdarex.getCause()).isEqualTo(transientConnEx); |
83 |
| - |
84 |
| - SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0); |
85 |
| - QueryTimeoutException tdarex2 = (QueryTimeoutException) sext.translate("task", "SQL", transientConnEx2); |
86 |
| - assertThat(tdarex2.getCause()).isEqualTo(transientConnEx2); |
87 |
| - |
88 |
| - SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0); |
89 |
| - RecoverableDataAccessException rdaex2 = (RecoverableDataAccessException) sext.translate("task", "SQL", recoverableEx); |
90 |
| - assertThat(rdaex2.getCause()).isEqualTo(recoverableEx); |
91 |
| - |
92 |
| - // Test classic error code translation. We should move there next if the exception we pass in is not one |
93 |
| - // of the new subclasses. |
94 |
| - SQLException sexEct = new SQLException("", "", 1); |
95 |
| - BadSqlGrammarException bsgEct = (BadSqlGrammarException) sext.translate("task", "SQL-ECT", sexEct); |
96 |
| - assertThat(bsgEct.getSql()).isEqualTo("SQL-ECT"); |
97 |
| - assertThat((Object) bsgEct.getSQLException()).isEqualTo(sexEct); |
98 |
| - |
| 67 | + public void fallbackStateTranslation() { |
99 | 68 | // Test fallback. We assume that no database will ever return this error code,
|
100 | 69 | // but 07xxx will be bad grammar picked up by the fallback SQLState translator
|
101 |
| - SQLException sexFbt = new SQLException("", "07xxx", 666666666); |
102 |
| - BadSqlGrammarException bsgFbt = (BadSqlGrammarException) sext.translate("task", "SQL-FBT", sexFbt); |
103 |
| - assertThat(bsgFbt.getSql()).isEqualTo("SQL-FBT"); |
104 |
| - assertThat((Object) bsgFbt.getSQLException()).isEqualTo(sexFbt); |
| 70 | + doTest(new SQLException("", "07xxx", 666666666), BadSqlGrammarException.class); |
105 | 71 | // and 08xxx will be data resource failure (non-transient) picked up by the fallback SQLState translator
|
106 |
| - SQLException sexFbt2 = new SQLException("", "08xxx", 666666666); |
107 |
| - DataAccessResourceFailureException darfFbt = (DataAccessResourceFailureException) sext.translate("task", "SQL-FBT2", sexFbt2); |
108 |
| - assertThat(darfFbt.getCause()).isEqualTo(sexFbt2); |
| 72 | + doTest(new SQLException("", "08xxx", 666666666), DataAccessResourceFailureException.class); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + private void doTest(SQLException ex, Class<?> dataAccessExceptionType) { |
| 77 | + SQLExceptionTranslator translator = new SQLExceptionSubclassTranslator(); |
| 78 | + DataAccessException dax = translator.translate("task", "SQL", ex); |
| 79 | + |
| 80 | + assertThat(dax).as("Specific translation must not result in null").isNotNull(); |
| 81 | + assertThat(dax).as("Wrong DataAccessException type returned").isExactlyInstanceOf(dataAccessExceptionType); |
| 82 | + assertThat(dax.getCause()).as("The exact same original SQLException must be preserved").isSameAs(ex); |
109 | 83 | }
|
110 | 84 |
|
111 | 85 | }
|
0 commit comments