@@ -31,16 +31,14 @@ public class ActiveRule { | |||
private final RuleKey ruleKey; | |||
private final String severity; | |||
private final Map<String, String> params; | |||
private final long createdAt; | |||
private final String pluginKey; | |||
private final long updatedAt; | |||
public ActiveRule(RuleKey ruleKey, String severity, Map<String, String> params, long createdAt, long updatedAt, @Nullable String pluginKey) { | |||
public ActiveRule(RuleKey ruleKey, String severity, Map<String, String> params, long updatedAt, @Nullable String pluginKey) { | |||
this.ruleKey = ruleKey; | |||
this.severity = severity; | |||
this.pluginKey = pluginKey; | |||
this.params = ImmutableMap.copyOf(params); | |||
this.createdAt = createdAt; | |||
this.updatedAt = updatedAt; | |||
} | |||
@@ -56,10 +54,6 @@ public class ActiveRule { | |||
return params; | |||
} | |||
public long getCreatedAt() { | |||
return createdAt; | |||
} | |||
public long getUpdatedAt() { | |||
return updatedAt; | |||
} |
@@ -72,6 +72,7 @@ public class LoadQualityProfilesStep implements ComputationStep { | |||
private static ActiveRule convert(ScannerReport.ActiveRule input, Rule rule) { | |||
RuleKey key = RuleKey.of(input.getRuleRepository(), input.getRuleKey()); | |||
Map<String, String> params = new HashMap<>(input.getParamsByKeyMap()); | |||
return new ActiveRule(key, input.getSeverity().name(), params, input.getCreatedAt(), input.getUpdatedAt(), rule.getPluginKey()); | |||
long updatedAt = input.getUpdatedAt(); | |||
return new ActiveRule(key, input.getSeverity().name(), params, updatedAt == 0 ? input.getCreatedAt() : updatedAt, rule.getPluginKey()); | |||
} | |||
} |
@@ -110,7 +110,7 @@ public class IssueCreationDateCalculatorTest { | |||
makeIssueNew(); | |||
noScm(); | |||
setRuleCreatedAt(2800L); | |||
setRuleUpdatedAt(2800L); | |||
run(); | |||
@@ -125,7 +125,7 @@ public class IssueCreationDateCalculatorTest { | |||
makeIssueNew(); | |||
configure.accept(issue, createMockScmInfo()); | |||
setRuleCreatedAt(1500L); | |||
setRuleUpdatedAt(1500L); | |||
rulePlugin("customjava"); | |||
pluginUpdatedAt("customjava", "java", 1700L); | |||
pluginUpdatedAt("java", 1700L); | |||
@@ -143,7 +143,7 @@ public class IssueCreationDateCalculatorTest { | |||
makeIssueNew(); | |||
configure.accept(issue, createMockScmInfo()); | |||
setRuleCreatedAt(1500L); | |||
setRuleUpdatedAt(1500L); | |||
rulePlugin("java"); | |||
pluginUpdatedAt("java", 1700L); | |||
@@ -160,7 +160,7 @@ public class IssueCreationDateCalculatorTest { | |||
makeIssueNotNew(); | |||
configure.accept(issue, createMockScmInfo()); | |||
setRuleCreatedAt(2800L); | |||
setRuleUpdatedAt(2800L); | |||
run(); | |||
@@ -201,7 +201,7 @@ public class IssueCreationDateCalculatorTest { | |||
makeIssueNew(); | |||
configure.accept(issue, createMockScmInfo()); | |||
setRuleCreatedAt(2800L); | |||
setRuleUpdatedAt(2800L); | |||
run(); | |||
@@ -216,7 +216,6 @@ public class IssueCreationDateCalculatorTest { | |||
makeIssueNew(); | |||
configure.accept(issue, createMockScmInfo()); | |||
setRuleCreatedAt(1200L); | |||
setRuleUpdatedAt(2800L); | |||
run(); | |||
@@ -260,7 +259,7 @@ public class IssueCreationDateCalculatorTest { | |||
makeIssueNew(); | |||
configure.accept(issue, createMockScmInfo()); | |||
setRuleCreatedAt(1500L); | |||
setRuleUpdatedAt(1500L); | |||
rulePlugin("java"); | |||
pluginUpdatedAt("java", 2500L); | |||
@@ -277,7 +276,7 @@ public class IssueCreationDateCalculatorTest { | |||
makeIssueNew(); | |||
configure.accept(issue, createMockScmInfo()); | |||
setRuleCreatedAt(1500L); | |||
setRuleUpdatedAt(1500L); | |||
rulePlugin("customjava"); | |||
pluginUpdatedAt("customjava", "java", 1000L); | |||
pluginUpdatedAt("java", 2500L); | |||
@@ -432,11 +431,6 @@ public class IssueCreationDateCalculatorTest { | |||
return scmInfo; | |||
} | |||
private void setRuleCreatedAt(long createdAt) { | |||
when(activeRule.getCreatedAt()).thenReturn(createdAt); | |||
when(activeRule.getUpdatedAt()).thenReturn(createdAt); | |||
} | |||
private void setRuleUpdatedAt(long updateAt) { | |||
when(activeRule.getUpdatedAt()).thenReturn(updateAt); | |||
} |
@@ -367,6 +367,6 @@ public class TrackerRawInputFactoryTest { | |||
} | |||
private void markRuleAsActive(RuleKey ruleKey) { | |||
activeRulesHolder.put(new ActiveRule(ruleKey, Severity.CRITICAL, emptyMap(), 1_000L, 1_000L, null)); | |||
activeRulesHolder.put(new ActiveRule(ruleKey, Severity.CRITICAL, emptyMap(), 1_000L, null)); | |||
} | |||
} |
@@ -77,7 +77,7 @@ public class CommentDensityRuleTest { | |||
@Test | |||
public void no_issues_if_enough_comments() { | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, ImmutableMap.of(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY, "25"), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, ImmutableMap.of(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY, "25"), 1_000L, PLUGIN_KEY)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_DENSITY_KEY, Measure.newMeasureBuilder().create(90.0, 1)); | |||
DefaultIssue issue = underTest.processFile(FILE, PLUGIN_KEY); | |||
@@ -135,7 +135,7 @@ public class CommentDensityRuleTest { | |||
} | |||
private void prepareForIssue(String minDensity, ReportComponent file, double commentLineDensity, int commentLines, int ncloc) { | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, ImmutableMap.of(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY, minDensity), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, ImmutableMap.of(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY, minDensity), 1_000L, PLUGIN_KEY)); | |||
measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_DENSITY_KEY, Measure.newMeasureBuilder().create(commentLineDensity, 1)); | |||
measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.COMMENT_LINES_KEY, Measure.newMeasureBuilder().create(commentLines)); | |||
measureRepository.addRawMeasure(file.getReportAttributes().getRef(), CoreMetrics.NCLOC_KEY, Measure.newMeasureBuilder().create(ncloc)); |
@@ -38,7 +38,7 @@ public class CommonRuleTest { | |||
@Test | |||
public void test_getMinDensityParam() { | |||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "30.5"), 1_000L, 1_000L, PLUGIN_KEY); | |||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "30.5"), 1_000L, PLUGIN_KEY); | |||
double minDensity = CommonRule.getMinDensityParam(activeRule, "minDensity"); | |||
assertThat(minDensity).isEqualTo(30.5); | |||
@@ -49,7 +49,7 @@ public class CommonRuleTest { | |||
thrown.expect(IllegalStateException.class); | |||
thrown.expectMessage("Required parameter [minDensity] is missing on rule [xoo:x1]"); | |||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of(), 1_000L, 1_000L, PLUGIN_KEY); | |||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of(), 1_000L, PLUGIN_KEY); | |||
CommonRule.getMinDensityParam(activeRule, "minDensity"); | |||
} | |||
@@ -58,7 +58,7 @@ public class CommonRuleTest { | |||
thrown.expect(IllegalStateException.class); | |||
thrown.expectMessage("Minimum density of rule [xoo:x1] is incorrect. Got [-30.5] but must be between 0 and 100."); | |||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "-30.5"), 1_000L, 1_000L, PLUGIN_KEY); | |||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "-30.5"), 1_000L, PLUGIN_KEY); | |||
CommonRule.getMinDensityParam(activeRule, "minDensity"); | |||
} | |||
@@ -67,7 +67,7 @@ public class CommonRuleTest { | |||
thrown.expect(IllegalStateException.class); | |||
thrown.expectMessage("Minimum density of rule [xoo:x1] is incorrect. Got [305] but must be between 0 and 100."); | |||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "305"), 1_000L, 1_000L, PLUGIN_KEY); | |||
ActiveRule activeRule = new ActiveRule(RuleTesting.XOO_X1, Severity.MAJOR, ImmutableMap.of("minDensity", "305"), 1_000L, PLUGIN_KEY); | |||
CommonRule.getMinDensityParam(activeRule, "minDensity"); | |||
} | |||
} |
@@ -86,7 +86,7 @@ public abstract class CoverageRuleTest { | |||
@Test | |||
public void no_issue_if_enough_coverage() { | |||
activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, PLUGIN_KEY)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(90.0, 1)); | |||
DefaultIssue issue = underTest.processFile(FILE, "java"); | |||
@@ -96,7 +96,7 @@ public abstract class CoverageRuleTest { | |||
@Test | |||
public void issue_if_coverage_is_too_low() { | |||
activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, PLUGIN_KEY)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getCoverageMetricKey(), Measure.newMeasureBuilder().create(20.0, 1)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getUncoveredMetricKey(), Measure.newMeasureBuilder().create(40)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), getToCoverMetricKey(), Measure.newMeasureBuilder().create(50)); | |||
@@ -114,7 +114,7 @@ public abstract class CoverageRuleTest { | |||
@Test | |||
public void no_issue_if_coverage_is_not_set() { | |||
activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(getRuleKey(), Severity.CRITICAL, ImmutableMap.of(getMinPropertyKey(), "65"), 1_000L, PLUGIN_KEY)); | |||
DefaultIssue issue = underTest.processFile(FILE, "java"); | |||
@@ -66,7 +66,7 @@ public class DuplicatedBlockRuleTest { | |||
@Test | |||
public void no_issue_if_no_duplicated_blocks() { | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.DUPLICATED_BLOCKS_KEY, Measure.newMeasureBuilder().create(0)); | |||
DefaultIssue issue = underTest.processFile(FILE, "java"); | |||
@@ -76,7 +76,7 @@ public class DuplicatedBlockRuleTest { | |||
@Test | |||
public void issue_if_duplicated_blocks() { | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.DUPLICATED_BLOCKS_KEY, Measure.newMeasureBuilder().create(3)); | |||
DefaultIssue issue = underTest.processFile(FILE, "java"); |
@@ -67,7 +67,7 @@ public class SkippedTestRuleTest { | |||
@Test | |||
public void issue_if_skipped_tests() { | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.SKIPPED_TESTS_KEY, Measure.newMeasureBuilder().create(2)); | |||
DefaultIssue issue = underTest.processFile(FILE, "java"); | |||
@@ -80,7 +80,7 @@ public class SkippedTestRuleTest { | |||
@Test | |||
public void no_issues_if_zero_skipped_tests() { | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.SKIPPED_TESTS_KEY, Measure.newMeasureBuilder().create(0)); | |||
DefaultIssue issue = underTest.processFile(FILE, "java"); | |||
@@ -90,7 +90,7 @@ public class SkippedTestRuleTest { | |||
@Test | |||
public void no_issues_if_measure_is_absent() { | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); | |||
DefaultIssue issue = underTest.processFile(FILE, "java"); | |||
@@ -69,7 +69,7 @@ public class TestErrorRuleTest { | |||
@Test | |||
public void issue_if_errors_or_failures() { | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.TEST_ERRORS_KEY, Measure.newMeasureBuilder().create(2)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.TEST_FAILURES_KEY, Measure.newMeasureBuilder().create(1)); | |||
@@ -83,7 +83,7 @@ public class TestErrorRuleTest { | |||
@Test | |||
public void no_issues_if_zero_errors_and_failures() { | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.TEST_ERRORS_KEY, Measure.newMeasureBuilder().create(0)); | |||
measureRepository.addRawMeasure(FILE.getReportAttributes().getRef(), CoreMetrics.TEST_FAILURES_KEY, Measure.newMeasureBuilder().create(0)); | |||
@@ -94,7 +94,7 @@ public class TestErrorRuleTest { | |||
@Test | |||
public void no_issues_if_test_measures_are_absent() { | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, 1_000L, PLUGIN_KEY)); | |||
activeRuleHolder.put(new ActiveRule(RULE_KEY, Severity.CRITICAL, Collections.emptyMap(), 1_000L, PLUGIN_KEY)); | |||
DefaultIssue issue = underTest.processFile(FILE, "java"); | |||
@@ -52,7 +52,7 @@ public class ActiveRulesHolderImplTest { | |||
@Test | |||
public void get_active_rule() { | |||
underTest.set(asList(new ActiveRule(RULE_KEY, Severity.BLOCKER, Collections.emptyMap(), SOME_DATE, SOME_DATE, PLUGIN_KEY))); | |||
underTest.set(asList(new ActiveRule(RULE_KEY, Severity.BLOCKER, Collections.emptyMap(), SOME_DATE, PLUGIN_KEY))); | |||
Optional<ActiveRule> activeRule = underTest.get(RULE_KEY); | |||
assertThat(activeRule.isPresent()).isTrue(); | |||
@@ -65,7 +65,7 @@ public class ActiveRulesHolderImplTest { | |||
thrown.expect(IllegalStateException.class); | |||
thrown.expectMessage("Active rules have already been initialized"); | |||
underTest.set(asList(new ActiveRule(RULE_KEY, Severity.BLOCKER, Collections.emptyMap(), SOME_DATE, SOME_DATE, PLUGIN_KEY))); | |||
underTest.set(asList(new ActiveRule(RULE_KEY, Severity.BLOCKER, Collections.emptyMap(), SOME_DATE, PLUGIN_KEY))); | |||
underTest.set(Collections.emptyList()); | |||
} | |||
@@ -84,7 +84,7 @@ public class ActiveRulesHolderImplTest { | |||
thrown.expectMessage("Active rule must not be declared multiple times: squid:S001"); | |||
underTest.set(asList( | |||
new ActiveRule(RULE_KEY, Severity.BLOCKER, Collections.emptyMap(), SOME_DATE, SOME_DATE, PLUGIN_KEY), | |||
new ActiveRule(RULE_KEY, Severity.MAJOR, Collections.emptyMap(), SOME_DATE, SOME_DATE, PLUGIN_KEY))); | |||
new ActiveRule(RULE_KEY, Severity.BLOCKER, Collections.emptyMap(), SOME_DATE, PLUGIN_KEY), | |||
new ActiveRule(RULE_KEY, Severity.MAJOR, Collections.emptyMap(), SOME_DATE, PLUGIN_KEY))); | |||
} | |||
} |
@@ -28,7 +28,7 @@ import static java.util.Collections.emptyMap; | |||
public class AlwaysActiveRulesHolderImpl implements ActiveRulesHolder { | |||
@Override | |||
public Optional<ActiveRule> get(RuleKey ruleKey) { | |||
return Optional.of(new ActiveRule(ruleKey, Severity.MAJOR, emptyMap(), 1_000L, 1_000L, null)); | |||
return Optional.of(new ActiveRule(ruleKey, Severity.MAJOR, emptyMap(), 1_000L, null)); | |||
} | |||
} |
@@ -32,11 +32,9 @@ import org.sonar.ce.task.projectanalysis.qualityprofile.ActiveRulesHolderImpl; | |||
import org.sonar.ce.task.step.TestComputationStepContext; | |||
import org.sonar.scanner.protocol.Constants; | |||
import org.sonar.scanner.protocol.output.ScannerReport; | |||
import org.sonarqube.ws.Rules; | |||
import static java.util.Arrays.asList; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.assertj.core.api.Assertions.assertThatCode; | |||
import static org.sonar.db.rule.RuleTesting.XOO_X1; | |||
import static org.sonar.db.rule.RuleTesting.XOO_X2; | |||
@@ -78,14 +76,12 @@ public class LoadQualityProfilesStepTest { | |||
assertThat(ar1.getSeverity()).isEqualTo(Severity.BLOCKER); | |||
assertThat(ar1.getParams()).containsExactly(MapEntry.entry("p1", "v1")); | |||
assertThat(ar1.getPluginKey()).isEqualTo("xoo"); | |||
assertThat(ar1.getCreatedAt()).isEqualTo(1000L); | |||
assertThat(ar1.getUpdatedAt()).isEqualTo(1200L); | |||
ActiveRule ar2 = activeRulesHolder.get(XOO_X2).get(); | |||
assertThat(ar2.getSeverity()).isEqualTo(Severity.MAJOR); | |||
assertThat(ar2.getParams()).isEmpty(); | |||
assertThat(ar2.getPluginKey()).isEqualTo("xoo"); | |||
assertThat(ar1.getCreatedAt()).isEqualTo(1000L); | |||
assertThat(ar1.getUpdatedAt()).isEqualTo(1200L); | |||
} | |||