aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2019-06-04 15:43:44 -0500
committerSonarTech <sonartech@sonarsource.com>2019-07-12 20:21:13 +0200
commite020f1425d815ae48e9b5c39bc0eaf92382c3c06 (patch)
tree8de8c513fe61a8c78a5c7d99d71cefa82e1d9bdc /sonar-scanner-engine/src/test/java/org/sonar/scanner
parent65c61f5006221de77fff2c314d07df8fb73ddc45 (diff)
downloadsonarqube-e020f1425d815ae48e9b5c39bc0eaf92382c3c06.tar.gz
sonarqube-e020f1425d815ae48e9b5c39bc0eaf92382c3c06.zip
Extract implementation from plugin API - fix tests
Diffstat (limited to 'sonar-scanner-engine/src/test/java/org/sonar/scanner')
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ModuleSensorExtensionDictionnaryTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/PostJobExtensionDictionnaryTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/JavaCpdBlockIndexerSensorTest.java4
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ExternalIssueImporterTest.java4
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultFileSystemTest.java139
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/FileMetadataTest.java310
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/charhandler/IntArrayListTest.java55
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/AndPredicateTest.java76
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/DefaultFilePredicatesTest.java245
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/FileExtensionPredicateTest.java72
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/FilenamePredicateTest.java63
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/OrPredicateTest.java66
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/RelativePathPredicateTest.java53
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParserTest.java4
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParserTest.java4
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionSensorTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/EnforceIssuesFilterTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsLoaderTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest.java4
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleCoverageAndDuplicationExclusionsTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleSensorsExecutorTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ProjectCoverageExclusionsTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/PostJobOptimizerTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/ActiveRulesBuilderTest.java104
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultRulesTest.java73
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/NewActiveRuleTest.java87
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java4
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/RulesBuilderTest.java112
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/AdditionalFilePredicatesTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/MetadataGeneratorTest.java6
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/DefaultBlameOutputTest.java2
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultAdHocRuleTest.java156
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultExternalIssueTest.java159
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultHighlightingTest.java126
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultIssueLocationTest.java107
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultIssueTest.java157
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultMeasureTest.java92
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorDescriptorTest.java51
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java3
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/InMemorySensorStorageTest.java59
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java6
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorOptimizerTest.java9
-rw-r--r--sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/SensorContextTesterTest.java375
54 files changed, 2780 insertions, 49 deletions
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java
index 19ce32d686c..d60f344a516 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/DefaultFileLinesContextTest.java
@@ -26,7 +26,7 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.ArgumentCaptor;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.scanner.sensor.DefaultMeasure;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ModuleSensorExtensionDictionnaryTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ModuleSensorExtensionDictionnaryTest.java
index 4d63704cae0..2c4ed265b27 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ModuleSensorExtensionDictionnaryTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/ModuleSensorExtensionDictionnaryTest.java
@@ -33,8 +33,8 @@ import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
-import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.core.platform.ComponentContainer;
+import org.sonar.scanner.sensor.DefaultSensorDescriptor;
import org.sonar.scanner.sensor.ModuleSensorContext;
import org.sonar.scanner.sensor.ModuleSensorExtensionDictionnary;
import org.sonar.scanner.sensor.ModuleSensorOptimizer;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/PostJobExtensionDictionnaryTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/PostJobExtensionDictionnaryTest.java
index 15cf84a2a18..ed35ee1943d 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/PostJobExtensionDictionnaryTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/PostJobExtensionDictionnaryTest.java
@@ -25,7 +25,7 @@ import org.sonar.api.batch.Phase;
import org.sonar.api.batch.postjob.PostJob;
import org.sonar.api.batch.postjob.PostJobContext;
import org.sonar.api.batch.postjob.PostJobDescriptor;
-import org.sonar.api.batch.postjob.internal.DefaultPostJobDescriptor;
+import org.sonar.scanner.postjob.DefaultPostJobDescriptor;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java
index 3e10ad5dad3..ecc155cd5e5 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdExecutorTest.java
@@ -38,7 +38,7 @@ import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentMatchers;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.core.util.CloseableIterator;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/JavaCpdBlockIndexerSensorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/JavaCpdBlockIndexerSensorTest.java
index 7a0a2f21695..3d7c7486034 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/JavaCpdBlockIndexerSensorTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/JavaCpdBlockIndexerSensorTest.java
@@ -33,10 +33,10 @@ import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
-import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.duplications.block.Block;
import org.sonar.scanner.cpd.index.SonarCpdBlockIndex;
+import org.sonar.scanner.fs.TestInputFileBuilder;
+import org.sonar.scanner.sensor.SensorContextTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ExternalIssueImporterTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ExternalIssueImporterTest.java
index 155e08b98c4..596a25f9275 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ExternalIssueImporterTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/externalissue/ExternalIssueImporterTest.java
@@ -28,12 +28,12 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.api.batch.rule.Severity;
-import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.batch.sensor.issue.ExternalIssue;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
+import org.sonar.scanner.sensor.SensorContextTester;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.commons.lang.ObjectUtils.defaultIfNull;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultFileSystemTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultFileSystemTest.java
new file mode 100644
index 00000000000..74a43db65f1
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultFileSystemTest.java
@@ -0,0 +1,139 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.fs;
+
+import java.io.File;
+import java.nio.charset.Charset;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DefaultFileSystemTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ private DefaultFileSystem fs;
+
+ private File basedir;
+
+ @Before
+ public void prepare() throws Exception {
+ basedir = temp.newFolder();
+ fs = new DefaultFileSystem(basedir.toPath());
+ }
+
+ @Test
+ public void test_directories() throws Exception {
+ assertThat(fs.baseDir()).isAbsolute().isDirectory().exists();
+ assertThat(fs.baseDir().getCanonicalPath()).isEqualTo(basedir.getCanonicalPath());
+
+ File workdir = temp.newFolder();
+ fs.setWorkDir(workdir.toPath());
+ assertThat(fs.workDir()).isAbsolute().isDirectory().exists();
+ assertThat(fs.workDir().getCanonicalPath()).isEqualTo(workdir.getCanonicalPath());
+ }
+
+ @Test
+ public void test_encoding() throws Exception {
+ fs.setEncoding(Charset.forName("ISO-8859-1"));
+ assertThat(fs.encoding()).isEqualTo(Charset.forName("ISO-8859-1"));
+ }
+
+ @Test
+ public void add_languages() {
+ assertThat(fs.languages()).isEmpty();
+
+ fs.add(new TestInputFileBuilder("foo", "src/Foo.php").setLanguage("php").build());
+ fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build());
+
+ assertThat(fs.languages()).containsOnly("java", "php");
+ }
+
+ @Test
+ public void files() {
+ assertThat(fs.inputFiles(fs.predicates().all())).isEmpty();
+
+ fs.add(new TestInputFileBuilder("foo", "src/Foo.php").setLanguage("php").build());
+ fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build());
+ fs.add(new TestInputFileBuilder("foo", "src/Baz.java").setLanguage("java").build());
+
+ // no language
+ fs.add(new TestInputFileBuilder("foo", "src/readme.txt").build());
+
+ assertThat(fs.inputFile(fs.predicates().hasRelativePath("src/Bar.java"))).isNotNull();
+ assertThat(fs.inputFile(fs.predicates().hasRelativePath("does/not/exist"))).isNull();
+
+ assertThat(fs.inputFile(fs.predicates().hasAbsolutePath(new File(basedir, "src/Bar.java").getAbsolutePath()))).isNotNull();
+ assertThat(fs.inputFile(fs.predicates().hasAbsolutePath(new File(basedir, "does/not/exist").getAbsolutePath()))).isNull();
+ assertThat(fs.inputFile(fs.predicates().hasAbsolutePath(new File(basedir, "../src/Bar.java").getAbsolutePath()))).isNull();
+
+ assertThat(fs.inputFile(fs.predicates().hasURI(new File(basedir, "src/Bar.java").toURI()))).isNotNull();
+ assertThat(fs.inputFile(fs.predicates().hasURI(new File(basedir, "does/not/exist").toURI()))).isNull();
+ assertThat(fs.inputFile(fs.predicates().hasURI(new File(basedir, "../src/Bar.java").toURI()))).isNull();
+
+ assertThat(fs.files(fs.predicates().all())).hasSize(4);
+ assertThat(fs.files(fs.predicates().hasLanguage("java"))).hasSize(2);
+ assertThat(fs.files(fs.predicates().hasLanguage("cobol"))).isEmpty();
+
+ assertThat(fs.hasFiles(fs.predicates().all())).isTrue();
+ assertThat(fs.hasFiles(fs.predicates().hasLanguage("java"))).isTrue();
+ assertThat(fs.hasFiles(fs.predicates().hasLanguage("cobol"))).isFalse();
+
+ assertThat(fs.inputFiles(fs.predicates().all())).hasSize(4);
+ assertThat(fs.inputFiles(fs.predicates().hasLanguage("php"))).hasSize(1);
+ assertThat(fs.inputFiles(fs.predicates().hasLanguage("java"))).hasSize(2);
+ assertThat(fs.inputFiles(fs.predicates().hasLanguage("cobol"))).isEmpty();
+
+ assertThat(fs.languages()).containsOnly("java", "php");
+ }
+
+ @Test
+ public void input_file_returns_null_if_file_not_found() {
+ assertThat(fs.inputFile(fs.predicates().hasRelativePath("src/Bar.java"))).isNull();
+ assertThat(fs.inputFile(fs.predicates().hasLanguage("cobol"))).isNull();
+ }
+
+ @Test
+ public void input_file_fails_if_too_many_results() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("expected one element");
+
+ fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build());
+ fs.add(new TestInputFileBuilder("foo", "src/Baz.java").setLanguage("java").build());
+
+ fs.inputFile(fs.predicates().all());
+ }
+
+ @Test
+ public void input_file_supports_non_indexed_predicates() {
+ fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build());
+
+ // it would fail if more than one java file
+ assertThat(fs.inputFile(fs.predicates().hasLanguage("java"))).isNotNull();
+ }
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/FileMetadataTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/FileMetadataTest.java
new file mode 100644
index 00000000000..826edf9d086
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/FileMetadataTest.java
@@ -0,0 +1,310 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.fs;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+
+import javax.annotation.Nullable;
+import org.apache.commons.codec.binary.Hex;
+import org.apache.commons.io.FileUtils;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.Metadata;
+import org.sonar.api.utils.log.LogTester;
+import org.sonar.api.utils.log.LoggerLevel;
+
+import static org.apache.commons.codec.digest.DigestUtils.md5Hex;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
+
+public class FileMetadataTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Rule
+ public LogTester logTester = new LogTester();
+
+ @Test
+ public void empty_file() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.touch(tempFile);
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(1);
+ assertThat(metadata.nonBlankLines()).isEqualTo(0);
+ assertThat(metadata.hash()).isNotEmpty();
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0);
+ assertThat(metadata.originalLineEndOffsets()).containsOnly(0);
+ assertThat(metadata.isEmpty()).isTrue();
+ }
+
+ @Test
+ public void windows_without_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\r\nbar\r\nbaz", StandardCharsets.UTF_8, true);
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(3);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 5, 10);
+ assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 8, 13);
+ assertThat(metadata.isEmpty()).isFalse();
+ }
+
+ @Test
+ public void read_with_wrong_encoding() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "marker´s\n", Charset.forName("cp1252"));
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(2);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("marker\ufffds\n"));
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 9);
+ }
+
+ @Test
+ public void non_ascii_utf_8() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "föo\r\nbàr\r\n\u1D11Ebaßz\r\n", StandardCharsets.UTF_8, true);
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("föo\nbàr\n\u1D11Ebaßz\n"));
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 5, 10, 18);
+ }
+
+ @Test
+ public void non_ascii_utf_16() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "föo\r\nbàr\r\n\u1D11Ebaßz\r\n", StandardCharsets.UTF_16, true);
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_16, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("föo\nbàr\n\u1D11Ebaßz\n".getBytes(StandardCharsets.UTF_8)));
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 5, 10, 18);
+ }
+
+ @Test
+ public void unix_without_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\nbar\nbaz", StandardCharsets.UTF_8, true);
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(3);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 4, 8);
+ assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 7, 11);
+ assertThat(metadata.isEmpty()).isFalse();
+ }
+
+ @Test
+ public void unix_with_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\nbar\nbaz\n", StandardCharsets.UTF_8, true);
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz\n"));
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 4, 8, 12);
+ assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 7, 11, 12);
+ }
+
+ @Test
+ public void mac_without_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\rbar\rbaz", StandardCharsets.UTF_8, true);
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(3);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 4, 8);
+ assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 7, 11);
+ }
+
+ @Test
+ public void mac_with_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\rbar\rbaz\r", StandardCharsets.UTF_8, true);
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz\n"));
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 4, 8, 12);
+ assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 7, 11, 12);
+ }
+
+ @Test
+ public void mix_of_newlines_with_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\nbar\r\nbaz\n", StandardCharsets.UTF_8, true);
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz\n"));
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 4, 9, 13);
+ assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 7, 12, 13);
+ }
+
+ @Test
+ public void several_new_lines() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\n\n\nbar", StandardCharsets.UTF_8, true);
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(2);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\n\n\nbar"));
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 4, 5, 6);
+ assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 4, 5, 9);
+ }
+
+ @Test
+ public void mix_of_newlines_without_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\nbar\r\nbaz", StandardCharsets.UTF_8, true);
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(3);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("foo\nbar\nbaz"));
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 4, 9);
+ assertThat(metadata.originalLineEndOffsets()).containsOnly(3, 7, 12);
+ }
+
+ @Test
+ public void start_with_newline() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "\nfoo\nbar\r\nbaz", StandardCharsets.UTF_8, true);
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(tempFile), StandardCharsets.UTF_8, tempFile.getName());
+ assertThat(metadata.lines()).isEqualTo(4);
+ assertThat(metadata.nonBlankLines()).isEqualTo(3);
+ assertThat(metadata.hash()).isEqualTo(md5Hex("\nfoo\nbar\nbaz"));
+ assertThat(metadata.originalLineStartOffsets()).containsOnly(0, 1, 5, 10);
+ assertThat(metadata.originalLineEndOffsets()).containsOnly(0, 4, 8, 13);
+ }
+
+ @Test
+ public void ignore_whitespace_when_computing_line_hashes() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, " foo\nb ar\r\nbaz \t", StandardCharsets.UTF_8, true);
+
+ org.sonar.api.batch.fs.internal.DefaultInputFile f = new TestInputFileBuilder("foo", tempFile.getName())
+ .setModuleBaseDir(tempFile.getParentFile().toPath())
+ .setCharset(StandardCharsets.UTF_8)
+ .build();
+ FileMetadata.computeLineHashesForIssueTracking(f, new FileMetadata.LineHashConsumer() {
+
+ @Override
+ public void consume(int lineIdx, @Nullable byte[] hash) {
+ switch (lineIdx) {
+ case 1:
+ assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("foo"));
+ break;
+ case 2:
+ assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("bar"));
+ break;
+ case 3:
+ assertThat(Hex.encodeHexString(hash)).isEqualTo(md5Hex("baz"));
+ break;
+ default:
+ fail("Invalid line");
+ }
+ }
+ });
+ }
+
+ @Test
+ public void dont_fail_on_empty_file() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "", StandardCharsets.UTF_8, true);
+
+ DefaultInputFile f = new TestInputFileBuilder("foo", tempFile.getName())
+ .setModuleBaseDir(tempFile.getParentFile().toPath())
+ .setCharset(StandardCharsets.UTF_8)
+ .build();
+ FileMetadata.computeLineHashesForIssueTracking(f, new FileMetadata.LineHashConsumer() {
+
+ @Override
+ public void consume(int lineIdx, @Nullable byte[] hash) {
+ switch (lineIdx) {
+ case 1:
+ assertThat(hash).isNull();
+ break;
+ default:
+ fail("Invalid line");
+ }
+ }
+ });
+ }
+
+ @Test
+ public void line_feed_is_included_into_hash() throws Exception {
+ File file1 = temp.newFile();
+ FileUtils.write(file1, "foo\nbar\n", StandardCharsets.UTF_8, true);
+
+ // same as file1, except an additional return carriage
+ File file1a = temp.newFile();
+ FileUtils.write(file1a, "foo\r\nbar\n", StandardCharsets.UTF_8, true);
+
+ File file2 = temp.newFile();
+ FileUtils.write(file2, "foo\nbar", StandardCharsets.UTF_8, true);
+
+ String hash1 = new FileMetadata().readMetadata(new FileInputStream(file1), StandardCharsets.UTF_8, file1.getName()).hash();
+ String hash1a = new FileMetadata().readMetadata(new FileInputStream(file1a), StandardCharsets.UTF_8, file1a.getName()).hash();
+ String hash2 = new FileMetadata().readMetadata(new FileInputStream(file2), StandardCharsets.UTF_8, file2.getName()).hash();
+
+ assertThat(hash1).isEqualTo(hash1a);
+ assertThat(hash1).isNotEqualTo(hash2);
+ }
+
+ @Test
+ public void binary_file_with_unmappable_character() throws Exception {
+ File woff = new File(this.getClass().getResource("glyphicons-halflings-regular.woff").toURI());
+
+ Metadata metadata = new FileMetadata().readMetadata(new FileInputStream(woff), StandardCharsets.UTF_8, woff.getAbsolutePath());
+
+ assertThat(metadata.lines()).isEqualTo(135);
+ assertThat(metadata.nonBlankLines()).isEqualTo(133);
+ assertThat(metadata.hash()).isNotEmpty();
+
+ assertThat(logTester.logs(LoggerLevel.WARN).get(0)).contains("Invalid character encountered in file");
+ assertThat(logTester.logs(LoggerLevel.WARN).get(0)).contains(
+ "glyphicons-halflings-regular.woff at line 1 for encoding UTF-8. Please fix file content or configure the encoding to be used using property 'sonar.sourceEncoding'.");
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/charhandler/IntArrayListTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/charhandler/IntArrayListTest.java
new file mode 100644
index 00000000000..048a0309e5f
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/charhandler/IntArrayListTest.java
@@ -0,0 +1,55 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.fs.charhandler;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class IntArrayListTest {
+
+ @Test
+ public void addElements() {
+ IntArrayList list = new IntArrayList();
+ assertThat(list.trimAndGet()).isEmpty();
+ list.add(1);
+ list.add(2);
+ assertThat(list.trimAndGet()).containsExactly(1, 2);
+ }
+
+ @Test
+ public void trimIfNeeded() {
+ IntArrayList list = new IntArrayList();
+ list.add(1);
+ list.add(2);
+ assertThat(list.trimAndGet()).isSameAs(list.trimAndGet());
+ }
+
+ @Test
+ public void grow() {
+ // Default capacity is 10
+ IntArrayList list = new IntArrayList();
+ for (int i = 1; i <= 11; i++) {
+ list.add(i);
+ }
+ assertThat(list.trimAndGet()).hasSize(11);
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/AndPredicateTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/AndPredicateTest.java
new file mode 100644
index 00000000000..d3ec2aa7b81
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/AndPredicateTest.java
@@ -0,0 +1,76 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.fs.predicates;
+
+import org.junit.Test;
+import org.sonar.api.batch.fs.FilePredicate;
+
+import java.util.Arrays;
+import org.sonar.api.batch.fs.internal.PathPattern;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class AndPredicateTest {
+
+ @Test
+ public void flattenNestedAnd() {
+ PathPatternPredicate pathPatternPredicate1 = new PathPatternPredicate(PathPattern.create("foo1/**"));
+ PathPatternPredicate pathPatternPredicate2 = new PathPatternPredicate(PathPattern.create("foo2/**"));
+ PathPatternPredicate pathPatternPredicate3 = new PathPatternPredicate(PathPattern.create("foo3/**"));
+ FilePredicate andPredicate = AndPredicate.create(Arrays.<FilePredicate>asList(pathPatternPredicate1,
+ AndPredicate.create(Arrays.<FilePredicate>asList(pathPatternPredicate2, pathPatternPredicate3))));
+ assertThat(((AndPredicate) andPredicate).predicates()).containsExactly(pathPatternPredicate1, pathPatternPredicate2, pathPatternPredicate3);
+ }
+
+ @Test
+ public void sortPredicatesByPriority() {
+ PathPatternPredicate pathPatternPredicate1 = new PathPatternPredicate(PathPattern.create("foo1/**"));
+ PathPatternPredicate pathPatternPredicate2 = new PathPatternPredicate(PathPattern.create("foo2/**"));
+ RelativePathPredicate relativePathPredicate = new RelativePathPredicate("foo");
+ FilePredicate andPredicate = AndPredicate.create(Arrays.<FilePredicate>asList(pathPatternPredicate1,
+ relativePathPredicate, pathPatternPredicate2));
+ assertThat(((AndPredicate) andPredicate).predicates()).containsExactly(relativePathPredicate, pathPatternPredicate1, pathPatternPredicate2);
+ }
+
+ @Test
+ public void simplifyAndExpressionsWhenEmpty() {
+ FilePredicate andPredicate = AndPredicate.create(Arrays.<FilePredicate>asList());
+ assertThat(andPredicate).isEqualTo(TruePredicate.TRUE);
+ }
+
+ @Test
+ public void simplifyAndExpressionsWhenTrue() {
+ PathPatternPredicate pathPatternPredicate1 = new PathPatternPredicate(PathPattern.create("foo1/**"));
+ PathPatternPredicate pathPatternPredicate2 = new PathPatternPredicate(PathPattern.create("foo2/**"));
+ FilePredicate andPredicate = AndPredicate.create(Arrays.<FilePredicate>asList(pathPatternPredicate1,
+ TruePredicate.TRUE, pathPatternPredicate2));
+ assertThat(((AndPredicate) andPredicate).predicates()).containsExactly(pathPatternPredicate1, pathPatternPredicate2);
+ }
+
+ @Test
+ public void simplifyAndExpressionsWhenFalse() {
+ PathPatternPredicate pathPatternPredicate1 = new PathPatternPredicate(PathPattern.create("foo1/**"));
+ PathPatternPredicate pathPatternPredicate2 = new PathPatternPredicate(PathPattern.create("foo2/**"));
+ FilePredicate andPredicate = AndPredicate.create(Arrays.<FilePredicate>asList(pathPatternPredicate1,
+ FalsePredicate.FALSE, pathPatternPredicate2));
+ assertThat(andPredicate).isEqualTo(FalsePredicate.FALSE);
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/DefaultFilePredicatesTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/DefaultFilePredicatesTest.java
new file mode 100644
index 00000000000..d0869f0d9a2
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/DefaultFilePredicatesTest.java
@@ -0,0 +1,245 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.fs.predicates;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.nio.file.Path;
+import java.util.Arrays;
+import java.util.Collections;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.fs.FilePredicate;
+import org.sonar.api.batch.fs.FilePredicates;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.InputFile.Status;
+import org.sonar.scanner.fs.TestInputFileBuilder;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DefaultFilePredicatesTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ private Path moduleBasePath;
+
+ @Before
+ public void setUp() throws IOException {
+ moduleBasePath = temp.newFolder().toPath();
+ }
+
+ InputFile javaFile;
+ FilePredicates predicates;
+
+ @Before
+ public void before() throws IOException {
+ predicates = new DefaultFilePredicates(temp.newFolder().toPath());
+ javaFile = new TestInputFileBuilder("foo", "src/main/java/struts/Action.java")
+ .setModuleBaseDir(moduleBasePath)
+ .setLanguage("java")
+ .setStatus(Status.SAME)
+ .build();
+
+ }
+
+ @Test
+ public void all() {
+ assertThat(predicates.all().apply(javaFile)).isTrue();
+ }
+
+ @Test
+ public void none() {
+ assertThat(predicates.none().apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void matches_inclusion_pattern() {
+ assertThat(predicates.matchesPathPattern("src/main/**/Action.java").apply(javaFile)).isTrue();
+ assertThat(predicates.matchesPathPattern("Action.java").apply(javaFile)).isFalse();
+ assertThat(predicates.matchesPathPattern("src/**/*.php").apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void matches_inclusion_patterns() {
+ assertThat(predicates.matchesPathPatterns(new String[] {"src/other/**.java", "src/main/**/Action.java"}).apply(javaFile)).isTrue();
+ assertThat(predicates.matchesPathPatterns(new String[] {}).apply(javaFile)).isTrue();
+ assertThat(predicates.matchesPathPatterns(new String[] {"src/other/**.java", "src/**/*.php"}).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void does_not_match_exclusion_pattern() {
+ assertThat(predicates.doesNotMatchPathPattern("src/main/**/Action.java").apply(javaFile)).isFalse();
+ assertThat(predicates.doesNotMatchPathPattern("Action.java").apply(javaFile)).isTrue();
+ assertThat(predicates.doesNotMatchPathPattern("src/**/*.php").apply(javaFile)).isTrue();
+ }
+
+ @Test
+ public void does_not_match_exclusion_patterns() {
+ assertThat(predicates.doesNotMatchPathPatterns(new String[] {}).apply(javaFile)).isTrue();
+ assertThat(predicates.doesNotMatchPathPatterns(new String[] {"src/other/**.java", "src/**/*.php"}).apply(javaFile)).isTrue();
+ assertThat(predicates.doesNotMatchPathPatterns(new String[] {"src/other/**.java", "src/main/**/Action.java"}).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void has_relative_path() {
+ assertThat(predicates.hasRelativePath("src/main/java/struts/Action.java").apply(javaFile)).isTrue();
+ assertThat(predicates.hasRelativePath("src/main/java/struts/Other.java").apply(javaFile)).isFalse();
+
+ // path is normalized
+ assertThat(predicates.hasRelativePath("src/main/java/../java/struts/Action.java").apply(javaFile)).isTrue();
+
+ assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\Action.java").apply(javaFile)).isTrue();
+ assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\Other.java").apply(javaFile)).isFalse();
+ assertThat(predicates.hasRelativePath("src\\main\\java\\struts\\..\\struts\\Action.java").apply(javaFile)).isTrue();
+ }
+
+ @Test
+ public void has_absolute_path() throws Exception {
+ String path = javaFile.file().getAbsolutePath();
+ assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue();
+ assertThat(predicates.hasAbsolutePath(path.replaceAll("/", "\\\\")).apply(javaFile)).isTrue();
+
+ assertThat(predicates.hasAbsolutePath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
+ assertThat(predicates.hasAbsolutePath("src/main/java/struts/Action.java").apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void has_uri() throws Exception {
+ URI uri = javaFile.uri();
+ assertThat(predicates.hasURI(uri).apply(javaFile)).isTrue();
+
+ assertThat(predicates.hasURI(temp.newFile().toURI()).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void has_path() throws Exception {
+ // is relative path
+ assertThat(predicates.hasPath("src/main/java/struts/Action.java").apply(javaFile)).isTrue();
+ assertThat(predicates.hasPath("src/main/java/struts/Other.java").apply(javaFile)).isFalse();
+
+ // is absolute path
+ String path = javaFile.file().getAbsolutePath();
+ assertThat(predicates.hasAbsolutePath(path).apply(javaFile)).isTrue();
+ assertThat(predicates.hasPath(temp.newFile().getAbsolutePath()).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void is_file() throws Exception {
+ // relative file
+ assertThat(predicates.is(new File(javaFile.relativePath())).apply(javaFile)).isTrue();
+
+ // absolute file
+ assertThat(predicates.is(javaFile.file()).apply(javaFile)).isTrue();
+ assertThat(predicates.is(javaFile.file().getAbsoluteFile()).apply(javaFile)).isTrue();
+ assertThat(predicates.is(new File(javaFile.file().toURI())).apply(javaFile)).isTrue();
+ assertThat(predicates.is(temp.newFile()).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void has_language() {
+ assertThat(predicates.hasLanguage("java").apply(javaFile)).isTrue();
+ assertThat(predicates.hasLanguage("php").apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void has_languages() {
+ assertThat(predicates.hasLanguages(Arrays.asList("java", "php")).apply(javaFile)).isTrue();
+ assertThat(predicates.hasLanguages(Arrays.asList("cobol", "php")).apply(javaFile)).isFalse();
+ assertThat(predicates.hasLanguages(Collections.<String>emptyList()).apply(javaFile)).isTrue();
+ }
+
+ @Test
+ public void has_type() {
+ assertThat(predicates.hasType(InputFile.Type.MAIN).apply(javaFile)).isTrue();
+ assertThat(predicates.hasType(InputFile.Type.TEST).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void has_status() {
+ assertThat(predicates.hasAnyStatus().apply(javaFile)).isTrue();
+ assertThat(predicates.hasStatus(InputFile.Status.SAME).apply(javaFile)).isTrue();
+ assertThat(predicates.hasStatus(InputFile.Status.ADDED).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void not() {
+ assertThat(predicates.not(predicates.hasType(InputFile.Type.MAIN)).apply(javaFile)).isFalse();
+ assertThat(predicates.not(predicates.hasType(InputFile.Type.TEST)).apply(javaFile)).isTrue();
+ }
+
+ @Test
+ public void and() {
+ // empty
+ assertThat(predicates.and().apply(javaFile)).isTrue();
+ assertThat(predicates.and(new FilePredicate[0]).apply(javaFile)).isTrue();
+ assertThat(predicates.and(Collections.<FilePredicate>emptyList()).apply(javaFile)).isTrue();
+
+ // two arguments
+ assertThat(predicates.and(predicates.all(), predicates.all()).apply(javaFile)).isTrue();
+ assertThat(predicates.and(predicates.all(), predicates.none()).apply(javaFile)).isFalse();
+ assertThat(predicates.and(predicates.none(), predicates.all()).apply(javaFile)).isFalse();
+
+ // collection
+ assertThat(predicates.and(Arrays.asList(predicates.all(), predicates.all())).apply(javaFile)).isTrue();
+ assertThat(predicates.and(Arrays.asList(predicates.all(), predicates.none())).apply(javaFile)).isFalse();
+
+ // array
+ assertThat(predicates.and(new FilePredicate[] {predicates.all(), predicates.all()}).apply(javaFile)).isTrue();
+ assertThat(predicates.and(new FilePredicate[] {predicates.all(), predicates.none()}).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void or() {
+ // empty
+ assertThat(predicates.or().apply(javaFile)).isTrue();
+ assertThat(predicates.or(new FilePredicate[0]).apply(javaFile)).isTrue();
+ assertThat(predicates.or(Collections.<FilePredicate>emptyList()).apply(javaFile)).isTrue();
+
+ // two arguments
+ assertThat(predicates.or(predicates.all(), predicates.all()).apply(javaFile)).isTrue();
+ assertThat(predicates.or(predicates.all(), predicates.none()).apply(javaFile)).isTrue();
+ assertThat(predicates.or(predicates.none(), predicates.all()).apply(javaFile)).isTrue();
+ assertThat(predicates.or(predicates.none(), predicates.none()).apply(javaFile)).isFalse();
+
+ // collection
+ assertThat(predicates.or(Arrays.asList(predicates.all(), predicates.all())).apply(javaFile)).isTrue();
+ assertThat(predicates.or(Arrays.asList(predicates.all(), predicates.none())).apply(javaFile)).isTrue();
+ assertThat(predicates.or(Arrays.asList(predicates.none(), predicates.none())).apply(javaFile)).isFalse();
+
+ // array
+ assertThat(predicates.or(new FilePredicate[] {predicates.all(), predicates.all()}).apply(javaFile)).isTrue();
+ assertThat(predicates.or(new FilePredicate[] {predicates.all(), predicates.none()}).apply(javaFile)).isTrue();
+ assertThat(predicates.or(new FilePredicate[] {predicates.none(), predicates.none()}).apply(javaFile)).isFalse();
+ }
+
+ @Test
+ public void hasFilename() {
+ assertThat(predicates.hasFilename("Action.java").apply(javaFile)).isTrue();
+ }
+
+ @Test
+ public void hasExtension() {
+ assertThat(predicates.hasExtension("java").apply(javaFile)).isTrue();
+ }
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/FileExtensionPredicateTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/FileExtensionPredicateTest.java
new file mode 100644
index 00000000000..8cae2f45ff0
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/FileExtensionPredicateTest.java
@@ -0,0 +1,72 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.fs.predicates;
+
+import java.io.IOException;
+import org.junit.Test;
+import org.sonar.api.batch.fs.InputFile;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.sonar.scanner.fs.predicates.FileExtensionPredicate.getExtension;
+
+public class FileExtensionPredicateTest {
+
+ @Test
+ public void should_match_correct_extension() throws IOException {
+ FileExtensionPredicate predicate = new FileExtensionPredicate("bat");
+ assertThat(predicate.apply(mockWithName("prog.bat"))).isTrue();
+ assertThat(predicate.apply(mockWithName("prog.bat.bat"))).isTrue();
+ }
+
+ @Test
+ public void should_not_match_incorrect_extension() throws IOException {
+ FileExtensionPredicate predicate = new FileExtensionPredicate("bat");
+ assertThat(predicate.apply(mockWithName("prog.batt"))).isFalse();
+ assertThat(predicate.apply(mockWithName("prog.abat"))).isFalse();
+ assertThat(predicate.apply(mockWithName("prog."))).isFalse();
+ assertThat(predicate.apply(mockWithName("prog.bat."))).isFalse();
+ assertThat(predicate.apply(mockWithName("prog.bat.batt"))).isFalse();
+ assertThat(predicate.apply(mockWithName("prog"))).isFalse();
+ }
+
+ @Test
+ public void should_match_correct_extension_case_insensitively() throws IOException {
+ FileExtensionPredicate predicate = new FileExtensionPredicate("jAVa");
+ assertThat(predicate.apply(mockWithName("Program.java"))).isTrue();
+ assertThat(predicate.apply(mockWithName("Program.JAVA"))).isTrue();
+ assertThat(predicate.apply(mockWithName("Program.Java"))).isTrue();
+ assertThat(predicate.apply(mockWithName("Program.JaVa"))).isTrue();
+ }
+
+ @Test
+ public void test_empty_extension() {
+ assertThat(getExtension("prog")).isEmpty();
+ assertThat(getExtension("prog.")).isEmpty();
+ assertThat(getExtension(".")).isEmpty();
+ }
+
+ private InputFile mockWithName(String filename) throws IOException {
+ InputFile inputFile = mock(InputFile.class);
+ when(inputFile.filename()).thenReturn(filename);
+ return inputFile;
+ }
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/FilenamePredicateTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/FilenamePredicateTest.java
new file mode 100644
index 00000000000..71c7d1f47de
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/FilenamePredicateTest.java
@@ -0,0 +1,63 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.fs.predicates;
+
+import java.io.IOException;
+import java.util.Collections;
+import org.junit.Test;
+import org.sonar.api.batch.fs.FileSystem;
+import org.sonar.api.batch.fs.InputFile;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class FilenamePredicateTest {
+ @Test
+ public void should_match_file_by_filename() throws IOException {
+ String filename = "some name";
+ InputFile inputFile = mock(InputFile.class);
+ when(inputFile.filename()).thenReturn(filename);
+
+ assertThat(new FilenamePredicate(filename).apply(inputFile)).isTrue();
+ }
+
+ @Test
+ public void should_not_match_file_by_different_filename() throws IOException {
+ String filename = "some name";
+ InputFile inputFile = mock(InputFile.class);
+ when(inputFile.filename()).thenReturn(filename + "x");
+
+ assertThat(new FilenamePredicate(filename).apply(inputFile)).isFalse();
+ }
+
+ @Test
+ public void should_find_matching_file_in_index() throws IOException {
+ String filename = "some name";
+ InputFile inputFile = mock(InputFile.class);
+ when(inputFile.filename()).thenReturn(filename);
+
+ FileSystem.Index index = mock(FileSystem.Index.class);
+ when(index.getFilesByName(filename)).thenReturn(Collections.singleton(inputFile));
+
+ assertThat(new FilenamePredicate(filename).get(index)).containsOnly(inputFile);
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/OrPredicateTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/OrPredicateTest.java
new file mode 100644
index 00000000000..0804d1f3f7e
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/OrPredicateTest.java
@@ -0,0 +1,66 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.fs.predicates;
+
+import org.junit.Test;
+import org.sonar.api.batch.fs.FilePredicate;
+
+import java.util.Arrays;
+import org.sonar.api.batch.fs.internal.PathPattern;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class OrPredicateTest {
+
+ @Test
+ public void flattenNestedOr() {
+ PathPatternPredicate pathPatternPredicate1 = new PathPatternPredicate(PathPattern.create("foo1/**"));
+ PathPatternPredicate pathPatternPredicate2 = new PathPatternPredicate(PathPattern.create("foo2/**"));
+ PathPatternPredicate pathPatternPredicate3 = new PathPatternPredicate(PathPattern.create("foo3/**"));
+ FilePredicate orPredicate = OrPredicate.create(Arrays.asList(pathPatternPredicate1,
+ OrPredicate.create(Arrays.asList(pathPatternPredicate2, pathPatternPredicate3))));
+ assertThat(((OrPredicate) orPredicate).predicates()).containsExactly(pathPatternPredicate1, pathPatternPredicate2, pathPatternPredicate3);
+ }
+
+ @Test
+ public void simplifyOrExpressionsWhenEmpty() {
+ FilePredicate orPredicate = OrPredicate.create(Arrays.asList());
+ assertThat(orPredicate).isEqualTo(TruePredicate.TRUE);
+ }
+
+ @Test
+ public void simplifyOrExpressionsWhenFalse() {
+ PathPatternPredicate pathPatternPredicate1 = new PathPatternPredicate(PathPattern.create("foo1/**"));
+ PathPatternPredicate pathPatternPredicate2 = new PathPatternPredicate(PathPattern.create("foo2/**"));
+ FilePredicate andPredicate = OrPredicate.create(Arrays.asList(pathPatternPredicate1,
+ FalsePredicate.FALSE, pathPatternPredicate2));
+ assertThat(((OrPredicate) andPredicate).predicates()).containsExactly(pathPatternPredicate1, pathPatternPredicate2);
+ }
+
+ @Test
+ public void simplifyAndExpressionsWhenTrue() {
+ PathPatternPredicate pathPatternPredicate1 = new PathPatternPredicate(PathPattern.create("foo1/**"));
+ PathPatternPredicate pathPatternPredicate2 = new PathPatternPredicate(PathPattern.create("foo2/**"));
+ FilePredicate andPredicate = OrPredicate.create(Arrays.asList(pathPatternPredicate1,
+ TruePredicate.TRUE, pathPatternPredicate2));
+ assertThat(andPredicate).isEqualTo(TruePredicate.TRUE);
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/RelativePathPredicateTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/RelativePathPredicateTest.java
new file mode 100644
index 00000000000..b3c9c2dc9ff
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/predicates/RelativePathPredicateTest.java
@@ -0,0 +1,53 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.fs.predicates;
+
+import org.junit.Test;
+import org.sonar.api.batch.fs.InputFile;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class RelativePathPredicateTest {
+ @Test
+ public void returns_false_when_path_is_invalid() {
+ RelativePathPredicate predicate = new RelativePathPredicate("..");
+ InputFile inputFile = mock(InputFile.class);
+ when(inputFile.relativePath()).thenReturn("path");
+ assertThat(predicate.apply(inputFile)).isFalse();
+ }
+
+ @Test
+ public void returns_true_if_matches() {
+ RelativePathPredicate predicate = new RelativePathPredicate("path");
+ InputFile inputFile = mock(InputFile.class);
+ when(inputFile.relativePath()).thenReturn("path");
+ assertThat(predicate.apply(inputFile)).isTrue();
+ }
+
+ @Test
+ public void returns_false_if_doesnt_match() {
+ RelativePathPredicate predicate = new RelativePathPredicate("path1");
+ InputFile inputFile = mock(InputFile.class);
+ when(inputFile.relativePath()).thenReturn("path2");
+ assertThat(predicate.apply(inputFile)).isFalse();
+ }
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParserTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParserTest.java
index af226b302ee..b90e6a19dd4 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParserTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericCoverageReportParserTest.java
@@ -28,9 +28,9 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
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.SensorContextTester;
import org.sonar.api.utils.MessageException;
+import org.sonar.scanner.fs.TestInputFileBuilder;
+import org.sonar.scanner.sensor.SensorContextTester;
import static org.assertj.core.api.Assertions.assertThat;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParserTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParserTest.java
index 817d7b4aae9..c9f44cd5b8f 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParserTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionReportParserTest.java
@@ -28,12 +28,12 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
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.SensorContextTester;
import org.sonar.api.test.MutableTestCase;
import org.sonar.api.test.MutableTestPlan;
import org.sonar.api.utils.MessageException;
import org.sonar.scanner.deprecated.test.TestPlanBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
+import org.sonar.scanner.sensor.SensorContextTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionSensorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionSensorTest.java
index eb479cc0bce..980ee96bbe8 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionSensorTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/genericcoverage/GenericTestExecutionSensorTest.java
@@ -28,7 +28,6 @@ import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-import org.sonar.api.batch.sensor.internal.SensorContextTester;
import org.sonar.api.config.Encryption;
import org.sonar.api.config.PropertyDefinitions;
import org.sonar.api.utils.log.LogTester;
@@ -36,6 +35,7 @@ import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.scanner.config.DefaultConfiguration;
import org.sonar.scanner.deprecated.test.TestPlanBuilder;
import org.sonar.scanner.scan.ProjectConfiguration;
+import org.sonar.scanner.sensor.SensorContextTester;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java
index 50ea80aceb7..50c247f5b51 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java
@@ -34,7 +34,7 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.scanner.rule.ActiveRulesBuilder;
import org.sonar.scanner.rule.NewActiveRule;
import org.sonar.scanner.rule.RulesBuilder;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/EnforceIssuesFilterTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/EnforceIssuesFilterTest.java
index 80ce4f25eb2..a656a767f71 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/EnforceIssuesFilterTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/EnforceIssuesFilterTest.java
@@ -26,7 +26,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.InputComponent;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.api.notifications.AnalysisWarnings;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.scan.issue.filter.IssueFilterChain;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsLoaderTest.java
index b52ad14b4af..6a597ef85b3 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsLoaderTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsLoaderTest.java
@@ -28,7 +28,7 @@ import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.api.notifications.AnalysisWarnings;
import org.sonar.scanner.issue.ignore.IgnoreIssuesFilter;
import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest.java
index ab54b184c7d..07c39d82113 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/scanner/IssueExclusionsRegexpScannerTest.java
@@ -35,8 +35,8 @@ import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.FileMetadata;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.FileMetadata;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer;
import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader.DoubleRegexpMatcher;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java
index f376d56999e..16154f5ff0c 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/BranchMediumTest.java
@@ -31,7 +31,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.FileMetadata;
+import org.sonar.scanner.fs.FileMetadata;
import org.sonar.scanner.mediumtest.AnalysisResult;
import org.sonar.scanner.mediumtest.ScannerMediumTester;
import org.sonar.scanner.protocol.output.ScannerReport;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleCoverageAndDuplicationExclusionsTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleCoverageAndDuplicationExclusionsTest.java
index 63239c0924a..fe18fdb725f 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleCoverageAndDuplicationExclusionsTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleCoverageAndDuplicationExclusionsTest.java
@@ -26,7 +26,7 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.scanner.scan.ModuleConfiguration;
import org.sonar.scanner.scan.filesystem.ModuleCoverageAndDuplicationExclusions;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleSensorsExecutorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleSensorsExecutorTest.java
index c715462d0ca..bce232d44f0 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleSensorsExecutorTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ModuleSensorsExecutorTest.java
@@ -29,7 +29,7 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
import org.sonar.api.batch.fs.internal.SensorStrategy;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.scanner.sensor.ModuleSensorExtensionDictionnary;
import org.sonar.scanner.bootstrap.ScannerPluginRepository;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ProjectCoverageExclusionsTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ProjectCoverageExclusionsTest.java
index ea27f1cd1f8..b041c6badc8 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ProjectCoverageExclusionsTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/phases/ProjectCoverageExclusionsTest.java
@@ -26,7 +26,7 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.scanner.scan.ProjectConfiguration;
import org.sonar.scanner.scan.filesystem.ProjectCoverageAndDuplicationExclusions;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/PostJobOptimizerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/PostJobOptimizerTest.java
index 45c25ef9568..1b604e11b6e 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/PostJobOptimizerTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/PostJobOptimizerTest.java
@@ -23,8 +23,6 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
-import org.sonar.api.batch.postjob.internal.DefaultPostJobDescriptor;
-import org.sonar.api.config.Settings;
import org.sonar.api.config.internal.MapSettings;
import static org.assertj.core.api.Assertions.assertThat;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java
index 2d155cea756..4d4a3476553 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ChangedLinesPublisherTest.java
@@ -33,7 +33,7 @@ import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.api.batch.scm.ScmProvider;
import org.sonar.scanner.protocol.output.ScannerReportReader;
import org.sonar.scanner.protocol.output.ScannerReportWriter;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java
index 41a5c2eeb78..36134513be9 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ComponentsPublisherTest.java
@@ -33,7 +33,7 @@ import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputFile.Type;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.api.utils.DateUtils;
import org.sonar.scanner.ProjectInfo;
import org.sonar.scanner.protocol.output.FileStructure;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java
index 3c1d845f785..7a66973ec20 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MetadataPublisherTest.java
@@ -41,7 +41,7 @@ import org.sonar.api.CoreProperties;
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.InputModuleHierarchy;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.api.batch.scm.ScmProvider;
import org.sonar.scanner.ProjectInfo;
import org.sonar.scanner.bootstrap.ScannerPlugin;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java
index 4028bdae0af..5f6054f81c9 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/SourcePublisherTest.java
@@ -29,7 +29,7 @@ import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.scanner.protocol.output.ScannerReportWriter;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.scan.filesystem.InputComponentStore;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/ActiveRulesBuilderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/ActiveRulesBuilderTest.java
new file mode 100644
index 00000000000..e892510d046
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/ActiveRulesBuilderTest.java
@@ -0,0 +1,104 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.rule;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.batch.rule.ActiveRule;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.Severity;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class ActiveRulesBuilderTest {
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void no_rules() {
+ ActiveRulesBuilder builder = new ActiveRulesBuilder();
+ ActiveRules rules = builder.build();
+ assertThat(rules.findAll()).isEmpty();
+ }
+
+ @Test
+ public void build_rules() {
+ NewActiveRule activeRule = new NewActiveRule.Builder()
+ .setRuleKey(RuleKey.of("squid", "S0001"))
+ .setName("My Rule")
+ .setSeverity(Severity.CRITICAL)
+ .setInternalKey("__S0001__")
+ .setParam("min", "20")
+ .build();
+
+ ActiveRules activeRules = new ActiveRulesBuilder()
+ .addRule(activeRule)
+ // most simple rule
+ .addRule(new NewActiveRule.Builder().setRuleKey(RuleKey.of("squid", "S0002")).build())
+ .addRule(new NewActiveRule.Builder()
+ .setRuleKey(RuleKey.of("findbugs", "NPE"))
+ .setInternalKey(null)
+ .setSeverity(null)
+ .setParam("foo", null)
+ .build())
+ .build();
+
+ assertThat(activeRules.findAll()).hasSize(3);
+ assertThat(activeRules.findByRepository("squid")).hasSize(2);
+ assertThat(activeRules.findByRepository("findbugs")).hasSize(1);
+ assertThat(activeRules.findByInternalKey("squid", "__S0001__")).isNotNull();
+ assertThat(activeRules.findByRepository("unknown")).isEmpty();
+
+ ActiveRule squid1 = activeRules.find(RuleKey.of("squid", "S0001"));
+ assertThat(squid1.ruleKey().repository()).isEqualTo("squid");
+ assertThat(squid1.ruleKey().rule()).isEqualTo("S0001");
+ assertThat(squid1.severity()).isEqualTo(Severity.CRITICAL);
+ assertThat(squid1.internalKey()).isEqualTo("__S0001__");
+ assertThat(squid1.params()).hasSize(1);
+ assertThat(squid1.param("min")).isEqualTo("20");
+
+ ActiveRule squid2 = activeRules.find(RuleKey.of("squid", "S0002"));
+ assertThat(squid2.ruleKey().repository()).isEqualTo("squid");
+ assertThat(squid2.ruleKey().rule()).isEqualTo("S0002");
+ assertThat(squid2.severity()).isEqualTo(Severity.defaultSeverity());
+ assertThat(squid2.params()).isEmpty();
+
+ ActiveRule findbugsRule = activeRules.find(RuleKey.of("findbugs", "NPE"));
+ assertThat(findbugsRule.severity()).isEqualTo(Severity.defaultSeverity());
+ assertThat(findbugsRule.internalKey()).isNull();
+ assertThat(findbugsRule.params()).isEmpty();
+ }
+
+ @Test
+ public void fail_to_add_twice_the_same_rule() {
+ ActiveRulesBuilder builder = new ActiveRulesBuilder();
+ NewActiveRule rule = new NewActiveRule.Builder()
+ .setRuleKey(RuleKey.of("squid", "S0001"))
+ .build();
+ builder.addRule(rule);
+
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("Rule 'squid:S0001' is already activated");
+
+ builder.addRule(rule);
+ }
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultRulesTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultRulesTest.java
new file mode 100644
index 00000000000..3beb53fcc04
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/DefaultRulesTest.java
@@ -0,0 +1,73 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.rule;
+
+import org.sonar.api.rule.RuleKey;
+import org.junit.Test;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DefaultRulesTest {
+ @Test
+ public void testRepeatedInternalKey() {
+ List<NewRule> newRules = new LinkedList<>();
+ newRules.add(createRule("key1", "repo", "internal"));
+ newRules.add(createRule("key2", "repo", "internal"));
+
+ DefaultRules rules = new DefaultRules(newRules);
+ assertThat(rules.findByInternalKey("repo", "internal")).hasSize(2);
+ assertThat(rules.find(RuleKey.of("repo", "key1"))).isNotNull();
+ assertThat(rules.find(RuleKey.of("repo", "key2"))).isNotNull();
+ assertThat(rules.findByRepository("repo")).hasSize(2);
+ }
+
+ @Test
+ public void testNonExistingKey() {
+ List<NewRule> newRules = new LinkedList<>();
+ newRules.add(createRule("key1", "repo", "internal"));
+ newRules.add(createRule("key2", "repo", "internal"));
+
+ DefaultRules rules = new DefaultRules(newRules);
+ assertThat(rules.findByInternalKey("xx", "xx")).hasSize(0);
+ assertThat(rules.find(RuleKey.of("xxx", "xx"))).isNull();
+ assertThat(rules.findByRepository("xxxx")).hasSize(0);
+ }
+
+ @Test
+ public void testRepeatedRule() {
+ List<NewRule> newRules = new LinkedList<>();
+ newRules.add(createRule("key", "repo", "internal"));
+ newRules.add(createRule("key", "repo", "internal"));
+
+ DefaultRules rules = new DefaultRules(newRules);
+ assertThat(rules.find(RuleKey.of("repo", "key"))).isNotNull();
+ }
+
+ private NewRule createRule(String key, String repo, String internalKey) {
+ RuleKey ruleKey = RuleKey.of(repo, key);
+ NewRule newRule = new NewRule(ruleKey);
+ newRule.setInternalKey(internalKey);
+
+ return newRule;
+ }
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/NewActiveRuleTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/NewActiveRuleTest.java
new file mode 100644
index 00000000000..a3a4e40393d
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/NewActiveRuleTest.java
@@ -0,0 +1,87 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.rule;
+
+import com.google.common.collect.ImmutableMap;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.Severity;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class NewActiveRuleTest {
+
+ private NewActiveRule.Builder builder;
+
+ @Before
+ public void setBuilder() {
+ builder = new NewActiveRule.Builder();
+ }
+
+ @Test
+ public void builder_should_set_every_param() {
+ NewActiveRule rule = builder
+ .setRuleKey(RuleKey.of("foo", "bar"))
+ .setName("name")
+ .setSeverity(Severity.CRITICAL)
+ .setParam("key", "value")
+ .setCreatedAt(1_000L)
+ .setUpdatedAt(1_000L)
+ .setInternalKey("internal_key")
+ .setLanguage("language")
+ .setTemplateRuleKey("templateRuleKey")
+ .setQProfileKey("qProfileKey")
+ .build();
+
+ assertThat(rule.ruleKey).isEqualTo(RuleKey.of("foo", "bar"));
+ assertThat(rule.name).isEqualTo("name");
+ assertThat(rule.severity).isEqualTo(Severity.CRITICAL);
+ assertThat(rule.params).isEqualTo(ImmutableMap.of("key", "value"));
+ assertThat(rule.createdAt).isEqualTo(1_000L);
+ assertThat(rule.updatedAt).isEqualTo(1_000L);
+ assertThat(rule.internalKey).isEqualTo("internal_key");
+ assertThat(rule.language).isEqualTo("language");
+ assertThat(rule.templateRuleKey).isEqualTo("templateRuleKey");
+ assertThat(rule.qProfileKey).isEqualTo("qProfileKey");
+ }
+
+ @Test
+ public void severity_should_have_default_value() {
+ NewActiveRule rule = builder.build();
+ assertThat(rule.severity).isEqualTo(Severity.defaultSeverity());
+ }
+
+ @Test
+ public void params_should_be_empty_map_if_no_params() {
+ NewActiveRule rule = builder.build();
+ assertThat(rule.params).isEqualTo(ImmutableMap.of());
+ }
+
+ @Test
+ public void set_param_remove_param_if_value_is_null() {
+ NewActiveRule rule = builder
+ .setParam("foo", "bar")
+ .setParam("removed", "value")
+ .setParam("removed", null)
+ .build();
+ assertThat(rule.params).isEqualTo(ImmutableMap.of("foo", "bar"));
+ }
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java
index faad5baa933..f7a64f06b63 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/QProfileVerifierTest.java
@@ -24,9 +24,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.slf4j.Logger;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
-import org.sonar.api.config.internal.MapSettings;
-import org.sonar.api.utils.MessageException;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.scan.filesystem.InputComponentStore;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/RulesBuilderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/RulesBuilderTest.java
new file mode 100644
index 00000000000..1bb2375bc21
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/rule/RulesBuilderTest.java
@@ -0,0 +1,112 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.rule;
+
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.batch.rule.Rule;
+import org.sonar.api.batch.rule.Rules;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.rule.Severity;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class RulesBuilderTest {
+ @org.junit.Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void no_rules() {
+ RulesBuilder builder = new RulesBuilder();
+ Rules rules = builder.build();
+ assertThat(rules.findAll()).isEmpty();
+ }
+
+ @Test
+ public void build_rules() {
+ RulesBuilder builder = new RulesBuilder();
+ NewRule newSquid1 = builder.add(RuleKey.of("squid", "S0001"));
+ newSquid1.setName("Detect bug");
+ newSquid1.setDescription("Detect potential bug");
+ newSquid1.setInternalKey("foo=bar");
+ newSquid1.setSeverity(Severity.CRITICAL);
+ newSquid1.setStatus(RuleStatus.BETA);
+ newSquid1.addParam("min");
+ newSquid1.addParam("max").setDescription("Maximum");
+ // most simple rule
+ builder.add(RuleKey.of("squid", "S0002"));
+ builder.add(RuleKey.of("findbugs", "NPE"));
+
+ Rules rules = builder.build();
+
+ assertThat(rules.findAll()).hasSize(3);
+ assertThat(rules.findByRepository("squid")).hasSize(2);
+ assertThat(rules.findByRepository("findbugs")).hasSize(1);
+ assertThat(rules.findByRepository("unknown")).isEmpty();
+
+ Rule squid1 = rules.find(RuleKey.of("squid", "S0001"));
+ assertThat(squid1.key().repository()).isEqualTo("squid");
+ assertThat(squid1.key().rule()).isEqualTo("S0001");
+ assertThat(squid1.name()).isEqualTo("Detect bug");
+ assertThat(squid1.description()).isEqualTo("Detect potential bug");
+ assertThat(squid1.internalKey()).isEqualTo("foo=bar");
+ assertThat(squid1.status()).isEqualTo(RuleStatus.BETA);
+ assertThat(squid1.severity()).isEqualTo(Severity.CRITICAL);
+ assertThat(squid1.params()).hasSize(2);
+ assertThat(squid1.param("min").key()).isEqualTo("min");
+ assertThat(squid1.param("min").description()).isNull();
+ assertThat(squid1.param("max").key()).isEqualTo("max");
+ assertThat(squid1.param("max").description()).isEqualTo("Maximum");
+
+ Rule squid2 = rules.find(RuleKey.of("squid", "S0002"));
+ assertThat(squid2.key().repository()).isEqualTo("squid");
+ assertThat(squid2.key().rule()).isEqualTo("S0002");
+ assertThat(squid2.description()).isNull();
+ assertThat(squid2.internalKey()).isNull();
+ assertThat(squid2.status()).isEqualTo(RuleStatus.defaultStatus());
+ assertThat(squid2.severity()).isEqualTo(Severity.defaultSeverity());
+ assertThat(squid2.params()).isEmpty();
+ }
+
+ @Test
+ public void fail_to_add_twice_the_same_rule() {
+ RulesBuilder builder = new RulesBuilder();
+ builder.add(RuleKey.of("squid", "S0001"));
+
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("Rule 'squid:S0001' already exists");
+
+ builder.add(RuleKey.of("squid", "S0001"));
+ }
+
+ @Test
+ public void fail_to_add_twice_the_same_param() {
+ RulesBuilder builder = new RulesBuilder();
+ NewRule newRule = builder.add(RuleKey.of("squid", "S0001"));
+ newRule.addParam("min");
+ newRule.addParam("max");
+
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("Parameter 'min' already exists on rule 'squid:S0001'");
+
+ newRule.addParam("min");
+ }
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/AdditionalFilePredicatesTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/AdditionalFilePredicatesTest.java
index be8da961f40..37775d19ae5 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/AdditionalFilePredicatesTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/AdditionalFilePredicatesTest.java
@@ -22,7 +22,7 @@ package org.sonar.scanner.scan.filesystem;
import org.junit.Test;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import static org.assertj.core.api.Assertions.assertThat;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java
index 98785e06e62..04490bd02e4 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java
@@ -35,7 +35,7 @@ import org.sonar.api.batch.fs.InputPath;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/MetadataGeneratorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/MetadataGeneratorTest.java
index cc4b6bf2485..9bd732b9287 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/MetadataGeneratorTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/MetadataGeneratorTest.java
@@ -33,10 +33,10 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
-import org.sonar.api.batch.fs.internal.FileMetadata;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.notifications.AnalysisWarnings;
import org.sonar.api.utils.PathUtils;
+import org.sonar.scanner.fs.FileMetadata;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.scanner.issue.ignore.IgnoreIssuesFilter;
import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer;
import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader;
@@ -59,7 +59,7 @@ public class MetadataGeneratorTest {
private MetadataGenerator generator;
@Before
- public void setUp() throws IOException {
+ public void setUp() {
MockitoAnnotations.initMocks(this);
metadata = new FileMetadata();
IssueExclusionsLoader issueExclusionsLoader = new IssueExclusionsLoader(mock(IssueExclusionPatternInitializer.class), mock(IgnoreIssuesFilter.class),
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java
index 29064edd51b..2ead8234b68 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ModuleInputComponentStoreTest.java
@@ -28,7 +28,7 @@ import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputModule;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.batch.fs.internal.SensorStrategy;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import static org.assertj.core.api.Assertions.assertThat;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionTest.java
index 857f12e9f69..ccd56feef74 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionTest.java
@@ -27,7 +27,7 @@ import org.junit.Before;
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.scanner.fs.TestInputFileBuilder;
import org.sonar.scanner.repository.FileData;
import org.sonar.scanner.repository.ProjectRepositoriesSupplier;
import org.sonar.scanner.repository.SingleProjectRepository;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/DefaultBlameOutputTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/DefaultBlameOutputTest.java
index 78a9c53a4f3..a408dde4a41 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/DefaultBlameOutputTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/DefaultBlameOutputTest.java
@@ -25,7 +25,7 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.api.batch.scm.BlameLine;
public class DefaultBlameOutputTest {
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultAdHocRuleTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultAdHocRuleTest.java
new file mode 100644
index 00000000000..44a53e7dbf9
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultAdHocRuleTest.java
@@ -0,0 +1,156 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.sensor;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.batch.rule.Severity;
+import org.sonar.api.batch.sensor.internal.SensorStorage;
+import org.sonar.api.batch.sensor.rule.NewAdHocRule;
+import org.sonar.api.rules.RuleType;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class DefaultAdHocRuleTest {
+
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+
+ @Test
+ public void store() {
+ SensorStorage storage = mock(SensorStorage.class);
+ DefaultAdHocRule rule = new DefaultAdHocRule(storage)
+ .engineId("engine")
+ .ruleId("ruleId")
+ .name("name")
+ .description("desc")
+ .severity(Severity.BLOCKER)
+ .type(RuleType.CODE_SMELL);
+ rule.save();
+
+ assertThat(rule.engineId()).isEqualTo("engine");
+ assertThat(rule.ruleId()).isEqualTo("ruleId");
+ assertThat(rule.name()).isEqualTo("name");
+ assertThat(rule.description()).isEqualTo("desc");
+ assertThat(rule.severity()).isEqualTo(Severity.BLOCKER);
+ assertThat(rule.type()).isEqualTo(RuleType.CODE_SMELL);
+
+ verify(storage).store(any(DefaultAdHocRule.class));
+ }
+
+
+ @Test
+ public void description_is_optional() {
+ SensorStorage storage = mock(SensorStorage.class);
+ new DefaultAdHocRule(storage)
+ .engineId("engine")
+ .ruleId("ruleId")
+ .name("name")
+ .severity(Severity.BLOCKER)
+ .type(RuleType.CODE_SMELL)
+ .save();
+
+ verify(storage).store(any(DefaultAdHocRule.class));
+ }
+
+ @Test
+ public void fail_to_store_if_no_engine_id() {
+ SensorStorage storage = mock(SensorStorage.class);
+ NewAdHocRule rule = new DefaultAdHocRule(storage)
+ .engineId(" ")
+ .ruleId("ruleId")
+ .name("name")
+ .description("desc")
+ .severity(Severity.BLOCKER)
+ .type(RuleType.CODE_SMELL);
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Engine id is mandatory");
+ rule.save();
+ }
+
+ @Test
+ public void fail_to_store_if_no_rule_id() {
+ SensorStorage storage = mock(SensorStorage.class);
+ NewAdHocRule rule = new DefaultAdHocRule(storage)
+ .engineId("engine")
+ .ruleId(" ")
+ .name("name")
+ .description("desc")
+ .severity(Severity.BLOCKER)
+ .type(RuleType.CODE_SMELL);
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Rule id is mandatory");
+ rule.save();
+ }
+
+ @Test
+ public void fail_to_store_if_no_name() {
+ SensorStorage storage = mock(SensorStorage.class);
+ NewAdHocRule rule = new DefaultAdHocRule(storage)
+ .engineId("engine")
+ .ruleId("ruleId")
+ .name(" ")
+ .description("desc")
+ .severity(Severity.BLOCKER)
+ .type(RuleType.CODE_SMELL);
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Name is mandatory");
+ rule.save();
+ }
+
+
+ @Test
+ public void fail_to_store_if_no_severity() {
+ SensorStorage storage = mock(SensorStorage.class);
+ NewAdHocRule rule = new DefaultAdHocRule(storage)
+ .engineId("engine")
+ .ruleId("ruleId")
+ .name("name")
+ .description("desc")
+ .type(RuleType.CODE_SMELL);
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Severity is mandatory");
+ rule.save();
+ }
+
+ @Test
+ public void fail_to_store_if_no_type() {
+ SensorStorage storage = mock(SensorStorage.class);
+ NewAdHocRule rule = new DefaultAdHocRule(storage)
+ .engineId("engine")
+ .ruleId("ruleId")
+ .name("name")
+ .description("desc")
+ .severity(Severity.BLOCKER);
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Type is mandatory");
+ rule.save();
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultExternalIssueTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultExternalIssueTest.java
new file mode 100644
index 00000000000..ca91e301cc8
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultExternalIssueTest.java
@@ -0,0 +1,159 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.sensor;
+
+import java.io.IOException;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.fs.InputComponent;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.DefaultInputProject;
+import org.sonar.api.batch.rule.Severity;
+import org.sonar.api.batch.sensor.internal.SensorStorage;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rules.RuleType;
+import org.sonar.scanner.fs.TestInputFileBuilder;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class DefaultExternalIssueTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ private DefaultInputProject project;
+
+ @Before
+ public void setup() throws IOException {
+ project = new DefaultInputProject(ProjectDefinition.create()
+ .setKey("foo")
+ .setBaseDir(temp.newFolder())
+ .setWorkDir(temp.newFolder()));
+ }
+
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+
+ private DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
+ .initMetadata("Foo\nBar\n")
+ .build();
+
+ @Test
+ public void build_file_issue() {
+ SensorStorage storage = mock(SensorStorage.class);
+ DefaultExternalIssue issue = new DefaultExternalIssue(project, storage)
+ .at(new DefaultIssueLocation()
+ .on(inputFile)
+ .at(inputFile.selectLine(1))
+ .message("Wrong way!"))
+ .forRule(RuleKey.of("repo", "rule"))
+ .remediationEffortMinutes(10l)
+ .type(RuleType.BUG)
+ .severity(Severity.BLOCKER);
+
+ assertThat(issue.primaryLocation().inputComponent()).isEqualTo(inputFile);
+ assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("external_repo", "rule"));
+ assertThat(issue.engineId()).isEqualTo("repo");
+ assertThat(issue.ruleId()).isEqualTo("rule");
+ assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(1);
+ assertThat(issue.remediationEffort()).isEqualTo(10l);
+ assertThat(issue.type()).isEqualTo(RuleType.BUG);
+ assertThat(issue.severity()).isEqualTo(Severity.BLOCKER);
+ assertThat(issue.primaryLocation().message()).isEqualTo("Wrong way!");
+
+ issue.save();
+
+ verify(storage).store(issue);
+ }
+
+ @Test
+ public void fail_to_store_if_no_type() {
+ SensorStorage storage = mock(SensorStorage.class);
+ DefaultExternalIssue issue = new DefaultExternalIssue(project, storage)
+ .at(new DefaultIssueLocation()
+ .on(inputFile)
+ .at(inputFile.selectLine(1))
+ .message("Wrong way!"))
+ .forRule(RuleKey.of("repo", "rule"))
+ .remediationEffortMinutes(10l)
+ .severity(Severity.BLOCKER);
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Type is mandatory");
+ issue.save();
+ }
+
+ @Test
+ public void fail_to_store_if_primary_location_is_not_a_file() {
+ SensorStorage storage = mock(SensorStorage.class);
+ DefaultExternalIssue issue = new DefaultExternalIssue(project, storage)
+ .at(new DefaultIssueLocation()
+ .on(mock(InputComponent.class))
+ .message("Wrong way!"))
+ .forRule(RuleKey.of("repo", "rule"))
+ .remediationEffortMinutes(10l)
+ .severity(Severity.BLOCKER);
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("External issues must be located in files");
+ issue.save();
+ }
+
+ @Test
+ public void fail_to_store_if_primary_location_has_no_message() {
+ SensorStorage storage = mock(SensorStorage.class);
+ DefaultExternalIssue issue = new DefaultExternalIssue(project, storage)
+ .at(new DefaultIssueLocation()
+ .on(inputFile)
+ .at(inputFile.selectLine(1)))
+ .forRule(RuleKey.of("repo", "rule"))
+ .remediationEffortMinutes(10l)
+ .type(RuleType.BUG)
+ .severity(Severity.BLOCKER);
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("External issues must have a message");
+ issue.save();
+ }
+
+ @Test
+ public void fail_to_store_if_no_severity() {
+ SensorStorage storage = mock(SensorStorage.class);
+ DefaultExternalIssue issue = new DefaultExternalIssue(project, storage)
+ .at(new DefaultIssueLocation()
+ .on(inputFile)
+ .at(inputFile.selectLine(1))
+ .message("Wrong way!"))
+ .forRule(RuleKey.of("repo", "rule"))
+ .remediationEffortMinutes(10l)
+ .type(RuleType.BUG);
+
+ exception.expect(IllegalStateException.class);
+ exception.expectMessage("Severity is mandatory");
+ issue.save();
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultHighlightingTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultHighlightingTest.java
new file mode 100644
index 00000000000..712703976c2
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultHighlightingTest.java
@@ -0,0 +1,126 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.sensor;
+
+import java.util.Collection;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.TextRange;
+import org.sonar.api.batch.fs.internal.DefaultTextPointer;
+import org.sonar.api.batch.fs.internal.DefaultTextRange;
+import org.sonar.api.batch.sensor.internal.SensorStorage;
+import org.sonar.scanner.fs.TestInputFileBuilder;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.sonar.api.batch.sensor.highlighting.TypeOfText.COMMENT;
+import static org.sonar.api.batch.sensor.highlighting.TypeOfText.CPP_DOC;
+import static org.sonar.api.batch.sensor.highlighting.TypeOfText.KEYWORD;
+
+public class DefaultHighlightingTest {
+
+ private static final InputFile INPUT_FILE = new TestInputFileBuilder("foo", "src/Foo.java")
+ .setLines(2)
+ .setOriginalLineStartOffsets(new int[] {0, 50})
+ .setOriginalLineEndOffsets(new int[] {49, 100})
+ .setLastValidOffset(101)
+ .build();
+
+ private Collection<SyntaxHighlightingRule> highlightingRules;
+
+ @Rule
+ public ExpectedException throwable = ExpectedException.none();
+
+ @Before
+ public void setUpSampleRules() {
+
+ DefaultHighlighting highlightingDataBuilder = new DefaultHighlighting(mock(SensorStorage.class))
+ .onFile(INPUT_FILE)
+ .highlight(0, 10, COMMENT)
+ .highlight(1, 10, 1, 12, KEYWORD)
+ .highlight(24, 38, KEYWORD)
+ .highlight(42, 50, KEYWORD)
+ .highlight(24, 65, CPP_DOC)
+ .highlight(12, 20, COMMENT);
+
+ highlightingDataBuilder.save();
+
+ highlightingRules = highlightingDataBuilder.getSyntaxHighlightingRuleSet();
+ }
+
+ @Test
+ public void should_register_highlighting_rule() {
+ assertThat(highlightingRules).hasSize(6);
+ }
+
+ private static TextRange rangeOf(int startLine, int startOffset, int endLine, int endOffset) {
+ return new DefaultTextRange(new DefaultTextPointer(startLine, startOffset), new DefaultTextPointer(endLine, endOffset));
+ }
+
+ @Test
+ public void should_order_by_start_then_end_offset() {
+ assertThat(highlightingRules).extracting("range", TextRange.class).containsExactly(
+ rangeOf(1, 0, 1, 10),
+ rangeOf(1, 10, 1, 12),
+ rangeOf(1, 12, 1, 20),
+ rangeOf(1, 24, 2, 15),
+ rangeOf(1, 24, 1, 38),
+ rangeOf(1, 42, 2, 0));
+ assertThat(highlightingRules).extracting("textType").containsExactly(COMMENT, KEYWORD, COMMENT, CPP_DOC, KEYWORD, KEYWORD);
+ }
+
+ @Test
+ public void should_support_overlapping() {
+ new DefaultHighlighting(mock(SensorStorage.class))
+ .onFile(INPUT_FILE)
+ .highlight(0, 15, KEYWORD)
+ .highlight(8, 12, CPP_DOC)
+ .save();
+ }
+
+ @Test
+ public void should_prevent_start_equal_end() {
+ throwable.expect(IllegalArgumentException.class);
+ throwable
+ .expectMessage("Unable to highlight file");
+
+ new DefaultHighlighting(mock(SensorStorage.class))
+ .onFile(INPUT_FILE)
+ .highlight(10, 10, KEYWORD)
+ .save();
+ }
+
+ @Test
+ public void should_prevent_boudaries_overlapping() {
+ throwable.expect(IllegalStateException.class);
+ throwable
+ .expectMessage("Cannot register highlighting rule for characters at Range[from [line=1, lineOffset=8] to [line=1, lineOffset=15]] as it overlaps at least one existing rule");
+
+ new DefaultHighlighting(mock(SensorStorage.class))
+ .onFile(INPUT_FILE)
+ .highlight(0, 10, KEYWORD)
+ .highlight(8, 15, KEYWORD)
+ .save();
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultIssueLocationTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultIssueLocationTest.java
new file mode 100644
index 00000000000..96bd5936b6b
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultIssueLocationTest.java
@@ -0,0 +1,107 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.sensor;
+
+import org.apache.commons.lang.StringUtils;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.hamcrest.TypeSafeMatcher;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.scanner.fs.TestInputFileBuilder;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.rules.ExpectedException.none;
+
+public class DefaultIssueLocationTest {
+
+ @Rule
+ public ExpectedException thrown = none();
+
+ private InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
+ .initMetadata("Foo\nBar\n")
+ .build();
+
+ @Test
+ public void should_build() {
+ assertThat(new DefaultIssueLocation()
+ .on(inputFile)
+ .message("pipo bimbo")
+ .message()
+ ).isEqualTo("pipo bimbo");
+ }
+
+ @Test
+ public void not_allowed_to_call_on_twice() {
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("on() already called");
+ new DefaultIssueLocation()
+ .on(inputFile)
+ .on(inputFile)
+ .message("Wrong way!");
+ }
+
+ @Test
+ public void prevent_too_long_messages() {
+ assertThat(new DefaultIssueLocation()
+ .on(inputFile)
+ .message(StringUtils.repeat("a", 4000)).message()).hasSize(4000);
+
+ assertThat(new DefaultIssueLocation()
+ .on(inputFile)
+ .message(StringUtils.repeat("a", 4001)).message()).hasSize(4000);
+ }
+
+ @Test
+ public void prevent_null_character_in_message_text() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Character \\u0000 is not supported in issue message");
+
+ new DefaultIssueLocation()
+ .message("pipo " + '\u0000' + " bimbo");
+ }
+
+ @Test
+ public void prevent_null_character_in_message_text_when_builder_has_been_initialized() {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage(customMatcher("Character \\u0000 is not supported in issue message", ", on component: src/Foo.php"));
+
+ new DefaultIssueLocation()
+ .on(inputFile)
+ .message("pipo " + '\u0000' + " bimbo");
+ }
+
+ private Matcher<String> customMatcher(String startWith, String endWith) {
+ return new TypeSafeMatcher<String>() {
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("Invalid message");
+ }
+
+ @Override
+ protected boolean matchesSafely(final String item) {
+ return item.startsWith(startWith) && item.endsWith(endWith);
+ }
+ };
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultIssueTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultIssueTest.java
new file mode 100644
index 00000000000..0ab0b99655b
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultIssueTest.java
@@ -0,0 +1,157 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.sensor;
+
+import java.io.File;
+import java.io.IOException;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.fs.internal.DefaultInputDir;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.DefaultInputProject;
+import org.sonar.api.batch.rule.Severity;
+import org.sonar.api.batch.sensor.internal.SensorStorage;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.scanner.fs.TestInputFileBuilder;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class DefaultIssueTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ private DefaultInputProject project;
+
+ private DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.php")
+ .initMetadata("Foo\nBar\n")
+ .build();
+
+ @Before
+ public void prepare() throws IOException {
+ project = new DefaultInputProject(ProjectDefinition.create()
+ .setKey("foo")
+ .setBaseDir(temp.newFolder())
+ .setWorkDir(temp.newFolder()));
+ }
+
+ @Test
+ public void build_file_issue() {
+ SensorStorage storage = mock(SensorStorage.class);
+ DefaultIssue issue = new DefaultIssue(project, storage)
+ .at(new DefaultIssueLocation()
+ .on(inputFile)
+ .at(inputFile.selectLine(1))
+ .message("Wrong way!"))
+ .forRule(RuleKey.of("repo", "rule"))
+ .gap(10.0);
+
+ assertThat(issue.primaryLocation().inputComponent()).isEqualTo(inputFile);
+ assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule"));
+ assertThat(issue.primaryLocation().textRange().start().line()).isEqualTo(1);
+ assertThat(issue.gap()).isEqualTo(10.0);
+ assertThat(issue.primaryLocation().message()).isEqualTo("Wrong way!");
+
+ issue.save();
+
+ verify(storage).store(issue);
+ }
+
+ @Test
+ public void move_directory_issue_to_project_root() {
+ SensorStorage storage = mock(SensorStorage.class);
+ DefaultIssue issue = new DefaultIssue(project, storage)
+ .at(new DefaultIssueLocation()
+ .on(new DefaultInputDir("foo", "src/main").setModuleBaseDir(project.getBaseDir()))
+ .message("Wrong way!"))
+ .forRule(RuleKey.of("repo", "rule"))
+ .overrideSeverity(Severity.BLOCKER);
+
+ assertThat(issue.primaryLocation().inputComponent()).isEqualTo(project);
+ assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule"));
+ assertThat(issue.primaryLocation().textRange()).isNull();
+ assertThat(issue.primaryLocation().message()).isEqualTo("[src/main] Wrong way!");
+ assertThat(issue.overriddenSeverity()).isEqualTo(Severity.BLOCKER);
+
+ issue.save();
+
+ verify(storage).store(issue);
+ }
+
+ @Test
+ public void move_submodule_issue_to_project_root() {
+ File subModuleDirectory = new File(project.getBaseDir().toString(), "bar");
+ subModuleDirectory.mkdir();
+
+ ProjectDefinition subModuleDefinition = ProjectDefinition.create()
+ .setKey("foo/bar")
+ .setBaseDir(subModuleDirectory)
+ .setWorkDir(subModuleDirectory);
+ project.definition().addSubProject(subModuleDefinition);
+ DefaultInputModule subModule = new DefaultInputModule(subModuleDefinition);
+
+ SensorStorage storage = mock(SensorStorage.class);
+ DefaultIssue issue = new DefaultIssue(project, storage)
+ .at(new DefaultIssueLocation()
+ .on(subModule)
+ .message("Wrong way!"))
+ .forRule(RuleKey.of("repo", "rule"))
+ .overrideSeverity(Severity.BLOCKER);
+
+ assertThat(issue.primaryLocation().inputComponent()).isEqualTo(project);
+ assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule"));
+ assertThat(issue.primaryLocation().textRange()).isNull();
+ assertThat(issue.primaryLocation().message()).isEqualTo("[bar] Wrong way!");
+ assertThat(issue.overriddenSeverity()).isEqualTo(Severity.BLOCKER);
+
+ issue.save();
+
+ verify(storage).store(issue);
+ }
+
+ @Test
+ public void build_project_issue() throws IOException {
+ SensorStorage storage = mock(SensorStorage.class);
+ DefaultInputModule inputModule = new DefaultInputModule(ProjectDefinition.create().setKey("foo").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
+ DefaultIssue issue = new DefaultIssue(project, storage)
+ .at(new DefaultIssueLocation()
+ .on(inputModule)
+ .message("Wrong way!"))
+ .forRule(RuleKey.of("repo", "rule"))
+ .gap(10.0);
+
+ assertThat(issue.primaryLocation().inputComponent()).isEqualTo(inputModule);
+ assertThat(issue.ruleKey()).isEqualTo(RuleKey.of("repo", "rule"));
+ assertThat(issue.primaryLocation().textRange()).isNull();
+ assertThat(issue.gap()).isEqualTo(10.0);
+ assertThat(issue.primaryLocation().message()).isEqualTo("Wrong way!");
+
+ issue.save();
+
+ verify(storage).store(issue);
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultMeasureTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultMeasureTest.java
new file mode 100644
index 00000000000..43844837e1e
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultMeasureTest.java
@@ -0,0 +1,92 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.sensor;
+
+import java.io.IOException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
+import org.sonar.api.batch.fs.internal.DefaultInputProject;
+import org.sonar.api.batch.sensor.internal.SensorStorage;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.scanner.fs.TestInputFileBuilder;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+public class DefaultMeasureTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void build_file_measure() {
+ SensorStorage storage = mock(SensorStorage.class);
+ DefaultMeasure<Integer> newMeasure = new DefaultMeasure<Integer>(storage)
+ .forMetric(CoreMetrics.LINES)
+ .on(new TestInputFileBuilder("foo", "src/Foo.php").build())
+ .withValue(3);
+
+ assertThat(newMeasure.inputComponent()).isEqualTo(new TestInputFileBuilder("foo", "src/Foo.php").build());
+ assertThat(newMeasure.metric()).isEqualTo(CoreMetrics.LINES);
+ assertThat(newMeasure.value()).isEqualTo(3);
+
+ newMeasure.save();
+
+ verify(storage).store(newMeasure);
+ }
+
+ @Test
+ public void build_project_measure() throws IOException {
+ SensorStorage storage = mock(SensorStorage.class);
+ AbstractProjectOrModule module = new DefaultInputProject(ProjectDefinition.create().setKey("foo").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
+ DefaultMeasure<Integer> newMeasure = new DefaultMeasure<Integer>(storage)
+ .forMetric(CoreMetrics.LINES)
+ .on(module)
+ .withValue(3);
+
+ assertThat(newMeasure.inputComponent()).isEqualTo(module);
+ assertThat(newMeasure.metric()).isEqualTo(CoreMetrics.LINES);
+ assertThat(newMeasure.value()).isEqualTo(3);
+
+ newMeasure.save();
+
+ verify(storage).store(newMeasure);
+ }
+
+ @Test
+ public void not_allowed_to_call_on_twice() throws IOException {
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("on() already called");
+ new DefaultMeasure<Integer>()
+ .on(new DefaultInputProject(ProjectDefinition.create().setKey("foo").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())))
+ .on(new TestInputFileBuilder("foo", "src/Foo.php").build())
+ .withValue(3)
+ .save();
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorDescriptorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorDescriptorTest.java
new file mode 100644
index 00000000000..a43272ba20d
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorDescriptorTest.java
@@ -0,0 +1,51 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.sensor;
+
+import org.junit.Test;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.config.internal.MapSettings;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DefaultSensorDescriptorTest {
+
+ @Test
+ public void describe() {
+ DefaultSensorDescriptor descriptor = new DefaultSensorDescriptor();
+ descriptor
+ .name("Foo")
+ .onlyOnLanguage("java")
+ .onlyOnFileType(InputFile.Type.MAIN)
+ .requireProperty("sonar.foo.reportPath", "sonar.foo.reportPath2")
+ .createIssuesForRuleRepository("squid-java");
+
+ assertThat(descriptor.name()).isEqualTo("Foo");
+ assertThat(descriptor.languages()).containsOnly("java");
+ assertThat(descriptor.type()).isEqualTo(InputFile.Type.MAIN);
+ MapSettings settings = new MapSettings();
+ settings.setProperty("sonar.foo.reportPath", "foo");
+ assertThat(descriptor.configurationPredicate().test(settings.asConfig())).isFalse();
+ settings.setProperty("sonar.foo.reportPath2", "foo");
+ assertThat(descriptor.configurationPredicate().test(settings.asConfig())).isTrue();
+ assertThat(descriptor.ruleRepositories()).containsOnly("squid-java");
+ }
+
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
index bdefb6f3436..d0aca624090 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java
@@ -33,11 +33,9 @@ import org.sonar.api.batch.fs.internal.DefaultInputDir;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
-import org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting;
import org.sonar.api.batch.sensor.issue.ExternalIssue;
import org.sonar.api.batch.sensor.issue.Issue;
import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable;
@@ -45,6 +43,7 @@ import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.core.metric.ScannerMetrics;
import org.sonar.scanner.cpd.index.SonarCpdBlockIndex;
+import org.sonar.scanner.fs.TestInputFileBuilder;
import org.sonar.scanner.issue.IssuePublisher;
import org.sonar.scanner.protocol.output.FileStructure;
import org.sonar.scanner.protocol.output.ScannerReport;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/InMemorySensorStorageTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/InMemorySensorStorageTest.java
new file mode 100644
index 00000000000..76e4874a41a
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/InMemorySensorStorageTest.java
@@ -0,0 +1,59 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.sensor;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.data.MapEntry.entry;
+
+public class InMemorySensorStorageTest {
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ InMemorySensorStorage underTest = new InMemorySensorStorage();
+
+ @Test
+ public void test_storeProperty() {
+ assertThat(underTest.contextProperties).isEmpty();
+
+ underTest.storeProperty("foo", "bar");
+ assertThat(underTest.contextProperties).containsOnly(entry("foo", "bar"));
+ }
+
+ @Test
+ public void storeProperty_throws_IAE_if_key_is_null() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Key of context property must not be null");
+
+ underTest.storeProperty(null, "bar");
+ }
+
+ @Test
+ public void storeProperty_throws_IAE_if_value_is_null() {
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage("Value of context property must not be null");
+
+ underTest.storeProperty("foo", null);
+ }
+}
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java
index f64ba90b3f1..01d3b6046e8 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorContextTest.java
@@ -19,25 +19,25 @@
*/
package org.sonar.scanner.sensor;
-import org.sonar.api.SonarEdition;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
+import org.sonar.api.SonarEdition;
import org.sonar.api.SonarQubeSide;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.fs.InputModule;
-import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.api.batch.measure.MetricFinder;
import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.scanner.rule.ActiveRulesBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.internal.SonarRuntimeImpl;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.utils.Version;
+import org.sonar.scanner.fs.DefaultFileSystem;
+import org.sonar.scanner.rule.ActiveRulesBuilder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorOptimizerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorOptimizerTest.java
index 2a31dae65cc..0a78c6b2984 100644
--- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorOptimizerTest.java
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/ModuleSensorOptimizerTest.java
@@ -25,14 +25,13 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.InputFile;
-import org.sonar.api.batch.fs.internal.DefaultFileSystem;
-import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.scanner.rule.ActiveRulesBuilder;
-import org.sonar.scanner.rule.NewActiveRule;
-import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.config.internal.MapSettings;
import org.sonar.api.rule.RuleKey;
+import org.sonar.scanner.fs.DefaultFileSystem;
+import org.sonar.scanner.fs.TestInputFileBuilder;
+import org.sonar.scanner.rule.ActiveRulesBuilder;
+import org.sonar.scanner.rule.NewActiveRule;
import static org.assertj.core.api.Assertions.assertThat;
diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/SensorContextTesterTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/SensorContextTesterTest.java
new file mode 100644
index 00000000000..cf1d452f76a
--- /dev/null
+++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/SensorContextTesterTest.java
@@ -0,0 +1,375 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2019 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.scanner.sensor;
+
+import java.io.File;
+import java.io.IOException;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
+import org.sonar.api.batch.fs.InputFile;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
+import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.batch.fs.internal.DefaultTextPointer;
+import org.sonar.scanner.fs.DefaultFileSystem;
+import org.sonar.scanner.fs.TestInputFileBuilder;
+import org.sonar.api.batch.rule.ActiveRules;
+import org.sonar.api.batch.rule.Severity;
+import org.sonar.api.batch.sensor.error.AnalysisError;
+import org.sonar.api.batch.sensor.error.NewAnalysisError;
+import org.sonar.api.batch.sensor.highlighting.TypeOfText;
+import org.sonar.api.batch.sensor.issue.NewExternalIssue;
+import org.sonar.api.batch.sensor.issue.NewIssue;
+import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
+import org.sonar.api.config.Settings;
+import org.sonar.api.config.internal.MapSettings;
+import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rules.RuleType;
+import org.sonar.scanner.rule.ActiveRulesBuilder;
+import org.sonar.scanner.rule.NewActiveRule;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
+import static org.assertj.core.data.MapEntry.entry;
+
+public class SensorContextTesterTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Rule
+ public ExpectedException exception = ExpectedException.none();
+
+ private SensorContextTester tester;
+ private File baseDir;
+
+ @Before
+ public void prepare() throws Exception {
+ baseDir = temp.newFolder();
+ tester = SensorContextTester.create(baseDir);
+ }
+
+ @Test
+ public void testSettings() {
+ Settings settings = new MapSettings();
+ settings.setProperty("foo", "bar");
+ tester.setSettings(settings);
+ assertThat(tester.settings().getString("foo")).isEqualTo("bar");
+ }
+
+ @Test
+ public void testActiveRules() {
+ NewActiveRule activeRule = new NewActiveRule.Builder()
+ .setRuleKey(RuleKey.of("foo", "bar"))
+ .build();
+ ActiveRules activeRules = new ActiveRulesBuilder().addRule(activeRule).build();
+ tester.setActiveRules(activeRules);
+ assertThat(tester.activeRules().findAll()).hasSize(1);
+ }
+
+ @Test
+ public void testFs() throws Exception {
+ DefaultFileSystem fs = new DefaultFileSystem(temp.newFolder());
+ tester.setFileSystem(fs);
+ assertThat(tester.fileSystem().baseDir()).isNotEqualTo(baseDir);
+ }
+
+ @Test
+ public void testIssues() {
+ assertThat(tester.allIssues()).isEmpty();
+ NewIssue newIssue = tester.newIssue();
+ newIssue
+ .at(newIssue.newLocation().on(new TestInputFileBuilder("foo", "src/Foo.java").build()))
+ .forRule(RuleKey.of("repo", "rule"))
+ .save();
+ newIssue = tester.newIssue();
+ newIssue
+ .at(newIssue.newLocation().on(new TestInputFileBuilder("foo", "src/Foo.java").build()))
+ .forRule(RuleKey.of("repo", "rule"))
+ .save();
+ assertThat(tester.allIssues()).hasSize(2);
+ }
+
+ @Test
+ public void testExternalIssues() {
+ assertThat(tester.allExternalIssues()).isEmpty();
+ NewExternalIssue newExternalIssue = tester.newExternalIssue();
+ newExternalIssue
+ .at(newExternalIssue.newLocation().message("message").on(new TestInputFileBuilder("foo", "src/Foo.java").build()))
+ .forRule(RuleKey.of("repo", "rule"))
+ .type(RuleType.BUG)
+ .severity(Severity.BLOCKER)
+ .save();
+ newExternalIssue = tester.newExternalIssue();
+ newExternalIssue
+ .at(newExternalIssue.newLocation().message("message").on(new TestInputFileBuilder("foo", "src/Foo.java").build()))
+ .type(RuleType.BUG)
+ .severity(Severity.BLOCKER)
+ .forRule(RuleKey.of("repo", "rule"))
+ .save();
+ assertThat(tester.allExternalIssues()).hasSize(2);
+ }
+
+ @Test
+ public void testAnalysisErrors() {
+ assertThat(tester.allAnalysisErrors()).isEmpty();
+ NewAnalysisError newAnalysisError = tester.newAnalysisError();
+
+ InputFile file = new TestInputFileBuilder("foo", "src/Foo.java").build();
+ newAnalysisError.onFile(file)
+ .message("error")
+ .at(new DefaultTextPointer(5, 2))
+ .save();
+
+ assertThat(tester.allAnalysisErrors()).hasSize(1);
+ AnalysisError analysisError = tester.allAnalysisErrors().iterator().next();
+
+ assertThat(analysisError.inputFile()).isEqualTo(file);
+ assertThat(analysisError.message()).isEqualTo("error");
+ assertThat(analysisError.location()).isEqualTo(new DefaultTextPointer(5, 2));
+
+ }
+
+ @Test
+ public void testMeasures() throws IOException {
+ assertThat(tester.measures("foo:src/Foo.java")).isEmpty();
+ assertThat(tester.measure("foo:src/Foo.java", "ncloc")).isNull();
+ tester.<Integer>newMeasure()
+ .on(new TestInputFileBuilder("foo", "src/Foo.java").build())
+ .forMetric(CoreMetrics.NCLOC)
+ .withValue(2)
+ .save();
+ assertThat(tester.measures("foo:src/Foo.java")).hasSize(1);
+ assertThat(tester.measure("foo:src/Foo.java", "ncloc")).isNotNull();
+ tester.<Integer>newMeasure()
+ .on(new TestInputFileBuilder("foo", "src/Foo.java").build())
+ .forMetric(CoreMetrics.LINES)
+ .withValue(4)
+ .save();
+ assertThat(tester.measures("foo:src/Foo.java")).hasSize(2);
+ assertThat(tester.measure("foo:src/Foo.java", "ncloc")).isNotNull();
+ assertThat(tester.measure("foo:src/Foo.java", "lines")).isNotNull();
+ tester.<Integer>newMeasure()
+ .on(new DefaultInputModule(ProjectDefinition.create().setKey("foo").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())))
+ .forMetric(CoreMetrics.DIRECTORIES)
+ .withValue(4)
+ .save();
+ assertThat(tester.measures("foo")).hasSize(1);
+ assertThat(tester.measure("foo", "directories")).isNotNull();
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void duplicateMeasures() {
+ tester.<Integer>newMeasure()
+ .on(new TestInputFileBuilder("foo", "src/Foo.java").build())
+ .forMetric(CoreMetrics.NCLOC)
+ .withValue(2)
+ .save();
+ tester.<Integer>newMeasure()
+ .on(new TestInputFileBuilder("foo", "src/Foo.java").build())
+ .forMetric(CoreMetrics.NCLOC)
+ .withValue(2)
+ .save();
+ }
+
+ @Test
+ public void testHighlighting() {
+ assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 3)).isEmpty();
+ tester.newHighlighting()
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build())
+ .highlight(1, 0, 1, 5, TypeOfText.ANNOTATION)
+ .highlight(8, 10, TypeOfText.CONSTANT)
+ .highlight(9, 10, TypeOfText.COMMENT)
+ .save();
+ assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 3)).containsExactly(TypeOfText.ANNOTATION);
+ assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 9)).containsExactly(TypeOfText.CONSTANT, TypeOfText.COMMENT);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void duplicateHighlighting() {
+ tester.newHighlighting()
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build())
+ .highlight(1, 0, 1, 5, TypeOfText.ANNOTATION)
+ .save();
+ tester.newHighlighting()
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build())
+ .highlight(1, 0, 1, 5, TypeOfText.ANNOTATION)
+ .save();
+ }
+
+ @Test
+ public void testSymbolReferences() {
+ assertThat(tester.referencesForSymbolAt("foo:src/Foo.java", 1, 0)).isNull();
+
+ NewSymbolTable symbolTable = tester.newSymbolTable()
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build());
+ symbolTable
+ .newSymbol(1, 8, 1, 10);
+
+ symbolTable
+ .newSymbol(1, 1, 1, 5)
+ .newReference(6, 9)
+ .newReference(1, 10, 1, 13);
+
+ symbolTable.save();
+
+ assertThat(tester.referencesForSymbolAt("foo:src/Foo.java", 1, 0)).isNull();
+ assertThat(tester.referencesForSymbolAt("foo:src/Foo.java", 1, 8)).isEmpty();
+ assertThat(tester.referencesForSymbolAt("foo:src/Foo.java", 1, 3)).extracting("start.line", "start.lineOffset", "end.line", "end.lineOffset").containsExactly(tuple(1, 6, 1, 9),
+ tuple(1, 10, 1, 13));
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void duplicateSymbolReferences() {
+ NewSymbolTable symbolTable = tester.newSymbolTable()
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build());
+ symbolTable
+ .newSymbol(1, 8, 1, 10);
+
+ symbolTable.save();
+
+ symbolTable = tester.newSymbolTable()
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build());
+ symbolTable
+ .newSymbol(1, 8, 1, 10);
+
+ symbolTable.save();
+ }
+
+ @Test
+ public void testCoverageAtLineZero() {
+ assertThat(tester.lineHits("foo:src/Foo.java", 1)).isNull();
+ assertThat(tester.lineHits("foo:src/Foo.java", 4)).isNull();
+
+ exception.expect(IllegalStateException.class);
+ tester.newCoverage()
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build())
+ .lineHits(0, 3);
+ }
+
+ @Test
+ public void testCoverageAtLineOutOfRange() {
+ assertThat(tester.lineHits("foo:src/Foo.java", 1)).isNull();
+ assertThat(tester.lineHits("foo:src/Foo.java", 4)).isNull();
+ exception.expect(IllegalStateException.class);
+
+ tester.newCoverage()
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build())
+ .lineHits(4, 3);
+ }
+
+ @Test
+ public void testLineHits() {
+ assertThat(tester.lineHits("foo:src/Foo.java", 1)).isNull();
+ assertThat(tester.lineHits("foo:src/Foo.java", 4)).isNull();
+ tester.newCoverage()
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar\nasdas").build())
+ .lineHits(1, 2)
+ .lineHits(2, 3)
+ .save();
+ assertThat(tester.lineHits("foo:src/Foo.java", 1)).isEqualTo(2);
+ assertThat(tester.lineHits("foo:src/Foo.java", 2)).isEqualTo(3);
+ }
+
+ public void multipleCoverage() {
+ tester.newCoverage()
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar\nasdas").build())
+ .lineHits(1, 2)
+ .conditions(3, 4, 2)
+ .save();
+ tester.newCoverage()
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar\nasdas").build())
+ .lineHits(1, 2)
+ .conditions(3, 4, 3)
+ .save();
+ assertThat(tester.lineHits("foo:src/Foo.java", 1)).isEqualTo(4);
+ assertThat(tester.conditions("foo:src/Foo.java", 3)).isEqualTo(4);
+ assertThat(tester.coveredConditions("foo:src/Foo.java", 3)).isEqualTo(3);
+ }
+
+ @Test
+ public void testConditions() {
+ assertThat(tester.conditions("foo:src/Foo.java", 1)).isNull();
+ assertThat(tester.coveredConditions("foo:src/Foo.java", 1)).isNull();
+ tester.newCoverage()
+ .onFile(new TestInputFileBuilder("foo", "src/Foo.java")
+ .initMetadata("annot dsf fds foo bar\nasd\nasdas\nasdfas")
+ .build())
+ .conditions(1, 4, 2)
+ .save();
+ assertThat(tester.conditions("foo:src/Foo.java", 1)).isEqualTo(4);
+ assertThat(tester.coveredConditions("foo:src/Foo.java", 1)).isEqualTo(2);
+ }
+
+ @Test
+ public void testCpdTokens() {
+ assertThat(tester.cpdTokens("foo:src/Foo.java")).isNull();
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
+ .initMetadata("public class Foo {\n\n}")
+ .build();
+ tester.newCpdTokens()
+ .onFile(inputFile)
+ .addToken(inputFile.newRange(0, 6), "public")
+ .addToken(inputFile.newRange(7, 12), "class")
+ .addToken(inputFile.newRange(13, 16), "$IDENTIFIER")
+ .addToken(inputFile.newRange(17, 18), "{")
+ .addToken(inputFile.newRange(3, 0, 3, 1), "}")
+ .save();
+ assertThat(tester.cpdTokens("foo:src/Foo.java")).extracting("value", "startLine", "startUnit", "endUnit")
+ .containsExactly(
+ tuple("publicclass$IDENTIFIER{", 1, 1, 4),
+ tuple("}", 3, 5, 5));
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void duplicateCpdTokens() {
+ DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java")
+ .initMetadata("public class Foo {\n\n}")
+ .build();
+ tester.newCpdTokens()
+ .onFile(inputFile)
+ .addToken(inputFile.newRange(0, 6), "public")
+ .save();
+
+ tester.newCpdTokens()
+ .onFile(inputFile)
+ .addToken(inputFile.newRange(0, 6), "public")
+ .save();
+ }
+
+ @Test
+ public void testCancellation() {
+ assertThat(tester.isCancelled()).isFalse();
+ tester.setCancelled(true);
+ assertThat(tester.isCancelled()).isTrue();
+ }
+
+ @Test
+ public void testContextProperties() {
+ assertThat(tester.getContextProperties()).isEmpty();
+
+ tester.addContextProperty("foo", "bar");
+ assertThat(tester.getContextProperties()).containsOnly(entry("foo", "bar"));
+ }
+}