diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2014-02-20 13:08:04 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2014-02-20 13:08:04 +0100 |
commit | 289b8f4694ed08197a357d9ed6b2368ec0b2a6cb (patch) | |
tree | d7223202636ab595d85d6e7d071b361d773fc556 /sonar-deprecated | |
parent | 483640abb816cb5ecf79f66978500658bd34e0e0 (diff) | |
download | sonarqube-289b8f4694ed08197a357d9ed6b2368ec0b2a6cb.tar.gz sonarqube-289b8f4694ed08197a357d9ed6b2368ec0b2a6cb.zip |
SONAR-926 move InputFileUtils to sonar-deprecated
Diffstat (limited to 'sonar-deprecated')
-rw-r--r-- | sonar-deprecated/src/main/java/org/sonar/api/resources/InputFileUtils.java | 169 | ||||
-rw-r--r-- | sonar-deprecated/src/test/java/org/sonar/api/resources/InputFileUtilsTest.java | 181 |
2 files changed, 350 insertions, 0 deletions
diff --git a/sonar-deprecated/src/main/java/org/sonar/api/resources/InputFileUtils.java b/sonar-deprecated/src/main/java/org/sonar/api/resources/InputFileUtils.java new file mode 100644 index 00000000000..c3dd66ef675 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/resources/InputFileUtils.java @@ -0,0 +1,169 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +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; + +/** + * @since 2.8 + * @deprecated in 4.2. Useless when using by {@link org.sonar.api.batch.fs.FileSystem}. + */ +@Deprecated +public final class InputFileUtils { + + private InputFileUtils() { + // only static methods + } + + /** + * @param inputFiles not nullable + * @return not null list + * @deprecated in 4.2. Use {@link org.sonar.api.batch.fs.FileSystem#files(org.sonar.api.batch.fs.FilePredicate)} + */ + 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. + */ + public static InputFile create(java.io.File basedir, java.io.File file) { + String relativePath = getRelativePath(basedir, file); + if (relativePath != null) { + return create(basedir, relativePath); + } + return null; + } + + /** + * For internal and for testing purposes. Please use the FileSystem component to access files. + */ + public static InputFile create(java.io.File basedir, String relativePath) { + 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(); + while (cursor != null) { + if (basedir.equals(cursor)) { + return StringUtils.join(stack, "/"); + } + stack.add(0, cursor.getName()); + cursor = cursor.getParentFile(); + } + return null; + } + + static final class DefaultInputFile implements InputFile { + private java.io.File basedir; + private String relativePath; + + DefaultInputFile(java.io.File basedir, String relativePath) { + this.basedir = basedir; + this.relativePath = relativePath; + } + + public java.io.File getFileBaseDir() { + return basedir; + } + + public java.io.File getFile() { + 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; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o instanceof DefaultInputFile) { + DefaultInputFile that = (DefaultInputFile) o; + return Objects.equal(basedir, that.basedir) && Objects.equal(relativePath, that.relativePath); + } + return false; + } + + @Override + public int hashCode() { + int result = basedir.hashCode(); + result = 31 * result + relativePath.hashCode(); + return result; + } + + @Override + public String toString() { + return String.format("%s -> %s", basedir.getAbsolutePath(), relativePath); + } + } +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/resources/InputFileUtilsTest.java b/sonar-deprecated/src/test/java/org/sonar/api/resources/InputFileUtilsTest.java new file mode 100644 index 00000000000..99bd3226c15 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/resources/InputFileUtilsTest.java @@ -0,0 +1,181 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.resources; + +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.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() { + String relativePath = "org/sonar/Foo.java"; + + InputFile inputFile = InputFileUtils.create(BASE_DIR, relativePath); + + 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() { + 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")); + + assertThat(inputFile).isNull(); + } + + @Test + public void shouldGuessRelativePath() { + File file = new File(BASE_DIR, "org/sonar/Foo.java"); + + InputFile inputFile = InputFileUtils.create(BASE_DIR, file); + + assertThat(inputFile.getFileBaseDir()).isEqualTo(BASE_DIR); + assertThat(inputFile.getFile()).isEqualTo(file); + assertThat(inputFile.getRelativePath()).isEqualTo("org/sonar/Foo.java"); + } + + @Test + public void testEqualsAndHashCode() { + 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"); + + assertThat(inputFile1).isEqualTo(inputFile1).isEqualTo(inputFile2); + assertThat(inputFile1.hashCode()).isEqualTo(inputFile2.hashCode()); + assertThat(inputFile1).isNotEqualTo(inputFile3); + } + + @Test + public void shouldNotEqualFile() { + File file = new File(BASE_DIR, "org/sonar/Foo.java"); + + InputFile inputFile = InputFileUtils.create(BASE_DIR, file); + + assertThat(inputFile.getFile()).isEqualTo(file); + assertThat(inputFile).isNotEqualTo(file); + } + + @Test + 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() { + InputFile inputFile = InputFileUtils.create(BASE_DIR, "org/sonar/Foo.java"); + + assertThat(inputFile.toString()).endsWith("InputFileUtilsTest -> org/sonar/Foo.java"); + } + + @Test + public void testToFiles() { + 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).containsExactly(new File(BASE_DIR, "Foo.java"), new File(BASE_DIR, "Bar.java")); + } + + @Test + public void testCreateList() { + 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(BASE_DIR, Arrays.asList(file1, file2, wrongFile)); + + assertThat(inputFiles).hasSize(2); + assertThat(inputFiles.get(0).getFile()).isEqualTo(file1); + assertThat(inputFiles.get(1).getFile()).isEqualTo(file2); + } + + @Test + public void shouldExtractRelativeDirectory() { + 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(); + } + + @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"); + + inputFile.getInputStream(); + } + + static void write(String content, File file) throws IOException { + file.getParentFile().mkdirs(); + Files.write(content, file, Charsets.UTF_8); + } + + static String read(InputStream input) throws IOException { + try { + return new String(ByteStreams.toByteArray(input), Charsets.UTF_8.displayName()); + } finally { + Closeables.closeQuietly(input); + } + } +} |