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