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);
}
}
json.endObject()
- .endObject();
+ .endObject()
+ .flush();
} catch (IOException e) {
throw new SonarException("Unable to export results", e);
} finally {
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;
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;
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));
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\"}]}}");
}
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
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();
+ }
}