]> source.dussan.org Git - sonarqube.git/commitdiff
Fix regression during local issue tracking on empty files
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 8 Apr 2015 08:12:04 +0000 (10:12 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 8 Apr 2015 08:18:12 +0000 (10:18 +0200)
sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/EmptyFileTest.java [new file with mode: 0644]
sonar-batch/src/test/resources/mediumtest/xoo/sample-with-empty-file/sonar-project.properties [new file with mode: 0644]
sonar-batch/src/test/resources/mediumtest/xoo/sample-with-empty-file/xources/hello/Empty.xoo [new file with mode: 0644]
sonar-batch/src/test/resources/mediumtest/xoo/sample-with-empty-file/xources/hello/HelloJava.xoo [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java

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 (file)
index 0000000..62ea53c
--- /dev/null
@@ -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 (file)
index 0000000..58f27e8
--- /dev/null
@@ -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 (file)
index 0000000..e69de29
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 (file)
index 0000000..1d9c60d
--- /dev/null
@@ -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
index ea7b38ce9c71453af247ef3affe481ad7a56c106..cb5ea5660af0c920fbab3d51ba0ecec45f9ae1d1 100644 (file)
@@ -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);
+      }
     }
 
   }
index 93f3a8e57d78b69a5d9acc23690615c5f77c5c23..9e06a3e9e5c88b4275c4a0213f5d9c57c1238622 100644 (file)
@@ -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();