aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2012-05-05 10:24:36 +0200
committerDavid Gageot <david@gageot.net>2012-05-07 07:29:25 +0200
commit23ce7323fc6044a4d889a3914112b1f93a7470b6 (patch)
tree4fda145cd29205994c1eb8214e3ade06150a3d3b
parentb2f924aab48b5f078715779875e077a9c2472f85 (diff)
downloadsonarqube-23ce7323fc6044a4d889a3914112b1f93a7470b6.tar.gz
sonarqube-23ce7323fc6044a4d889a3914112b1f93a7470b6.zip
InputFile should return an input stream to make it easier to test code reading InputFiles
-rw-r--r--sonar-plugin-api/pom.xml17
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java38
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java22
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/InputFileUtilsTest.java160
4 files changed, 150 insertions, 87 deletions
diff --git a/sonar-plugin-api/pom.xml b/sonar-plugin-api/pom.xml
index de299284334..22a6a4fdef2 100644
--- a/sonar-plugin-api/pom.xml
+++ b/sonar-plugin-api/pom.xml
@@ -84,18 +84,6 @@
<artifactId>commons-configuration</artifactId>
</dependency>
<dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- </dependency>
- <dependency>
- <groupId>commons-lang</groupId>
- <artifactId>commons-lang</artifactId>
- </dependency>
- <dependency>
- <groupId>commons-collections</groupId>
- <artifactId>commons-collections</artifactId>
- </dependency>
- <dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
@@ -161,6 +149,11 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.easytesting</groupId>
+ <artifactId>fest-assert</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java
index 6b3c6342ea5..dfc45491844 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java
@@ -20,28 +20,48 @@
package org.sonar.api.resources;
import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
/**
* @since 2.6
*/
public interface InputFile {
-
/**
- * The source base directory, different than the project basedir. For example in maven projects, the basedir of a source file stored in
- * src/main/java/org/foo/ is the directory src/main/java.
+ * The source base directory, different than the project basedir.
+ *
+ * <p>For example in maven projects, the basedir of a source file stored in
+ * <code>src/main/java/org/foo/</code> is the directory <code>src/main/java</code>.</p>
*/
File getFileBaseDir();
+ /**
+ * Get the underlying file.
+ *
+ * @return the file
+ */
File getFile();
/**
- * Path relative to basedir. Directory separator is slash '/', whatever the platform.
- *
- * Example on windows: if file basedir is c:\project\src\ and file is c:\project\src\org\foo\Bar.java, then relative path
- * is org/foo/Bar.java
+ * Path relative to basedir. Directory separator is slash <code>'/'</code>, whatever the platform.
*
- * Example on unix: if file basedir is /project/src and file is /project/src/org/foo/Bar.java, then relative path
- * is org/foo/Bar.java as well.
+ * <p>Example on windows: if file basedir is <code>c:\project\src\</code> and file is <code>c:\project\src\org\foo\Bar.java</code>, then relative path
+ * is <code>org/foo/Bar.java</code></p>
+ *
+ * <p>Example on unix: if file basedir is <code>/project/src</code> and file is <code>/project/src/org/foo/Bar.java</code>, then relative path
+ * is <code>org/foo/Bar.java</code> as well.</p>
*/
String getRelativePath();
+
+ /**
+ * Get an {@link InputStream} that reads from the file.
+ *
+ * <p>The returned stream is buffered so there is no need to use a
+ * <code>BufferedInputStream</code></p>
+ *
+ * @return the stream
+ * @throws FileNotFoundException if the file is not found
+ * @since 3.1
+ */
+ InputStream getInputStream() throws FileNotFoundException;
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java
index 6551a1217e8..ef401174de5 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java
@@ -19,10 +19,16 @@
*/
package org.sonar.api.resources;
+import com.google.common.base.Objects;
+
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
+import java.io.BufferedInputStream;
import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
import java.util.Collection;
import java.util.List;
@@ -122,6 +128,13 @@ public final class InputFileUtils {
return new java.io.File(basedir, relativePath);
}
+ /**
+ * @since 3.1
+ */
+ public InputStream getInputStream() throws FileNotFoundException {
+ return new BufferedInputStream(new FileInputStream(getFile()));
+ }
+
public String getRelativePath() {
return relativePath;
}
@@ -131,12 +144,11 @@ public final class InputFileUtils {
if (this == o) {
return true;
}
- if (o == null || getClass() != o.getClass()) {
- return false;
+ if (o instanceof DefaultInputFile) {
+ DefaultInputFile that = (DefaultInputFile) o;
+ return Objects.equal(basedir, that.basedir) && Objects.equal(relativePath, that.relativePath);
}
- DefaultInputFile that = (DefaultInputFile) o;
- return basedir.equals(that.basedir) && relativePath.equals(that.relativePath);
-
+ return false;
}
@Override
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/InputFileUtilsTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/InputFileUtilsTest.java
index 330874a748c..86a84590735 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/InputFileUtilsTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/InputFileUtilsTest.java
@@ -19,126 +19,164 @@
*/
package org.sonar.api.resources;
-import com.google.common.collect.Lists;
+import com.google.common.io.Closeables;
+
+import org.junit.rules.ExpectedException;
+
+import org.junit.Rule;
+
+import com.google.common.base.Charsets;
+import com.google.common.io.ByteStreams;
+import com.google.common.io.Files;
import org.junit.Test;
+import java.io.BufferedInputStream;
import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
import java.util.Arrays;
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.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
public class InputFileUtilsTest {
+ private static final File BASE_DIR = new File("target/tmp/InputFileUtilsTest");
+
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
@Test
public void shouldCreateInputFileWithRelativePath() {
- java.io.File basedir = new java.io.File("target/tmp/InputFileUtilsTest");
+ String relativePath = "org/sonar/Foo.java";
- InputFile inputFile = InputFileUtils.create(basedir, "org/sonar/Foo.java");
+ InputFile inputFile = InputFileUtils.create(BASE_DIR, relativePath);
- assertThat(inputFile.getFileBaseDir(), is(basedir));
- assertThat(inputFile.getFile(), is(new java.io.File("target/tmp/InputFileUtilsTest/org/sonar/Foo.java")));
- assertThat(inputFile.getRelativePath(), is("org/sonar/Foo.java"));
+ assertThat(inputFile.getFileBaseDir()).isEqualTo(BASE_DIR);
+ assertThat(inputFile.getRelativePath()).isEqualTo(relativePath);
+ assertThat(inputFile.getFile()).isEqualTo(new File("target/tmp/InputFileUtilsTest/org/sonar/Foo.java"));
}
@Test
- public void shouldNotAcceptFileWithWrongBasedir() {
- java.io.File basedir1 = new java.io.File("target/tmp/InputFileUtilsTest/basedir1");
- java.io.File basedir2 = new java.io.File("target/tmp/InputFileUtilsTest/basedir2");
+ public void shouldNotAcceptFileWithWrongbaseDir() {
+ File baseDir1 = new File(BASE_DIR, "baseDir1");
+ File baseDir2 = new File(BASE_DIR, "baseDir2");
- InputFile inputFile = InputFileUtils.create(basedir1, new File(basedir2, "org/sonar/Foo.java"));
+ InputFile inputFile = InputFileUtils.create(baseDir1, new File(baseDir2, "org/sonar/Foo.java"));
- assertThat(inputFile, nullValue());
+ assertThat(inputFile).isNull();
}
@Test
public void shouldGuessRelativePath() {
- java.io.File basedir = new java.io.File("target/tmp/InputFileUtilsTest");
+ File file = new File(BASE_DIR, "org/sonar/Foo.java");
- java.io.File file = new java.io.File(basedir, "org/sonar/Foo.java");
- InputFile inputFile = InputFileUtils.create(basedir, file);
+ InputFile inputFile = InputFileUtils.create(BASE_DIR, file);
- assertThat(inputFile.getFileBaseDir(), is(basedir));
- assertThat(inputFile.getFile(), is(file));
- assertThat(inputFile.getRelativePath(), is("org/sonar/Foo.java"));
+ assertThat(inputFile.getFileBaseDir()).isEqualTo(BASE_DIR);
+ assertThat(inputFile.getFile()).isEqualTo(file);
+ assertThat(inputFile.getRelativePath()).isEqualTo("org/sonar/Foo.java");
}
@Test
public void testEqualsAndHashCode() {
- java.io.File basedir = new java.io.File("target/tmp/InputFileUtilsTest");
-
- InputFile inputFile1 = InputFileUtils.create(basedir, "org/sonar/Foo.java");
- InputFile inputFile2 = InputFileUtils.create(basedir, "org/sonar/Foo.java");
-
- assertEquals(inputFile1, inputFile1);
- assertEquals(inputFile1, inputFile2);
+ InputFile inputFile1 = InputFileUtils.create(BASE_DIR, "org/sonar/Foo.java");
+ InputFile inputFile2 = InputFileUtils.create(BASE_DIR, "org/sonar/Foo.java");
+ InputFile inputFile3 = InputFileUtils.create(BASE_DIR, "org/sonar/Bar.java");
- assertEquals(inputFile1.hashCode(), inputFile1.hashCode());
+ assertThat(inputFile1).isEqualTo(inputFile1).isEqualTo(inputFile2);
+ assertThat(inputFile1.hashCode()).isEqualTo(inputFile2.hashCode());
+ assertThat(inputFile1).isNotEqualTo(inputFile3);
}
@Test
public void shouldNotEqualFile() {
- java.io.File basedir = new java.io.File("target/tmp/InputFileUtilsTest");
- File file = new File(basedir, "org/sonar/Foo.java");
- InputFile inputFile = InputFileUtils.create(basedir, file);
+ File file = new File(BASE_DIR, "org/sonar/Foo.java");
- assertThat(inputFile.getFile(), is(file));
- assertThat(inputFile.equals(file), is(false));
+ InputFile inputFile = InputFileUtils.create(BASE_DIR, file);
+
+ assertThat(inputFile.getFile()).isEqualTo(file);
+ assertThat(inputFile).isNotEqualTo(file);
}
@Test
- public void shouldNotEqualIfBasedirAreDifferents() {
- InputFile inputFile1 = InputFileUtils.create(new File("target/tmp/InputFileUtilsTest"), "org/sonar/Foo.java");
- InputFile inputFile2 = InputFileUtils.create(new File("target/tmp/InputFileUtilsTest/org"), "sonar/Foo.java");
- assertThat(inputFile1.equals(inputFile2), is(false));
+ public void shouldNotEqualIfbaseDirAreDifferents() {
+ InputFile inputFile1 = InputFileUtils.create(BASE_DIR, "org/sonar/Foo.java");
+ InputFile inputFile2 = InputFileUtils.create(new File(BASE_DIR, "org"), "sonar/Foo.java");
+
+ assertThat(inputFile1).isNotEqualTo(inputFile2);
}
@Test
public void testToString() {
- File basedir = new File("target/tmp/InputFileUtilsTest");
- InputFile inputFile = InputFileUtils.create(basedir, "org/sonar/Foo.java");
- assertThat(inputFile.toString(), endsWith("InputFileUtilsTest -> org/sonar/Foo.java"));
+ InputFile inputFile = InputFileUtils.create(BASE_DIR, "org/sonar/Foo.java");
+
+ assertThat(inputFile.toString()).endsWith("InputFileUtilsTest -> org/sonar/Foo.java");
}
@Test
public void testToFiles() {
- File basedir = new File("target/tmp/InputFileUtilsTest");
- List<InputFile> inputFiles = Arrays.asList(
- InputFileUtils.create(basedir, "Foo.java"), InputFileUtils.create(basedir, "Bar.java"));
-
+ List<InputFile> inputFiles = Arrays.asList(InputFileUtils.create(BASE_DIR, "Foo.java"), InputFileUtils.create(BASE_DIR, "Bar.java"));
List<File> files = InputFileUtils.toFiles(inputFiles);
- assertThat(files.size(), is(2));
- assertThat(files.get(0), is(new File(basedir, "Foo.java")));
- assertThat(files.get(1), is(new File(basedir, "Bar.java")));
+ assertThat(files).containsExactly(new File(BASE_DIR, "Foo.java"), new File(BASE_DIR, "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 file1 = new File(BASE_DIR, "org/sonar/Foo.java");
+ File file2 = new File(BASE_DIR, "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));
+ List<InputFile> inputFiles = InputFileUtils.create(BASE_DIR, Arrays.asList(file1, file2, wrongFile));
- assertThat(inputFiles.size(), is(2));
- assertThat(inputFiles.get(0).getFile(), is(file1));
- assertThat(inputFiles.get(1).getFile(), is(file2));
+ assertThat(inputFiles).hasSize(2);
+ assertThat(inputFiles.get(0).getFile()).isEqualTo(file1);
+ assertThat(inputFiles.get(1).getFile()).isEqualTo(file2);
}
@Test
public void shouldExtractRelativeDirectory() {
- java.io.File basedir = new java.io.File("target/tmp/InputFileUtilsTest");
+ InputFile inputFile = InputFileUtils.create(BASE_DIR, "org/sonar/Foo.java");
+ assertThat(InputFileUtils.getRelativeDirectory(inputFile)).isEqualTo("org/sonar");
+
+ inputFile = InputFileUtils.create(BASE_DIR, "Foo.java");
+ assertThat(InputFileUtils.getRelativeDirectory(inputFile)).isEmpty();
+ }
- InputFile inputFile = InputFileUtils.create(basedir, "org/sonar/Foo.java");
- assertThat(InputFileUtils.getRelativeDirectory(inputFile), is("org/sonar"));
+ @Test
+ public void should_get_file_content_as_buffered_input_stream() throws IOException {
+ InputFile inputFile = InputFileUtils.create(BASE_DIR, "org/sonar/Foo.java");
+ write("<FILE CONTENT>", inputFile.getFile());
+
+ InputStream inputStream = inputFile.getInputStream();
+
+ assertThat(inputStream).isInstanceOf(BufferedInputStream.class);
+ assertThat(read(inputStream)).isEqualTo("<FILE CONTENT>");
+ }
+
+ @Test
+ public void should_fail_to_get_input_stream_of_unknown_file() throws IOException {
+ InputFile inputFile = InputFileUtils.create(BASE_DIR, "UNKNOWN.java");
+
+ exception.expect(FileNotFoundException.class);
+ exception.expectMessage(BASE_DIR.getPath());
+ exception.expectMessage("UNKNOWN.java");
+ exception.expectMessage("No such file");
+
+ inputFile.getInputStream();
+ }
+
+ static void write(String content, File file) throws IOException {
+ file.getParentFile().mkdirs();
+ Files.write("<FILE CONTENT>", file, Charsets.UTF_8);
+ }
- inputFile = InputFileUtils.create(basedir, "Foo.java");
- assertThat(InputFileUtils.getRelativeDirectory(inputFile), is(""));
+ static String read(InputStream input) throws IOException {
+ try {
+ return new String(ByteStreams.toByteArray(input), Charsets.UTF_8.displayName());
+ } finally {
+ Closeables.closeQuietly(input);
+ }
}
}