]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6847 Fix NPE when creating issues on root directory 569/head
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 5 Oct 2015 16:02:31 +0000 (18:02 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Mon, 5 Oct 2015 16:44:44 +0000 (18:44 +0200)
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/DeprecatedResourceApiSensor.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesOnDirMediumTest.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java

index 03a952fa2852458d50fa8cc37089adab4fd1d51f..123eb2130de2212e9be54118eaf315bce765b629 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.api.scan.filesystem.ModuleFileSystem;
 import org.sonar.api.scan.filesystem.PathResolver;
 import org.sonar.xoo.Xoo;
 
+@SuppressWarnings("deprecation")
 public class DeprecatedResourceApiSensor implements Sensor {
 
   public static final String RULE_KEY = "DeprecatedResourceApi";
index 7e8cdbcaf95f4ea0750ca4995d8857f29101db2e..11c49c564c84e92dab24d19e5ab4138f4c8d838f 100644 (file)
@@ -95,4 +95,40 @@ public class DeprecatedApiMediumTest {
 
   }
 
+  @Test
+  public void createIssueOnRootDir() throws IOException {
+
+    File baseDir = temp.getRoot();
+
+    File xooFileInRootDir = new File(baseDir, "sample.xoo");
+    FileUtils.write(xooFileInRootDir, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
+
+    File xooFileInAnotherDir = new File(baseDir, "package/sample.xoo");
+    FileUtils.write(xooFileInAnotherDir, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
+
+    TaskResult result = tester.newTask()
+      .properties(ImmutableMap.<String, String>builder()
+        .put("sonar.task", "scan")
+        .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
+        .put("sonar.projectKey", "com.foo.project")
+        .put("sonar.projectName", "Foo Project")
+        .put("sonar.projectVersion", "1.0-SNAPSHOT")
+        .put("sonar.projectDescription", "Description of Foo Project")
+        .put("sonar.sources", ".")
+        .build())
+      .start();
+
+    assertThat(result.issuesFor(result.inputFile("sample.xoo"))).extracting("msg", "line").containsOnly(
+      tuple("Issue created using deprecated API", 0),
+      tuple("Issue created using deprecated API", 1));
+    assertThat(result.issuesFor(result.inputFile("package/sample.xoo"))).extracting("msg", "line").containsOnly(
+      tuple("Issue created using deprecated API", 0),
+      tuple("Issue created using deprecated API", 1));
+    assertThat(result.issuesFor(result.inputDir(""))).extracting("msg", "line").containsOnly(
+      tuple("Issue created using deprecated API", 0));
+    assertThat(result.issuesFor(result.inputDir("package"))).extracting("msg", "line").containsOnly(
+      tuple("Issue created using deprecated API", 0));
+
+  }
+
 }
index 4fad08a8c99967d82618a16e33f0999c25fc88ad..4280af482f4a66d8b111ed13c72aa98848eb96ad 100644 (file)
@@ -84,4 +84,30 @@ public class IssuesOnDirMediumTest {
     assertThat(result.issuesFor(result.inputDir("src"))).hasSize(2);
   }
 
+  @Test
+  public void issueOnRootFolder() throws IOException {
+
+    File baseDir = temp.getRoot();
+
+    File xooFile1 = new File(baseDir, "sample1.xoo");
+    FileUtils.write(xooFile1, "Sample1 xoo\ncontent");
+
+    File xooFile2 = new File(baseDir, "sample2.xoo");
+    FileUtils.write(xooFile2, "Sample2 xoo\ncontent");
+
+    TaskResult result = tester.newTask()
+      .properties(ImmutableMap.<String, String>builder()
+        .put("sonar.task", "scan")
+        .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
+        .put("sonar.projectKey", "com.foo.project")
+        .put("sonar.projectName", "Foo Project")
+        .put("sonar.projectVersion", "1.0-SNAPSHOT")
+        .put("sonar.projectDescription", "Description of Foo Project")
+        .put("sonar.sources", ".")
+        .build())
+      .start();
+
+    assertThat(result.issuesFor(result.inputDir(""))).hasSize(2);
+  }
+
 }
index 5e1ebb651c238756846443e0c9dba3e30dde5d65..c788f55ebd9670f4c4f2758ee6bb0ed5b73be8bc 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.api.batch.fs.internal;
 
 import java.io.File;
 import java.nio.file.Path;
+import org.apache.commons.lang.StringUtils;
 import org.sonar.api.batch.fs.InputDir;
 import org.sonar.api.utils.PathUtils;
 
@@ -67,7 +68,13 @@ public class DefaultInputDir extends DefaultInputComponent implements InputDir {
 
   @Override
   public String key() {
-    return new StringBuilder().append(moduleKey).append(":").append(relativePath).toString();
+    StringBuilder sb = new StringBuilder().append(moduleKey).append(":");
+    if (StringUtils.isEmpty(relativePath)) {
+      sb.append("/");
+    } else {
+      sb.append(relativePath);
+    }
+    return sb.toString();
   }
 
   /**
index fe72745346e3bc41537e75ba459247a794b318b4..21e810aec942dc62cfeb8eae9d0d8c78e3c82171 100644 (file)
  */
 package org.sonar.api.resources;
 
+import javax.annotation.CheckForNull;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import org.sonar.api.batch.fs.FileSystem;
 import org.sonar.api.scan.filesystem.PathResolver;
 import org.sonar.api.utils.WildcardPattern;
 
-import javax.annotation.CheckForNull;
-
 /**
  * @since 1.10
  */
@@ -140,8 +139,8 @@ public class Directory extends Resource {
   public static Directory create(String relativePathFromBaseDir) {
     Directory d = new Directory();
     String normalizedPath = normalize(relativePathFromBaseDir);
-    d.setKey(normalizedPath);
-    d.setPath(normalizedPath);
+    d.setKey(normalizedPath == null ? SEPARATOR : normalizedPath);
+    d.setPath(normalizedPath == null ? "" : normalizedPath);
     return d;
   }