private final ActivityService log;
public RuleActivator(DbClient db, IndexClient index,
- RuleActivatorContextFactory contextFactory, TypeValidations typeValidations,
- PreviewCache previewCache, ActivityService log) {
+ RuleActivatorContextFactory contextFactory, TypeValidations typeValidations,
+ PreviewCache previewCache, ActivityService log) {
this.db = db;
this.index = index;
this.contextFactory = contextFactory;
context.defaultSeverity()));
for (RuleParamDto ruleParamDto : context.ruleParams()) {
String paramKey = ruleParamDto.getName();
- change.setParameter(paramKey, validateParam(ruleParamDto, firstNonNull(
- context.requestParamValue(request, paramKey),
- context.parentParamValue(paramKey),
- context.defaultParamValue(paramKey))));
+ change.setParameter(paramKey, validateParam(ruleParamDto,
+ firstNonNull(
+ context.requestParamValue(request, paramKey),
+ context.parentParamValue(paramKey),
+ context.defaultParamValue(paramKey))));
}
}
}
@CheckForNull
String firstNonNull(String... strings) {
for (String s : strings) {
- if (s!=null) {
+ if (s != null) {
return s;
}
}
ImmutableMap.of("max", "10"));
}
+ /**
+ * SONAR-5841
+ */
@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()));
+ public void activate_with_empty_parameter_having_no_default_value() throws Exception {
+ activate(new RuleActivation(RuleTesting.XOO_X1)
+ .setParameter("min", ""),
+ XOO_P1_KEY);
- activate(new RuleActivation(ruleKey).setParameter("max", "-10"), XOO_P1_KEY);
+ assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
+ verifyHasActiveRule(ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1), Severity.MINOR, null,
+ // Max should be set to default value, min has not value it should be ignored
+ ImmutableMap.of("max", "10"));
+ }
+
+ /**
+ * SONAR-5841
+ */
+ @Test
+ public void activate_with_empty_parameters() throws Exception {
+ activate(new RuleActivation(RuleTesting.XOO_X1)
+ .setParameters(ImmutableMap.of("max", "", "min", "")),
+ XOO_P1_KEY);
+
+ assertThat(countActiveRules(XOO_P1_KEY)).isEqualTo(1);
+ // Max should be set to default value, min has not value it should be ignored
+ verifyHasActiveRule(ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1), Severity.MINOR, null,
+ ImmutableMap.of("max", "10"));
+ }
+
+ /**
+ * SONAR-5840
+ */
+ @Test
+ public void activate_rule_with_negative_integer_value_on_parameter_having_no_default_value() throws Exception {
+ activate(new RuleActivation(RuleTesting.XOO_X1)
+ .setParameter("min", "-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"));
+ // Max should be set to default value, min should be set to -10
+ verifyHasActiveRule(ActiveRuleKey.of(XOO_P1_KEY, RuleTesting.XOO_X1), Severity.MINOR, null,
+ ImmutableMap.of("max", "10", "min", "-10"));
}
@Test
}
private void verifyOneActiveRule(String profileKey, RuleKey ruleKey, String expectedSeverity,
- @Nullable String expectedInheritance, Map<String, String> expectedParams) {
+ @Nullable String expectedInheritance, Map<String, String> expectedParams) {
assertThat(countActiveRules(profileKey)).isEqualTo(1);
verifyHasActiveRule(profileKey, ruleKey, expectedSeverity, expectedInheritance, expectedParams);
}
private void verifyHasActiveRule(String profileKey, RuleKey ruleKey, String expectedSeverity,
- @Nullable String expectedInheritance, Map<String, String> expectedParams) {
+ @Nullable String expectedInheritance, Map<String, String> expectedParams) {
verifyHasActiveRule(ActiveRuleKey.of(profileKey, ruleKey), expectedSeverity, expectedInheritance, expectedParams);
}
private void verifyHasActiveRule(ActiveRuleKey activeRuleKey, String expectedSeverity,
- @Nullable String expectedInheritance, Map<String, String> expectedParams) {
+ @Nullable String expectedInheritance, Map<String, String> expectedParams) {
// verify db
boolean found = false;
List<ActiveRuleDto> activeRuleDtos = db.activeRuleDao().findByProfileKey(dbSession, activeRuleKey.qProfile());
assertThat(level.hashCode()).isEqualTo(level.hashCode());
}
+ @Test
+ public void define_rule_parameter_with_empty_default_value() {
+ RulesDefinition.NewRepository newFindbugs = context.createRepository("findbugs", "java");
+ RulesDefinition.NewRule newNpe = newFindbugs.createRule("NPE").setName("NPE").setHtmlDescription("NPE");
+ newNpe.createParam("level").setDefaultValue("").setName("Level").setDescription("The level").setType(RuleParamType.INTEGER);
+ newFindbugs.done();
+
+ RulesDefinition.Rule rule = context.repository("findbugs").rule("NPE");
+ assertThat(rule.params()).hasSize(1);
+
+ RulesDefinition.Param level = rule.param("level");
+ assertThat(level.key()).isEqualTo("level");
+ assertThat(level.name()).isEqualTo("Level");
+ assertThat(level.description()).isEqualTo("The level");
+ // Empty value is converted in null value
+ assertThat(level.defaultValue()).isNull();
+ assertThat(level.type()).isEqualTo(RuleParamType.INTEGER);
+ }
+
@Test
public void sanitize_rule_name() {
RulesDefinition.NewRepository newFindbugs = context.createRepository("findbugs", "java");