From: Juergen H. <jho...@us...> - 2008-10-16 19:17:29
|
Update of /cvsroot/springframework/spring/test/org/springframework/jdbc/support In directory fdv4jf1.ch3.sourceforge.com:/tmp/cvs-serv23860/test/org/springframework/jdbc/support Modified Files: SQLExceptionSubclassTranslatorTests.java SQLStateSQLExceptionTranslatorTests.java SQLExceptionSubclassFactory.java Log Message: added "transientDataAccessResourceCodes" category to "sql-error-codes.xml", with default codes for DB2 and Sybase; SQLErrorCodeSQLExceptionTranslator applies default SQLExceptionSubclassTranslator after error code translation only; SQLErrorCodeSQLExceptionTranslator is able to work without fallback SQLState translation as well; SQLExceptionSubclassTranslator translates SQLFeatureNotSupportedException to InvalidDataAccessApiUsageException; SQLStateSQLExceptionTranslator returns TransientDataAccessResourceException for well-known vendor-specific SQL states; factored out AbstractFallbackSQLExceptionTranslator base class for consistent "fallbackTranslator" capabilities Index: SQLExceptionSubclassTranslatorTests.java =================================================================== RCS file: /cvsroot/springframework/spring/test/org/springframework/jdbc/support/SQLExceptionSubclassTranslatorTests.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SQLExceptionSubclassTranslatorTests.java 19 Jun 2007 13:45:56 -0000 1.1 --- SQLExceptionSubclassTranslatorTests.java 16 Oct 2008 19:17:16 -0000 1.2 *************** *** 1,4 **** /* ! * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); --- 1,4 ---- /* ! * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); *************** *** 17,27 **** package org.springframework.jdbc.support; - import junit.framework.TestCase; - import java.sql.SQLException; ! import org.springframework.jdbc.BadSqlGrammarException; ! import org.springframework.dao.*; import org.springframework.core.JdkVersion; /** --- 17,33 ---- package org.springframework.jdbc.support; import java.sql.SQLException; ! import junit.framework.TestCase; ! import org.springframework.core.JdkVersion; + import org.springframework.dao.ConcurrencyFailureException; + import org.springframework.dao.DataAccessResourceFailureException; + import org.springframework.dao.DataIntegrityViolationException; + import org.springframework.dao.InvalidDataAccessApiUsageException; + import org.springframework.dao.PermissionDeniedDataAccessException; + import org.springframework.dao.RecoverableDataAccessException; + import org.springframework.dao.TransientDataAccessResourceException; + import org.springframework.jdbc.BadSqlGrammarException; /** *************** *** 31,105 **** private static SQLErrorCodes ERROR_CODES = new SQLErrorCodes(); static { ERROR_CODES.setBadSqlGrammarCodes(new String[] { "1" }); } - public void testErrorCodeTranslation() { - SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES); - - if (JdkVersion.getMajorJavaVersion() >= JdkVersion.JAVA_16) { ! SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 0); ! DataIntegrityViolationException divex = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx); ! assertEquals(dataIntegrityViolationEx, divex.getCause()); ! SQLException badSqlEx = SQLExceptionSubclassFactory.newSQLFeatureNotSupportedException("", "", 0); ! BadSqlGrammarException bsgex = (BadSqlGrammarException) sext.translate("task", "SQL", badSqlEx); ! assertEquals("SQL", bsgex.getSql()); ! assertEquals(badSqlEx, bsgex.getSQLException()); ! SQLException dataIntegrityViolationEx2 = SQLExceptionSubclassFactory.newSQLIntegrityConstraintViolationException("", "", 0); ! DataIntegrityViolationException divex2 = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx2); ! assertEquals(dataIntegrityViolationEx2, divex2.getCause()); ! SQLException permissionDeniedEx = SQLExceptionSubclassFactory.newSQLInvalidAuthorizationSpecException("", "", 0); ! PermissionDeniedDataAccessException pdaex = (PermissionDeniedDataAccessException) sext.translate("task", "SQL", permissionDeniedEx); ! assertEquals(permissionDeniedEx, pdaex.getCause()); ! SQLException dataAccesResourceEx = SQLExceptionSubclassFactory.newSQLNonTransientConnectionException("", "", 0); ! DataAccessResourceFailureException darex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataAccesResourceEx); ! assertEquals(dataAccesResourceEx, darex.getCause()); ! SQLException badSqlEx2 = SQLExceptionSubclassFactory.newSQLSyntaxErrorException("", "", 0); ! BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", badSqlEx2); ! assertEquals("SQL2", bsgex2.getSql()); ! assertEquals(badSqlEx2, bsgex2.getSQLException()); ! SQLException tranRollbackEx = SQLExceptionSubclassFactory.newSQLTransactionRollbackException("", "", 0); ! ConcurrencyFailureException cfex = (ConcurrencyFailureException) sext.translate("task", "SQL", tranRollbackEx); ! assertEquals(tranRollbackEx, cfex.getCause()); ! SQLException transientConnEx = SQLExceptionSubclassFactory.newSQLTransientConnectionException("", "", 0); ! TransientDataAccessResourceException tdarex = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx); ! assertEquals(transientConnEx, tdarex.getCause()); ! SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0); ! TransientDataAccessResourceException tdarex2 = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx2); ! assertEquals(transientConnEx2, tdarex2.getCause()); ! SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0); ! RecoverableDataAccessException rdaex2 = (RecoverableDataAccessException) sext.translate("task", "SQL", recoverableEx); ! assertEquals(recoverableEx, rdaex2.getCause()); ! // Test classic error code translation. We should move there next if the exception we pass in is not one ! // of the new sub-classes. ! SQLException sexEct = new SQLException("", "", 1); ! BadSqlGrammarException bsgEct = (BadSqlGrammarException) sext.translate("task", "SQL-ECT", sexEct); ! assertEquals("SQL-ECT", bsgEct.getSql()); ! assertEquals(sexEct, bsgEct.getSQLException()); ! // Test fallback. We assume that no database will ever return this error code, ! // but 07xxx will be bad grammar picked up by the fallback SQLState translator ! SQLException sexFbt = new SQLException("", "07xxx", 666666666); ! BadSqlGrammarException bsgFbt = (BadSqlGrammarException) sext.translate("task", "SQL-FBT", sexFbt); ! assertEquals("SQL-FBT", bsgFbt.getSql()); ! assertEquals(sexFbt, bsgFbt.getSQLException()); ! // and 08xxx will be data resource failure (non-transient) picked up by the fallback SQLState translator ! SQLException sexFbt2 = new SQLException("", "08xxx", 666666666); ! DataAccessResourceFailureException darfFbt = (DataAccessResourceFailureException) sext.translate("task", "SQL-FBT2", sexFbt2); ! assertEquals(sexFbt2, darfFbt.getCause()); ! } } --- 37,111 ---- private static SQLErrorCodes ERROR_CODES = new SQLErrorCodes(); + static { ERROR_CODES.setBadSqlGrammarCodes(new String[] { "1" }); } ! public void testErrorCodeTranslation() { ! if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_16) { ! return; ! } ! SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES); ! SQLException dataIntegrityViolationEx = SQLExceptionSubclassFactory.newSQLDataException("", "", 0); ! DataIntegrityViolationException divex = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx); ! assertEquals(dataIntegrityViolationEx, divex.getCause()); ! SQLException featureNotSupEx = SQLExceptionSubclassFactory.newSQLFeatureNotSupportedException("", "", 0); ! InvalidDataAccessApiUsageException idaex = (InvalidDataAccessApiUsageException) sext.translate("task", "SQL", featureNotSupEx); ! assertEquals(featureNotSupEx, idaex.getCause()); ! SQLException dataIntegrityViolationEx2 = SQLExceptionSubclassFactory.newSQLIntegrityConstraintViolationException("", "", 0); ! DataIntegrityViolationException divex2 = (DataIntegrityViolationException) sext.translate("task", "SQL", dataIntegrityViolationEx2); ! assertEquals(dataIntegrityViolationEx2, divex2.getCause()); ! SQLException permissionDeniedEx = SQLExceptionSubclassFactory.newSQLInvalidAuthorizationSpecException("", "", 0); ! PermissionDeniedDataAccessException pdaex = (PermissionDeniedDataAccessException) sext.translate("task", "SQL", permissionDeniedEx); ! assertEquals(permissionDeniedEx, pdaex.getCause()); ! SQLException dataAccessResourceEx = SQLExceptionSubclassFactory.newSQLNonTransientConnectionException("", "", 0); ! DataAccessResourceFailureException darex = (DataAccessResourceFailureException) sext.translate("task", "SQL", dataAccessResourceEx); ! assertEquals(dataAccessResourceEx, darex.getCause()); ! SQLException badSqlEx2 = SQLExceptionSubclassFactory.newSQLSyntaxErrorException("", "", 0); ! BadSqlGrammarException bsgex2 = (BadSqlGrammarException) sext.translate("task", "SQL2", badSqlEx2); ! assertEquals("SQL2", bsgex2.getSql()); ! assertEquals(badSqlEx2, bsgex2.getSQLException()); ! SQLException tranRollbackEx = SQLExceptionSubclassFactory.newSQLTransactionRollbackException("", "", 0); ! ConcurrencyFailureException cfex = (ConcurrencyFailureException) sext.translate("task", "SQL", tranRollbackEx); ! assertEquals(tranRollbackEx, cfex.getCause()); ! SQLException transientConnEx = SQLExceptionSubclassFactory.newSQLTransientConnectionException("", "", 0); ! TransientDataAccessResourceException tdarex = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx); ! assertEquals(transientConnEx, tdarex.getCause()); ! SQLException transientConnEx2 = SQLExceptionSubclassFactory.newSQLTimeoutException("", "", 0); ! TransientDataAccessResourceException tdarex2 = (TransientDataAccessResourceException) sext.translate("task", "SQL", transientConnEx2); ! assertEquals(transientConnEx2, tdarex2.getCause()); ! SQLException recoverableEx = SQLExceptionSubclassFactory.newSQLRecoverableException("", "", 0); ! RecoverableDataAccessException rdaex2 = (RecoverableDataAccessException) sext.translate("task", "SQL", recoverableEx); ! assertEquals(recoverableEx, rdaex2.getCause()); ! // Test classic error code translation. We should move there next if the exception we pass in is not one ! // of the new sub-classes. ! SQLException sexEct = new SQLException("", "", 1); ! BadSqlGrammarException bsgEct = (BadSqlGrammarException) sext.translate("task", "SQL-ECT", sexEct); ! assertEquals("SQL-ECT", bsgEct.getSql()); ! assertEquals(sexEct, bsgEct.getSQLException()); + // Test fallback. We assume that no database will ever return this error code, + // but 07xxx will be bad grammar picked up by the fallback SQLState translator + SQLException sexFbt = new SQLException("", "07xxx", 666666666); + BadSqlGrammarException bsgFbt = (BadSqlGrammarException) sext.translate("task", "SQL-FBT", sexFbt); + assertEquals("SQL-FBT", bsgFbt.getSql()); + assertEquals(sexFbt, bsgFbt.getSQLException()); + // and 08xxx will be data resource failure (non-transient) picked up by the fallback SQLState translator + SQLException sexFbt2 = new SQLException("", "08xxx", 666666666); + DataAccessResourceFailureException darfFbt = (DataAccessResourceFailureException) sext.translate("task", "SQL-FBT2", sexFbt2); + assertEquals(sexFbt2, darfFbt.getCause()); } Index: SQLStateSQLExceptionTranslatorTests.java =================================================================== RCS file: /cvsroot/springframework/spring/test/org/springframework/jdbc/support/SQLStateSQLExceptionTranslatorTests.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SQLStateSQLExceptionTranslatorTests.java 2 Aug 2006 16:16:34 -0000 1.1 --- SQLStateSQLExceptionTranslatorTests.java 16 Oct 2008 19:17:16 -0000 1.2 *************** *** 1,4 **** /* ! * Copyright 2002-2006 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); --- 1,4 ---- /* ! * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); *************** *** 17,37 **** package org.springframework.jdbc.support; import junit.framework.TestCase; import org.springframework.dao.ConcurrencyFailureException; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.dao.DataIntegrityViolationException; import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.UncategorizedSQLException; import org.springframework.test.AssertThrows; - import java.sql.SQLException; - /** - * Unit tests for the {@link SQLStateSQLExceptionTranslator} class. - * * @author Rick Evans */ ! public final class SQLStateSQLExceptionTranslatorTests extends TestCase { private static final String REASON = "The game is afoot!"; --- 17,38 ---- package org.springframework.jdbc.support; + import java.sql.SQLException; + import junit.framework.TestCase; + import org.springframework.dao.ConcurrencyFailureException; import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessResourceFailureException; import org.springframework.dao.DataIntegrityViolationException; + import org.springframework.dao.TransientDataAccessResourceException; import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.UncategorizedSQLException; import org.springframework.test.AssertThrows; /** * @author Rick Evans + * @author Juergen Hoeller */ ! public class SQLStateSQLExceptionTranslatorTests extends TestCase { private static final String REASON = "The game is afoot!"; *************** *** 50,63 **** } ! public void testTranslateBadSql() throws Exception { doTest("07", BadSqlGrammarException.class); } ! public void testTranslateIntegrityViolation() throws Exception { ! doTest("22", DataIntegrityViolationException.class); } ! public void testTranslateUncategorized() throws Exception { ! doTest("00000000", UncategorizedSQLException.class); } --- 51,68 ---- } ! public void testTranslateBadSqlGrammar() throws Exception { doTest("07", BadSqlGrammarException.class); } ! public void testTranslateDataIntegrityViolation() throws Exception { ! doTest("23", DataIntegrityViolationException.class); } ! public void testTranslateDataAccessResourceFailure() throws Exception { ! doTest("53", DataAccessResourceFailureException.class); ! } ! ! public void testTranslateTransientDataAccessResourceFailure() throws Exception { ! doTest("S1", TransientDataAccessResourceException.class); } *************** *** 66,71 **** } ! public void testTranslateDataAccessResourceFailure() throws Exception { ! doTest("53", DataAccessResourceFailureException.class); } --- 71,76 ---- } ! public void testTranslateUncategorized() throws Exception { ! doTest("00000000", UncategorizedSQLException.class); } Index: SQLExceptionSubclassFactory.java =================================================================== RCS file: /cvsroot/springframework/spring/test/org/springframework/jdbc/support/SQLExceptionSubclassFactory.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** SQLExceptionSubclassFactory.java 19 Jun 2007 13:45:56 -0000 1.1 --- SQLExceptionSubclassFactory.java 16 Oct 2008 19:17:16 -0000 1.2 *************** *** 1,4 **** /* ! * Copyright 2002-2007 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); --- 1,4 ---- /* ! * Copyright 2002-2008 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); *************** *** 17,26 **** package org.springframework.jdbc.support; ! import java.sql.*; /** * Class to generate Java 6 SQLException subclasses for testing purposes. * ! * @author trisberg */ public class SQLExceptionSubclassFactory { --- 17,36 ---- package org.springframework.jdbc.support; ! import java.sql.SQLDataException; ! import java.sql.SQLException; ! import java.sql.SQLFeatureNotSupportedException; ! import java.sql.SQLIntegrityConstraintViolationException; ! import java.sql.SQLInvalidAuthorizationSpecException; ! import java.sql.SQLNonTransientConnectionException; ! import java.sql.SQLRecoverableException; ! import java.sql.SQLSyntaxErrorException; ! import java.sql.SQLTimeoutException; ! import java.sql.SQLTransactionRollbackException; ! import java.sql.SQLTransientConnectionException; /** * Class to generate Java 6 SQLException subclasses for testing purposes. * ! * @author Thomas Risberg */ public class SQLExceptionSubclassFactory { |