From: Julien HENRY Date: Wed, 8 Apr 2015 08:12:04 +0000 (+0200) Subject: Fix regression during local issue tracking on empty files X-Git-Tag: 5.2-RC1~2345 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5cfd5f674d24b304db1ded615ee371905593fd40;p=sonarqube.git Fix regression during local issue tracking on empty files --- diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/EmptyFileTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/EmptyFileTest.java new file mode 100644 index 00000000000..62ea53ce6ae --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/EmptyFileTest.java @@ -0,0 +1,90 @@ +/* + * 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.mediumtest.preview; + +import com.google.common.collect.ImmutableMap; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.CoreProperties; +import org.sonar.api.utils.log.LogTester; +import org.sonar.batch.mediumtest.BatchMediumTester; +import org.sonar.batch.mediumtest.TaskResult; +import org.sonar.batch.protocol.input.ActiveRule; +import org.sonar.xoo.XooPlugin; + +import java.io.File; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import static org.assertj.core.api.Assertions.assertThat; + +public class EmptyFileTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Rule + public LogTester logTester = new LogTester(); + + private static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); + + private static Long date(String date) { + try { + return sdf.parse(date).getTime(); + } catch (ParseException e) { + throw new IllegalStateException(e); + } + } + + public BatchMediumTester tester = BatchMediumTester.builder() + .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW)) + .registerPlugin("xoo", new XooPlugin()) + .addDefaultQProfile("xoo", "Sonar Way") + .activateRule(new ActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "my/internal/key", "xoo")) + .setPreviousAnalysisDate(new Date()) + .build(); + + @Before + public void prepare() { + tester.start(); + } + + @After + public void stop() { + tester.stop(); + } + + @Test + public void testIssueTrackingWithIssueOnEmptyFile() throws Exception { + File projectDir = new File(EmptyFileTest.class.getResource("/mediumtest/xoo/sample-with-empty-file").toURI()); + + TaskResult result = tester + .newScanTask(new File(projectDir, "sonar-project.properties")) + .property("sonar.xoo.internalKey", "my/internal/key") + .start(); + + assertThat(result.issues()).hasSize(10); + } + +} diff --git a/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-empty-file/sonar-project.properties b/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-empty-file/sonar-project.properties new file mode 100644 index 00000000000..58f27e81f61 --- /dev/null +++ b/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-empty-file/sonar-project.properties @@ -0,0 +1,5 @@ +sonar.projectKey=sample-with-empty-file +sonar.projectName=Sample With Empty +sonar.projectVersion=0.1-SNAPSHOT +sonar.sources=xources +sonar.language=xoo diff --git a/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-empty-file/xources/hello/Empty.xoo b/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-empty-file/xources/hello/Empty.xoo new file mode 100644 index 00000000000..e69de29bb2d diff --git a/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-empty-file/xources/hello/HelloJava.xoo b/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-empty-file/xources/hello/HelloJava.xoo new file mode 100644 index 00000000000..1d9c60d56b7 --- /dev/null +++ b/sonar-batch/src/test/resources/mediumtest/xoo/sample-with-empty-file/xources/hello/HelloJava.xoo @@ -0,0 +1,8 @@ +package hello; + +public class HelloJava { + + public static void main(String[] args) { + System.out.println("Hello"); + } +} \ No newline at end of file diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java index ea7b38ce9c7..cb5ea5660af 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java @@ -156,12 +156,19 @@ public class FileMetadata implements BatchComponent { private final MessageDigest lineMd5Digest = DigestUtils.getMd5Digest(); private final StringBuilder sb = new StringBuilder(); private final LineHashConsumer consumer; - private int line = 1; + private int line = 0; public LineHashComputer(LineHashConsumer consumer) { this.consumer = consumer; } + @Override + protected void handleAll(char c) { + if (this.line == 0) { + this.line = 1; + } + } + @Override protected void handleIgnoreEoL(char c) { if (!Character.isWhitespace(c)) { @@ -178,7 +185,9 @@ public class FileMetadata implements BatchComponent { @Override protected void eof() { - consumer.consume(line, sb.length() > 0 ? lineMd5Digest.digest(sb.toString().getBytes(Charsets.UTF_8)) : null); + if (this.line > 0) { + consumer.consume(line, sb.length() > 0 ? lineMd5Digest.digest(sb.toString().getBytes(Charsets.UTF_8)) : null); + } } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java index 93f3a8e57d7..9e06a3e9e5c 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java @@ -37,6 +37,7 @@ import java.nio.charset.Charset; import static org.apache.commons.codec.digest.DigestUtils.md5Hex; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; public class FileMetadataTest { @@ -223,6 +224,23 @@ public class FileMetadataTest { }); } + @Test + public void dont_fail_on_empty_file() throws Exception { + File tempFile = temp.newFile(); + FileUtils.write(tempFile, "", Charsets.UTF_8, true); + + DefaultInputFile f = new DefaultInputFile("foo", tempFile.getName()); + f.setModuleBaseDir(tempFile.getParentFile().toPath()); + f.setCharset(Charsets.UTF_8); + FileMetadata.computeLineHashesForIssueTracking(f, new LineHashConsumer() { + + @Override + public void consume(int lineIdx, @Nullable byte[] hash) { + fail("File is empty and should not report any line hash"); + } + }); + } + @Test public void should_throw_if_file_does_not_exist() throws Exception { File tempFolder = temp.newFolder();