Java Code Examples for org.springframework.dao.PermissionDeniedDataAccessException

The following examples show how to use org.springframework.dao.PermissionDeniedDataAccessException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {

	if (ex instanceof DataAccessException) {
		return (DataAccessException) ex;
	} else if (ex instanceof DiscoveryException) {
		return translateImpl((Neo4jException) ex, TransientDataAccessResourceException::new);
	} else if (ex instanceof DatabaseException) {
		return translateImpl((Neo4jException) ex, NonTransientDataAccessResourceException::new);
	} else if (ex instanceof ServiceUnavailableException) {
		return translateImpl((Neo4jException) ex, NonTransientDataAccessResourceException::new);
	} else if (ex instanceof SessionExpiredException) {
		return translateImpl((Neo4jException) ex, RecoverableDataAccessException::new);
	} else if (ex instanceof ProtocolException) {
		return translateImpl((Neo4jException) ex, NonTransientDataAccessResourceException::new);
	} else if (ex instanceof TransientException) {
		return translateImpl((Neo4jException) ex, TransientDataAccessResourceException::new);
	} else if (ex instanceof ValueException) {
		return translateImpl((Neo4jException) ex, InvalidDataAccessApiUsageException::new);
	} else if (ex instanceof AuthenticationException) {
		return translateImpl((Neo4jException) ex, PermissionDeniedDataAccessException::new);
	} else if (ex instanceof ResultConsumedException) {
		return translateImpl((Neo4jException) ex, InvalidDataAccessApiUsageException::new);
	} else if (ex instanceof FatalDiscoveryException) {
		return translateImpl((Neo4jException) ex, NonTransientDataAccessResourceException::new);
	} else if (ex instanceof TransactionNestingException) {
		return translateImpl((Neo4jException) ex, InvalidDataAccessApiUsageException::new);
	} else if (ex instanceof ClientException) {
		return translateImpl((Neo4jException) ex, InvalidDataAccessResourceUsageException::new);
	}

	log.warn(() -> String.format("Don't know how to translate exception of type %s", ex.getClass()));
	return null;
}
 
Example 2
private boolean isValid(ComAdmin admin, UserGroupForm form, Popups popups) {
    int userGroupId = form.getId();

    if(!admin.permissionAllowed(Permission.ROLE_CHANGE)) {
        throw new PermissionDeniedDataAccessException("Missing permission to change rights", null);
    }

    String shortname = form.getShortname();
    if (StringUtils.isBlank(shortname)) {
        popups.field("shortname", "error.name.is.empty");
        return false;
    }

    if (StringUtils.length(shortname) < 3) {
        popups.field("shortname", "error.name.too.short");
        return false;
    }

    if (StringUtils.length(shortname) > 100) {
        popups.field("shortname", "error.username.tooLong");
        return false;
    }

    if(!userGroupService.isShortnameUnique(shortname, userGroupId, form.getCompanyId())) {
        popups.field("shortname", "error.usergroup.duplicate");
        return false;
    }

    return  true;
}
 
Example 3
@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
	if (ex instanceof SQLTransientException) {
		if (ex instanceof SQLTransientConnectionException) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTransactionRollbackException) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTimeoutException) {
			return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLNonTransientException) {
		if (ex instanceof SQLNonTransientConnectionException) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLDataException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLIntegrityConstraintViolationException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLInvalidAuthorizationSpecException) {
			return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLSyntaxErrorException) {
			return new BadSqlGrammarException(task, sql, ex);
		}
		else if (ex instanceof SQLFeatureNotSupportedException) {
			return new InvalidDataAccessApiUsageException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLRecoverableException) {
		return new RecoverableDataAccessException(buildMessage(task, sql, ex), ex);
	}

	// Fallback to Spring's own SQL state translation...
	return null;
}
 
Example 4
Source Project: dubbox   Source File: SolrExceptionTranslator.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public DataAccessException translateExceptionIfPossible(RuntimeException ex) {
	if (ex.getCause() instanceof SolrServerException) {
		SolrServerException solrServerException = (SolrServerException) ex.getCause();
		if (solrServerException.getCause() instanceof SolrException) {
			SolrException solrException = (SolrException) solrServerException.getCause();
			// solr 4.x moved ParseExecption from
			// org.apache.lucene.queryParser to
			// org.apache.lucene.queryparser.classic
			// therefore compare ShortClassName instead of using instanceof
			// expression
			if (solrException.getCause() != null && ClassUtils.getShortName(solrException.getCause().getClass())
					.equalsIgnoreCase("ParseException")) {
				return new InvalidDataAccessApiUsageException((solrException.getCause()).getMessage(),
						solrException.getCause());
			} else {
				ErrorCode errorCode = ErrorCode.getErrorCode(solrException.code());
				switch (errorCode) {
				case NOT_FOUND:
				case SERVICE_UNAVAILABLE:
				case SERVER_ERROR:
					return new DataAccessResourceFailureException(solrException.getMessage(), solrException);
				case FORBIDDEN:
				case UNAUTHORIZED:
					return new PermissionDeniedDataAccessException(solrException.getMessage(), solrException);
				case BAD_REQUEST:
					return new InvalidDataAccessApiUsageException(solrException.getMessage(), solrException);
				case UNKNOWN:
					return new UncategorizedSolrException(solrException.getMessage(), solrException);
				default:
					break;
				}
			}
		} else if (solrServerException.getCause() instanceof ConnectException) {
			return new DataAccessResourceFailureException(solrServerException.getCause().getMessage(),
					solrServerException.getCause());
		}
	}
	return null;
}
 
Example 5
@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
	if (ex instanceof SQLTransientException) {
		if (ex instanceof SQLTransientConnectionException) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTransactionRollbackException) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTimeoutException) {
			return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLNonTransientException) {
		if (ex instanceof SQLNonTransientConnectionException) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLDataException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLIntegrityConstraintViolationException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLInvalidAuthorizationSpecException) {
			return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLSyntaxErrorException) {
			return new BadSqlGrammarException(task, sql, ex);
		}
		else if (ex instanceof SQLFeatureNotSupportedException) {
			return new InvalidDataAccessApiUsageException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLRecoverableException) {
		return new RecoverableDataAccessException(buildMessage(task, sql, ex), ex);
	}

	// Fallback to Spring's own SQL state translation...
	return null;
}
 
Example 6
Source Project: kaif   Source File: AbstractRestExceptionHandler.java    License: Apache License 2.0 5 votes vote down vote up
@ExceptionHandler({ PermissionDeniedDataAccessException.class })
@ResponseBody
public ResponseEntity<E> handlePermissionDeniedDataAccessException(final PermissionDeniedDataAccessException ex,
    final WebRequest request) {
  final HttpStatus status = HttpStatus.UNAUTHORIZED;
  final E errorResponse = createErrorResponse(status,
      i18n(request, "rest-error.PermissionDeniedDataAccessException"));
  logException(ex, errorResponse, request);
  return new ResponseEntity<>(errorResponse, status);
}
 
Example 7
@Override
protected DataAccessException doTranslate(String task, String sql, SQLException ex) {
	if (ex instanceof SQLTransientException) {
		if (ex instanceof SQLTransientConnectionException) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTransactionRollbackException) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTimeoutException) {
			return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLNonTransientException) {
		if (ex instanceof SQLNonTransientConnectionException) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLDataException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLIntegrityConstraintViolationException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLInvalidAuthorizationSpecException) {
			return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLSyntaxErrorException) {
			return new BadSqlGrammarException(task, sql, ex);
		}
		else if (ex instanceof SQLFeatureNotSupportedException) {
			return new InvalidDataAccessApiUsageException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLRecoverableException) {
		return new RecoverableDataAccessException(buildMessage(task, sql, ex), ex);
	}

	// Fallback to Spring's own SQL state translation...
	return null;
}
 
Example 8
@Override
@Nullable
protected DataAccessException doTranslate(String task, @Nullable String sql, SQLException ex) {
	if (ex instanceof SQLTransientException) {
		if (ex instanceof SQLTransientConnectionException) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTransactionRollbackException) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTimeoutException) {
			return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLNonTransientException) {
		if (ex instanceof SQLNonTransientConnectionException) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLDataException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLIntegrityConstraintViolationException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLInvalidAuthorizationSpecException) {
			return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLSyntaxErrorException) {
			return new BadSqlGrammarException(task, (sql != null ? sql : ""), ex);
		}
		else if (ex instanceof SQLFeatureNotSupportedException) {
			return new InvalidDataAccessApiUsageException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLRecoverableException) {
		return new RecoverableDataAccessException(buildMessage(task, sql, ex), ex);
	}

	// Fallback to Spring's own SQL state translation...
	return null;
}
 
Example 9
@Test
public void errorCodeTranslation() {
	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);
	QueryTimeoutException tdarex2 = (QueryTimeoutException) 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());
}
 
Example 10
@Override
@Nullable
protected DataAccessException doTranslate(String task, @Nullable String sql, SQLException ex) {
	if (ex instanceof SQLTransientException) {
		if (ex instanceof SQLTransientConnectionException) {
			return new TransientDataAccessResourceException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTransactionRollbackException) {
			return new ConcurrencyFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLTimeoutException) {
			return new QueryTimeoutException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLNonTransientException) {
		if (ex instanceof SQLNonTransientConnectionException) {
			return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLDataException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLIntegrityConstraintViolationException) {
			return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLInvalidAuthorizationSpecException) {
			return new PermissionDeniedDataAccessException(buildMessage(task, sql, ex), ex);
		}
		else if (ex instanceof SQLSyntaxErrorException) {
			return new BadSqlGrammarException(task, (sql != null ? sql : ""), ex);
		}
		else if (ex instanceof SQLFeatureNotSupportedException) {
			return new InvalidDataAccessApiUsageException(buildMessage(task, sql, ex), ex);
		}
	}
	else if (ex instanceof SQLRecoverableException) {
		return new RecoverableDataAccessException(buildMessage(task, sql, ex), ex);
	}

	// Fallback to Spring's own SQL state translation...
	return null;
}
 
Example 11
@Test
public void errorCodeTranslation() {
	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);
	QueryTimeoutException tdarex2 = (QueryTimeoutException) 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());
}
 
Example 12
@Test
public void errorCodeTranslation() {
	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);
	QueryTimeoutException tdarex2 = (QueryTimeoutException) 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());
}
 
Example 13
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);
	QueryTimeoutException tdarex2 = (QueryTimeoutException) 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());
}