1 16 17 package org.springframework.jdbc.support; 18 19 import java.sql.SQLException ; 20 21 import junit.framework.TestCase; 22 23 import org.springframework.jdbc.BadSqlGrammarException; 24 import org.springframework.jdbc.UncategorizedSQLException; 25 26 31 public class SQLStateExceptionTranslatorTests extends TestCase { 32 33 private SQLStateSQLExceptionTranslator trans = new SQLStateSQLExceptionTranslator(); 34 35 38 public void testBadSqlGrammar() { 39 String sql = "SELECT FOO FROM BAR"; 40 SQLException sex = new SQLException ("Message", "42001", 1); 41 try { 42 throw this.trans.translate("task", sql, sex); 43 } 44 catch (BadSqlGrammarException ex) { 45 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 sql = "SELECT FOO FROM BAR"; 53 SQLException sex = new SQLException ("Message", "NO SUCH CODE", 1); 54 try { 55 throw this.trans.translate("task", sql, sex); 56 } 57 catch (UncategorizedSQLException ex) { 58 assertTrue("SQL is correct", sql.equals(ex.getSql())); 60 assertTrue("Exception matches", sex.equals(ex.getSQLException())); 61 } 62 } 63 64 69 public void testMalformedSqlStateCodes() { 70 String sql = "SELECT FOO FROM BAR"; 71 SQLException sex = new SQLException ("Message", null, 1); 72 testMalformedSqlStateCode(sex); 73 74 sex = new SQLException ("Message", "", 1); 75 testMalformedSqlStateCode(sex); 76 77 sex = new SQLException ("Message", "I", 1); 79 testMalformedSqlStateCode(sex); 80 } 81 82 83 private void testMalformedSqlStateCode(SQLException sex) { 84 String sql = "SELECT FOO FROM BAR"; 85 try { 86 throw this.trans.translate("task", sql, sex); 87 } 88 catch (UncategorizedSQLException ex) { 89 assertTrue("SQL is correct", sql.equals(ex.getSql())); 91 assertTrue("Exception matches", sex.equals(ex.getSQLException())); 92 } 93 } 94 95 } 96 | Popular Tags |