aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2017-08-01 14:43:55 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2017-08-04 14:58:16 +0200
commite6b28c1ba4e7627f4599590ddfcc21875843ce87 (patch)
tree76d3344308689e5f448ba160f3f4bd90649344cf /sonar-plugin-api
parentef26b6a20f4a813079538b60f97dba53dc4108c1 (diff)
downloadsonarqube-e6b28c1ba4e7627f4599590ddfcc21875843ce87.tar.gz
sonarqube-e6b28c1ba4e7627f4599590ddfcc21875843ce87.zip
Store project relative path inside DefaultIndexedFile
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java5
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java38
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/SensorStrategy.java38
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java2
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java66
5 files changed, 102 insertions, 47 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java
index 92253347869..951f8d8ecff 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java
@@ -26,6 +26,7 @@ import java.nio.charset.Charset;
import java.nio.file.Path;
import javax.annotation.CheckForNull;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.api.batch.sensor.SensorDescriptor;
/**
* This layer over {@link java.io.File} adds information for code analyzers.
@@ -54,8 +55,8 @@ public interface InputFile extends IndexedFile {
}
/**
- * Path relative to module base directory. Path is unique and identifies file
- * within given <code>{@link FileSystem}</code>. File separator is the forward
+ * Relative path to module (for normal Sensors) or project (for {@link SensorDescriptor#global() global} Sensors) base directory.
+ * File separator is the forward
* slash ('/'), even on Microsoft Windows.
* <br>
* Returns <code>src/main/java/com/Foo.java</code> if module base dir is
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java
index a91ab8ad2aa..0d07003dce5 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java
@@ -37,37 +37,37 @@ import org.sonar.api.utils.PathUtils;
*/
@Immutable
public class DefaultIndexedFile extends DefaultInputComponent implements IndexedFile {
- private final String relativePath;
+ private final String projectRelativePath;
+ private final String moduleRelativePath;
private final String moduleKey;
- private final Path moduleBaseDir;
private final String language;
private final Type type;
- private final Path path;
+ private final Path absolutePath;
+ private final SensorStrategy sensorStrategy;
/**
* Testing purposes only!
*/
- public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, @Nullable String language) {
- this(moduleKey, moduleBaseDir, relativePath, language, TestInputFileBuilder.nextBatchId());
+ public DefaultIndexedFile(String moduleKey, Path baseDir, String relativePath, @Nullable String language) {
+ this(baseDir.resolve(relativePath), moduleKey, PathUtils.sanitize(relativePath), PathUtils.sanitize(relativePath), Type.MAIN, language, TestInputFileBuilder.nextBatchId(),
+ new SensorStrategy());
}
- public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, @Nullable String language, int batchId) {
- this(moduleKey, moduleBaseDir, relativePath, Type.MAIN, language, batchId);
- }
-
- public DefaultIndexedFile(String moduleKey, Path moduleBaseDir, String relativePath, Type type, @Nullable String language, int batchId) {
+ public DefaultIndexedFile(Path absolutePath, String moduleKey, String projectRelativePath, String moduleRelativePath, Type type, @Nullable String language, int batchId,
+ SensorStrategy sensorStrategy) {
super(batchId);
this.moduleKey = moduleKey;
- this.relativePath = PathUtils.sanitize(relativePath);
- this.moduleBaseDir = moduleBaseDir.normalize();
+ this.projectRelativePath = projectRelativePath;
+ this.moduleRelativePath = moduleRelativePath;
this.type = type;
this.language = language;
- this.path = this.moduleBaseDir.resolve(this.relativePath);
+ this.sensorStrategy = sensorStrategy;
+ this.absolutePath = absolutePath;
}
@Override
public String relativePath() {
- return relativePath;
+ return sensorStrategy.isGlobal() ? projectRelativePath : moduleRelativePath;
}
@Override
@@ -82,7 +82,7 @@ public class DefaultIndexedFile extends DefaultInputComponent implements Indexed
@Override
public Path path() {
- return path;
+ return absolutePath;
}
@Override
@@ -106,7 +106,7 @@ public class DefaultIndexedFile extends DefaultInputComponent implements Indexed
*/
@Override
public String key() {
- return new StringBuilder().append(moduleKey).append(":").append(relativePath).toString();
+ return new StringBuilder().append(moduleKey).append(":").append(moduleRelativePath).toString();
}
public String moduleKey() {
@@ -124,17 +124,17 @@ public class DefaultIndexedFile extends DefaultInputComponent implements Indexed
}
DefaultIndexedFile that = (DefaultIndexedFile) o;
- return moduleKey.equals(that.moduleKey) && relativePath.equals(that.relativePath);
+ return projectRelativePath.equals(that.projectRelativePath);
}
@Override
public int hashCode() {
- return moduleKey.hashCode() + relativePath.hashCode() * 13;
+ return projectRelativePath.hashCode();
}
@Override
public String toString() {
- return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]";
+ return projectRelativePath;
}
@Override
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/SensorStrategy.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/SensorStrategy.java
new file mode 100644
index 00000000000..5e8b34eb86b
--- /dev/null
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/SensorStrategy.java
@@ -0,0 +1,38 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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.batch.fs.internal;
+
+/**
+ * A shared, mutable object in the module container.
+ * It's used during the execution of sensors to decide whether
+ * sensors should be executed once for the entire project, or per-module.
+ */
+public class SensorStrategy {
+
+ private boolean global = false;
+
+ public boolean isGlobal() {
+ return global;
+ }
+
+ public void setGlobal(boolean global) {
+ this.global = global;
+ }
+}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java
index df2bc17d5fa..671790bdf43 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java
@@ -192,7 +192,7 @@ public class TestInputFileBuilder {
}
public DefaultInputFile build() {
- DefaultIndexedFile indexedFile = new DefaultIndexedFile(moduleKey, moduleBaseDir, relativePath, type, language, id);
+ DefaultIndexedFile indexedFile = new DefaultIndexedFile(moduleBaseDir.resolve(relativePath), moduleKey, relativePath, relativePath, type, language, id, new SensorStrategy());
DefaultInputFile inputFile = new DefaultInputFile(indexedFile,
f -> f.setMetadata(new Metadata(lines, nonBlankLines, hash, originalLineOffsets, lastValidOffset)),
contents);
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java
index cf66115c695..7d699d5bf39 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java
@@ -34,6 +34,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.stream.Collectors;
+import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -46,36 +47,52 @@ import static org.mockito.Mockito.mock;
public class DefaultInputFileTest {
+ private static final String PROJECT_RELATIVE_PATH = "module1/src/Foo.php";
+ private static final String MODULE_RELATIVE_PATH = "src/Foo.php";
+
@Rule
public TemporaryFolder temp = new TemporaryFolder();
+ private DefaultIndexedFile indexedFile;
+
+ private Path baseDir;
+ private SensorStrategy sensorStrategy;
+
+ @Before
+ public void prepare() throws IOException {
+ baseDir = temp.newFolder().toPath();
+ sensorStrategy = new SensorStrategy();
+ indexedFile = new DefaultIndexedFile(baseDir.resolve(PROJECT_RELATIVE_PATH), "ABCDE", PROJECT_RELATIVE_PATH, MODULE_RELATIVE_PATH, InputFile.Type.TEST, "php", 0,
+ sensorStrategy);
+ }
+
@Test
public void test() throws Exception {
- Path baseDir = temp.newFolder().toPath();
Metadata metadata = new Metadata(42, 42, "", new int[0], 0);
- DefaultIndexedFile indexedFile = new DefaultIndexedFile("ABCDE", baseDir, "src/Foo.php", InputFile.Type.TEST, "php", 0);
DefaultInputFile inputFile = new DefaultInputFile(indexedFile, (f) -> f.setMetadata(metadata))
.setStatus(InputFile.Status.ADDED)
.setCharset(StandardCharsets.ISO_8859_1);
- assertThat(inputFile.relativePath()).isEqualTo("src/Foo.php");
+ assertThat(inputFile.relativePath()).isEqualTo(MODULE_RELATIVE_PATH);
assertThat(new File(inputFile.relativePath())).isRelative();
assertThat(inputFile.absolutePath()).endsWith("Foo.php");
assertThat(inputFile.filename()).isEqualTo("Foo.php");
- assertThat(inputFile.uri()).hasPath(baseDir.resolve("src/Foo.php").toUri().getPath());
+ assertThat(inputFile.uri()).hasPath(baseDir.resolve(PROJECT_RELATIVE_PATH).toUri().getPath());
assertThat(new File(inputFile.absolutePath())).isAbsolute();
assertThat(inputFile.language()).isEqualTo("php");
assertThat(inputFile.status()).isEqualTo(InputFile.Status.ADDED);
assertThat(inputFile.type()).isEqualTo(InputFile.Type.TEST);
assertThat(inputFile.lines()).isEqualTo(42);
assertThat(inputFile.charset()).isEqualTo(StandardCharsets.ISO_8859_1);
+
+ sensorStrategy.setGlobal(true);
+ assertThat(inputFile.relativePath()).isEqualTo(PROJECT_RELATIVE_PATH);
}
@Test
public void test_content() throws IOException {
- Path baseDir = temp.newFolder().toPath();
- Path testFile = baseDir.resolve("src").resolve("Foo.php");
+ Path testFile = baseDir.resolve(PROJECT_RELATIVE_PATH);
Files.createDirectories(testFile.getParent());
String content = "test é string";
Files.write(testFile, content.getBytes(StandardCharsets.ISO_8859_1));
@@ -84,7 +101,7 @@ public class DefaultInputFileTest {
Metadata metadata = new Metadata(42, 30, "", new int[0], 0);
- DefaultInputFile inputFile = new DefaultInputFile(new DefaultIndexedFile("ABCDE", baseDir, "src/Foo.php", InputFile.Type.TEST, "php", 0), f -> f.setMetadata(metadata))
+ DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> f.setMetadata(metadata))
.setStatus(InputFile.Status.ADDED)
.setCharset(StandardCharsets.ISO_8859_1);
@@ -98,8 +115,7 @@ public class DefaultInputFileTest {
@Test
public void test_content_exclude_bom() throws IOException {
- Path baseDir = temp.newFolder().toPath();
- Path testFile = baseDir.resolve("src").resolve("Foo.php");
+ Path testFile = baseDir.resolve(PROJECT_RELATIVE_PATH);
Files.createDirectories(testFile.getParent());
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(testFile.toFile()), StandardCharsets.UTF_8))) {
out.write('\ufeff');
@@ -111,7 +127,7 @@ public class DefaultInputFileTest {
Metadata metadata = new Metadata(42, 30, "", new int[0], 0);
- DefaultInputFile inputFile = new DefaultInputFile(new DefaultIndexedFile("ABCDE", baseDir, "src/Foo.php", InputFile.Type.TEST, "php", 0), f -> f.setMetadata(metadata))
+ DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> f.setMetadata(metadata))
.setStatus(InputFile.Status.ADDED)
.setCharset(StandardCharsets.UTF_8);
@@ -125,8 +141,8 @@ public class DefaultInputFileTest {
@Test
public void test_equals_and_hashcode() throws Exception {
- DefaultInputFile f1 = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php", null), (f) -> mock(Metadata.class));
- DefaultInputFile f1a = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php", null), (f) -> mock(Metadata.class));
+ DefaultInputFile f1 = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), (f) -> mock(Metadata.class));
+ DefaultInputFile f1a = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), (f) -> mock(Metadata.class));
DefaultInputFile f2 = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Bar.php", null), (f) -> mock(Metadata.class));
assertThat(f1).isEqualTo(f1);
@@ -141,14 +157,14 @@ public class DefaultInputFileTest {
@Test
public void test_toString() throws Exception {
- DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php", null), (f) -> mock(Metadata.class));
- assertThat(file.toString()).isEqualTo("[moduleKey=ABCDE, relative=src/Foo.php, basedir=module]");
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), (f) -> mock(Metadata.class));
+ assertThat(file.toString()).isEqualTo(MODULE_RELATIVE_PATH);
}
@Test
public void checkValidPointer() {
Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, 15);
- DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php", null), f -> f.setMetadata(metadata));
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
assertThat(file.newPointer(1, 0).line()).isEqualTo(1);
assertThat(file.newPointer(1, 0).lineOffset()).isEqualTo(0);
// Don't fail
@@ -166,7 +182,7 @@ public class DefaultInputFileTest {
file.newPointer(3, 1);
fail();
} catch (Exception e) {
- assertThat(e).hasMessage("3 is not a valid line for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=module] has 2 line(s)");
+ assertThat(e).hasMessage("3 is not a valid line for pointer. File src/Foo.php has 2 line(s)");
}
try {
file.newPointer(1, -1);
@@ -178,14 +194,14 @@ public class DefaultInputFileTest {
file.newPointer(1, 10);
fail();
} catch (Exception e) {
- assertThat(e).hasMessage("10 is not a valid line offset for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=module] has 9 character(s) at line 1");
+ assertThat(e).hasMessage("10 is not a valid line offset for pointer. File src/Foo.php has 9 character(s) at line 1");
}
}
@Test
public void checkValidPointerUsingGlobalOffset() {
Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, 15);
- DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php", null), f -> f.setMetadata(metadata));
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
assertThat(file.newPointer(0).line()).isEqualTo(1);
assertThat(file.newPointer(0).lineOffset()).isEqualTo(0);
@@ -209,14 +225,14 @@ public class DefaultInputFileTest {
file.newPointer(16);
fail();
} catch (Exception e) {
- assertThat(e).hasMessage("16 is not a valid offset for file [moduleKey=ABCDE, relative=src/Foo.php, basedir=module]. Max offset is 15");
+ assertThat(e).hasMessage("16 is not a valid offset for file src/Foo.php. Max offset is 15");
}
}
@Test
public void checkValidRange() {
Metadata metadata = new FileMetadata().readMetadata(new StringReader("bla bla a\nabcde"));
- DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php", null), f -> f.setMetadata(metadata));
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 1)).start().line()).isEqualTo(1);
// Don't fail
@@ -235,14 +251,14 @@ public class DefaultInputFileTest {
file.newRange(file.newPointer(1, 0), file.newPointer(1, 10));
fail();
} catch (Exception e) {
- assertThat(e).hasMessage("10 is not a valid line offset for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=module] has 9 character(s) at line 1");
+ assertThat(e).hasMessage("10 is not a valid line offset for pointer. File src/Foo.php has 9 character(s) at line 1");
}
}
@Test
public void selectLine() {
Metadata metadata = new FileMetadata().readMetadata(new StringReader("bla bla a\nabcde\n\nabc"));
- DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php", null), f -> f.setMetadata(metadata));
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
assertThat(file.selectLine(1).start().line()).isEqualTo(1);
assertThat(file.selectLine(1).start().lineOffset()).isEqualTo(0);
@@ -259,14 +275,14 @@ public class DefaultInputFileTest {
file.selectLine(5);
fail();
} catch (Exception e) {
- assertThat(e).hasMessage("5 is not a valid line for pointer. File [moduleKey=ABCDE, relative=src/Foo.php, basedir=module] has 4 line(s)");
+ assertThat(e).hasMessage("5 is not a valid line for pointer. File src/Foo.php has 4 line(s)");
}
}
@Test
public void checkValidRangeUsingGlobalOffset() {
Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, 15);
- DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php", null), f -> f.setMetadata(metadata));
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
TextRange newRange = file.newRange(10, 13);
assertThat(newRange.start().line()).isEqualTo(2);
assertThat(newRange.start().lineOffset()).isEqualTo(0);
@@ -277,7 +293,7 @@ public class DefaultInputFileTest {
@Test
public void testRangeOverlap() {
Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, 15);
- DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Foo.php", null), f -> f.setMetadata(metadata));
+ DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata));
// Don't fail
assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)))).isTrue();
assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 2)))).isTrue();