From 43adad0053aca847f656346d1bc85e89f15d6ec0 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 14 Dec 2015 10:24:59 +0100 Subject: [PATCH] Fix quality flaws in PathUtils --- .../java/org/sonar/api/utils/PathUtils.java | 15 ++++++---- .../org/sonar/api/utils/PathUtilsTest.java | 29 +++++++++++-------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/PathUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/PathUtils.java index 5ab9090dc01..94b1978a368 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/PathUtils.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/PathUtils.java @@ -19,24 +19,25 @@ */ package org.sonar.api.utils; -import org.apache.commons.io.FilenameUtils; - -import javax.annotation.Nullable; import java.io.File; import java.io.IOException; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.apache.commons.io.FilenameUtils; /** * @since 4.0 */ public class PathUtils { - PathUtils() { + private PathUtils() { // only static methods } /** * Normalize path and replace file separators by forward slash */ + @CheckForNull public static String sanitize(@Nullable String path) { return FilenameUtils.normalize(path, true); } @@ -45,9 +46,13 @@ public class PathUtils { * Get canonical path and replace file separators by forward slash. This * method does not throw boring checked exception. */ + @CheckForNull public static String canonicalPath(@Nullable File file) { + if (file == null) { + return null; + } try { - return file != null ? FilenameUtils.separatorsToUnix(file.getCanonicalPath()) : null; + return FilenameUtils.separatorsToUnix(file.getCanonicalPath()); } catch (IOException e) { throw new IllegalStateException("Fail to get the canonical path of " + file.getAbsolutePath(), e); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/PathUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/PathUtilsTest.java index 3b76299f032..66b3c33d157 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/PathUtilsTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/PathUtilsTest.java @@ -26,6 +26,7 @@ import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; +import org.sonar.test.TestUtils; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; @@ -44,10 +45,23 @@ public class PathUtilsTest { public void testSanitize() throws Exception { assertThat(PathUtils.sanitize("foo/bar/..")).isEqualTo("foo/"); assertThat(PathUtils.sanitize("C:\\foo\\..\\bar")).isEqualTo("C:/bar"); + assertThat(PathUtils.sanitize(null)).isNull(); } @Test - public void testCanonicalPath_unchecked_exception() throws Exception { + public void test_canonicalPath() throws Exception { + File file = temp.newFile(); + String path = PathUtils.canonicalPath(file); + assertThat(path).isEqualTo(FilenameUtils.separatorsToUnix(file.getCanonicalPath())); + } + + @Test + public void canonicalPath_returns_null_if_null_input() { + assertThat(PathUtils.canonicalPath(null)).isNull(); + } + + @Test + public void canonicalPath_fails_to_get_canonical_path() throws Exception { File file = mock(File.class); when(file.getCanonicalPath()).thenThrow(new IOException()); @@ -60,16 +74,7 @@ public class PathUtilsTest { } @Test - public void testCanonicalPath() throws Exception { - File file = temp.newFile(); - String path = PathUtils.canonicalPath(file); - assertThat(path).isEqualTo(FilenameUtils.separatorsToUnix(file.getCanonicalPath())); - assertThat(PathUtils.canonicalPath(null)).isNull(); - } - - @Test - public void haveFunGetCoverage() { - // does not fail - new PathUtils(); + public void only_statics() { + assertThat(TestUtils.hasOnlyPrivateConstructors(PathUtils.class)).isTrue(); } } -- 2.39.5