KickJava   Java API By Example, From Geeks To Geeks.

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


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.jdbc.BadSqlGrammarException;
24 import org.springframework.jdbc.UncategorizedSQLException;
25
26 /**
27  *
28  * @author Rod Johnson
29  * @since 13-Jan-03
30  */

31 public class SQLStateExceptionTranslatorTests extends TestCase {
32     
33     private SQLStateSQLExceptionTranslator trans = new SQLStateSQLExceptionTranslator();
34
35     // ALSO CHECK CHAIN of SQLExceptions!?
36
// also allow chain of translators? default if can't do specific?
37

38     public void testBadSqlGrammar() {
39         String JavaDoc sql = "SELECT FOO FROM BAR";
40         SQLException JavaDoc sex = new SQLException JavaDoc("Message", "42001", 1);
41         try {
42             throw this.trans.translate("task", sql, sex);
43         }
44         catch (BadSqlGrammarException ex) {
45             // OK
46
assertTrue("SQL is correct", sql.equals(ex.getSql()));
47             assertTrue("Exception matches", sex.equals(ex.getSQLException()));
48         }
49     }
50     
51     public void testInvalidSqlStateCode() {
52         String JavaDoc sql = "SELECT FOO FROM BAR";
53         SQLException JavaDoc sex = new SQLException JavaDoc("Message", "NO SUCH CODE", 1);
54         try {
55             throw this.trans.translate("task", sql, sex);
56         }
57         catch (UncategorizedSQLException ex) {
58             // OK
59
assertTrue("SQL is correct", sql.equals(ex.getSql()));
60             assertTrue("Exception matches", sex.equals(ex.getSQLException()));
61         }
62     }
63     
64     /**
65      * PostgreSQL can return null
66      * SAP DB can apparently return empty SQL code
67      * Bug 729170
68      */

69     public void testMalformedSqlStateCodes() {
70         String JavaDoc sql = "SELECT FOO FROM BAR";
71         SQLException JavaDoc sex = new SQLException JavaDoc("Message", null, 1);
72         testMalformedSqlStateCode(sex);
73         
74         sex = new SQLException JavaDoc("Message", "", 1);
75         testMalformedSqlStateCode(sex);
76                 
77         // One char's not allowed
78
sex = new SQLException JavaDoc("Message", "I", 1);
79         testMalformedSqlStateCode(sex);
80     }
81     
82     
83     private void testMalformedSqlStateCode(SQLException JavaDoc sex) {
84         String JavaDoc sql = "SELECT FOO FROM BAR";
85         try {
86             throw this.trans.translate("task", sql, sex);
87         }
88         catch (UncategorizedSQLException ex) {
89             // OK
90
assertTrue("SQL is correct", sql.equals(ex.getSql()));
91             assertTrue("Exception matches", sex.equals(ex.getSQLException()));
92         }
93     }
94
95 }
96
Popular Tags