]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3895 Fix export
authorDavid Gageot <david@gageot.net>
Wed, 31 Oct 2012 15:45:58 +0000 (16:45 +0100)
committerDavid Gageot <david@gageot.net>
Wed, 31 Oct 2012 15:45:58 +0000 (16:45 +0100)
sonar-batch/src/main/java/org/sonar/batch/local/DryRunExporter.java
sonar-batch/src/test/java/org/sonar/batch/local/DryRunExporterTest.java

index 8926eecfb2840d3ad60e8ae13c8e8929f16edb30..3d25d1fb163b044ba1fa7814338e5ade8ba17607 100644 (file)
@@ -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 {
index 1fb06100ad3545e87d9851870e39734fc617f04c..35a5ce2f4bdd9395c3315502a9a696fd6e7261c9 100644 (file)
@@ -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();
+  }
 }