]> source.dussan.org Git - sonarqube.git/commitdiff
Fix NPE when using old Issuable API
authorJulien HENRY <julien.henry@sonarsource.com>
Thu, 23 Jul 2015 14:01:22 +0000 (16:01 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 23 Jul 2015 14:01:51 +0000 (16:01 +0200)
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/DeprecatedResourceApiSensor.java
sonar-batch/src/main/java/org/sonar/batch/issue/DeprecatedIssueBuilderWrapper.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java

index 99a6e39fe1ae7f11c00189a8e473b7be1ab55871..03a952fa2852458d50fa8cc37089adab4fd1d51f 100644 (file)
@@ -65,6 +65,13 @@ public class DeprecatedResourceApiSensor implements Sensor {
         .line(1)
         .build());
 
+      // Message and line are nullable
+      issuable.addIssue(issuable.newIssueBuilder()
+        .ruleKey(RuleKey.of(XooRulesDefinition.XOO_REPOSITORY, RULE_KEY))
+        .message(null)
+        .line(null)
+        .build());
+
       sonarFile = context.getResource(sonarFile);
       Directory parent = sonarFile.getParent();
       createIssueOnDir(parent);
index 99186f950069c37c2c710211d345bc0937072fd7..ae85a999274025d5a15a1aa0be69f0e5d685035f 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.batch.issue;
 
 import java.util.ArrayList;
 import java.util.List;
+import javax.annotation.Nullable;
 import org.sonar.api.batch.fs.InputDir;
 import org.sonar.api.batch.fs.InputFile;
 import org.sonar.api.batch.fs.TextRange;
@@ -54,9 +55,11 @@ public class DeprecatedIssueBuilderWrapper implements Issuable.IssueBuilder {
   }
 
   @Override
-  public IssueBuilder line(Integer line) {
+  public IssueBuilder line(@Nullable Integer line) {
     if (primaryComponent.isFile()) {
-      this.primaryRange = ((InputFile) primaryComponent.inputPath()).selectLine(line);
+      if (line != null) {
+        this.primaryRange = ((InputFile) primaryComponent.inputPath()).selectLine(line.intValue());
+      }
       return this;
     } else {
       throw new IllegalArgumentException("Unable to set line for issues on project or directory");
@@ -111,7 +114,10 @@ public class DeprecatedIssueBuilderWrapper implements Issuable.IssueBuilder {
   @Override
   public Issue build() {
     if (primaryMessage != null || primaryRange != null || locations.isEmpty()) {
-      NewIssueLocation newLocation = newIssue.newLocation().message(primaryMessage);
+      NewIssueLocation newLocation = newIssue.newLocation();
+      if (primaryMessage != null) {
+        newLocation.message(primaryMessage);
+      }
       if (primaryComponent.isProjectOrModule()) {
         newLocation.onProject();
       } else if (primaryComponent.isFile()) {
index 671b51375145d298f1fbbae0d6ad8397182c0d3c..8287a310bfb3cfc459ad45f1244983be544a50dc 100644 (file)
@@ -20,6 +20,8 @@
 package org.sonar.batch.mediumtest.deprecated;
 
 import com.google.common.collect.ImmutableMap;
+import java.io.File;
+import java.io.IOException;
 import org.apache.commons.io.FileUtils;
 import org.junit.After;
 import org.junit.Before;
@@ -30,10 +32,8 @@ import org.sonar.batch.mediumtest.TaskResult;
 import org.sonar.batch.protocol.input.ActiveRule;
 import org.sonar.xoo.XooPlugin;
 
-import java.io.File;
-import java.io.IOException;
-
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.groups.Tuple.tuple;
 
 public class DeprecatedApiMediumTest {
 
@@ -81,8 +81,14 @@ public class DeprecatedApiMediumTest {
         .build())
       .start();
 
-    // 1 issue on root dir + 1 issue on each file + 1 issue on each file parent dir
-    assertThat(result.issues()).hasSize(5);
+    assertThat(result.issues()).extracting("componentKey", "message", "line").containsOnly(
+      tuple("com.foo.project:src/sample.xoo", "One issue per line", null),
+      tuple("com.foo.project:src/sample.xoo", "Issue created using deprecated API", 1),
+      tuple("com.foo.project:src/package/sample.xoo", "One issue per line", null),
+      tuple("com.foo.project:src/package/sample.xoo", "Issue created using deprecated API", 1),
+      tuple("com.foo.project:src", "Issue created using deprecated API", null),
+      tuple("com.foo.project:src/package", "Issue created using deprecated API", null));
+
   }
 
 }