aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2012-10-31 12:08:38 +0100
committerDavid Gageot <david@gageot.net>2012-10-31 12:08:38 +0100
commit833cf2e19ec89cbc4a8f75d9a5129e359f11a7cd (patch)
treef0d01dffe91b7b4723ce676de82c9dfe8f0911f0 /sonar-batch
parent9eae08814e457dcc6049090202811201c4781946 (diff)
downloadsonarqube-833cf2e19ec89cbc4a8f75d9a5129e359f11a7cd.tar.gz
sonarqube-833cf2e19ec89cbc4a8f75d9a5129e359f11a7cd.zip
SONAR-3895 export violations with no line
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java16
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java18
2 files changed, 27 insertions, 7 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java b/sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
index d146d21379e..432228ef477 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
@@ -23,6 +23,7 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
+import com.google.common.collect.Maps;
import com.google.common.io.Closeables;
import com.google.common.io.Files;
import com.google.gson.Gson;
@@ -95,12 +96,15 @@ public class DryRunExporter implements BatchComponent {
for (Resource resource : resources) {
List<Map<String, Object>> resourceViolations = Lists.newArrayList();
for (Violation violation : getViolations(resource)) {
- resourceViolations.add(ImmutableMap.<String, Object> of(
- "line", violation.getLineId(),
- "message", violation.getMessage(),
- "severity", violation.getSeverity().name(),
- "rule_key", violation.getRule().getKey(),
- "rule_name", violation.getRule().getName()));
+ Map<String, Object> json = Maps.newLinkedHashMap();
+ if (null != violation.getLineId()) {
+ json.put("line", violation.getLineId());
+ }
+ json.put("message", violation.getMessage());
+ json.put("severity", violation.getSeverity().name());
+ json.put("rule_key", violation.getRule().getKey());
+ json.put("rule_name", violation.getRule().getName());
+ resourceViolations.add(json);
}
Map<String, Object> obj = ImmutableMap.of(
diff --git a/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java b/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java
index e6dfca20b37..d2dc09d5b82 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java
@@ -76,6 +76,22 @@ public class DryRunExporterTest {
String json = dryRunExporter.getResultsAsJson(ImmutableSet.of(resource));
assertThat(json).isEqualTo(
- "[{\"resource\":\"KEY\",\"violations\":[{\"line\":1,\"message\":\"VIOLATION\",\"severity\":\"INFO\",\"rule_key\":\"RULE_KEY\",\"rule_name\":\"RULE_NAME\"}]}]");
+ "[{\"resource\":\"KEY\",\"violations\":[{\"line\":1,\"message\":\"VIOLATION\",\"severity\":\"INFO\",\"rule_key\":\"RULE_KEY\",\"rule_name\":\"RULE_NAME\"}]}]");
+ }
+
+ @Test
+ public void should_export_violation_with_no_line() {
+ when(dryRun.isEnabled()).thenReturn(true);
+ when(violation.getResource()).thenReturn(resource);
+ when(violation.getLineId()).thenReturn(null);
+ when(violation.getMessage()).thenReturn("VIOLATION");
+ when(violation.getRule()).thenReturn(Rule.create("pmd", "RULE_KEY").setName("RULE_NAME"));
+ when(violation.getSeverity()).thenReturn(RulePriority.INFO);
+ doReturn(Arrays.asList(violation)).when(dryRunExporter).getViolations(resource);
+
+ String json = dryRunExporter.getResultsAsJson(ImmutableSet.of(resource));
+
+ assertThat(json).isEqualTo(
+ "[{\"resource\":\"KEY\",\"violations\":[{\"message\":\"VIOLATION\",\"severity\":\"INFO\",\"rule_key\":\"RULE_KEY\",\"rule_name\":\"RULE_NAME\"}]}]");
}
}