diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-06-05 11:01:49 -0500 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-07-12 20:21:14 +0200 |
commit | 97e15208790028ed50187e58cd4580e6cef8e6b3 (patch) | |
tree | b6232897a8e69eb219b0479599b29962a804338b /sonar-scanner-engine/src/test | |
parent | e020f1425d815ae48e9b5c39bc0eaf92382c3c06 (diff) | |
download | sonarqube-97e15208790028ed50187e58cd4580e6cef8e6b3.tar.gz sonarqube-97e15208790028ed50187e58cd4580e6cef8e6b3.zip |
Extract implementation from plugin API - Scanner FS
Diffstat (limited to 'sonar-scanner-engine/src/test')
57 files changed, 1256 insertions, 114 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 d60f344a516..f6e59a75f9a 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 @@ -25,12 +25,12 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.ArgumentCaptor; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -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; import org.sonar.api.measures.CoreMetrics; +import org.sonar.scanner.fs.DefaultInputFile; +import org.sonar.scanner.fs.TestInputFileBuilder; +import org.sonar.scanner.sensor.DefaultMeasure; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; @@ -151,5 +151,4 @@ public class DefaultFileLinesContextTest { fileLineMeasures.setIntValue(HITS_METRIC_KEY, 1, 2); } - } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/AnalysisTempFolderProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/AnalysisTempFolderProviderTest.java index df848a56006..d625da5f8df 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/AnalysisTempFolderProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/analysis/AnalysisTempFolderProviderTest.java @@ -20,13 +20,12 @@ package org.sonar.scanner.analysis; 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.fs.internal.DefaultInputProject; import org.sonar.api.utils.TempFolder; +import org.sonar.scanner.fs.DefaultInputProject; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -47,7 +46,7 @@ public class AnalysisTempFolderProviderTest { } @Test - public void createTempFolder() throws IOException { + public void createTempFolder() { File defaultDir = new File(temp.getRoot(), AnalysisTempFolderProvider.TMP_NAME); TempFolder tempFolder = tempFolderProvider.provide(project); 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 ecc155cd5e5..a649c5e9f24 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 @@ -36,9 +36,6 @@ import org.junit.Test; import org.junit.rules.ExpectedException; 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.scanner.fs.TestInputFileBuilder; import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; import org.sonar.core.util.CloseableIterator; @@ -47,6 +44,9 @@ import org.sonar.duplications.block.ByteArray; import org.sonar.duplications.index.CloneGroup; import org.sonar.duplications.index.ClonePart; import org.sonar.scanner.cpd.index.SonarCpdBlockIndex; +import org.sonar.scanner.fs.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputProject; +import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.protocol.output.ScannerReport.Duplicate; import org.sonar.scanner.protocol.output.ScannerReport.Duplication; import org.sonar.scanner.protocol.output.ScannerReportReader; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdSettingsTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdSettingsTest.java index 627114cce88..2e3e6d2898d 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdSettingsTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/cpd/CpdSettingsTest.java @@ -22,8 +22,8 @@ package org.sonar.scanner.cpd; import java.util.Optional; import org.junit.Before; import org.junit.Test; -import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.config.Configuration; +import org.sonar.scanner.fs.DefaultInputProject; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.anyString; 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 3d7c7486034..f62bb729def 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 @@ -32,9 +32,9 @@ import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.duplications.block.Block; import org.sonar.scanner.cpd.index.SonarCpdBlockIndex; +import org.sonar.scanner.fs.DefaultInputFile; import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.sensor.SensorContextTester; 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 596a25f9275..3eaeb9997ad 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 @@ -27,12 +27,12 @@ import org.junit.Rule; 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.scanner.fs.TestInputFileBuilder; import org.sonar.api.batch.rule.Severity; 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.fs.DefaultInputFile; +import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.sensor.SensorContextTester; import static java.nio.charset.StandardCharsets.UTF_8; @@ -105,8 +105,8 @@ public class ExternalIssueImporterTest { ReportParser.TextRange input = new ReportParser.TextRange(); input.startLine = 1; input.startColumn = 4; - input.endLine= 2; - input.endColumn= 3; + input.endLine = 2; + input.endColumn = 3; runOn(newIssue(input)); @@ -124,8 +124,8 @@ public class ExternalIssueImporterTest { ReportParser.TextRange input = new ReportParser.TextRange(); input.startLine = 1; input.startColumn = null; - input.endLine= 2; - input.endColumn= null; + input.endLine = 2; + input.endColumn = null; runOn(newIssue(input)); @@ -145,8 +145,8 @@ public class ExternalIssueImporterTest { ReportParser.TextRange input = new ReportParser.TextRange(); input.startLine = 1; input.startColumn = 3; - input.endLine= 2; - input.endColumn= null; + input.endLine = 2; + input.endColumn = null; runOn(newIssue(input)); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultInputDirTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultInputDirTest.java new file mode 100644 index 00000000000..1679a8df405 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultInputDirTest.java @@ -0,0 +1,64 @@ +/* + * 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 org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DefaultInputDirTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void test() throws Exception { + File baseDir = temp.newFolder(); + DefaultInputDir inputDir = new DefaultInputDir("ABCDE", "src") + .setModuleBaseDir(baseDir.toPath()); + + assertThat(inputDir.key()).isEqualTo("ABCDE:src"); + assertThat(inputDir.file().getAbsolutePath()).isEqualTo(new File(baseDir, "src").getAbsolutePath()); + assertThat(inputDir.relativePath()).isEqualTo("src"); + assertThat(new File(inputDir.relativePath())).isRelative(); + assertThat(inputDir.absolutePath()).endsWith("src"); + assertThat(new File(inputDir.absolutePath())).isAbsolute(); + } + + @Test + public void testEqualsAndHashCode() throws Exception { + DefaultInputDir inputDir1 = new DefaultInputDir("ABCDE", "src"); + + DefaultInputDir inputDir2 = new DefaultInputDir("ABCDE", "src"); + + assertThat(inputDir1.equals(inputDir1)).isTrue(); + assertThat(inputDir1.equals(inputDir2)).isTrue(); + assertThat(inputDir1.equals("foo")).isFalse(); + + assertThat(inputDir1.hashCode()).isEqualTo(63545559); + + assertThat(inputDir1.toString()).contains("[moduleKey=ABCDE, relative=src, basedir=null"); + + } + +} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultInputFileTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultInputFileTest.java new file mode 100644 index 00000000000..6673977bfe4 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultInputFileTest.java @@ -0,0 +1,315 @@ +/* + * 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.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.StringReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; +import java.util.stream.Collectors; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.TextRange; +import org.sonar.api.batch.fs.internal.Metadata; +import org.sonar.api.batch.fs.internal.SensorStrategy; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; + +public class DefaultInputFileTest { + + private static final String PROJECT_RELATIVE_PATH = "module1/src/Foo.php"; + private static final String MODULE_RELATIVE_PATH = "src/Foo.php"; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + private DefaultIndexedFile indexedFile; + + private Path baseDir; + private SensorStrategy sensorStrategy; + + @Before + public void prepare() throws IOException { + baseDir = temp.newFolder().toPath(); + sensorStrategy = new SensorStrategy(); + indexedFile = new DefaultIndexedFile(baseDir.resolve(PROJECT_RELATIVE_PATH), "ABCDE", PROJECT_RELATIVE_PATH, MODULE_RELATIVE_PATH, InputFile.Type.TEST, "php", 0, + sensorStrategy); + } + + @Test + public void test() throws Exception { + + Metadata metadata = new Metadata(42, 42, "", new int[0], new int[0], 10); + DefaultInputFile inputFile = new DefaultInputFile(indexedFile, (f) -> f.setMetadata(metadata)) + .setStatus(InputFile.Status.ADDED) + .setCharset(StandardCharsets.ISO_8859_1); + + assertThat(inputFile.absolutePath()).endsWith("Foo.php"); + assertThat(inputFile.filename()).isEqualTo("Foo.php"); + assertThat(inputFile.uri()).hasPath(baseDir.resolve(PROJECT_RELATIVE_PATH).toUri().getPath()); + assertThat(new File(inputFile.absolutePath())).isAbsolute(); + assertThat(inputFile.language()).isEqualTo("php"); + assertThat(inputFile.status()).isEqualTo(InputFile.Status.ADDED); + assertThat(inputFile.type()).isEqualTo(InputFile.Type.TEST); + assertThat(inputFile.lines()).isEqualTo(42); + assertThat(inputFile.charset()).isEqualTo(StandardCharsets.ISO_8859_1); + + assertThat(inputFile.getModuleRelativePath()).isEqualTo(MODULE_RELATIVE_PATH); + assertThat(inputFile.getProjectRelativePath()).isEqualTo(PROJECT_RELATIVE_PATH); + + sensorStrategy.setGlobal(false); + assertThat(inputFile.relativePath()).isEqualTo(MODULE_RELATIVE_PATH); + assertThat(new File(inputFile.relativePath())).isRelative(); + sensorStrategy.setGlobal(true); + assertThat(inputFile.relativePath()).isEqualTo(PROJECT_RELATIVE_PATH); + assertThat(new File(inputFile.relativePath())).isRelative(); + } + + @Test + public void test_content() throws IOException { + Path testFile = baseDir.resolve(PROJECT_RELATIVE_PATH); + Files.createDirectories(testFile.getParent()); + String content = "test é string"; + Files.write(testFile, content.getBytes(StandardCharsets.ISO_8859_1)); + + assertThat(Files.readAllLines(testFile, StandardCharsets.ISO_8859_1).get(0)).hasSize(content.length()); + + Metadata metadata = new Metadata(42, 30, "", new int[0], new int[0], 10); + + DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> f.setMetadata(metadata)) + .setStatus(InputFile.Status.ADDED) + .setCharset(StandardCharsets.ISO_8859_1); + + assertThat(inputFile.contents()).isEqualTo(content); + try (InputStream inputStream = inputFile.inputStream()) { + String result = new BufferedReader(new InputStreamReader(inputStream, inputFile.charset())).lines().collect(Collectors.joining()); + assertThat(result).isEqualTo(content); + } + + } + + @Test + public void test_content_exclude_bom() throws IOException { + Path testFile = baseDir.resolve(PROJECT_RELATIVE_PATH); + Files.createDirectories(testFile.getParent()); + try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(testFile.toFile()), StandardCharsets.UTF_8))) { + out.write('\ufeff'); + } + String content = "test é string €"; + Files.write(testFile, content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND); + + assertThat(Files.readAllLines(testFile, StandardCharsets.UTF_8).get(0)).hasSize(content.length() + 1); + + Metadata metadata = new Metadata(42, 30, "", new int[0], new int[0], 10); + + DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> f.setMetadata(metadata)) + .setStatus(InputFile.Status.ADDED) + .setCharset(StandardCharsets.UTF_8); + + assertThat(inputFile.contents()).isEqualTo(content); + try (InputStream inputStream = inputFile.inputStream()) { + String result = new BufferedReader(new InputStreamReader(inputStream, inputFile.charset())).lines().collect(Collectors.joining()); + assertThat(result).isEqualTo(content); + } + + } + + @Test + public void test_equals_and_hashcode() throws Exception { + DefaultInputFile f1 = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), (f) -> mock(Metadata.class)); + DefaultInputFile f1a = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), (f) -> mock(Metadata.class)); + DefaultInputFile f2 = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), "src/Bar.php", null), (f) -> mock(Metadata.class)); + + assertThat(f1).isEqualTo(f1); + assertThat(f1).isEqualTo(f1a); + assertThat(f1).isNotEqualTo(f2); + assertThat(f1.equals("foo")).isFalse(); + assertThat(f1.equals(null)).isFalse(); + + assertThat(f1.hashCode()).isEqualTo(f1.hashCode()); + assertThat(f1.hashCode()).isEqualTo(f1a.hashCode()); + } + + @Test + public void test_toString() throws Exception { + DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), (f) -> mock(Metadata.class)); + assertThat(file.toString()).isEqualTo(MODULE_RELATIVE_PATH); + } + + @Test + public void checkValidPointer() { + Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {9, 15}, 16); + DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); + assertThat(file.newPointer(1, 0).line()).isEqualTo(1); + assertThat(file.newPointer(1, 0).lineOffset()).isEqualTo(0); + // Don't fail + file.newPointer(1, 9); + file.newPointer(2, 0); + file.newPointer(2, 5); + + try { + file.newPointer(0, 1); + fail(); + } catch (Exception e) { + assertThat(e).hasMessage("0 is not a valid line for a file"); + } + try { + file.newPointer(3, 1); + fail(); + } catch (Exception e) { + assertThat(e).hasMessage("3 is not a valid line for pointer. File src/Foo.php has 2 line(s)"); + } + try { + file.newPointer(1, -1); + fail(); + } catch (Exception e) { + assertThat(e).hasMessage("-1 is not a valid line offset for a file"); + } + try { + file.newPointer(1, 10); + fail(); + } catch (Exception e) { + assertThat(e).hasMessage("10 is not a valid line offset for pointer. File src/Foo.php has 9 character(s) at line 1"); + } + } + + @Test + public void checkValidPointerUsingGlobalOffset() { + Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {8, 15}, 16); + DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); + assertThat(file.newPointer(0).line()).isEqualTo(1); + assertThat(file.newPointer(0).lineOffset()).isEqualTo(0); + + assertThat(file.newPointer(9).line()).isEqualTo(1); + // Ignore eol characters + assertThat(file.newPointer(9).lineOffset()).isEqualTo(8); + + assertThat(file.newPointer(10).line()).isEqualTo(2); + assertThat(file.newPointer(10).lineOffset()).isEqualTo(0); + + assertThat(file.newPointer(15).line()).isEqualTo(2); + assertThat(file.newPointer(15).lineOffset()).isEqualTo(5); + + assertThat(file.newPointer(16).line()).isEqualTo(2); + // Ignore eol characters + assertThat(file.newPointer(16).lineOffset()).isEqualTo(5); + + try { + file.newPointer(-1); + fail(); + } catch (Exception e) { + assertThat(e).hasMessage("-1 is not a valid offset for a file"); + } + + try { + file.newPointer(17); + fail(); + } catch (Exception e) { + assertThat(e).hasMessage("17 is not a valid offset for file src/Foo.php. Max offset is 16"); + } + } + + @Test + public void checkValidRange() { + Metadata metadata = new FileMetadata().readMetadata(new StringReader("bla bla a\nabcde")); + DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); + + assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 1)).start().line()).isEqualTo(1); + // Don't fail + file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)); + file.newRange(file.newPointer(1, 0), file.newPointer(1, 9)); + file.newRange(file.newPointer(1, 0), file.newPointer(2, 0)); + assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(2, 5))).isEqualTo(file.newRange(0, 15)); + + try { + file.newRange(file.newPointer(1, 0), file.newPointer(1, 0)); + fail(); + } catch (Exception e) { + assertThat(e).hasMessage("Start pointer [line=1, lineOffset=0] should be before end pointer [line=1, lineOffset=0]"); + } + try { + file.newRange(file.newPointer(1, 0), file.newPointer(1, 10)); + fail(); + } catch (Exception e) { + assertThat(e).hasMessage("10 is not a valid line offset for pointer. File src/Foo.php has 9 character(s) at line 1"); + } + } + + @Test + public void selectLine() { + Metadata metadata = new FileMetadata().readMetadata(new StringReader("bla bla a\nabcde\n\nabc")); + DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); + + assertThat(file.selectLine(1).start().line()).isEqualTo(1); + assertThat(file.selectLine(1).start().lineOffset()).isEqualTo(0); + assertThat(file.selectLine(1).end().line()).isEqualTo(1); + assertThat(file.selectLine(1).end().lineOffset()).isEqualTo(9); + + // Don't fail when selecting empty line + assertThat(file.selectLine(3).start().line()).isEqualTo(3); + assertThat(file.selectLine(3).start().lineOffset()).isEqualTo(0); + assertThat(file.selectLine(3).end().line()).isEqualTo(3); + assertThat(file.selectLine(3).end().lineOffset()).isEqualTo(0); + + try { + file.selectLine(5); + fail(); + } catch (Exception e) { + assertThat(e).hasMessage("5 is not a valid line for pointer. File src/Foo.php has 4 line(s)"); + } + } + + @Test + public void checkValidRangeUsingGlobalOffset() { + Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {9, 15}, 16); + DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); + TextRange newRange = file.newRange(10, 13); + assertThat(newRange.start().line()).isEqualTo(2); + assertThat(newRange.start().lineOffset()).isEqualTo(0); + assertThat(newRange.end().line()).isEqualTo(2); + assertThat(newRange.end().lineOffset()).isEqualTo(3); + } + + @Test + public void testRangeOverlap() { + Metadata metadata = new Metadata(2, 2, "", new int[] {0, 10}, new int[] {9, 15}, 16); + DefaultInputFile file = new DefaultInputFile(new DefaultIndexedFile("ABCDE", Paths.get("module"), MODULE_RELATIVE_PATH, null), f -> f.setMetadata(metadata)); + // Don't fail + assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)))).isTrue(); + assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 2)))).isTrue(); + assertThat(file.newRange(file.newPointer(1, 0), file.newPointer(1, 1)).overlap(file.newRange(file.newPointer(1, 1), file.newPointer(1, 2)))).isFalse(); + assertThat(file.newRange(file.newPointer(1, 2), file.newPointer(1, 3)).overlap(file.newRange(file.newPointer(1, 0), file.newPointer(1, 2)))).isFalse(); + } +} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultInputModuleTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultInputModuleTest.java new file mode 100644 index 00000000000..02570d36735 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultInputModuleTest.java @@ -0,0 +1,110 @@ +/* + * 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.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.bootstrap.ProjectDefinition; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DefaultInputModuleTest { + + private static final String FILE_1 = "file1"; + private static final String TEST_1 = "test1"; + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void check_getters() throws IOException { + ProjectDefinition def = ProjectDefinition.create(); + def.setKey("moduleKey"); + File baseDir = temp.newFolder(); + Path src = baseDir.toPath().resolve(FILE_1); + Files.createFile(src); + Path test = baseDir.toPath().resolve(TEST_1); + Files.createFile(test); + def.setBaseDir(baseDir); + File workDir = temp.newFolder(); + def.setWorkDir(workDir); + def.setSources(FILE_1); + def.setTests(TEST_1); + DefaultInputModule module = new DefaultInputModule(def); + + assertThat(module.key()).isEqualTo("moduleKey"); + assertThat(module.definition()).isEqualTo(def); + assertThat(module.getBranch()).isNull(); + assertThat(module.getBaseDir()).isEqualTo(baseDir.toPath()); + assertThat(module.getKeyWithBranch()).isEqualTo("moduleKey"); + assertThat(module.getWorkDir()).isEqualTo(workDir.toPath()); + assertThat(module.getEncoding()).isEqualTo(Charset.defaultCharset()); + assertThat(module.getSourceDirsOrFiles().get()).containsExactlyInAnyOrder(src); + assertThat(module.getTestDirsOrFiles().get()).containsExactlyInAnyOrder(test); + assertThat(module.getEncoding()).isEqualTo(Charset.defaultCharset()); + + assertThat(module.isFile()).isFalse(); + } + + @Test + public void no_sources() throws IOException { + ProjectDefinition def = ProjectDefinition.create(); + def.setKey("moduleKey"); + File baseDir = temp.newFolder(); + Path src = baseDir.toPath().resolve(FILE_1); + Files.createFile(src); + Path test = baseDir.toPath().resolve(TEST_1); + Files.createFile(test); + def.setBaseDir(baseDir); + File workDir = temp.newFolder(); + def.setWorkDir(workDir); + DefaultInputModule module = new DefaultInputModule(def); + + assertThat(module.key()).isEqualTo("moduleKey"); + assertThat(module.definition()).isEqualTo(def); + assertThat(module.getBranch()).isNull(); + assertThat(module.getBaseDir()).isEqualTo(baseDir.toPath()); + assertThat(module.getKeyWithBranch()).isEqualTo("moduleKey"); + assertThat(module.getWorkDir()).isEqualTo(workDir.toPath()); + assertThat(module.getEncoding()).isEqualTo(Charset.defaultCharset()); + assertThat(module.getSourceDirsOrFiles()).isNotPresent(); + assertThat(module.getTestDirsOrFiles()).isNotPresent(); + assertThat(module.getEncoding()).isEqualTo(Charset.defaultCharset()); + + assertThat(module.isFile()).isFalse(); + } + + @Test + public void working_directory_should_be_hidden() throws IOException { + ProjectDefinition def = ProjectDefinition.create(); + File workDir = temp.newFolder(".sonar"); + def.setWorkDir(workDir); + File baseDir = temp.newFolder(); + def.setBaseDir(baseDir); + DefaultInputModule module = new DefaultInputModule(def); + assertThat(workDir.isHidden()).isTrue(); + } + +} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultInputProjectTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultInputProjectTest.java new file mode 100644 index 00000000000..03248472335 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/DefaultInputProjectTest.java @@ -0,0 +1,86 @@ +/* + * 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.IOException; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.bootstrap.ProjectDefinition; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DefaultInputProjectTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void testGetters() throws IOException { + ProjectDefinition def = ProjectDefinition.create(); + def.setKey("projectKey"); + def.setName("projectName"); + File baseDir = temp.newFolder(); + def.setBaseDir(baseDir); + def.setDescription("desc"); + File workDir = temp.newFolder(); + def.setWorkDir(workDir); + def.setSources("file1"); + def.setTests("test1"); + AbstractProjectOrModule project = new DefaultInputProject(def); + + assertThat(project.key()).isEqualTo("projectKey"); + assertThat(project.getName()).isEqualTo("projectName"); + assertThat(project.getOriginalName()).isEqualTo("projectName"); + assertThat(project.definition()).isEqualTo(def); + assertThat(project.getBranch()).isNull(); + assertThat(project.getBaseDir()).isEqualTo(baseDir.toPath()); + assertThat(project.getKeyWithBranch()).isEqualTo("projectKey"); + assertThat(project.getDescription()).isEqualTo("desc"); + assertThat(project.getWorkDir()).isEqualTo(workDir.toPath()); + assertThat(project.getEncoding()).isEqualTo(Charset.defaultCharset()); + + assertThat(project.properties()).hasSize(5); + + assertThat(project.isFile()).isFalse(); + } + + @Test + public void testEncoding() throws IOException { + ProjectDefinition def = ProjectDefinition.create(); + def.setKey("projectKey"); + def.setName("projectName"); + File baseDir = temp.newFolder(); + def.setBaseDir(baseDir); + def.setProjectVersion("version"); + def.setDescription("desc"); + File workDir = temp.newFolder(); + def.setWorkDir(workDir); + def.setSources("file1"); + def.setProperty("sonar.sourceEncoding", "UTF-16"); + AbstractProjectOrModule project = new DefaultInputProject(def); + + assertThat(project.getEncoding()).isEqualTo(StandardCharsets.UTF_16); + } + +} 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 index 826edf9d086..c2665324ebc 100644 --- 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 @@ -23,7 +23,6 @@ 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; @@ -31,7 +30,6 @@ 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; @@ -223,7 +221,7 @@ public class FileMetadataTest { 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()) + DefaultInputFile f = new TestInputFileBuilder("foo", tempFile.getName()) .setModuleBaseDir(tempFile.getParentFile().toPath()) .setCharset(StandardCharsets.UTF_8) .build(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/TestInputFileBuilderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/TestInputFileBuilderTest.java new file mode 100644 index 00000000000..2d83007ff13 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/fs/TestInputFileBuilderTest.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; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import org.apache.commons.io.IOUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.fs.InputFile.Status; +import org.sonar.api.batch.fs.InputFile.Type; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TestInputFileBuilderTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void setContent() throws IOException { + DefaultInputFile file = TestInputFileBuilder.create("module", "invalidPath") + .setContents("my content") + .setCharset(StandardCharsets.UTF_8) + .build(); + assertThat(file.contents()).isEqualTo("my content"); + assertThat(IOUtils.toString(file.inputStream())).isEqualTo("my content"); + } + + @Test + public void testGetters() { + DefaultInputFile file = TestInputFileBuilder.create("module", new File("baseDir"), new File("baseDir", "path")) + .setStatus(Status.SAME) + .setType(Type.MAIN) + .build(); + + assertThat(file.type()).isEqualTo(Type.MAIN); + assertThat(file.status()).isEqualTo(Status.SAME); + assertThat(file.isPublished()).isTrue(); + assertThat(file.type()).isEqualTo(Type.MAIN); + assertThat(file.relativePath()).isEqualTo("path"); + assertThat(file.absolutePath()).isEqualTo("baseDir/path"); + + } + + @Test + public void testCreateInputModule() throws IOException { + File baseDir = temp.newFolder(); + AbstractProjectOrModule module = TestInputFileBuilder.newDefaultInputModule("key", baseDir); + assertThat(module.key()).isEqualTo("key"); + assertThat(module.getBaseDir()).isEqualTo(baseDir.toPath()); + } +} 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 b90e6a19dd4..ccd628b0a17 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 @@ -27,8 +27,8 @@ import org.junit.Rule; 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.utils.MessageException; +import org.sonar.scanner.fs.DefaultInputFile; import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.sensor.SensorContextTester; 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 c9f44cd5b8f..40ac5200719 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 @@ -27,11 +27,11 @@ import org.junit.Rule; 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.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.DefaultInputFile; import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.sensor.SensorContextTester; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultFilterableIssueTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultFilterableIssueTest.java index b506b78f074..5250e1aa5d1 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultFilterableIssueTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultFilterableIssueTest.java @@ -23,8 +23,8 @@ import java.util.Date; import org.junit.Before; import org.junit.Test; import org.sonar.api.batch.fs.InputComponent; -import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.scanner.ProjectInfo; +import org.sonar.scanner.fs.DefaultInputProject; import org.sonar.scanner.protocol.Constants.Severity; import org.sonar.scanner.protocol.output.ScannerReport.Issue; import org.sonar.scanner.protocol.output.ScannerReport.TextRange; 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 50c247f5b51..881a3629a04 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 @@ -32,20 +32,18 @@ import org.mockito.Mock; import org.mockito.runners.MockitoJUnitRunner; 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.scanner.fs.TestInputFileBuilder; -import org.sonar.scanner.rule.ActiveRulesBuilder; -import org.sonar.scanner.rule.NewActiveRule; -import org.sonar.scanner.rule.RulesBuilder; -import org.sonar.scanner.sensor.DefaultExternalIssue; -import org.sonar.scanner.sensor.DefaultIssue; -import org.sonar.scanner.sensor.DefaultIssueLocation; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.Severity; import org.sonar.api.rules.RuleType; +import org.sonar.scanner.fs.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputProject; +import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.report.ReportPublisher; +import org.sonar.scanner.rule.ActiveRulesBuilder; +import org.sonar.scanner.rule.NewActiveRule; +import org.sonar.scanner.rule.RulesBuilder; +import org.sonar.scanner.sensor.DefaultExternalIssue; 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/issue/ignore/IgnoreIssuesFilterTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilterTest.java index 8a9df4a744a..d5a99ff296f 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilterTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ignore/IgnoreIssuesFilterTest.java @@ -21,10 +21,10 @@ package org.sonar.scanner.issue.ignore; import org.junit.Before; import org.junit.Test; -import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.rule.RuleKey; import org.sonar.api.scan.issue.filter.IssueFilterChain; import org.sonar.api.utils.WildcardPattern; +import org.sonar.scanner.fs.DefaultInputFile; import org.sonar.scanner.issue.DefaultFilterableIssue; import static org.assertj.core.api.Assertions.assertThat; 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 6a597ef85b3..94cebe5f0bd 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 @@ -27,9 +27,9 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.api.notifications.AnalysisWarnings; +import org.sonar.scanner.fs.DefaultInputFile; +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.pattern.IssuePattern; 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 07c39d82113..fb4165825e7 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 @@ -34,7 +34,7 @@ import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputFile; import org.sonar.scanner.fs.FileMetadata; import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.issue.ignore.pattern.IssueExclusionPatternInitializer; 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 16154f5ff0c..40695d3529e 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 @@ -30,7 +30,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.fs.internal.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputFile; import org.sonar.scanner.fs.FileMetadata; import org.sonar.scanner.mediumtest.AnalysisResult; import org.sonar.scanner.mediumtest.ScannerMediumTester; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/DeprecatedBranchMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/DeprecatedBranchMediumTest.java index 627e285af3a..d6f141aa0f8 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/DeprecatedBranchMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/branch/DeprecatedBranchMediumTest.java @@ -32,8 +32,8 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.sonar.api.SonarEdition; -import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.utils.MessageException; +import org.sonar.scanner.fs.DefaultInputFile; import org.sonar.scanner.mediumtest.AnalysisResult; import org.sonar.scanner.mediumtest.ScannerMediumTester; import org.sonar.xoo.XooPlugin; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java index 6ae40643542..e64c47b7edb 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumTest.java @@ -36,12 +36,12 @@ import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; import org.sonar.api.SonarEdition; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.utils.MessageException; import org.sonar.api.utils.PathUtils; import org.sonar.api.utils.System2; import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; +import org.sonar.scanner.fs.DefaultInputFile; import org.sonar.scanner.mediumtest.AnalysisResult; import org.sonar.scanner.mediumtest.ScannerMediumTester; import org.sonar.xoo.XooPlugin; 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 fe18fdb725f..6bfffc32384 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 @@ -25,7 +25,7 @@ import org.junit.Rule; 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.scanner.fs.DefaultInputFile; 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 bce232d44f0..d48402ea9ed 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 @@ -26,15 +26,15 @@ 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.DefaultInputModule; -import org.sonar.api.batch.fs.internal.InputModuleHierarchy; import org.sonar.api.batch.fs.internal.SensorStrategy; -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; -import org.sonar.scanner.sensor.ModuleSensorsExecutor; +import org.sonar.scanner.fs.DefaultInputModule; +import org.sonar.scanner.fs.InputModuleHierarchy; +import org.sonar.scanner.fs.TestInputFileBuilder; +import org.sonar.scanner.sensor.ModuleSensorExtensionDictionnary; import org.sonar.scanner.sensor.ModuleSensorWrapper; +import org.sonar.scanner.sensor.ModuleSensorsExecutor; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; 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 b041c6badc8..a225093a650 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 @@ -25,7 +25,7 @@ import org.junit.Rule; 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.scanner.fs.DefaultInputFile; 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/report/AnalysisContextReportPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisContextReportPublisherTest.java index 22c8528301f..f795cf97408 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisContextReportPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/AnalysisContextReportPublisherTest.java @@ -32,14 +32,14 @@ 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.DefaultInputModule; -import org.sonar.api.batch.fs.internal.InputModuleHierarchy; import org.sonar.api.utils.System2; import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; import org.sonar.core.platform.PluginInfo; import org.sonar.scanner.bootstrap.GlobalServerSettings; import org.sonar.scanner.bootstrap.ScannerPluginRepository; +import org.sonar.scanner.fs.DefaultInputModule; +import org.sonar.scanner.fs.InputModuleHierarchy; import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.scan.ProjectServerSettings; import org.sonar.scanner.scan.filesystem.InputComponentStore; @@ -148,6 +148,66 @@ public class AnalysisContextReportPublisherTest { } @Test + public void shouldNotDumpSQPropsInSystemProps() throws Exception { + logTester.setLevel(LoggerLevel.DEBUG); + ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder()); + Properties props = new Properties(); + props.setProperty(COM_FOO, "bar"); + props.setProperty(SONAR_SKIP, "true"); + when(system2.properties()).thenReturn(props); + DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create() + .setBaseDir(temp.newFolder()) + .setWorkDir(temp.newFolder()) + .setProperty("sonar.projectKey", "foo") + .setProperty(COM_FOO, "bar") + .setProperty(SONAR_SKIP, "true")); + when(store.allModules()).thenReturn(singletonList(rootModule)); + when(hierarchy.root()).thenReturn(rootModule); + + publisher.init(writer); + + List<String> lines = FileUtils.readLines(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8); + assertThat(lines).containsExactly("Environment variables:", + "System properties:", + " - com.foo=bar", + "SonarQube plugins:", + "Global server settings:", + "Project server settings:", + "Project scanner properties:", + " - sonar.projectKey=foo", + " - sonar.skip=true"); + } + + @Test + public void shouldNotDumpEnvTwice() throws Exception { + logTester.setLevel(LoggerLevel.DEBUG); + ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder()); + + Map<String, String> env = new HashMap<>(); + env.put(FOO, "BAR"); + env.put(BIZ, "BAZ"); + when(system2.envVariables()).thenReturn(env); + DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create() + .setBaseDir(temp.newFolder()) + .setWorkDir(temp.newFolder()) + .setProperty("sonar.projectKey", "foo") + .setProperty("env." + FOO, "BAR")); + when(store.allModules()).thenReturn(singletonList(rootModule)); + when(hierarchy.root()).thenReturn(rootModule); + publisher.init(writer); + + String content = FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8); + assertThat(content).containsOnlyOnce(FOO); + assertThat(content).containsOnlyOnce(BIZ); + assertThat(content).containsSubsequence(BIZ, FOO); + + content = FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8); + assertThat(content).containsOnlyOnce(FOO); + assertThat(content).containsOnlyOnce(BIZ); + assertThat(content).doesNotContain("env." + FOO); + } + + @Test public void shouldNotDumpSensitiveModuleProperties() throws Exception { ScannerReportWriter writer = new ScannerReportWriter(temp.newFolder()); DefaultInputModule rootModule = new DefaultInputModule(ProjectDefinition.create() @@ -187,7 +247,6 @@ public class AnalysisContextReportPublisherTest { assertThat(writer.getFileStructure().analysisLog()).exists(); - assertThat(FileUtils.readFileToString(writer.getFileStructure().analysisLog(), StandardCharsets.UTF_8)).containsSubsequence( "sonar.aVeryLongProp=" + StringUtils.repeat("abcde", 199) + "ab...", "sonar.projectBaseDir=" + baseDir.toString(), 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 4d4a3476553..18ce32fab0a 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 @@ -30,11 +30,11 @@ import org.junit.Before; 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.DefaultInputProject; -import org.sonar.api.batch.fs.internal.InputModuleHierarchy; -import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.api.batch.scm.ScmProvider; +import org.sonar.scanner.fs.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputProject; +import org.sonar.scanner.fs.InputModuleHierarchy; +import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.scan.branch.BranchConfiguration; 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 36134513be9..b812747c692 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 @@ -31,11 +31,11 @@ import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; 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.scanner.fs.TestInputFileBuilder; import org.sonar.api.utils.DateUtils; import org.sonar.scanner.ProjectInfo; +import org.sonar.scanner.fs.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputProject; +import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.protocol.output.FileStructure; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReport.Component; 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 7a66973ec20..3eb07e5e945 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 @@ -39,14 +39,14 @@ import org.junit.rules.TemporaryFolder; import org.junit.runner.RunWith; 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.scanner.fs.TestInputFileBuilder; import org.sonar.api.batch.scm.ScmProvider; import org.sonar.scanner.ProjectInfo; import org.sonar.scanner.bootstrap.ScannerPlugin; import org.sonar.scanner.bootstrap.ScannerPluginRepository; import org.sonar.scanner.cpd.CpdSettings; +import org.sonar.scanner.fs.DefaultInputModule; +import org.sonar.scanner.fs.InputModuleHierarchy; +import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; @@ -145,9 +145,9 @@ public class MetadataPublisherTest { .setRulesUpdatedAt(date.getTime()) .build())); assertThat(metadata.getPluginsByKey()).containsOnly(entry("java", org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin.newBuilder() - .setKey("java") - .setUpdatedAt(12345) - .build()), + .setKey("java") + .setUpdatedAt(12345) + .build()), entry("php", org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin.newBuilder() .setKey("php") .setUpdatedAt(45678) diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java index 4c3699326a0..3c7893722ad 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ReportPublisherTest.java @@ -34,8 +34,6 @@ import org.junit.rules.TemporaryFolder; import org.mockito.ArgumentCaptor; import org.mockito.Mockito; 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.platform.Server; import org.sonar.api.utils.MessageException; import org.sonar.api.utils.TempFolder; @@ -43,6 +41,8 @@ import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.bootstrap.ScannerWsClient; +import org.sonar.scanner.fs.DefaultInputModule; +import org.sonar.scanner.fs.InputModuleHierarchy; import org.sonar.scanner.scan.ScanProperties; import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonarqube.ws.Ce; 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 5f6054f81c9..835e423f9ee 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 @@ -27,8 +27,8 @@ import org.junit.Before; 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.DefaultInputProject; +import org.sonar.scanner.fs.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputProject; import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.protocol.output.ScannerReportWriter; import org.sonar.scanner.scan.branch.BranchConfiguration; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java index 26bb3a6e1aa..f2888be4c65 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoaderTest.java @@ -29,10 +29,10 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.utils.MessageException; import org.sonar.scanner.WsTestUtil; import org.sonar.scanner.bootstrap.ScannerWsClient; +import org.sonar.scanner.fs.DefaultInputFile; import org.sonarqube.ws.Batch.WsProjectResponse; import org.sonarqube.ws.client.HttpException; import org.sonarqube.ws.client.WsRequest; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DefaultInputModuleHierarchyTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DefaultInputModuleHierarchyTest.java index 87da83b2cd5..aadf37e6a75 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DefaultInputModuleHierarchyTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DefaultInputModuleHierarchyTest.java @@ -28,7 +28,7 @@ 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.DefaultInputModule; +import org.sonar.scanner.fs.DefaultInputModule; import static org.assertj.core.api.Assertions.assertThat; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ModuleIndexerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ModuleIndexerTest.java index 8c09090dff4..3c4cb6ffb15 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ModuleIndexerTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ModuleIndexerTest.java @@ -22,8 +22,7 @@ package org.sonar.scanner.scan; import java.util.Arrays; import org.junit.Test; import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.fs.internal.DefaultInputModule; -import org.sonar.api.batch.fs.internal.DefaultInputProject; +import org.sonar.scanner.fs.DefaultInputModule; import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; @@ -36,7 +35,7 @@ public class ModuleIndexerTest { private DefaultInputModuleHierarchy moduleHierarchy; private InputComponentStore componentStore; - public void createIndexer(DefaultInputProject rootProject) { + public void createIndexer() { componentStore = new InputComponentStore(mock(BranchConfiguration.class)); moduleHierarchy = mock(DefaultInputModuleHierarchy.class); indexer = new ModuleIndexer(componentStore, moduleHierarchy); @@ -69,7 +68,7 @@ public class ModuleIndexerTest { when(mod2.definition()).thenReturn(def); when(mod3.definition()).thenReturn(def); - createIndexer(mock(DefaultInputProject.class)); + createIndexer(); when(moduleHierarchy.root()).thenReturn(root); when(moduleHierarchy.children(root)).thenReturn(Arrays.asList(mod1, mod2, mod3)); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectLockTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectLockTest.java index 4a1b0b5a4a2..12f20933bdb 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectLockTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ProjectLockTest.java @@ -29,7 +29,7 @@ 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.DefaultInputProject; +import org.sonar.scanner.fs.DefaultInputProject; import static org.assertj.core.api.Assertions.assertThat; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ScanPropertiesTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ScanPropertiesTest.java index 49f1804c550..a2622ae4cbd 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ScanPropertiesTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/ScanPropertiesTest.java @@ -27,9 +27,9 @@ 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.DefaultInputProject; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.utils.MessageException; +import org.sonar.scanner.fs.DefaultInputProject; 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/scan/WorkDirectoriesInitializerTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java index b4497362413..ba5aedb451f 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java @@ -26,8 +26,8 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.fs.internal.DefaultInputModule; -import org.sonar.api.batch.fs.internal.InputModuleHierarchy; +import org.sonar.scanner.fs.DefaultInputModule; +import org.sonar.scanner.fs.InputModuleHierarchy; 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/scan/filesystem/InputComponentStoreTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputComponentStoreTest.java index 04490bd02e4..e9365140bd2 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 @@ -32,9 +32,9 @@ import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Status; import org.sonar.api.batch.fs.InputFile.Type; 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.scanner.fs.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputModule; +import org.sonar.scanner.fs.DefaultInputProject; import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.scan.branch.BranchConfiguration; 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 9bd732b9287..0682ccaf345 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 @@ -32,9 +32,9 @@ import org.junit.rules.TemporaryFolder; 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.notifications.AnalysisWarnings; import org.sonar.api.utils.PathUtils; +import org.sonar.scanner.fs.DefaultInputFile; import org.sonar.scanner.fs.FileMetadata; import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.issue.ignore.IgnoreIssuesFilter; 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 2ead8234b68..c624be2c6f1 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 @@ -26,8 +26,8 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; 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.scanner.fs.DefaultInputProject; import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.scan.branch.BranchConfiguration; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ProjectExclusionFiltersTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ProjectExclusionFiltersTest.java index 7e6540f69f1..9f613f48ed7 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ProjectExclusionFiltersTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/ProjectExclusionFiltersTest.java @@ -30,7 +30,7 @@ import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; import org.sonar.api.batch.fs.IndexedFile; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.internal.DefaultIndexedFile; +import org.sonar.scanner.fs.DefaultIndexedFile; 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/scan/filesystem/StatusDetectionTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/StatusDetectionTest.java index ccd56feef74..167b4e33a9c 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 @@ -26,7 +26,7 @@ import java.util.Map; 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.scanner.fs.DefaultInputFile; import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.repository.FileData; import org.sonar.scanner.repository.ProjectRepositoriesSupplier; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesProviderTest.java index 4b81bb45f5e..681433767ff 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmChangedFilesProviderTest.java @@ -28,9 +28,9 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Mock; import org.mockito.MockitoAnnotations; -import org.sonar.api.batch.fs.internal.DefaultInputProject; -import org.sonar.api.batch.fs.internal.InputModuleHierarchy; import org.sonar.api.batch.scm.ScmProvider; +import org.sonar.scanner.fs.DefaultInputProject; +import org.sonar.scanner.fs.InputModuleHierarchy; 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/scm/ScmConfigurationTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmConfigurationTest.java index a5ca3a8d26e..a8f6ec82940 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmConfigurationTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmConfigurationTest.java @@ -32,13 +32,13 @@ import org.junit.runner.RunWith; import org.mockito.Answers; import org.sonar.api.CoreProperties; import org.sonar.api.batch.AnalysisMode; -import org.sonar.api.batch.fs.internal.InputModuleHierarchy; import org.sonar.api.batch.scm.ScmProvider; import org.sonar.api.config.Configuration; import org.sonar.api.notifications.AnalysisWarnings; import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.LogTester; import org.sonar.core.config.ScannerProperties; +import org.sonar.scanner.fs.InputModuleHierarchy; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.any; diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmRevisionImplTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmRevisionImplTest.java index ff6ca35bca7..77d29126e44 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmRevisionImplTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scm/ScmRevisionImplTest.java @@ -24,9 +24,9 @@ import java.util.Map; import java.util.Optional; import javax.annotation.Nullable; import org.junit.Test; -import org.sonar.api.batch.fs.internal.InputModuleHierarchy; import org.sonar.scanner.bootstrap.RawScannerProperties; import org.sonar.scanner.ci.CiConfiguration; +import org.sonar.scanner.fs.InputModuleHierarchy; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -60,7 +60,7 @@ public class ScmRevisionImplTest { public void ignore_failure_if_scm_does_not_support_revisions() { CiConfiguration ciConfiguration = mock(CiConfiguration.class); when(ciConfiguration.getScmRevision()).thenReturn(Optional.empty()); - Map<String,String> scannerConfiguration = new HashMap<>(); + Map<String, String> scannerConfiguration = new HashMap<>(); ScmConfiguration scmConfiguration = mock(ScmConfiguration.class, RETURNS_DEEP_STUBS); when(scmConfiguration.provider().revisionId(any())).thenThrow(new UnsupportedOperationException("BOOM")); InputModuleHierarchy moduleHierarchy = mock(InputModuleHierarchy.class, RETURNS_DEEP_STUBS); @@ -73,7 +73,7 @@ public class ScmRevisionImplTest { private Optional<String> testGet(@Nullable String cliValue, @Nullable String ciValue, @Nullable String scmValue) { CiConfiguration ciConfiguration = mock(CiConfiguration.class); when(ciConfiguration.getScmRevision()).thenReturn(Optional.ofNullable(ciValue)); - Map<String,String> scannerConfiguration = new HashMap<>(); + Map<String, String> scannerConfiguration = new HashMap<>(); scannerConfiguration.put("sonar.scm.revision", cliValue); ScmConfiguration scmConfiguration = mock(ScmConfiguration.class, RETURNS_DEEP_STUBS); when(scmConfiguration.provider().revisionId(any())).thenReturn(scmValue); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultAnalysisErrorTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultAnalysisErrorTest.java new file mode 100644 index 00000000000..afff924afdf --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultAnalysisErrorTest.java @@ -0,0 +1,114 @@ +/* + * 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.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.TextPointer; +import org.sonar.api.batch.sensor.error.NewAnalysisError; +import org.sonar.api.batch.sensor.internal.SensorStorage; +import org.sonar.scanner.fs.DefaultTextPointer; +import org.sonar.scanner.fs.TestInputFileBuilder; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; + +public class DefaultAnalysisErrorTest { + private InputFile inputFile; + private SensorStorage storage; + private TextPointer textPointer; + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Before + public void setUp() { + inputFile = new TestInputFileBuilder("module1", "src/File.java").build(); + textPointer = new DefaultTextPointer(5, 2); + storage = mock(SensorStorage.class); + } + + @Test + public void test_analysis_error() { + DefaultAnalysisError analysisError = new DefaultAnalysisError(storage); + analysisError.onFile(inputFile) + .at(textPointer) + .message("msg"); + + assertThat(analysisError.location()).isEqualTo(textPointer); + assertThat(analysisError.message()).isEqualTo("msg"); + assertThat(analysisError.inputFile()).isEqualTo(inputFile); + } + + @Test + public void test_save() { + DefaultAnalysisError analysisError = new DefaultAnalysisError(storage); + analysisError.onFile(inputFile).save(); + + verify(storage).store(analysisError); + verifyNoMoreInteractions(storage); + } + + @Test + public void test_no_storage() { + exception.expect(NullPointerException.class); + DefaultAnalysisError analysisError = new DefaultAnalysisError(); + analysisError.onFile(inputFile).save(); + } + + @Test + public void test_validation() { + try { + new DefaultAnalysisError(storage).onFile(null); + fail("Expected exception"); + } catch (IllegalArgumentException e) { + // expected + } + + NewAnalysisError error = new DefaultAnalysisError(storage).onFile(inputFile); + try { + error.onFile(inputFile); + fail("Expected exception"); + } catch (IllegalStateException e) { + // expected + } + + error = new DefaultAnalysisError(storage).at(textPointer); + try { + error.at(textPointer); + fail("Expected exception"); + } catch (IllegalStateException e) { + // expected + } + + try { + new DefaultAnalysisError(storage).save(); + fail("Expected exception"); + } catch (NullPointerException e) { + // expected + } + } +} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultCpdTokensTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultCpdTokensTest.java new file mode 100644 index 00000000000..7a559792723 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultCpdTokensTest.java @@ -0,0 +1,170 @@ +/* + * 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.batch.sensor.internal.SensorStorage; +import org.sonar.scanner.fs.DefaultInputFile; +import org.sonar.scanner.fs.TestInputFileBuilder; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.Assertions.tuple; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; + +public class DefaultCpdTokensTest { + private final SensorStorage sensorStorage = mock(SensorStorage.class); + + private final DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java") + .setLines(2) + .setOriginalLineStartOffsets(new int[] {0, 50}) + .setOriginalLineEndOffsets(new int[] {49, 100}) + .setLastValidOffset(101) + .build(); + + @Test + public void save_no_tokens() { + DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage) + .onFile(inputFile); + + tokens.save(); + + verify(sensorStorage).store(tokens); + + assertThat(tokens.inputFile()).isEqualTo(inputFile); + } + + @Test + public void save_one_token() { + DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage) + .onFile(inputFile) + .addToken(inputFile.newRange(1, 2, 1, 5), "foo"); + + tokens.save(); + + verify(sensorStorage).store(tokens); + + assertThat(tokens.getTokenLines()).extracting("value", "startLine", "hashCode", "startUnit", "endUnit").containsExactly(tuple("foo", 1, "foo".hashCode(), 1, 1)); + } + + @Test + public void handle_exclusions() { + inputFile.setExcludedForDuplication(true); + DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage) + .onFile(inputFile) + .addToken(inputFile.newRange(1, 2, 1, 5), "foo"); + + tokens.save(); + + verifyZeroInteractions(sensorStorage); + + assertThat(tokens.getTokenLines()).isEmpty(); + } + + @Test + public void dont_save_for_test_files() { + DefaultInputFile testInputFile = new TestInputFileBuilder("foo", "src/Foo.java") + .setLines(2) + .setOriginalLineStartOffsets(new int[] {0, 50}) + .setOriginalLineEndOffsets(new int[] {49, 100}) + .setLastValidOffset(101) + .setType(InputFile.Type.TEST) + .build(); + + DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage) + .onFile(testInputFile) + .addToken(testInputFile.newRange(1, 2, 1, 5), "foo"); + + tokens.save(); + verifyZeroInteractions(sensorStorage); + assertThat(tokens.getTokenLines()).isEmpty(); + } + + @Test + public void save_many_tokens() { + DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage) + .onFile(inputFile) + .addToken(inputFile.newRange(1, 2, 1, 5), "foo") + .addToken(inputFile.newRange(1, 6, 1, 10), "bar") + .addToken(inputFile.newRange(1, 20, 1, 25), "biz") + .addToken(inputFile.newRange(2, 1, 2, 10), "next"); + + tokens.save(); + + verify(sensorStorage).store(tokens); + + assertThat(tokens.getTokenLines()) + .extracting("value", "startLine", "hashCode", "startUnit", "endUnit") + .containsExactly( + tuple("foobarbiz", 1, "foobarbiz".hashCode(), 1, 3), + tuple("next", 2, "next".hashCode(), 4, 4)); + } + + @Test + public void basic_validation() { + SensorStorage sensorStorage = mock(SensorStorage.class); + DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage); + try { + tokens.save(); + fail("Expected exception"); + } catch (Exception e) { + assertThat(e).hasMessage("Call onFile() first"); + } + try { + tokens.addToken(inputFile.newRange(1, 2, 1, 5), "foo"); + fail("Expected exception"); + } catch (Exception e) { + assertThat(e).hasMessage("Call onFile() first"); + } + try { + tokens.addToken(null, "foo"); + fail("Expected exception"); + } catch (Exception e) { + assertThat(e).hasMessage("Range should not be null"); + } + try { + tokens.addToken(inputFile.newRange(1, 2, 1, 5), null); + fail("Expected exception"); + } catch (Exception e) { + assertThat(e).hasMessage("Image should not be null"); + } + } + + @Test + public void validate_tokens_order() { + SensorStorage sensorStorage = mock(SensorStorage.class); + DefaultCpdTokens tokens = new DefaultCpdTokens(sensorStorage) + .onFile(inputFile) + .addToken(inputFile.newRange(1, 6, 1, 10), "bar"); + + try { + tokens.addToken(inputFile.newRange(1, 2, 1, 5), "foo"); + fail("Expected exception"); + } catch (Exception e) { + assertThat(e).hasMessage("Tokens of file src/Foo.java should be provided in order.\n" + + "Previous token: Range[from [line=1, lineOffset=6] to [line=1, lineOffset=10]]\n" + + "Last token: Range[from [line=1, lineOffset=2] to [line=1, lineOffset=5]]"); + } + } + +} 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 index ca91e301cc8..0081480f466 100644 --- 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 @@ -27,13 +27,14 @@ 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.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputProject; import org.sonar.scanner.fs.TestInputFileBuilder; +import org.sonar.scanner.issue.DefaultIssueLocation; 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/DefaultHighlightingTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultHighlightingTest.java index 712703976c2..f70f92dd982 100644 --- 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 @@ -26,9 +26,9 @@ 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.DefaultTextPointer; +import org.sonar.scanner.fs.DefaultTextRange; 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/sensor/DefaultIssueLocationTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultIssueLocationTest.java index 96bd5936b6b..f869f6f17f0 100644 --- 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 @@ -28,6 +28,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.batch.fs.InputFile; import org.sonar.scanner.fs.TestInputFileBuilder; +import org.sonar.scanner.issue.DefaultIssueLocation; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.rules.ExpectedException.none; 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 index 0ab0b99655b..c6659dbc8a1 100644 --- 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 @@ -26,14 +26,16 @@ 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.DefaultInputDir; +import org.sonar.scanner.fs.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputModule; +import org.sonar.scanner.fs.DefaultInputProject; import org.sonar.scanner.fs.TestInputFileBuilder; +import org.sonar.scanner.issue.DefaultIssue; +import org.sonar.scanner.issue.DefaultIssueLocation; 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/DefaultMeasureTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultMeasureTest.java index 43844837e1e..43de69f19d0 100644 --- 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 @@ -25,10 +25,10 @@ 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.AbstractProjectOrModule; +import org.sonar.scanner.fs.DefaultInputProject; 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/sensor/DefaultSensorStorageTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java index d0aca624090..d95d44a69ed 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 @@ -29,21 +29,21 @@ import org.junit.rules.TemporaryFolder; import org.mockito.ArgumentCaptor; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputFile; -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.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.issue.ExternalIssue; import org.sonar.api.batch.sensor.issue.Issue; -import org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable; 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.DefaultInputDir; +import org.sonar.scanner.fs.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputModule; +import org.sonar.scanner.fs.DefaultInputProject; import org.sonar.scanner.fs.TestInputFileBuilder; +import org.sonar.scanner.issue.DefaultIssue; +import org.sonar.scanner.issue.DefaultIssueLocation; 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/DefaultSignificantCodeTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSignificantCodeTest.java new file mode 100644 index 00000000000..73a7f1c4299 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSignificantCodeTest.java @@ -0,0 +1,82 @@ +/* + * 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.fs.InputFile; +import org.sonar.api.batch.sensor.internal.SensorStorage; +import org.sonar.scanner.fs.TestInputFileBuilder; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class DefaultSignificantCodeTest { + private SensorStorage sensorStorage = mock(SensorStorage.class); + private DefaultSignificantCode underTest = new DefaultSignificantCode(sensorStorage); + private InputFile inputFile = TestInputFileBuilder.create("module", "file1.xoo") + .setContents("this is\na file\n with some code") + .build(); + + @Rule + public ExpectedException exception = ExpectedException.none(); + + @Test + public void should_save_ranges() { + underTest.onFile(inputFile) + .addRange(inputFile.selectLine(1)) + .save(); + verify(sensorStorage).store(underTest); + } + + @Test + public void fail_if_save_without_file() { + exception.expect(IllegalStateException.class); + exception.expectMessage("Call onFile() first"); + underTest.save(); + } + + @Test + public void fail_if_add_range_to_same_line_twice() { + underTest.onFile(inputFile); + underTest.addRange(inputFile.selectLine(1)); + + exception.expect(IllegalStateException.class); + exception.expectMessage("Significant code was already reported for line '1'."); + underTest.addRange(inputFile.selectLine(1)); + } + + @Test + public void fail_if_range_includes_many_lines() { + underTest.onFile(inputFile); + + exception.expect(IllegalArgumentException.class); + exception.expectMessage("Ranges of significant code must be located in a single line"); + underTest.addRange(inputFile.newRange(1, 1, 2, 1)); + } + + @Test + public void fail_if_add_range_before_setting_file() { + exception.expect(IllegalStateException.class); + exception.expectMessage("addRange() should be called after on()"); + underTest.addRange(inputFile.selectLine(1)); + } +} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSymbolTableTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSymbolTableTest.java new file mode 100644 index 00000000000..3339e1d2bed --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSymbolTableTest.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.sensor; + +import java.util.Map; +import java.util.Set; +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.sensor.internal.SensorStorage; +import org.sonar.scanner.fs.TestInputFileBuilder; +import org.sonar.scanner.sensor.DefaultSymbolTable; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class DefaultSymbolTableTest { + + 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 Map<TextRange, Set<TextRange>> referencesPerSymbol; + + @Rule + public ExpectedException throwable = ExpectedException.none(); + + @Before + public void setUpSampleSymbols() { + + DefaultSymbolTable symbolTableBuilder = new DefaultSymbolTable(mock(SensorStorage.class)) + .onFile(INPUT_FILE); + symbolTableBuilder + .newSymbol(0, 10) + .newReference(12, 15) + .newReference(2, 10, 2, 15); + + symbolTableBuilder.newSymbol(1, 12, 1, 15).newReference(52, 55); + + symbolTableBuilder.save(); + + referencesPerSymbol = symbolTableBuilder.getReferencesBySymbol(); + } + + @Test + public void should_register_symbols() { + assertThat(referencesPerSymbol).hasSize(2); + } + +} 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 01d3b6046e8..668a050119a 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 @@ -28,7 +28,6 @@ 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.DefaultInputProject; import org.sonar.api.batch.measure.MetricFinder; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.sensor.internal.SensorStorage; @@ -37,6 +36,7 @@ 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.fs.DefaultInputProject; import org.sonar.scanner.rule.ActiveRulesBuilder; 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 index cf1d452f76a..24e93796c31 100644 --- 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 @@ -28,11 +28,6 @@ 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; @@ -46,6 +41,11 @@ 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.fs.DefaultFileSystem; +import org.sonar.scanner.fs.DefaultInputFile; +import org.sonar.scanner.fs.DefaultInputModule; +import org.sonar.scanner.fs.DefaultTextPointer; +import org.sonar.scanner.fs.TestInputFileBuilder; import org.sonar.scanner.rule.ActiveRulesBuilder; import org.sonar.scanner.rule.NewActiveRule; |