import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
+import java.io.File;
import java.util.Collection;
import java.util.List;
/**
* @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.
*/
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();
*/
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 {
@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));
@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(""));
}
}