From 7f639c575c9a4f7a6793fdcd2ff24086e8b0dbd5 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 1 Mar 2017 11:20:25 +0100 Subject: [PATCH] SONAR-8460 Remove check from Errors --- .../org/sonar/server/exceptions/Errors.java | 7 ------- .../QualityGateConditionsUpdater.java | 19 +++++++++++++------ .../qualitygate/QualityGateUpdater.java | 4 +++- .../server/qualitygate/QualityGates.java | 4 +++- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/exceptions/Errors.java b/server/sonar-server/src/main/java/org/sonar/server/exceptions/Errors.java index 6ff48495684..85f91f986be 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/exceptions/Errors.java +++ b/server/sonar-server/src/main/java/org/sonar/server/exceptions/Errors.java @@ -58,13 +58,6 @@ public class Errors { return messages.isEmpty(); } - public boolean check(boolean expression, String l10nKey, Object... l10nParams) { - if (!expression) { - add(Message.of(l10nKey, l10nParams)); - } - return expression; - } - @Override public String toString() { return MoreObjects.toStringHelper(this) diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateConditionsUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateConditionsUpdater.java index f2e08156c0b..b06cc208c41 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateConditionsUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateConditionsUpdater.java @@ -141,7 +141,7 @@ public class QualityGateConditionsUpdater { } private static void validateMetric(MetricDto metric, Errors errors) { - errors.check(isAlertable(metric), format("Metric '%s' cannot be used to define a condition.", metric.getKey())); + check(errors, isAlertable(metric), format("Metric '%s' cannot be used to define a condition.", metric.getKey())); } private static boolean isAlertable(MetricDto metric) { @@ -154,18 +154,18 @@ public class QualityGateConditionsUpdater { private static void checkOperator(MetricDto metric, String operator, Errors errors) { ValueType valueType = valueOf(metric.getValueType()); - errors.check(isOperatorAllowed(operator, valueType), format("Operator %s is not allowed for metric type %s.", operator, metric.getValueType())); + check(errors, isOperatorAllowed(operator, valueType), format("Operator %s is not allowed for metric type %s.", operator, metric.getValueType())); } private static void checkThresholds(@Nullable String warningThreshold, @Nullable String errorThreshold, Errors errors) { - errors.check(warningThreshold != null || errorThreshold != null, "At least one threshold (warning, error) must be set."); + check(errors, warningThreshold != null || errorThreshold != null, "At least one threshold (warning, error) must be set."); } private static void checkPeriod(MetricDto metric, @Nullable Integer period, Errors errors) { if (period == null) { - errors.check(!metric.getKey().startsWith("new_"), "A period must be selected for differential metrics."); + check(errors, !metric.getKey().startsWith("new_"), "A period must be selected for differential metrics."); } else { - errors.check(period == 1, "The only valid quality gate period is 1, the leak period."); + check(errors, period == 1, "The only valid quality gate period is 1, the leak period."); } } @@ -210,7 +210,7 @@ public class QualityGateConditionsUpdater { } private static void checkRatingGreaterThanOperator(@Nullable String value, Errors errors) { - errors.check(isNullOrEmpty(value) || !Objects.equals(toRating(value), E), format("There's no worse rating than E (%s)", value)); + check(errors, isNullOrEmpty(value) || !Objects.equals(toRating(value), E), format("There's no worse rating than E (%s)", value)); } private static Rating toRating(String value) { @@ -220,4 +220,11 @@ public class QualityGateConditionsUpdater { private static boolean isValidRating(@Nullable String value) { return isNullOrEmpty(value) || RATING_VALID_INT_VALUES.contains(value); } + + private static boolean check(Errors errors, boolean expression, String message) { + if (!expression) { + errors.add(Message.of(message)); + } + return expression; + } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateUpdater.java index 6726ff563ca..645cd0f61f1 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateUpdater.java @@ -60,6 +60,8 @@ public class QualityGateUpdater { private void checkQualityGateDoesNotAlreadyExist(@Nullable Long qGateId, String name, Errors errors) { QualityGateDto existingQgate = dbClient.qualityGateDao().selectByName(name); boolean isModifyingCurrentQgate = qGateId != null && existingQgate != null && existingQgate.getId().equals(qGateId); - errors.check(isModifyingCurrentQgate || existingQgate == null, Validation.IS_ALREADY_USED_MESSAGE, "Name"); + if (!isModifyingCurrentQgate && existingQgate != null) { + errors.add(Message.of(Validation.IS_ALREADY_USED_MESSAGE, "Name")); + } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java index f260216f69c..506ab073ac8 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java @@ -240,7 +240,9 @@ public class QualityGates { private void checkQgateNotAlreadyExists(@Nullable Long updatingQgateId, String name, Errors errors) { QualityGateDto existingQgate = dao.selectByName(name); boolean isModifyingCurrentQgate = updatingQgateId != null && existingQgate != null && existingQgate.getId().equals(updatingQgateId); - errors.check(isModifyingCurrentQgate || existingQgate == null, Validation.IS_ALREADY_USED_MESSAGE, "Name"); + if (!isModifyingCurrentQgate && existingQgate != null) { + errors.add(Message.of(Validation.IS_ALREADY_USED_MESSAGE, "Name")); + } } private void checkIsSystemAdministrator() { -- 2.39.5