From 40c58462ac2260d53bc517f18236a1cdbcc4572b Mon Sep 17 00:00:00 2001 From: Benoit <43733395+benoit-sns@users.noreply.github.com> Date: Tue, 27 Nov 2018 12:29:15 +0000 Subject: SONAR-11514 Backdate issue when rule parameter is updated (#971) --- .../org/sonar/api/batch/rule/CheckFactoryTest.java | 55 +++++++++++--- .../rule/internal/ActiveRulesBuilderTest.java | 25 +++++-- .../api/batch/rule/internal/NewActiveRuleTest.java | 85 ++++++++++++++++++++++ .../sensor/internal/SensorContextTesterTest.java | 9 ++- 4 files changed, 157 insertions(+), 17 deletions(-) create mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/NewActiveRuleTest.java (limited to 'sonar-plugin-api/src/test/java') diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java index ab1b7a1ac8b..a67603a568e 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/CheckFactoryTest.java @@ -22,6 +22,7 @@ package org.sonar.api.batch.rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; +import org.sonar.api.batch.rule.internal.NewActiveRule; import org.sonar.api.rule.RuleKey; import org.sonar.api.utils.SonarException; @@ -46,7 +47,10 @@ public class CheckFactoryTest { @Test public void class_name_as_check_key() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithoutProperties"); - builder.create(ruleKey).activate(); + NewActiveRule rule = new NewActiveRule.Builder() + .setRuleKey(ruleKey) + .build(); + builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithoutProperties.class); @@ -60,7 +64,11 @@ public class CheckFactoryTest { @Test public void param_as_string_field() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty"); - builder.create(ruleKey).setParam("pattern", "foo").activate(); + NewActiveRule rule = new NewActiveRule.Builder() + .setRuleKey(ruleKey) + .setParam("pattern", "foo") + .build(); + builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithStringProperty.class); @@ -77,7 +85,11 @@ public class CheckFactoryTest { thrown.expectMessage("The field 'unknown' does not exist or is not annotated with @RuleProperty in the class org.sonar.api.batch.rule.CheckWithStringProperty"); RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty"); - builder.create(ruleKey).setParam("unknown", "foo").activate(); + NewActiveRule rule = new NewActiveRule.Builder() + .setRuleKey(ruleKey) + .setParam("unknown", "foo") + .build(); + builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); checkFactory.create("squid").addAnnotatedChecks(CheckWithStringProperty.class); @@ -86,7 +98,12 @@ public class CheckFactoryTest { @Test public void param_as_primitive_fields() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithPrimitiveProperties"); - builder.create(ruleKey).setParam("max", "300").setParam("ignore", "true").activate(); + NewActiveRule rule = new NewActiveRule.Builder() + .setRuleKey(ruleKey) + .setParam("max", "300") + .setParam("ignore", "true") + .build(); + builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithPrimitiveProperties.class); @@ -103,7 +120,11 @@ public class CheckFactoryTest { @Test public void param_as_inherited_field() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithPrimitiveProperties"); - builder.create(ruleKey).setParam("max", "300").activate(); + NewActiveRule rule = new NewActiveRule.Builder() + .setRuleKey(ruleKey) + .setParam("max", "300") + .build(); + builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithPrimitiveProperties.class); @@ -116,7 +137,11 @@ public class CheckFactoryTest { @Test public void use_template_rule_key() { RuleKey ruleKey = RuleKey.of("squid", "S0001_123"); - builder.create(ruleKey).setTemplateRuleKey("S0001").activate(); + NewActiveRule rule = new NewActiveRule.Builder() + .setRuleKey(ruleKey) + .setTemplateRuleKey("S0001") + .build(); + builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithKey.class); @@ -133,7 +158,11 @@ public class CheckFactoryTest { thrown.expect(SonarException.class); RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithUnsupportedPropertyType"); - builder.create(ruleKey).setParam("max", "300").activate(); + NewActiveRule rule = new NewActiveRule.Builder() + .setRuleKey(ruleKey) + .setParam("max", "300") + .build(); + builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); checkFactory.create("squid").addAnnotatedChecks(CheckWithUnsupportedPropertyType.class); @@ -142,7 +171,11 @@ public class CheckFactoryTest { @Test public void override_field_key() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithOverriddenPropertyKey"); - builder.create(ruleKey).setParam("maximum", "300").activate(); + NewActiveRule rule = new NewActiveRule.Builder() + .setRuleKey(ruleKey) + .setParam("maximum", "300") + .build(); + builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); Checks checks = checkFactory.create("squid").addAnnotatedChecks(CheckWithOverriddenPropertyKey.class); @@ -158,7 +191,11 @@ public class CheckFactoryTest { @Test public void checks_as_objects() { RuleKey ruleKey = RuleKey.of("squid", "org.sonar.api.batch.rule.CheckWithStringProperty"); - builder.create(ruleKey).setParam("pattern", "foo").activate(); + NewActiveRule rule = new NewActiveRule.Builder() + .setRuleKey(ruleKey) + .setParam("pattern", "foo") + .build(); + builder.addRule(rule); CheckFactory checkFactory = new CheckFactory(builder.build()); CheckWithStringProperty check = new CheckWithStringProperty(); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java index 29eca75ed17..3591f59f3af 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java @@ -42,16 +42,24 @@ public class ActiveRulesBuilderTest { @Test public void build_rules() { - ActiveRules activeRules = new ActiveRulesBuilder() - .create(RuleKey.of("squid", "S0001")) + NewActiveRule activeRule = new NewActiveRule.Builder() + .setRuleKey(RuleKey.of("squid", "S0001")) .setName("My Rule") .setSeverity(Severity.CRITICAL) .setInternalKey("__S0001__") .setParam("min", "20") - .activate() + .build(); + + ActiveRules activeRules = new ActiveRulesBuilder() + .addRule(activeRule) // most simple rule - .create(RuleKey.of("squid", "S0002")).activate() - .create(RuleKey.of("findbugs", "NPE")).setInternalKey(null).setSeverity(null).setParam("foo", null).activate() + .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("squid", "S0002")).build()) + .addRule(new NewActiveRule.Builder() + .setRuleKey(RuleKey.of("findbugs", "NPE")) + .setInternalKey(null) + .setSeverity(null) + .setParam("foo", null) + .build()) .build(); assertThat(activeRules.findAll()).hasSize(3); @@ -83,11 +91,14 @@ public class ActiveRulesBuilderTest { @Test public void fail_to_add_twice_the_same_rule() { ActiveRulesBuilder builder = new ActiveRulesBuilder(); - builder.create(RuleKey.of("squid", "S0001")).activate(); + NewActiveRule rule = new NewActiveRule.Builder() + .setRuleKey(RuleKey.of("squid", "S0001")) + .build(); + builder.addRule(rule); thrown.expect(IllegalStateException.class); thrown.expectMessage("Rule 'squid:S0001' is already activated"); - builder.create(RuleKey.of("squid", "S0001")).activate(); + builder.addRule(rule); } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/NewActiveRuleTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/NewActiveRuleTest.java new file mode 100644 index 00000000000..4ecbd6e6917 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/NewActiveRuleTest.java @@ -0,0 +1,85 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.batch.rule.internal; + +import com.google.common.collect.ImmutableMap; +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.rule.RuleKey; +import org.sonar.api.rule.Severity; + +import static org.assertj.core.api.Assertions.assertThat; + +public class NewActiveRuleTest { + + private NewActiveRule.Builder builder; + + @Before + public void setBuilder() { + builder = new NewActiveRule.Builder(); + } + + @Test + public void builder_should_set_every_param() { + NewActiveRule rule = builder + .setRuleKey(RuleKey.of("foo", "bar")) + .setName("name") + .setSeverity(Severity.CRITICAL) + .setParam("key", "value") + .setCreatedAt(1_000L) + .setUpdatedAt(1_000L) + .setInternalKey("internal_key") + .setLanguage("language") + .setTemplateRuleKey("templateRuleKey") + .build(); + + assertThat(rule.ruleKey).isEqualTo(RuleKey.of("foo", "bar")); + assertThat(rule.name).isEqualTo("name"); + assertThat(rule.severity).isEqualTo(Severity.CRITICAL); + assertThat(rule.params).isEqualTo(ImmutableMap.of("key", "value")); + assertThat(rule.createdAt).isEqualTo(1_000L); + assertThat(rule.updatedAt).isEqualTo(1_000L); + assertThat(rule.internalKey).isEqualTo("internal_key"); + assertThat(rule.language).isEqualTo("language"); + assertThat(rule.templateRuleKey).isEqualTo("templateRuleKey"); + } + + @Test + public void severity_should_have_default_value() { + NewActiveRule rule = builder.build(); + assertThat(rule.severity).isEqualTo(Severity.defaultSeverity()); + } + + @Test + public void params_should_be_empty_map_if_no_params() { + NewActiveRule rule = builder.build(); + assertThat(rule.params).isEqualTo(ImmutableMap.of()); + } + + @Test + public void set_param_remove_param_if_value_is_null() { + NewActiveRule rule = builder + .setParam("foo", "bar") + .setParam("removed", "value") + .setParam("removed", null) + .build(); + assertThat(rule.params).isEqualTo(ImmutableMap.of("foo", "bar")); + } +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java index a4d659e29f0..3a3aca0fbc7 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/internal/SensorContextTesterTest.java @@ -26,6 +26,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.mockito.Mockito; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultFileSystem; @@ -36,6 +37,7 @@ import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.rule.Severity; import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; +import org.sonar.api.batch.rule.internal.NewActiveRule; import org.sonar.api.batch.sensor.error.AnalysisError; import org.sonar.api.batch.sensor.error.NewAnalysisError; import org.sonar.api.batch.sensor.highlighting.TypeOfText; @@ -51,6 +53,8 @@ import org.sonar.api.rules.RuleType; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; import static org.assertj.core.data.MapEntry.entry; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class SensorContextTesterTest { @@ -79,7 +83,10 @@ public class SensorContextTesterTest { @Test public void testActiveRules() { - ActiveRules activeRules = new ActiveRulesBuilder().create(RuleKey.of("repo", "rule")).activate().build(); + NewActiveRule activeRule = new NewActiveRule.Builder() + .setRuleKey(RuleKey.of("foo", "bar")) + .build(); + ActiveRules activeRules = new ActiveRulesBuilder().addRule(activeRule).build(); tester.setActiveRules(activeRules); assertThat(tester.activeRules().findAll()).hasSize(1); } -- cgit v1.2.3