diff options
Diffstat (limited to 'sonar-scanner-engine')
-rw-r--r-- | sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java | 31 | ||||
-rw-r--r-- | sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java | 46 |
2 files changed, 75 insertions, 2 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java index cdd771266b5..e679d683df9 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java @@ -57,7 +57,11 @@ import org.sonar.api.batch.sensor.rule.AdHocRule; import org.sonar.api.batch.sensor.symbol.NewSymbolTable; import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable; import org.sonar.api.config.Configuration; +import org.sonar.api.issue.impact.Severity; +import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.measures.CoreMetrics; +import org.sonar.api.rules.CleanCodeAttribute; +import org.sonar.api.rules.RuleType; import org.sonar.api.utils.KeyValueFormat; import org.sonar.core.metric.ScannerMetrics; import org.sonar.core.util.CloseableIterator; @@ -257,11 +261,34 @@ public class DefaultSensorStorage implements SensorStorage { if (description != null) { builder.setDescription(description); } - builder.setSeverity(Constants.Severity.valueOf(adHocRule.severity().name())); - builder.setType(ScannerReport.IssueType.valueOf(adHocRule.type().name())); + + + org.sonar.api.batch.rule.Severity severity = adHocRule.severity(); + if (severity != null) { + builder.setSeverity(Constants.Severity.valueOf(severity.name())); + } + + RuleType type = adHocRule.type(); + if (type != null) { + builder.setType(ScannerReport.IssueType.valueOf(type.name())); + } + builder.addAllDefaultImpacts(mapImpacts(adHocRule.defaultImpacts())); + + CleanCodeAttribute cleanCodeAttribute = adHocRule.cleanCodeAttribute(); + if (cleanCodeAttribute != null) { + builder.setCleanCodeAttribute(cleanCodeAttribute.name()); + } writer.appendAdHocRule(builder.build()); } + private static List<ScannerReport.Impact> mapImpacts(Map<SoftwareQuality, Severity> impactsMap) { + return impactsMap.entrySet().stream() + .map(e -> ScannerReport.Impact.newBuilder() + .setSoftwareQuality(e.getKey().name()) + .setSeverity(e.getValue().name()).build()) + .collect(toList()); + } + @Override public void store(NewHighlighting newHighlighting) { DefaultHighlighting highlighting = (DefaultHighlighting) newHighlighting; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java index 10f0b9662e8..46cc94f03cf 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java @@ -23,6 +23,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import org.assertj.core.groups.Tuple; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -46,12 +47,19 @@ import org.sonar.api.batch.sensor.issue.internal.DefaultExternalIssue; import org.sonar.api.batch.sensor.issue.internal.DefaultIssue; import org.sonar.api.batch.sensor.issue.internal.DefaultIssueLocation; import org.sonar.api.batch.sensor.measure.internal.DefaultMeasure; +import org.sonar.api.batch.sensor.rule.internal.DefaultAdHocRule; import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable; import org.sonar.api.config.internal.MapSettings; +import org.sonar.api.issue.impact.Severity; +import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.measures.CoreMetrics; +import org.sonar.api.rules.CleanCodeAttribute; +import org.sonar.api.rules.RuleType; import org.sonar.core.metric.ScannerMetrics; +import org.sonar.core.util.CloseableIterator; import org.sonar.scanner.cpd.index.SonarCpdBlockIndex; import org.sonar.scanner.issue.IssuePublisher; +import org.sonar.scanner.protocol.Constants; import org.sonar.scanner.protocol.output.FileStructure; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReportReader; @@ -347,4 +355,42 @@ public class DefaultSensorStorageTest { assertThat(contextPropertiesCache.getAll()).containsOnly(entry("foo", "bar")); } + @Test + public void store_whenAdhocRuleIsSpecified_shouldWriteAdhocRuleToReport() throws IOException { + + underTest.store(new DefaultAdHocRule().ruleId("ruleId").engineId("engineId") + .name("name") + .addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH) + .addDefaultImpact(SoftwareQuality.RELIABILITY, Severity.MEDIUM) + .cleanCodeAttribute(CleanCodeAttribute.CLEAR) + .severity(org.sonar.api.batch.rule.Severity.MAJOR) + .type(RuleType.CODE_SMELL) + .description("description")); + + try (CloseableIterator<ScannerReport.AdHocRule> adhocRuleIt = reportReader.readAdHocRules()) { + ScannerReport.AdHocRule adhocRule = adhocRuleIt.next(); + assertThat(adhocRule).extracting(r -> r.getRuleId(), r -> r.getName(), r -> r.getSeverity(), r -> r.getType(), r -> r.getDescription()) + .containsExactlyInAnyOrder("ruleId", "name", Constants.Severity.MAJOR, ScannerReport.IssueType.CODE_SMELL, "description"); + assertThat(adhocRule.getDefaultImpactsList()).hasSize(2).extracting(i -> i.getSoftwareQuality(), i -> i.getSeverity()) + .containsExactlyInAnyOrder( + Tuple.tuple(SoftwareQuality.MAINTAINABILITY.name(), Severity.HIGH.name()), + Tuple.tuple(SoftwareQuality.RELIABILITY.name(), Severity.MEDIUM.name())); + assertThat(adhocRule.getCleanCodeAttribute()) + .isEqualTo(CleanCodeAttribute.CLEAR.name()); + } + } + + @Test + public void store_whenAdhocRuleIsSpecifiedWithOptionalFieldEmpty_shouldWriteAdhocRuleToReport() throws IOException { + underTest.store(new DefaultAdHocRule().ruleId("ruleId").engineId("engineId") + .name("name") + .description("description")); + try (CloseableIterator<ScannerReport.AdHocRule> adhocRuleIt = reportReader.readAdHocRules()) { + ScannerReport.AdHocRule adhocRule = adhocRuleIt.next(); + assertThat(adhocRule).extracting(r -> r.getSeverity(), r -> r.getType()) + .containsExactlyInAnyOrder(Constants.Severity.UNSET_SEVERITY, ScannerReport.IssueType.UNSET); + assertThat(adhocRule.getDefaultImpactsList()).isEmpty(); + assertThat(adhocRule.getCleanCodeAttribute()).isNullOrEmpty(); + } + } } |