]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-926 Add a fromIOFile method on Directory to be consistent with File
authorJulien HENRY <julien.henry@sonarsource.com>
Fri, 31 Jan 2014 14:26:06 +0000 (15:26 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 31 Jan 2014 14:26:53 +0000 (15:26 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/resources/Directory.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/File.java
sonar-plugin-api/src/test/java/org/sonar/api/resources/DirectoryTest.java
sonar-plugin-api/src/test/java/org/sonar/api/resources/FileTest.java

index d3ea1c9e17b23869ac192900563922fd7ce8afab..d916dcec5564236216c1d17112c79f09d55ecdac 100644 (file)
@@ -22,8 +22,11 @@ package org.sonar.api.resources;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import org.sonar.api.batch.SensorContext;
+import org.sonar.api.scan.filesystem.PathResolver;
 import org.sonar.api.utils.WildcardPattern;
 
+import javax.annotation.CheckForNull;
+
 /**
  * @since 1.10
  * Extends JavaPackage to allow smooth migration from JavaPackage to Directory
@@ -38,7 +41,7 @@ public class Directory extends JavaPackage {
   }
 
   /**
-   * @deprecated since 4.2 use {@link #create(String, String)}
+   * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)}
    */
   @Deprecated
   public Directory(String relativePathFromSourceDir) {
@@ -46,7 +49,7 @@ public class Directory extends JavaPackage {
   }
 
   /**
-   * @deprecated since 4.2 use {@link #create(String, String)}
+   * @deprecated since 4.2 use {@link #fromIOFile(java.io.File, Project)}
    */
   @Deprecated
   public Directory(String relativePathFromSourceDir, Language language) {
@@ -106,13 +109,30 @@ public class Directory extends JavaPackage {
     return normalizedKey;
   }
 
+  /**
+   * Creates a {@link Directory} from an absolute {@link java.io.File} and a module.
+   * The returned {@link Directory} can be then passed for example to
+   * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)}.
+   * @param dir absolute path to a directory
+   * @param module
+   * @return null if the directory is not under module basedir.
+   * @since 4.2
+   */
+  @CheckForNull
+  public static Directory fromIOFile(java.io.File dir, Project module) {
+    String relativePathFromBasedir = new PathResolver().relativePath(module.getFileSystem().getBasedir(), dir);
+    if (relativePathFromBasedir != null) {
+      return Directory.create(relativePathFromBasedir);
+    }
+    return null;
+  }
+
   /**
    * Create a Directory that is partially initialized. But that's enough to call for example
    * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)} when resources are already indexed.
    * Internal use only.
-   * @since 4.2
    */
-  public static Directory create(String relativePathFromBaseDir) {
+  static Directory create(String relativePathFromBaseDir) {
     Directory d = new Directory();
     String normalizedPath = normalize(relativePathFromBaseDir);
     d.setKey(normalizedPath);
index 013b1236adbc5e6ed749ad3a0f2ac50d8baeea8b..57c4f585c2c7666f7a657ebd102ce0d43509389f 100644 (file)
@@ -156,8 +156,8 @@ public class File extends Resource {
   }
 
   /**
-   * Creates a SonarQube File from an absolute Java IO File and a module.
-   * The returned SonarQube File can be then passed for example to
+   * Creates a {@link File} from an absolute {@link java.io.File} and a module.
+   * The returned {@link File} can be then passed for example to
    * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)}.
    * @param file absolute path to a file
    * @param module
@@ -243,9 +243,8 @@ public class File extends Resource {
    * Create a File that is partially initialized. But that's enough to call for example
    * {@link SensorContext#saveMeasure(Resource, org.sonar.api.measures.Measure)} when resources are already indexed.
    * Internal use only.
-   * @since 4.2
    */
-  public static File create(String relativePathFromBasedir) {
+  private static File create(String relativePathFromBasedir) {
     File file = new File();
     String normalizedPath = normalize(relativePathFromBasedir);
     file.setKey(normalizedPath);
index a478a0ea7293780b1d97feaad050c0cc5dd7806b..765443ae78144ce45753c052c4ae11666e7bd2d7 100644 (file)
  */
 package org.sonar.api.resources;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.IOException;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class DirectoryTest {
 
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
+  @Test
+  public void createFromIoFileShouldComputeCorrectKey() throws IOException {
+    java.io.File baseDir = temp.newFolder();
+    Project project = mock(Project.class);
+    ProjectFileSystem fileSystem = mock(ProjectFileSystem.class);
+    when(project.getFileSystem()).thenReturn(fileSystem);
+    when(fileSystem.getBasedir()).thenReturn(baseDir);
+    Resource dir = Directory.fromIOFile(new java.io.File(baseDir, "src/foo/bar/"), project);
+    assertThat(dir.getKey(), is("src/foo/bar"));
+  }
+
   @Test
   public void shouldStartBySlashAndNotEndBySlash() {
     Resource dir = Directory.create("src/foo/bar/", "      /foo/bar/  ");
index 061fa08fec308e7c179d736f4e4b2af4ef846b7d..81189d1163f734fff628d6a1e7cfae4ab6c08170 100644 (file)
  */
 package org.sonar.api.resources;
 
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.IOException;
 
 import static org.fest.assertions.Assertions.assertThat;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class FileTest {
 
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
+  @Test
+  public void createFromIoFileShouldComputeCorrectKey() throws IOException {
+    java.io.File baseDir = temp.newFolder();
+    Project project = mock(Project.class);
+    ProjectFileSystem fileSystem = mock(ProjectFileSystem.class);
+    when(project.getFileSystem()).thenReturn(fileSystem);
+    when(fileSystem.getBasedir()).thenReturn(baseDir);
+    Resource file = File.fromIOFile(new java.io.File(baseDir, "src/foo/bar/toto.sql"), project);
+    assertThat(file.getKey(), is("src/foo/bar/toto.sql"));
+  }
+
   @Test
   public void trimKeyAndName() {
     File file = new File("   foo/bar/  ", "  toto.sql  ");