summaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2012-10-31 16:45:58 +0100
committerDavid Gageot <david@gageot.net>2012-10-31 16:45:58 +0100
commitd23c3e0fea299af829cf81066e97574e2276facd (patch)
treeca3d4d6f0644aa43acfabc9741f2e1cad0f02118 /sonar-batch
parent3cc3f64462d9e06baa8436023da0ee2546b0cb35 (diff)
downloadsonarqube-d23c3e0fea299af829cf81066e97574e2276facd.tar.gz
sonarqube-d23c3e0fea299af829cf81066e97574e2276facd.zip
SONAR-3895 Fix export
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java6
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java26
2 files changed, 27 insertions, 5 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 8926eecfb28..3d25d1fb163 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
@@ -76,9 +76,8 @@ public class DryRunExporter implements BatchComponent {
try {
output = new BufferedWriter(new FileWriter(exportFile));
writeJson(resources, output);
- output.flush();
} catch (IOException e) {
- throw new SonarException("Unable to write DryRun results in file " + exportFile.getAbsolutePath());
+ throw new SonarException("Unable to write DryRun results in file " + exportFile.getAbsolutePath(), e);
} finally {
Closeables.closeQuietly(output);
}
@@ -119,7 +118,8 @@ public class DryRunExporter implements BatchComponent {
}
json.endObject()
- .endObject();
+ .endObject()
+ .flush();
} catch (IOException e) {
throw new SonarException("Unable to export results", e);
} finally {
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 1fb06100ad3..35a5ce2f4bd 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
@@ -22,6 +22,7 @@ package org.sonar.batch.local;
import com.google.common.collect.ImmutableSet;
import org.junit.Before;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.platform.Server;
import org.sonar.api.resources.ProjectFileSystem;
@@ -33,6 +34,8 @@ import org.sonar.batch.bootstrap.DryRun;
import org.sonar.batch.index.DefaultIndex;
import org.sonar.java.api.JavaClass;
+import java.io.File;
+import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
@@ -54,6 +57,9 @@ public class DryRunExporterTest {
ProjectFileSystem projectFileSystem = mock(ProjectFileSystem.class);
Server server = mock(Server.class);
+ @org.junit.Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
@Before
public void setUp() {
dryRunExporter = spy(new DryRunExporter(dryRun, sonarIndex, projectFileSystem, server));
@@ -81,7 +87,8 @@ public class DryRunExporterTest {
dryRunExporter.writeJson(ImmutableSet.of(resource), output);
String json = output.toString();
- assertThat(json).isEqualTo(
+ assertThat(json)
+ .isEqualTo(
"{\"version\":\"3.4\",\"violations_per_resource\":{\"KEY\":[{\"line\":1,\"message\":\"VIOLATION\",\"severity\":\"INFO\",\"rule_key\":\"RULE_KEY\",\"rule_name\":\"RULE_NAME\"}]}}");
}
@@ -101,7 +108,7 @@ public class DryRunExporterTest {
String json = output.toString();
assertThat(json).isEqualTo(
- "{\"version\":\"3.4\",\"violations_per_resource\":{\"KEY\":[{\"message\":\"VIOLATION\",\"severity\":\"INFO\",\"rule_key\":\"RULE_KEY\",\"rule_name\":\"RULE_NAME\"}]}}");
+ "{\"version\":\"3.4\",\"violations_per_resource\":{\"KEY\":[{\"message\":\"VIOLATION\",\"severity\":\"INFO\",\"rule_key\":\"RULE_KEY\",\"rule_name\":\"RULE_NAME\"}]}}");
}
@Test
@@ -116,4 +123,19 @@ public class DryRunExporterTest {
assertThat(json).isEqualTo("{\"version\":\"3.4\",\"violations_per_resource\":{}}");
}
+
+ @Test
+ public void should_export_violations_to_file() throws IOException {
+ File sonarDirectory = temporaryFolder.newFolder("sonar");
+ when(dryRun.isEnabled()).thenReturn(true);
+ when(dryRun.isEnabled()).thenReturn(true);
+ when(server.getVersion()).thenReturn("3.4");
+ doReturn(Arrays.<Violation> asList()).when(dryRunExporter).getViolations(resource);
+ when(dryRun.getExportPath()).thenReturn("output.json");
+ when(projectFileSystem.getSonarWorkingDirectory()).thenReturn(sonarDirectory);
+
+ dryRunExporter.execute(sensorContext);
+
+ assertThat(new File(sonarDirectory, "output.json")).exists();
+ }
}