forked from spring-projects/spring-framework
/
SQLErrorCodeSQLExceptionTranslatorTests.java
216 lines (177 loc) · 8.74 KB
/
SQLErrorCodeSQLExceptionTranslatorTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.jdbc.support;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.DataTruncation;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.dao.CannotSerializeTransactionException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DeadlockLoserDataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.InvalidResultSetAccessException;
import org.springframework.lang.Nullable;
import static org.junit.Assert.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* @author Rod Johnson
* @author Juergen Hoeller
* @author Sam Brannen
*/
public class SQLErrorCodeSQLExceptionTranslatorTests {
private static SQLErrorCodes ERROR_CODES = new SQLErrorCodes();
static {
ERROR_CODES.setBadSqlGrammarCodes(new String[] { "1", "2" });
ERROR_CODES.setInvalidResultSetAccessCodes(new String[] { "3", "4" });
ERROR_CODES.setDuplicateKeyCodes(new String[] {"10"});
ERROR_CODES.setDataAccessResourceFailureCodes(new String[] { "5" });
ERROR_CODES.setDataIntegrityViolationCodes(new String[] { "6" });
ERROR_CODES.setCannotAcquireLockCodes(new String[] { "7" });
ERROR_CODES.setDeadlockLoserCodes(new String[] { "8" });
ERROR_CODES.setCannotSerializeTransactionCodes(new String[] { "9" });
}
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void errorCodeTranslation() {
SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
SQLException badSqlEx = new SQLException("", "", 1);
BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", badSqlEx);
assertEquals("SQL", bsgex.getSql());
assertEquals(badSqlEx, bsgex.getSQLException());
SQLException invResEx = new SQLException("", "", 4);
InvalidResultSetAccessException irsex = (InvalidResultSetAccessException) sext.translate("task", "SQL", invResEx);
assertEquals("SQL", irsex.getSql());
assertEquals(invResEx, irsex.getSQLException());
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);
assertTrue("Not instance of DataIntegrityViolationException",
DataIntegrityViolationException.class.isAssignableFrom(dksex.getClass()));
// 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);
assertEquals("SQL2", bsgex2.getSql());
assertEquals(sex, bsgex2.getSQLException());
}
private void checkTranslation(SQLExceptionTranslator sext, int errorCode, Class<?> exClass) {
SQLException sex = new SQLException("", "", errorCode);
DataAccessException ex = sext.translate("", "", sex);
assertTrue(exClass.isInstance(ex));
assertTrue(ex.getCause() == sex);
}
@Test
public void batchExceptionTranslation() {
SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
SQLException badSqlEx = new SQLException("", "", 1);
BatchUpdateException batchUpdateEx = new BatchUpdateException();
batchUpdateEx.setNextException(badSqlEx);
BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", batchUpdateEx);
assertEquals("SQL", bsgex.getSql());
assertEquals(badSqlEx, bsgex.getSQLException());
}
@Test
public void dataTruncationTranslation() {
SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
SQLException dataAccessEx = new SQLException("", "", 5);
DataTruncation dataTruncation = new DataTruncation(1, true, true, 1, 1, dataAccessEx);
DataAccessResourceFailureException daex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataTruncation);
assertEquals(dataTruncation, daex.getCause());
}
@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) {
assertEquals(TASK, task);
assertEquals(SQL, sql);
return (sqlex == badSqlEx) ? customDex : null;
}
};
sext.setSqlErrorCodes(ERROR_CODES);
// Shouldn't custom translate this
assertEquals(customDex, sext.translate(TASK, SQL, badSqlEx));
DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, intVioEx);
assertEquals(intVioEx, diex.getCause());
}
@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(new String[] {"1", "2"});
customErrorCodes.setDataIntegrityViolationCodes(new String[] {"3", "4"});
customTranslation.setErrorCodes(new String[] {"1"});
customTranslation.setExceptionClass(CustomErrorCodeException.class);
customErrorCodes.setCustomTranslations(new CustomSQLErrorCodesTranslation[] {customTranslation});
SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
sext.setSqlErrorCodes(customErrorCodes);
// Should custom translate this
SQLException badSqlEx = new SQLException("", "", 1);
assertEquals(CustomErrorCodeException.class, sext.translate(TASK, SQL, badSqlEx).getClass());
assertEquals(badSqlEx, sext.translate(TASK, SQL, badSqlEx).getCause());
// Shouldn't custom translate this
SQLException invResEx = new SQLException("", "", 3);
DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, invResEx);
assertEquals(invResEx, diex.getCause());
// Shouldn't custom translate this - invalid class
exception.expect(IllegalArgumentException.class);
customTranslation.setExceptionClass(String.class);
}
@Test
public void dataSourceInitialization() throws Exception {
SQLException connectionException = new SQLException();
SQLException duplicateKeyException = new SQLException("test", "", 1);
DataSource dataSource = mock(DataSource.class);
given(dataSource.getConnection()).willThrow(connectionException);
SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(dataSource);
assertFalse(sext.translate("test", null, duplicateKeyException) instanceof DuplicateKeyException);
DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
given(databaseMetaData.getDatabaseProductName()).willReturn("Oracle");
Connection connection = mock(Connection.class);
given(connection.getMetaData()).willReturn(databaseMetaData);
Mockito.reset(dataSource);
given(dataSource.getConnection()).willReturn(connection);
assertTrue(sext.translate("test", null, duplicateKeyException) instanceof DuplicateKeyException);
verify(connection).close();
}
}