]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7727 Remove org.sonar.api.resources.InputFile
authorJulien HENRY <henryju@yahoo.fr>
Tue, 5 Jul 2016 15:52:47 +0000 (17:52 +0200)
committerJulien HENRY <henryju@yahoo.fr>
Wed, 6 Jul 2016 07:32:40 +0000 (09:32 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java
sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFile.java [deleted file]
sonar-plugin-api/src/main/java/org/sonar/api/resources/InputFileUtils.java [deleted file]
sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java
sonar-plugin-api/src/test/java/org/sonar/api/resources/InputFileUtilsTest.java [deleted file]

index 2d0c593a5defe79b179514410ef0e93dfe39ad05..b43e33d916e095a12aa24fec7fcdb05b36519adf 100644 (file)
 package org.sonar.api.batch.fs.internal;
 
 import com.google.common.base.Preconditions;
-import java.io.BufferedInputStream;
 import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
 import java.io.StringReader;
 import java.nio.charset.Charset;
 import java.nio.file.Path;
@@ -40,7 +36,7 @@ import org.sonar.api.utils.PathUtils;
 /**
  * @since 4.2
  */
-public class DefaultInputFile extends DefaultInputComponent implements InputFile, org.sonar.api.resources.InputFile {
+public class DefaultInputFile extends DefaultInputComponent implements InputFile {
 
   private final String relativePath;
   private final String moduleKey;
@@ -318,26 +314,6 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile
     return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]";
   }
 
-  @Override
-  public File getFileBaseDir() {
-    return moduleBaseDir.toFile();
-  }
-
-  @Override
-  public File getFile() {
-    return file();
-  }
-
-  @Override
-  public String getRelativePath() {
-    return relativePath();
-  }
-
-  @Override
-  public InputStream getInputStream() throws FileNotFoundException {
-    return new BufferedInputStream(new FileInputStream(file()));
-  }
-
   @Override
   public boolean isFile() {
     return true;
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
deleted file mode 100644 (file)
index ec0e14b..0000000
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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 java.io.File;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
-
-/**
- * @since 2.6
- * @deprecated in 4.2. Replaced by {@link org.sonar.api.batch.fs.InputFile}
- */
-@Deprecated
-public interface InputFile {
-  /**
-   * 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>.
-   */
-  File getFileBaseDir();
-
-  /**
-   * Get the underlying file.
-   *
-   * @return the file
-   */
-  File getFile();
-
-  /**
-   * Path relative to basedir. Directory separator is slash <code>'/'</code>, whatever the platform.
-   *
-   * <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>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.
-   */
-  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>
-   *
-   * @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
deleted file mode 100644 (file)
index cf5ed30..0000000
+++ /dev/null
@@ -1,171 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.collect.Lists;
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.Objects;
-import org.apache.commons.lang.StringUtils;
-
-/**
- * @since 2.8
- * @deprecated since 5.0 as {@link InputFile} is deprecated
- */
-@Deprecated
-public final class InputFileUtils {
-
-  private InputFileUtils() {
-    // only static methods
-  }
-
-  /**
-   * @param inputFiles not nullable
-   * @return not null list
-   */
-  public static List<java.io.File> toFiles(Collection<InputFile> inputFiles) {
-    List<java.io.File> files = new ArrayList<>();
-    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 = new ArrayList<>();
-    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;
-    }
-
-    @Override
-    public java.io.File getFileBaseDir() {
-      return basedir;
-    }
-
-    @Override
-    public java.io.File getFile() {
-      return new java.io.File(basedir, relativePath);
-    }
-
-    /**
-     * @since 3.1
-     */
-    @Override
-    public InputStream getInputStream() throws FileNotFoundException {
-      return new BufferedInputStream(new FileInputStream(getFile()));
-    }
-
-    @Override
-    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.equals(basedir, that.basedir) && Objects.equals(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);
-    }
-  }
-}
index b9e1aea77d5faef6467dd5b43c0e1a3ea7d4edfb..1f51a777425bbe389ac7ab0ba4e063541a50e43d 100644 (file)
@@ -46,8 +46,6 @@ public class DefaultInputFileTest {
       .setType(InputFile.Type.TEST);
 
     assertThat(inputFile.relativePath()).isEqualTo("src/Foo.php");
-    assertThat(inputFile.getRelativePath()).isEqualTo("src/Foo.php");
-    assertThat(inputFile.getFile()).isEqualTo(new File(baseDir.toFile(), "src/Foo.php"));
     assertThat(new File(inputFile.relativePath())).isRelative();
     assertThat(inputFile.absolutePath()).endsWith("Foo.php");
     assertThat(new File(inputFile.absolutePath())).isAbsolute();
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
deleted file mode 100644 (file)
index 80ed802..0000000
+++ /dev/null
@@ -1,178 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.ByteStreams;
-import com.google.common.io.Closeables;
-import com.google.common.io.Files;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
-import java.io.BufferedInputStream;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-import java.util.List;
-
-import static org.assertj.core.api.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, StandardCharsets.UTF_8);
-  }
-
-  static String read(InputStream input) throws IOException {
-    try {
-      return new String(ByteStreams.toByteArray(input), StandardCharsets.UTF_8.displayName());
-    } finally {
-      Closeables.closeQuietly(input);
-    }
-  }
-}