From: David Gageot Date: Wed, 31 Oct 2012 15:45:58 +0000 (+0100) Subject: SONAR-3895 Fix export X-Git-Tag: 3.4~409 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=d23c3e0fea299af829cf81066e97574e2276facd;p=sonarqube.git SONAR-3895 Fix export --- 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. 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(); + } }