]> source.dussan.org Git - sonarqube.git/commitdiff
API: add methods to InputFileUtils
authorsimonbrandhof <simon.brandhof@gmail.com>
Mon, 11 Apr 2011 14:51:33 +0000 (16:51 +0200)
committersimonbrandhof <simon.brandhof@gmail.com>
Mon, 11 Apr 2011 14:51:33 +0000 (16:51 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java
sonar-plugin-api/src/test/java/org/sonar/api/resources/InputFileUtilsTest.java

index 01441a3f26f909dfc461454dd09e4b68d12d11b9..ce41722ae4f542ee4a3fc86a8efbf32044fdf9d2 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.api.resources;
 import com.google.common.collect.Lists;
 import org.apache.commons.lang.StringUtils;
 
+import java.io.File;
 import java.util.Collection;
 import java.util.List;
 
@@ -36,16 +37,29 @@ public final class InputFileUtils {
 
   /**
    * @param inputFiles not nullable
-   * @return not null collection
+   * @return not null list
    */
-  public static Collection<java.io.File> toFiles(Collection<InputFile> inputFiles) {
-    Collection<java.io.File> files = Lists.newArrayList();
+  public static List<java.io.File> toFiles(Collection<InputFile> inputFiles) {
+    List<java.io.File> files = Lists.newArrayList();
     for (InputFile inputFile : inputFiles) {
       files.add(inputFile.getFile());
     }
     return files;
   }
 
+  /**
+   * Extract the directory from relative path. Examples :
+   * - returns "org/foo" when relative path is "org/foo/Bar.java"
+   * - returns "" when relative path is "Bar.java"
+   */
+  public static String getRelativeDirectory(InputFile inputFile) {
+    String relativePath = inputFile.getRelativePath();
+    if (StringUtils.contains(relativePath, "/")) {
+      return StringUtils.substringBeforeLast(relativePath, "/");
+    }
+    return "";
+  }
+
   /**
    * For internal and for testing purposes. Please use the FileSystem component to access files.
    */
@@ -64,6 +78,20 @@ public final class InputFileUtils {
     return new DefaultInputFile(basedir, relativePath);
   }
 
+  /**
+   * For internal and for testing purposes. Please use the FileSystem component to access files.
+   */
+  public static List<InputFile> create(java.io.File basedir, Collection<java.io.File> files) {
+    List<InputFile> inputFiles = Lists.newArrayList();
+    for (File file : files) {
+      InputFile inputFile = create(basedir, file);
+      if (inputFile != null) {
+        inputFiles.add(inputFile);
+      }
+    }
+    return inputFiles;
+  }
+
   static String getRelativePath(java.io.File basedir, java.io.File file) {
     List<String> stack = Lists.newArrayList(file.getName());
     java.io.File cursor = file.getParentFile();
index 3ed05a06173a6a2d55bd062d9e2c994e1369eed9..23988df7d7c45e9e39820ead5047fcc970e7e5df 100644 (file)
  */
 package org.sonar.api.resources;
 
-import com.google.common.collect.Collections2;
-import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 import org.junit.Test;
 
-import java.io.*;
 import java.io.File;
 import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
+import java.util.List;
 
 import static org.hamcrest.Matchers.nullValue;
 import static org.hamcrest.core.Is.is;
 import static org.hamcrest.text.StringEndsWith.endsWith;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertThat;
 
 public class InputFileUtilsTest {
@@ -42,7 +37,7 @@ public class InputFileUtilsTest {
   @Test
   public void shouldCreateInputFileWithRelativePath() {
     java.io.File basedir = new java.io.File("target/tmp/InputFileUtilsTest");
-    
+
     InputFile inputFile = InputFileUtils.create(basedir, "org/sonar/Foo.java");
 
     assertThat(inputFile.getFileBaseDir(), is(basedir));
@@ -112,14 +107,38 @@ public class InputFileUtilsTest {
   @Test
   public void testToFiles() {
     File basedir = new File("target/tmp/InputFileUtilsTest");
-    Collection<InputFile> inputFiles = Arrays.asList(
+    List<InputFile> inputFiles = Arrays.asList(
         InputFileUtils.create(basedir, "Foo.java"), InputFileUtils.create(basedir, "Bar.java"));
 
-    Collection<File> files = InputFileUtils.toFiles(inputFiles);
+    List<File> files = InputFileUtils.toFiles(inputFiles);
 
     assertThat(files.size(), is(2));
-    Iterator<File> it = files.iterator();
-    assertThat(it.next(), is(new File(basedir, "Foo.java")));
-    assertThat(it.next(), is(new File(basedir, "Bar.java")));
+    assertThat(files.get(0), is(new File(basedir, "Foo.java")));
+    assertThat(files.get(1), is(new File(basedir, "Bar.java")));
+  }
+
+  @Test
+  public void testCreateList() {
+    java.io.File basedir = new java.io.File("target/tmp/InputFileUtilsTest");
+    File file1 = new File(basedir, "org/sonar/Foo.java");
+    File file2 = new File(basedir, "org/sonar/Bar.java");
+    File wrongFile = new File("somewhere/else/org/sonar/Foo.java");
+
+    List<InputFile> inputFiles = InputFileUtils.create(basedir, Lists.newArrayList(file1, file2, wrongFile));
+
+    assertThat(inputFiles.size(), is(2));
+    assertThat(inputFiles.get(0).getFile(), is(file1));
+    assertThat(inputFiles.get(1).getFile(), is(file2));
+  }
+
+  @Test
+  public void shouldExtractRelativeDirectory() {
+    java.io.File basedir = new java.io.File("target/tmp/InputFileUtilsTest");
+
+    InputFile inputFile = InputFileUtils.create(basedir, "org/sonar/Foo.java");
+    assertThat(InputFileUtils.getRelativeDirectory(inputFile), is("org/sonar"));
+
+    inputFile = InputFileUtils.create(basedir, "Foo.java");
+    assertThat(InputFileUtils.getRelativeDirectory(inputFile), is(""));
   }
 }