From 59e1f8eca37d7a37e0837e437ab3e6eb38a7dfea Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 5 Oct 2015 18:02:31 +0200 Subject: [PATCH] SONAR-6847 Fix NPE when creating issues on root directory --- .../xoo/rule/DeprecatedResourceApiSensor.java | 1 + .../deprecated/DeprecatedApiMediumTest.java | 36 +++++++++++++++++++ .../issues/IssuesOnDirMediumTest.java | 26 ++++++++++++++ .../batch/fs/internal/DefaultInputDir.java | 9 ++++- .../org/sonar/api/resources/Directory.java | 7 ++-- 5 files changed, 74 insertions(+), 5 deletions(-) diff --git a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/DeprecatedResourceApiSensor.java b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/DeprecatedResourceApiSensor.java index 03a952fa285..123eb2130de 100644 --- a/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/DeprecatedResourceApiSensor.java +++ b/plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/DeprecatedResourceApiSensor.java @@ -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"; diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java index 7e8cdbcaf95..11c49c564c8 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java @@ -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.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)); + + } + } diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesOnDirMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesOnDirMediumTest.java index 4fad08a8c99..4280af482f4 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesOnDirMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesOnDirMediumTest.java @@ -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.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); + } + } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java index 5e1ebb651c2..c788f55ebd9 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java @@ -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(); } /** diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java index fe72745346e..21e810aec94 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java @@ -19,14 +19,13 @@ */ 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; } -- 2.39.5