@Test @SuppressWarnings("resource") public void customErrorCodeTranslation() { new ClassPathXmlApplicationContext("test-custom-translators-context.xml", CustomSQLExceptionTranslatorRegistrarTests.class); SQLErrorCodes codes = SQLErrorCodesFactory.getInstance().getErrorCodes("H2"); SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(); sext.setSqlErrorCodes(codes); DataAccessException exFor4200 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 42000)); assertNotNull("Should have been translated", exFor4200); assertTrue("Should have been instance of BadSqlGrammarException", BadSqlGrammarException.class.isAssignableFrom(exFor4200.getClass())); DataAccessException exFor2 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 2)); assertNotNull("Should have been translated", exFor2); assertTrue("Should have been instance of TransientDataAccessResourceException", TransientDataAccessResourceException.class.isAssignableFrom(exFor2.getClass())); DataAccessException exFor3 = sext.doTranslate("", "", new SQLException("Ouch", "42000", 3)); assertNull("Should not have been translated", exFor3); }
@SuppressWarnings("serial") @Test public void customTranslateMethodTranslation() { final String TASK = "TASK"; final String SQL = "SQL SELECT *"; final DataAccessException customDex = new DataAccessException("") {}; final SQLException badSqlEx = new SQLException("", "", 1); SQLException intVioEx = new SQLException("", "", 6); SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator() { @Override @Nullable protected DataAccessException customTranslate(String task, @Nullable String sql, SQLException sqlex) { assertEquals(TASK, task); assertEquals(SQL, sql); return (sqlex == badSqlEx) ? customDex : null; } }; sext.setSqlErrorCodes(ERROR_CODES); // Shouldn't custom translate this assertEquals(customDex, sext.translate(TASK, SQL, badSqlEx)); DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, intVioEx); assertEquals(intVioEx, diex.getCause()); }
@Test public void customExceptionTranslation() { final String TASK = "TASK"; final String SQL = "SQL SELECT *"; final SQLErrorCodes customErrorCodes = new SQLErrorCodes(); final CustomSQLErrorCodesTranslation customTranslation = new CustomSQLErrorCodesTranslation(); customErrorCodes.setBadSqlGrammarCodes(new String[] {"1", "2"}); customErrorCodes.setDataIntegrityViolationCodes(new String[] {"3", "4"}); customTranslation.setErrorCodes(new String[] {"1"}); customTranslation.setExceptionClass(CustomErrorCodeException.class); customErrorCodes.setCustomTranslations(new CustomSQLErrorCodesTranslation[] {customTranslation}); SQLErrorCodeSQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(); sext.setSqlErrorCodes(customErrorCodes); // Should custom translate this SQLException badSqlEx = new SQLException("", "", 1); assertEquals(CustomErrorCodeException.class, sext.translate(TASK, SQL, badSqlEx).getClass()); assertEquals(badSqlEx, sext.translate(TASK, SQL, badSqlEx).getCause()); // Shouldn't custom translate this SQLException invResEx = new SQLException("", "", 3); DataIntegrityViolationException diex = (DataIntegrityViolationException) sext.translate(TASK, SQL, invResEx); assertEquals(invResEx, diex.getCause()); // Shouldn't custom translate this - invalid class exception.expect(IllegalArgumentException.class); customTranslation.setExceptionClass(String.class); }