aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2015-04-02 18:13:52 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2015-04-07 17:21:33 +0200
commitb16c02760d933b45d519b2f5c1f4529aac574df5 (patch)
tree7730fc290bcf4f3fe978399d5bda328cf191f687 /sonar-batch
parenta9de737d0e2b6342c55a946665af99a35a7f2cb8 (diff)
downloadsonarqube-b16c02760d933b45d519b2f5c1f4529aac574df5.tar.gz
sonarqube-b16c02760d933b45d519b2f5c1f4529aac574df5.zip
SONAR-6277 Feed file sources in compute report
Diffstat (limited to 'sonar-batch')
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/report/SourcePublisher.java68
-rw-r--r--sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java1
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java1
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/report/SourcePublisherTest.java104
4 files changed, 173 insertions, 1 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/SourcePublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/SourcePublisher.java
new file mode 100644
index 00000000000..ccb91681e2b
--- /dev/null
+++ b/sonar-batch/src/main/java/org/sonar/batch/report/SourcePublisher.java
@@ -0,0 +1,68 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.report;
+
+import com.google.common.base.Charsets;
+import org.apache.commons.io.ByteOrderMark;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.input.BOMInputStream;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.batch.index.BatchResource;
+import org.sonar.batch.index.ResourceCache;
+import org.sonar.batch.protocol.output.BatchReportWriter;
+
+import java.io.*;
+
+public class SourcePublisher implements ReportPublisherStep {
+
+ private final ResourceCache resourceCache;
+
+ public SourcePublisher(ResourceCache resourceCache) {
+ this.resourceCache = resourceCache;
+ }
+
+ @Override
+ public void publish(BatchReportWriter writer) {
+ for (final BatchResource resource : resourceCache.all()) {
+ if (!resource.isFile()) {
+ continue;
+ }
+
+ DefaultInputFile inputFile = (DefaultInputFile) resource.inputPath();
+ File iofile = writer.getSourceFile(1);
+ int line = 0;
+ try (FileOutputStream output = new FileOutputStream(iofile); BOMInputStream bomIn = new BOMInputStream(new FileInputStream(inputFile.file()),
+ ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);
+ BufferedReader reader = new BufferedReader(new InputStreamReader(bomIn, inputFile.charset()))) {
+ String lineStr = reader.readLine();
+ while (lineStr != null) {
+ IOUtils.write(lineStr, output, Charsets.UTF_8);
+ line++;
+ if (line < inputFile.lines()) {
+ IOUtils.write("\n", output, Charsets.UTF_8);
+ }
+ lineStr = reader.readLine();
+ }
+ } catch (IOException e) {
+ throw new IllegalStateException("Unable to store file source in the report", e);
+ }
+ }
+ }
+}
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
index 43ba3500126..12b0bc0f21d 100644
--- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
+++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java
@@ -188,6 +188,7 @@ public class ProjectScanContainer extends ComponentContainer {
MeasuresPublisher.class,
DuplicationsPublisher.class,
CoveragePublisher.class,
+ SourcePublisher.class,
ScanTaskObservers.class);
}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java
index 2e1614bbdf1..347761203f7 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/report/CoveragePublisherTest.java
@@ -51,7 +51,6 @@ public class CoveragePublisherTest {
private MeasureCache measureCache;
private CoveragePublisher publisher;
- private org.sonar.api.resources.File aFile = org.sonar.api.resources.File.create("org/foo/Bar.java");
private org.sonar.api.resources.Resource sampleFile;
diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/SourcePublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/SourcePublisherTest.java
new file mode 100644
index 00000000000..824a6da2db9
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/report/SourcePublisherTest.java
@@ -0,0 +1,104 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * SonarQube is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.batch.report;
+
+import com.google.common.base.Charsets;
+import org.apache.commons.io.FileUtils;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.api.resources.Project;
+import org.sonar.batch.index.ResourceCache;
+import org.sonar.batch.protocol.output.BatchReportWriter;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Date;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class SourcePublisherTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ private SourcePublisher publisher;
+
+ private File sourceFile;
+
+ private BatchReportWriter writer;
+
+ @Before
+ public void prepare() throws IOException {
+ Project p = new Project("foo").setAnalysisDate(new Date(1234567L));
+ ResourceCache resourceCache = new ResourceCache();
+ org.sonar.api.resources.Resource sampleFile = org.sonar.api.resources.File.create("src/Foo.php").setEffectiveKey("foo:src/Foo.php");
+ resourceCache.add(p, null).setSnapshot(new Snapshot().setId(2));
+ File baseDir = temp.newFolder();
+ sourceFile = new File(baseDir, "src/Foo.php");
+ resourceCache.add(sampleFile, null).setInputPath(new DefaultInputFile("foo", "src/Foo.php").setLines(5).setModuleBaseDir(baseDir.toPath()).setCharset(Charsets.ISO_8859_1));
+ publisher = new SourcePublisher(resourceCache);
+ File outputDir = temp.newFolder();
+ writer = new BatchReportWriter(outputDir);
+ }
+
+ @Test
+ public void publishEmptySource() throws Exception {
+ FileUtils.write(sourceFile, "", Charsets.ISO_8859_1);
+
+ publisher.publish(writer);
+
+ File out = writer.getSourceFile(1);
+ assertThat(FileUtils.readFileToString(out, Charsets.UTF_8)).isEqualTo("");
+ }
+
+ @Test
+ public void publishSourceWithLastEmptyLine() throws Exception {
+ FileUtils.write(sourceFile, "1\n2\n3\n4\n", Charsets.ISO_8859_1);
+
+ publisher.publish(writer);
+
+ File out = writer.getSourceFile(1);
+ assertThat(FileUtils.readFileToString(out, Charsets.UTF_8)).isEqualTo("1\n2\n3\n4\n");
+ }
+
+ @Test
+ public void publishSourceWithLastLineNotEmpty() throws Exception {
+ FileUtils.write(sourceFile, "1\n2\n3\n4\n5", Charsets.ISO_8859_1);
+
+ publisher.publish(writer);
+
+ File out = writer.getSourceFile(1);
+ assertThat(FileUtils.readFileToString(out, Charsets.UTF_8)).isEqualTo("1\n2\n3\n4\n5");
+ }
+
+ @Test
+ public void cleanLineEnds() throws Exception {
+ FileUtils.write(sourceFile, "\n2\r\n3\n4\r5", Charsets.ISO_8859_1);
+
+ publisher.publish(writer);
+
+ File out = writer.getSourceFile(1);
+ assertThat(FileUtils.readFileToString(out, Charsets.UTF_8)).isEqualTo("\n2\n3\n4\n5");
+ }
+}