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.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 35 public class SQLErrorCodeSQLExceptionTranslatorTests extends TestCase { 36 37 private static SQLErrorCodes ERROR_CODES = new SQLErrorCodes(); 38 static { 39 ERROR_CODES.setBadSqlGrammarCodes(new String [] { "1", "2" }); 40 ERROR_CODES.setInvalidResultSetAccessCodes(new String [] { "3", "4" }); 41 ERROR_CODES.setDataAccessResourceFailureCodes(new String [] { "5" }); 42 ERROR_CODES.setDataIntegrityViolationCodes(new String [] { "6" }); 43 ERROR_CODES.setCannotAcquireLockCodes(new String [] { "7" }); 44 ERROR_CODES.setDeadlockLoserCodes(new String [] { "8" }); 45 ERROR_CODES.setCannotSerializeTransactionCodes(new String [] { "9" }); 46 } 47 48 public void testErrorCodeTranslation() { 49 SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES); 50 51 SQLException badSqlEx = new SQLException ("", "", 1); 52 BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", badSqlEx); 53 assertEquals("SQL", bsgex.getSql()); 54 assertEquals(badSqlEx, bsgex.getSQLException()); 55 56 SQLException invResEx = new SQLException ("", "", 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 SQLException sex = new SQLException ("", "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 exClass) { 76 SQLException sex = new SQLException ("", "", 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 TASK = "TASK"; 84 final String SQL = "SQL SELECT *"; 85 final DataAccessException customDex = new DataAccessException("") {}; 86 87 final SQLException badSqlEx = new SQLException ("", "", 1); 88 SQLException intVioEx = new SQLException ("", "", 6); 89 90 SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator() { 91 protected DataAccessException customTranslate(String task, String sql, SQLException sqlex) { 92 assertEquals(TASK, task); 93 assertEquals(SQL, sql); 94 return (sqlex == badSqlEx) ? customDex : null; 95 } 96 }; 97 sext.setSqlErrorCodes(ERROR_CODES); 98 99 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 TASK = "TASK"; 107 final String SQL = "SQL SELECT *"; 108 final SQLErrorCodes customErrorCodes = new SQLErrorCodes(); 109 final CustomSQLErrorCodesTranslation customTranslation = new CustomSQLErrorCodesTranslation(); 110 111 customErrorCodes.setBadSqlGrammarCodes(new String [] {"1", "2"}); 112 customErrorCodes.setDataIntegrityViolationCodes(new String [] {"3", "4"}); 113 customTranslation.setErrorCodes(new String [] {"1"}); 114 customTranslation.setExceptionClass(CustomErrorCodeException.class); 115 customErrorCodes.setCustomTranslations(new CustomSQLErrorCodesTranslation[] {customTranslation}); 116 117 SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(); 118 sext.setSqlErrorCodes(customErrorCodes); 119 120 SQLException badSqlEx = new SQLException ("", "", 1); 122 assertEquals(CustomErrorCodeException.class, sext.translate(TASK, SQL, badSqlEx).getClass()); 123 assertEquals(badSqlEx, sext.translate(TASK, SQL, badSqlEx).getCause()); 124 125 SQLException invResEx = new SQLException ("", "", 3); 127 DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, invResEx); 128 assertEquals(invResEx, diex.getCause()); 129 130 try { 132 customTranslation.setExceptionClass(String .class); 133 fail("Should have thrown IllegalArgumentException"); 134 } 135 catch (IllegalArgumentException ex) { 136 } 138 } 139 140 } 141 | Popular Tags |