]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2280 ProjectFileSystem.resolvePath should return canonical file
authorEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 16 Mar 2011 10:01:32 +0000 (13:01 +0300)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Wed, 16 Mar 2011 11:46:46 +0000 (14:46 +0300)
sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/ProjectFileSystem.java
sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java

index 0c2520fd226bd470108538129f93797197e12438..0387bef2e5a1eebc3ee15df2f4f270f79cd78d77 100644 (file)
@@ -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;
   }
index de5bd42d4cc0101c953569e4fc4f34d36a88c650..c6840a40c91fc2cd3fef5f70d78108be848bede2 100644 (file)
@@ -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);
 
index a32c84bc3dd5c20c710df94e6bef6693c40a7f33..72e88d2979e52d9d4f0bc1f8db060c3ef94b358a 100644 (file)
  */
 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
    */