aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2018-12-20 11:48:17 +0100
committersonartech <sonartech@sonarsource.com>2019-01-16 09:43:10 +0100
commite3b3e6950a4dcb728025b4a80343ad73c68516bf (patch)
tree2baa5f2744234675dc26e71356c699695f4c1943 /sonar-plugin-api/src
parentbbff7949d06677e808d7c6f2cf4fd7cfc1f1a079 (diff)
downloadsonarqube-e3b3e6950a4dcb728025b4a80343ad73c68516bf.tar.gz
sonarqube-e3b3e6950a4dcb728025b4a80343ad73c68516bf.zip
SONAR-11598 Rework CPD exclusions
Diffstat (limited to 'sonar-plugin-api/src')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java10
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokens.java24
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java2
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokensTest.java55
4 files changed, 45 insertions, 46 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java
index 0b60c5e227c..c0fd7c38bcc 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java
@@ -60,6 +60,7 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile
private Metadata metadata;
private boolean published;
private boolean excludedForCoverage;
+ private boolean excludedForDuplication;
private final Set<Integer> noSonarLines = new HashSet<>();
private boolean ignoreAllIssues;
private Collection<int[]> ignoreIssuesOnlineRanges = new ArrayList<>();
@@ -127,6 +128,15 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile
return excludedForCoverage;
}
+ public DefaultInputFile setExcludedForDuplication(boolean excludedForDuplication) {
+ this.excludedForDuplication = excludedForDuplication;
+ return this;
+ }
+
+ public boolean isExcludedForDuplication() {
+ return excludedForDuplication;
+ }
+
/**
* @deprecated since 6.6
*/
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokens.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokens.java
index d2cae805cac..a30a4accc76 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokens.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokens.java
@@ -19,17 +19,14 @@
*/
package org.sonar.api.batch.sensor.cpd.internal;
-import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
-import org.sonar.api.CoreProperties;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
-import org.sonar.api.batch.fs.internal.PathPattern;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.sensor.cpd.NewCpdTokens;
import org.sonar.api.batch.sensor.internal.DefaultStorable;
import org.sonar.api.batch.sensor.internal.SensorStorage;
-import org.sonar.api.config.Configuration;
import static com.google.common.base.Preconditions.checkState;
import static java.util.Collections.unmodifiableList;
@@ -37,7 +34,6 @@ import static java.util.Objects.requireNonNull;
public class DefaultCpdTokens extends DefaultStorable implements NewCpdTokens {
- private final Configuration config;
private final List<TokensLine> result = new ArrayList<>();
private InputFile inputFile;
private int startLine = Integer.MIN_VALUE;
@@ -45,22 +41,14 @@ public class DefaultCpdTokens extends DefaultStorable implements NewCpdTokens {
private int currentIndex = 0;
private StringBuilder sb = new StringBuilder();
private TextRange lastRange;
- private boolean excluded;
- public DefaultCpdTokens(Configuration config, SensorStorage storage) {
+ public DefaultCpdTokens(SensorStorage storage) {
super(storage);
- this.config = config;
}
@Override
public DefaultCpdTokens onFile(InputFile inputFile) {
this.inputFile = requireNonNull(inputFile, "file can't be null");
- String[] cpdExclusions = config.getStringArray(CoreProperties.CPD_EXCLUSIONS);
- for (PathPattern cpdExclusion : PathPattern.create(cpdExclusions)) {
- if (cpdExclusion.match(inputFile.path(), Paths.get(inputFile.relativePath()))) {
- this.excluded = true;
- }
- }
return this;
}
@@ -85,7 +73,7 @@ public class DefaultCpdTokens extends DefaultStorable implements NewCpdTokens {
requireNonNull(range, "Range should not be null");
requireNonNull(image, "Image should not be null");
checkInputFileNotNull();
- if (excluded) {
+ if (isExcludedForDuplication()) {
return this;
}
checkState(lastRange == null || lastRange.end().compareTo(range.start()) <= 0,
@@ -106,6 +94,10 @@ public class DefaultCpdTokens extends DefaultStorable implements NewCpdTokens {
return this;
}
+ private boolean isExcludedForDuplication() {
+ return ((DefaultInputFile) inputFile).isExcludedForDuplication();
+ }
+
public List<TokensLine> getTokenLines() {
return unmodifiableList(new ArrayList<>(result));
}
@@ -120,7 +112,7 @@ public class DefaultCpdTokens extends DefaultStorable implements NewCpdTokens {
@Override
protected void doSave() {
checkState(inputFile != null, "Call onFile() first");
- if (excluded) {
+ if (isExcludedForDuplication()) {
return;
}
addNewTokensLine(result, startIndex, currentIndex, startLine, sb);
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java
index e8cc9f81b8f..0ee4c211f7b 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/SensorContextTester.java
@@ -323,7 +323,7 @@ public class SensorContextTester implements SensorContext {
@Override
public NewCpdTokens newCpdTokens() {
- return new DefaultCpdTokens(config(), sensorStorage);
+ return new DefaultCpdTokens(sensorStorage);
}
@Override
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokensTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokensTest.java
index 2ccd9fd01cf..44827ebcebb 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokensTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/sensor/cpd/internal/DefaultCpdTokensTest.java
@@ -19,12 +19,10 @@
*/
package org.sonar.api.batch.sensor.cpd.internal;
-import java.io.File;
import org.junit.Test;
-import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
-import org.sonar.api.config.internal.MapSettings;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
@@ -35,7 +33,7 @@ import static org.mockito.Mockito.verifyZeroInteractions;
public class DefaultCpdTokensTest {
- private static final InputFile INPUT_FILE = new TestInputFileBuilder("foo", "src/Foo.java")
+ private final DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
.setLines(2)
.setOriginalLineStartOffsets(new int[] {0, 50})
.setOriginalLineEndOffsets(new int[] {49, 100})
@@ -45,22 +43,22 @@ public class DefaultCpdTokensTest {
@Test
public void save_no_tokens() {
SensorStorage sensorStorage = mock(SensorStorage.class);
- DefaultCpdTokens tokens = new DefaultCpdTokens(new MapSettings().asConfig(), sensorStorage)
- .onFile(INPUT_FILE);
+ DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage)
+ .onFile(inputFile);
tokens.save();
verify(sensorStorage).store(tokens);
- assertThat(tokens.inputFile()).isEqualTo(INPUT_FILE);
+ assertThat(tokens.inputFile()).isEqualTo(inputFile);
}
@Test
public void save_one_token() {
SensorStorage sensorStorage = mock(SensorStorage.class);
- DefaultCpdTokens tokens = new DefaultCpdTokens(new MapSettings().asConfig(), sensorStorage)
- .onFile(INPUT_FILE)
- .addToken(INPUT_FILE.newRange(1, 2, 1, 5), "foo");
+ DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage)
+ .onFile(inputFile)
+ .addToken(inputFile.newRange(1, 2, 1, 5), "foo");
tokens.save();
@@ -70,13 +68,12 @@ public class DefaultCpdTokensTest {
}
@Test
- public void handle_exclusions_by_pattern() {
+ public void handle_exclusions() {
SensorStorage sensorStorage = mock(SensorStorage.class);
- MapSettings settings = new MapSettings();
- settings.setProperty("sonar.cpd.exclusions", "src/Foo.java,another");
- DefaultCpdTokens tokens = new DefaultCpdTokens(settings.asConfig(), sensorStorage)
- .onFile(INPUT_FILE)
- .addToken(INPUT_FILE.newRange(1, 2, 1, 5), "foo");
+ inputFile.setExcludedForDuplication(true);
+ DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage)
+ .onFile(inputFile)
+ .addToken(inputFile.newRange(1, 2, 1, 5), "foo");
tokens.save();
@@ -88,12 +85,12 @@ public class DefaultCpdTokensTest {
@Test
public void save_many_tokens() {
SensorStorage sensorStorage = mock(SensorStorage.class);
- DefaultCpdTokens tokens = new DefaultCpdTokens(new MapSettings().asConfig(), sensorStorage)
- .onFile(INPUT_FILE)
- .addToken(INPUT_FILE.newRange(1, 2, 1, 5), "foo")
- .addToken(INPUT_FILE.newRange(1, 6, 1, 10), "bar")
- .addToken(INPUT_FILE.newRange(1, 20, 1, 25), "biz")
- .addToken(INPUT_FILE.newRange(2, 1, 2, 10), "next");
+ DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage)
+ .onFile(inputFile)
+ .addToken(inputFile.newRange(1, 2, 1, 5), "foo")
+ .addToken(inputFile.newRange(1, 6, 1, 10), "bar")
+ .addToken(inputFile.newRange(1, 20, 1, 25), "biz")
+ .addToken(inputFile.newRange(2, 1, 2, 10), "next");
tokens.save();
@@ -109,7 +106,7 @@ public class DefaultCpdTokensTest {
@Test
public void basic_validation() {
SensorStorage sensorStorage = mock(SensorStorage.class);
- DefaultCpdTokens tokens = new DefaultCpdTokens(new MapSettings().asConfig(), sensorStorage);
+ DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage);
try {
tokens.save();
fail("Expected exception");
@@ -117,7 +114,7 @@ public class DefaultCpdTokensTest {
assertThat(e).hasMessage("Call onFile() first");
}
try {
- tokens.addToken(INPUT_FILE.newRange(1, 2, 1, 5), "foo");
+ tokens.addToken(inputFile.newRange(1, 2, 1, 5), "foo");
fail("Expected exception");
} catch (Exception e) {
assertThat(e).hasMessage("Call onFile() first");
@@ -129,7 +126,7 @@ public class DefaultCpdTokensTest {
assertThat(e).hasMessage("Range should not be null");
}
try {
- tokens.addToken(INPUT_FILE.newRange(1, 2, 1, 5), null);
+ tokens.addToken(inputFile.newRange(1, 2, 1, 5), null);
fail("Expected exception");
} catch (Exception e) {
assertThat(e).hasMessage("Image should not be null");
@@ -139,12 +136,12 @@ public class DefaultCpdTokensTest {
@Test
public void validate_tokens_order() {
SensorStorage sensorStorage = mock(SensorStorage.class);
- DefaultCpdTokens tokens = new DefaultCpdTokens(new MapSettings().asConfig(), sensorStorage)
- .onFile(INPUT_FILE)
- .addToken(INPUT_FILE.newRange(1, 6, 1, 10), "bar");
+ DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage)
+ .onFile(inputFile)
+ .addToken(inputFile.newRange(1, 6, 1, 10), "bar");
try {
- tokens.addToken(INPUT_FILE.newRange(1, 2, 1, 5), "foo");
+ tokens.addToken(inputFile.newRange(1, 2, 1, 5), "foo");
fail("Expected exception");
} catch (Exception e) {
assertThat(e).hasMessage("Tokens of file src/Foo.java should be provided in order.\n" +