diff options
author | Jenkins CI <ci@sonarsource.com> | 2015-12-14 10:27:28 +0100 |
---|---|---|
committer | Jenkins CI <ci@sonarsource.com> | 2015-12-14 10:27:28 +0100 |
commit | a4ed263e412aa7fc37d7e5dac4c231acafb1d1d6 (patch) | |
tree | 7a23e790384556c4ee3246c3a5b44c4064fbbd26 /sonar-plugin-api | |
parent | 9b709a4fe8323c9f8db0a021c4b89e46ba1b1fe8 (diff) | |
parent | 43adad0053aca847f656346d1bc85e89f15d6ec0 (diff) | |
download | sonarqube-a4ed263e412aa7fc37d7e5dac4c231acafb1d1d6.tar.gz sonarqube-a4ed263e412aa7fc37d7e5dac4c231acafb1d1d6.zip |
Automatic merge from branch-5.3
* origin/branch-5.3:
Fix quality flaws in PathUtils
SONAR-6939 decrease maximum nb of decimal digits to 5
Diffstat (limited to 'sonar-plugin-api')
4 files changed, 30 insertions, 20 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java index 8fdb94f44a6..4783b6e178e 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/measures/Metric.java @@ -51,7 +51,7 @@ public class Metric<G extends Serializable> implements Serializable, org.sonar.a * The maximum supported value of scale for decimal metrics * @since 5.3 */ - public static final int MAX_DECIMAL_SCALE = 20; + public static final int MAX_DECIMAL_SCALE = 5; /** * A metric bigger value means a degradation 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/measures/MetricTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java index 0a088f53d3a..e5765d51961 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/measures/MetricTest.java @@ -84,10 +84,10 @@ public class MetricTest { @Test public void fail_if_decimal_scale_is_greater_than_max_supported_value() { expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("Scale of decimal metric [foo] must be less than or equal 20: 21"); + expectedException.expectMessage("Scale of decimal metric [foo] must be less than or equal 5: 6"); new Metric.Builder("foo", "Foo", Metric.ValueType.FLOAT) - .setDecimalScale(Metric.MAX_DECIMAL_SCALE + 1) + .setDecimalScale(6) .create(); } 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(); } } |