From be5def62257b64e1c7ce9d722dfdf73e887a3b3d Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Wed, 16 Mar 2011 13:01:32 +0300 Subject: [PATCH] SONAR-2280 ProjectFileSystem.resolvePath should return canonical file --- .../resources/DefaultProjectFileSystem.java | 6 +++- .../api/resources/ProjectFileSystem.java | 4 +-- .../DefaultProjectFileSystemTest.java | 28 +++++++++++++++---- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java index 0c2520fd226..0387bef2e5a 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java @@ -143,7 +143,11 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { public File resolvePath(String path) { File file = new File(path); if (!file.isAbsolute()) { - file = new File(getBasedir(), path); + try { + file = new File(getBasedir(), path).getCanonicalFile(); + } catch (IOException e) { + throw new SonarException("Unable to resolve path '" + path + "'", e); + } } return file; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java index de5bd42d4cc..c6840a40c91 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java @@ -93,8 +93,8 @@ public interface ProjectFileSystem extends BatchComponent { File getSonarWorkingDirectory(); /** - * Get file from path. It can be absolute or relative to project basedir. For example resolvePath("pom.xml") or - * resolvePath("src/main/java") + * @return file in canonical form from specified path. Path can be absolute or relative to project basedir. + * For example resolvePath("pom.xml") or resolvePath("src/main/java") */ File resolvePath(String path); diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java index a32c84bc3dd..72e88d2979e 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java @@ -19,8 +19,17 @@ */ package org.sonar.api.resources; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.SystemUtils; +import org.apache.maven.project.MavenProject; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; @@ -32,11 +41,6 @@ import org.sonar.api.test.MavenTestUtils; import java.io.File; import java.util.List; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.Matchers.hasItem; -import static org.hamcrest.Matchers.not; -import static org.junit.Assert.assertThat; - public class DefaultProjectFileSystemTest { private Project project = null; @@ -143,6 +147,20 @@ public class DefaultProjectFileSystemTest { assertThat(files.size(), is(2)); } + /** + * See http://jira.codehaus.org/browse/SONAR-2280 + */ + @Test + public void resolvePathShouldReturnCanonicalFile() { + MavenProject pom = mock(MavenProject.class); + when(pom.getBasedir()).thenReturn(new File("/project")); + Project project = new Project("").setPom(pom); + DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project, null); + + assertThat(fs.resolvePath(".").getAbsolutePath(), endsWith("project")); + assertThat(fs.resolvePath("../project").getAbsolutePath(), endsWith("project")); + } + /** * Example of hidden files/directories : .DSStore, .svn, .git */ -- 2.39.5