Skip to content

Commit 9b1edea

Browse files
author
Thomas Risberg
committedMar 12, 2009
added tests for custom SQLException translation, polished (SPR-4899)
1 parent a3942c5 commit 9b1edea

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-1
lines changed
 

‎org.springframework.jdbc/src/main/java/org/springframework/jdbc/support/SQLErrorCodes.java

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.jdbc.support;
1818

1919
import org.springframework.util.StringUtils;
20-
import org.springframework.util.Assert;
2120
import org.springframework.dao.InvalidDataAccessResourceUsageException;
2221

2322
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2009 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.jdbc.support;
18+
19+
import org.springframework.dao.DataAccessException;
20+
import org.springframework.dao.TransientDataAccessResourceException;
21+
22+
import java.sql.SQLException;
23+
24+
/**
25+
* Custom SQLException translation for testing.
26+
*
27+
* @author Thomas Risberg
28+
*/
29+
public class CustomSqlExceptionTranslator implements SQLExceptionTranslator {
30+
public DataAccessException translate(String task, String sql, SQLException ex) {
31+
if (ex.getErrorCode() == 2) {
32+
return new TransientDataAccessResourceException("Custom", ex);
33+
}
34+
return null;
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2002-2008 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.jdbc.support;
18+
19+
import org.springframework.dao.DataAccessException;
20+
import org.springframework.dao.TransientDataAccessResourceException;
21+
import org.springframework.jdbc.BadSqlGrammarException;
22+
23+
import java.sql.SQLException;
24+
25+
import junit.framework.TestCase;
26+
import org.junit.runner.RunWith;
27+
import org.junit.runners.JUnit4;
28+
import org.junit.Test;
29+
30+
/**
31+
* Class to test custom SQLException translation.
32+
*
33+
* @author Thomas Risberg
34+
*/
35+
@RunWith(JUnit4.class)
36+
public class SQLExceptionCustomTranslatorTests extends TestCase {
37+
38+
private static SQLErrorCodes ERROR_CODES = new SQLErrorCodes();
39+
40+
static {
41+
ERROR_CODES.setBadSqlGrammarCodes(new String[] { "1" });
42+
ERROR_CODES.setDataAccessResourceFailureCodes(new String[] { "2" });
43+
ERROR_CODES.setCustomSqlExceptionTranslatorClass(CustomSqlExceptionTranslator.class);
44+
}
45+
46+
47+
@Test
48+
public void testCustomErrorCodeTranslation() {
49+
50+
SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
51+
52+
SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 1);
53+
DataAccessException daeex = sext.translate("task", "SQL", dataIntegrityViolationEx);
54+
assertEquals(dataIntegrityViolationEx, daeex.getCause());
55+
assertTrue(daeex instanceof BadSqlGrammarException);
56+
57+
SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 2);
58+
DataAccessException darex = sext.translate("task", "SQL", dataAccessResourceEx);
59+
assertEquals(dataIntegrityViolationEx, daeex.getCause());
60+
assertTrue(darex instanceof TransientDataAccessResourceException);
61+
62+
}
63+
64+
}

0 commit comments

Comments
 (0)
Please sign in to comment.