KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > support > SQLErrorCodeSQLExceptionTranslatorTests


1 /*
2  * Copyright 2002-2005 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 java.sql.SQLException JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.springframework.dao.CannotAcquireLockException;
24 import org.springframework.dao.CannotSerializeTransactionException;
25 import org.springframework.dao.DataAccessException;
26 import org.springframework.dao.DataAccessResourceFailureException;
27 import org.springframework.dao.DataIntegrityViolationException;
28 import org.springframework.dao.DeadlockLoserDataAccessException;
29 import org.springframework.jdbc.BadSqlGrammarException;
30 import org.springframework.jdbc.InvalidResultSetAccessException;
31
32 /**
33  * @author Rod Johnson
34  */

35 public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase {
36     
37     private static SQLErrorCodes ERROR_CODES = new SQLErrorCodes();
38     static {
39         ERROR_CODES.setBadSqlGrammarCodes(new String JavaDoc[] { "1", "2" });
40         ERROR_CODES.setInvalidResultSetAccessCodes(new String JavaDoc[] { "3", "4" });
41         ERROR_CODES.setDataAccessResourceFailureCodes(new String JavaDoc[] { "5" });
42         ERROR_CODES.setDataIntegrityViolationCodes(new String JavaDoc[] { "6" });
43         ERROR_CODES.setCannotAcquireLockCodes(new String JavaDoc[] { "7" });
44         ERROR_CODES.setDeadlockLoserCodes(new String JavaDoc[] { "8" });
45         ERROR_CODES.setCannotSerializeTransactionCodes(new String JavaDoc[] { "9" });
46     }
47
48     public void testErrorCodeTranslation() {
49         SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
50
51         SQLException JavaDoc badSqlEx = new SQLException JavaDoc("", "", 1);
52         BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", badSqlEx);
53         assertEquals("SQL", bsgex.getSql());
54         assertEquals(badSqlEx, bsgex.getSQLException());
55
56         SQLException JavaDoc invResEx = new SQLException JavaDoc("", "", 4);
57         InvalidResultSetAccessException irsex = (InvalidResultSetAccessException) sext.translate("task", "SQL", invResEx);
58         assertEquals("SQL", irsex.getSql());
59         assertEquals(invResEx, irsex.getSQLException());
60
61         checkTranslation(sext, 5, DataAccessResourceFailureException.class);
62         checkTranslation(sext, 6, DataIntegrityViolationException.class);
63         checkTranslation(sext, 7, CannotAcquireLockException.class);
64         checkTranslation(sext, 8, DeadlockLoserDataAccessException.class);
65         checkTranslation(sext, 9, CannotSerializeTransactionException.class);
66
67         // Test fallback. We assume that no database will ever return this error code,
68
// but 07xxx will be bad grammar picked up by the fallback SQLState translator
69
SQLException JavaDoc sex = new SQLException JavaDoc("", "07xxx", 666666666);
70         BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", sex);
71         assertEquals("SQL2", bsgex2.getSql());
72         assertEquals(sex, bsgex2.getSQLException());
73     }
74
75     private void checkTranslation(SQLExceptionTranslator sext, int errorCode, Class JavaDoc exClass) {
76         SQLException JavaDoc sex = new SQLException JavaDoc("", "", errorCode);
77         DataAccessException ex = sext.translate("", "", sex);
78         assertTrue(exClass.isInstance(ex));
79         assertTrue(ex.getCause() == sex);
80     }
81
82     public void testCustomTranslateMethodTranslation() {
83         final String JavaDoc TASK = "TASK";
84         final String JavaDoc SQL = "SQL SELECT *";
85         final DataAccessException customDex = new DataAccessException("") {};
86
87         final SQLException JavaDoc badSqlEx = new SQLException JavaDoc("", "", 1);
88         SQLException JavaDoc intVioEx = new SQLException JavaDoc("", "", 6);
89
90         SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator() {
91             protected DataAccessException customTranslate(String JavaDoc task, String JavaDoc sql, SQLException JavaDoc sqlex) {
92                 assertEquals(TASK, task);
93                 assertEquals(SQL, sql);
94                 return (sqlex == badSqlEx) ? customDex : null;
95             }
96         };
97         sext.setSqlErrorCodes(ERROR_CODES);
98         
99         // Shouldn't custom translate this
100
assertEquals(customDex, sext.translate(TASK, SQL, badSqlEx));
101         DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, intVioEx);
102         assertEquals(intVioEx, diex.getCause());
103     }
104     
105     public void testCustomExceptionTranslation() {
106         final String JavaDoc TASK = "TASK";
107         final String JavaDoc SQL = "SQL SELECT *";
108         final SQLErrorCodes customErrorCodes = new SQLErrorCodes();
109         final CustomSQLErrorCodesTranslation customTranslation = new CustomSQLErrorCodesTranslation();
110         
111         customErrorCodes.setBadSqlGrammarCodes(new String JavaDoc[] {"1", "2"});
112         customErrorCodes.setDataIntegrityViolationCodes(new String JavaDoc[] {"3", "4"});
113         customTranslation.setErrorCodes(new String JavaDoc[] {"1"});
114         customTranslation.setExceptionClass(CustomErrorCodeException.class);
115         customErrorCodes.setCustomTranslations(new CustomSQLErrorCodesTranslation[] {customTranslation});
116
117         SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator();
118         sext.setSqlErrorCodes(customErrorCodes);
119         
120         // Should custom translate this
121
SQLException JavaDoc badSqlEx = new SQLException JavaDoc("", "", 1);
122         assertEquals(CustomErrorCodeException.class, sext.translate(TASK, SQL, badSqlEx).getClass());
123         assertEquals(badSqlEx, sext.translate(TASK, SQL, badSqlEx).getCause());
124
125         // Shouldn't custom translate this
126
SQLException JavaDoc invResEx = new SQLException JavaDoc("", "", 3);
127         DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, invResEx);
128         assertEquals(invResEx, diex.getCause());
129
130         // Shouldn't custom translate this - invalid class
131
try {
132             customTranslation.setExceptionClass(String JavaDoc.class);
133             fail("Should have thrown IllegalArgumentException");
134         }
135         catch (IllegalArgumentException JavaDoc ex) {
136             // expected
137
}
138     }
139
140 }
141
Popular Tags