From 9c3c913e1da39e25e6a49ed526d9a2740ccd7756 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 5 Nov 2014 17:45:13 +0100 Subject: [PATCH] SONAR-5840 It should be possible to specify a negative value on rule parameter of integer type --- .../sonar/server/util/IntegerTypeValidation.java | 5 +++-- .../qualityprofile/RuleActivatorMediumTest.java | 15 +++++++++++++++ .../server/util/FloatTypeValidationTest.java | 1 + .../server/util/IntegerTypeValidationTest.java | 15 ++++++++++++++- 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/util/IntegerTypeValidation.java b/server/sonar-server/src/main/java/org/sonar/server/util/IntegerTypeValidation.java index 31f87e6a0de..67dd1690f14 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/util/IntegerTypeValidation.java +++ b/server/sonar-server/src/main/java/org/sonar/server/util/IntegerTypeValidation.java @@ -20,7 +20,6 @@ package org.sonar.server.util; -import org.apache.commons.lang.math.NumberUtils; import org.sonar.api.PropertyType; import org.sonar.server.exceptions.BadRequestException; @@ -35,7 +34,9 @@ public class IntegerTypeValidation implements TypeValidation { @Override public void validate(String value, List options) { - if (!NumberUtils.isDigits(value)) { + try { + Integer.parseInt(value); + } catch (NumberFormatException e) { throw new BadRequestException("errors.type.notInteger", value); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java index c93870cf723..a9f7c6f8c52 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java @@ -157,6 +157,21 @@ public class RuleActivatorMediumTest { ImmutableMap.of("max", "10")); } + @Test + public void activate_rule_with_negative_integer_value_on_parameter_without_default_value() throws Exception { + RuleKey ruleKey = RuleKey.of("negative", "value"); + RuleDto rule = RuleTesting.newDto(ruleKey).setLanguage("xoo").setSeverity(Severity.MINOR); + db.ruleDao().insert(dbSession, rule); + db.ruleDao().addRuleParam(dbSession, rule, RuleParamDto.createFor(rule) + .setName("max").setType(RuleParamType.INTEGER.type())); + + activate(new RuleActivation(ruleKey).setParameter("max", "-10"), XOO_P1_KEY); + + assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1); + verifyHasActiveRule(ActiveRuleKey.of(XOO_P1_KEY, ruleKey), Severity.MINOR, null, + ImmutableMap.of("max", "-10")); + } + @Test public void activation_ignores_unsupported_parameters() throws Exception { RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1); diff --git a/server/sonar-server/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java b/server/sonar-server/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java index 7c7b170475f..b5830388efb 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java @@ -45,6 +45,7 @@ public class FloatTypeValidationTest { public void not_fail_on_valid_float() { validation.validate("10.2", null); validation.validate("10", null); + validation.validate("-10.3", null); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java b/server/sonar-server/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java index 43620979026..4ba7acf9eed 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java @@ -44,10 +44,11 @@ public class IntegerTypeValidationTest { @Test public void not_fail_on_valid_integer() { validation.validate("10", null); + validation.validate("-10", null); } @Test - public void fail_on_invalid_integer() { + public void fail_on_string() { try { validation.validate("abc", null); fail(); @@ -58,4 +59,16 @@ public class IntegerTypeValidationTest { } } + @Test + public void fail_on_float() { + try { + validation.validate("10.1", null); + fail(); + } catch (Exception e) { + assertThat(e).isInstanceOf(BadRequestException.class); + BadRequestException badRequestException = (BadRequestException) e; + assertThat(badRequestException.firstError().getParams()[0]).isEqualTo("10.1"); + } + } + } -- 2.39.5