]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5840 It should be possible to specify a negative value on rule parameter of...
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 5 Nov 2014 16:45:13 +0000 (17:45 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 6 Nov 2014 07:35:16 +0000 (08:35 +0100)
server/sonar-server/src/main/java/org/sonar/server/util/IntegerTypeValidation.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/util/FloatTypeValidationTest.java
server/sonar-server/src/test/java/org/sonar/server/util/IntegerTypeValidationTest.java

index 31f87e6a0de800ed97c6a4c0ca5b980dc390a11c..67dd1690f14fa7880dfeb7bf78f9689cad5c43d8 100644 (file)
@@ -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<String> options) {
-    if (!NumberUtils.isDigits(value)) {
+    try {
+      Integer.parseInt(value);
+    } catch (NumberFormatException e) {
       throw new BadRequestException("errors.type.notInteger", value);
     }
   }
index c93870cf723f8a99e929dfa1a1d5fde484c4f83f..a9f7c6f8c528083308714327307551be17e5ef62 100644 (file)
@@ -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);
index 7c7b170475f146d4b54af74ddf30cdb79551e071..b5830388efb925aa43afb62effa53e5763636ff2 100644 (file)
@@ -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
index 43620979026aaa243e618ed5b1162462b41e3a5e..4ba7acf9eedb25208f2f4694693c3688471c5812 100644 (file)
@@ -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");
+    }
+  }
+
 }