DomaPersistenceExceptionTranslator implements PersistenceExceptionTranslator { @Override public DataAccessException translateExceptionIfPossible(RuntimeException ex) { if (!(ex instanceof JdbcException)) { return null; } if (ex instanceof OptimisticLockException) { return new OptimisticLockingFailureException(ex.getMessage(), ex); } else if (ex instanceof UniqueConstraintException) { return new DuplicateKeyException(ex.getMessage(), ex); } else if (ex instanceof NonUniqueResultException || ex instanceof NonSingleColumnException) { return new IncorrectResultSizeDataAccessException(ex.getMessage(), 1, ex); } else if (ex instanceof NoResultException) { return new EmptyResultDataAccessException(ex.getMessage(), 1, ex); } else if (ex instanceof UnknownColumnException || ex instanceof ResultMappingException) { return new TypeMismatchDataAccessException(ex.getMessage(), ex); } if (ex.getCause() instanceof SQLException) { SQLException e = (SQLException) ex.getCause(); String sql = null; if (ex instanceof SqlExecutionException) { sql = ((SqlExecutionException) ex).getRawSql(); } return translator.translate(ex.getMessage(), sql, e); } return new UncategorizedDataAccessException(ex.getMessage(), ex) { }; } DomaPersistenceExceptionTranslator(
			SQLExceptionTranslator sqlExceptionTranslator); @Override DataAccessException translateExceptionIfPossible(RuntimeException ex);  }
@Test public void testOccurNotJdbcException() { DataAccessException dataAccessException = translator .translateExceptionIfPossible(new DomaException(Message.DOMA2008)); assertThat(dataAccessException, nullValue()); }
@Test public void testOccurSqlExecutionException() { DataAccessException dataAccessException = translator .translateExceptionIfPossible(new SqlExecutionException( SqlLogType.FORMATTED, SqlKind.SELECT, "select * from todo where todo_id = ?", "select * from todo where todo_id = '000000001'", "TodoDao/findOne.sql", new SQLException(), null)); assertThat(dataAccessException, is(instanceOf(UncategorizedSQLException.class))); assertThat(UncategorizedSQLException.class.cast(dataAccessException).getSql(), is("select * from todo where todo_id = ?")); }
@Test public void testThrowOptimisticLockingFailureException() { DataAccessException dataAccessException = translator .translateExceptionIfPossible(new OptimisticLockException( SqlLogType.FORMATTED, SqlKind.SELECT, "update todo set title = ? where todo_id = ? and version = ?", "update todo set title = 'Modified Title' where todo_id = '000000001' and version = 1", "TodoDao/update.sql")); assertThat(dataAccessException, is(instanceOf(OptimisticLockingFailureException.class))); }
@Test public void testThrowDuplicateKeyException() { DataAccessException dataAccessException = translator .translateExceptionIfPossible(new UniqueConstraintException( SqlLogType.FORMATTED, SqlKind.INSERT, "insert into todo (todo_id, title) values (?, ?)", "insert into todo (todo_id, title) values ('000000001', 'Title')", "TodoDao/insert.sql", new SQLException())); assertThat(dataAccessException, is(instanceOf(DuplicateKeyException.class))); }
@Test public void testThrowIncorrectResultSizeDataAccessException() { { DataAccessException dataAccessException = translator .translateExceptionIfPossible(new NonUniqueResultException( SqlLogType.FORMATTED, SqlKind.SELECT, "select * from todo where created_at = ?", "select * from todo where created_at = '2016-03-06'", "TodoDao/findBy.sql")); assertThat(dataAccessException, is(instanceOf(IncorrectResultSizeDataAccessException.class))); } { DataAccessException dataAccessException = translator .translateExceptionIfPossible(new NonSingleColumnException( SqlLogType.FORMATTED, SqlKind.SELECT, "select todo_id, title from todo where created_at = ?", "select todo_id, title from todo where created_at = '2016-03-06'", "TodoDao/findBy.sql")); assertThat(dataAccessException, is(instanceOf(IncorrectResultSizeDataAccessException.class))); } }
@Test public void testThrowEmptyResultDataAccessException() { DataAccessException dataAccessException = translator .translateExceptionIfPossible(new NoResultException(SqlLogType.FORMATTED, SqlKind.SELECT, "select * from todo where todo_id = ?", "select * from todo where todo_id = '000000001'", "TodoDao/findOne.sql")); assertThat(dataAccessException, is(instanceOf(EmptyResultDataAccessException.class))); }
@Test public void testThrowTypeMismatchDataAccessException() { { DataAccessException dataAccessException = translator .translateExceptionIfPossible(new UnknownColumnException( SqlLogType.FORMATTED, "todo_id", "todoId", "model.Todo", SqlKind.SELECT, "select * from todo where created_at = ?", "select * from todo where created_at = '2016-03-06'", "TodoDao/findBy.sql")); assertThat(dataAccessException, is(instanceOf(TypeMismatchDataAccessException.class))); } { DataAccessException dataAccessException = translator .translateExceptionIfPossible(new ResultMappingException( SqlLogType.FORMATTED, "Todo", Collections.singletonList("finished"), Collections.singletonList("modified_at"), SqlKind.SELECT, "select todo_id, title from todo where created_at = ?", "select todo_id, title from todo where created_at = '2016-03-06'", "TodoDao/findBy.sql")); assertThat(dataAccessException, is(instanceOf(TypeMismatchDataAccessException.class))); } }
@Test public void testThrowUncategorizedDataAccessException() { DataAccessException dataAccessException = translator .translateExceptionIfPossible(new ConfigException("DomaConfig", "configure")); assertThat(dataAccessException, is(instanceOf(UncategorizedDataAccessException.class))); }