package org.sonar.server.util;
-import org.apache.commons.lang.math.NumberUtils;
import org.sonar.api.PropertyType;
import org.sonar.server.exceptions.BadRequestException;
@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);
}
}
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);
public void not_fail_on_valid_float() {
validation.validate("10.2", null);
validation.validate("10", null);
+ validation.validate("-10.3", null);
}
@Test
@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();
}
}
+ @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");
+ }
+ }
+
}