Skip to content

Commit 064c618

Browse files
committed
Drop SQLExceptionSubclassFactory and unify SQLStateSQLExceptionTranslator tests
1 parent 8c80ec1 commit 064c618

5 files changed

+83
-247
lines changed

spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLExceptionCustomTranslatorTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.jdbc.support;
1818

19+
import java.sql.SQLDataException;
1920
import java.sql.SQLException;
2021

2122
import org.junit.jupiter.api.Test;
@@ -37,8 +38,8 @@ public class SQLExceptionCustomTranslatorTests {
3738
private static SQLErrorCodes ERROR_CODES = new SQLErrorCodes();
3839

3940
static {
40-
ERROR_CODES.setBadSqlGrammarCodes(new String[] { "1" });
41-
ERROR_CODES.setDataAccessResourceFailureCodes(new String[] { "2" });
41+
ERROR_CODES.setBadSqlGrammarCodes("1");
42+
ERROR_CODES.setDataAccessResourceFailureCodes("2");
4243
ERROR_CODES.setCustomSqlExceptionTranslatorClass(CustomSqlExceptionTranslator.class);
4344
}
4445

@@ -47,15 +48,15 @@ public class SQLExceptionCustomTranslatorTests {
4748

4849
@Test
4950
public void badSqlGrammarException() {
50-
SQLException badSqlGrammarExceptionEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 1);
51+
SQLException badSqlGrammarExceptionEx = new SQLDataException("", "", 1);
5152
DataAccessException dae = sext.translate("task", "SQL", badSqlGrammarExceptionEx);
5253
assertThat(dae.getCause()).isEqualTo(badSqlGrammarExceptionEx);
5354
assertThat(dae).isInstanceOf(BadSqlGrammarException.class);
5455
}
5556

5657
@Test
5758
public void dataAccessResourceException() {
58-
SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 2);
59+
SQLException dataAccessResourceEx = new SQLDataException("", "", 2);
5960
DataAccessException dae = sext.translate("task", "SQL", dataAccessResourceEx);
6061
assertThat(dae.getCause()).isEqualTo(dataAccessResourceEx);
6162
assertThat(dae).isInstanceOf(TransientDataAccessResourceException.class);

spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLExceptionSubclassFactory.java

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 38 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,22 @@
1616

1717
package org.springframework.jdbc.support;
1818

19+
import java.sql.SQLDataException;
1920
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;
2030

2131
import org.junit.jupiter.api.Test;
2232

2333
import org.springframework.dao.ConcurrencyFailureException;
34+
import org.springframework.dao.DataAccessException;
2435
import org.springframework.dao.DataAccessResourceFailureException;
2536
import org.springframework.dao.DataIntegrityViolationException;
2637
import org.springframework.dao.InvalidDataAccessApiUsageException;
@@ -34,78 +45,41 @@
3445

3546
/**
3647
* @author Thomas Risberg
48+
* @author Juergen Hoeller
3749
*/
3850
public class SQLExceptionSubclassTranslatorTests {
3951

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);
4464
}
4565

46-
4766
@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() {
9968
// Test fallback. We assume that no database will ever return this error code,
10069
// 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);
10571
// 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);
10983
}
11084

11185
}

spring-jdbc/src/test/java/org/springframework/jdbc/support/SQLStateExceptionTranslatorTests.java

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)