diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2018-11-16 15:19:14 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2019-01-16 09:43:02 +0100 |
commit | b6f878d8d48c55c03bcbf2ba8010526f1fa12f49 (patch) | |
tree | 01f2d3038a65ea2d7f79978d17a77f4d33f69522 /sonar-scanner-engine/src/test | |
parent | 6a598e7d7973171e04d78483085ede97a4e21dcb (diff) | |
download | sonarqube-b6f878d8d48c55c03bcbf2ba8010526f1fa12f49.tar.gz sonarqube-b6f878d8d48c55c03bcbf2ba8010526f1fa12f49.zip |
SONAR-11459 Stop publishing modules and folders in the scanner report
Diffstat (limited to 'sonar-scanner-engine/src/test')
24 files changed, 101 insertions, 774 deletions
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 611e42c7793..6588f2e3c0a 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 @@ -234,7 +234,7 @@ public class CpdExecutorTest { .setLines(lines); config.accept(fileBuilder); DefaultInputFile file = fileBuilder.build(); - componentStore.put(file); + componentStore.put("foo", file); return file; } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/tracking/SourceHashHolderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/tracking/SourceHashHolderTest.java index 6aa8686690f..590d4119282 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/tracking/SourceHashHolderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/tracking/SourceHashHolderTest.java @@ -28,11 +28,9 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.mockito.Mockito; -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.internal.DefaultInputFile; -import org.sonar.api.batch.fs.internal.DefaultInputModule; +import org.sonar.api.batch.fs.internal.DefaultInputProject; import static org.apache.commons.codec.digest.DigestUtils.md5Hex; import static org.assertj.core.api.Assertions.assertThat; @@ -51,11 +49,10 @@ public class SourceHashHolderTest { DefaultInputFile file; private File ioFile; - private ProjectDefinition def; + private DefaultInputProject project; @Before public void setUp() throws Exception { - def = ProjectDefinition.create().setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()); lastSnapshots = mock(ServerLineHashesLoader.class); file = mock(DefaultInputFile.class); ioFile = temp.newFile(); @@ -65,7 +62,8 @@ public class SourceHashHolderTest { when(file.lines()).thenReturn(1); when(file.charset()).thenReturn(StandardCharsets.UTF_8); - sourceHashHolder = new SourceHashHolder(new DefaultInputModule(def, 1), file, lastSnapshots); + project = mock(DefaultInputProject.class); + sourceHashHolder = new SourceHashHolder(project, file, lastSnapshots); } @Test @@ -84,8 +82,8 @@ public class SourceHashHolderTest { public void should_lazy_load_reference_hashes_when_status_changed() throws Exception { final String source = "source"; FileUtils.write(ioFile, source, StandardCharsets.UTF_8); - def.setKey("foo"); - when(file.relativePath()).thenReturn("src/Foo.java"); + when(project.getKeyWithBranch()).thenReturn("foo"); + when(file.getProjectRelativePath()).thenReturn("src/Foo.java"); String key = "foo:src/Foo.java"; when(file.status()).thenReturn(InputFile.Status.CHANGED); when(lastSnapshots.getLineHashes(key)).thenReturn(new String[] {md5Hex(source)}); @@ -101,9 +99,8 @@ public class SourceHashHolderTest { public void should_lazy_load_reference_hashes_when_status_changed_on_branch() throws Exception { final String source = "source"; FileUtils.write(ioFile, source, StandardCharsets.UTF_8); - def.setKey("foo"); - def.setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "myBranch"); - when(file.relativePath()).thenReturn("src/Foo.java"); + when(project.getKeyWithBranch()).thenReturn("foo:myBranch"); + when(file.getProjectRelativePath()).thenReturn("src/Foo.java"); String key = "foo:myBranch:src/Foo.java"; when(file.status()).thenReturn(InputFile.Status.CHANGED); when(lastSnapshots.getLineHashes(key)).thenReturn(new String[] {md5Hex(source)}); 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 21d88ed6f5d..62899cf17f1 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 @@ -73,7 +73,7 @@ public class BranchMediumTest { public void should_not_skip_report_for_unchanged_files_in_short_branch() { // sanity check, normally report gets generated AnalysisResult result = getResult(tester); - assertThat(getResult(tester).getReportComponent(result.inputFile(FILE_PATH).key())).isNotNull(); + assertThat(getResult(tester).getReportComponent(result.inputFile(FILE_PATH))).isNotNull(); int fileId = 2; assertThat(result.getReportReader().readChangesets(fileId)).isNotNull(); assertThat(result.getReportReader().hasCoverage(fileId)).isTrue(); @@ -81,7 +81,7 @@ public class BranchMediumTest { // file is not skipped for short branches (need coverage, duplications coming soon) AnalysisResult result2 = getResult(tester.setBranchType(BranchType.SHORT)); - assertThat(result2.getReportComponent(result2.inputFile(FILE_PATH).key())).isNotNull(); + assertThat(result2.getReportComponent(result2.inputFile(FILE_PATH))).isNotNull(); assertThat(result2.getReportReader().readChangesets(fileId)).isNull(); assertThat(result2.getReportReader().hasCoverage(fileId)).isTrue(); assertThat(result2.getReportReader().readFileSource(fileId)).isNull(); 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 bcc46d488d4..41ea161bc72 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 @@ -93,7 +93,7 @@ public class DeprecatedBranchMediumTest { assertThat(result.inputFile("src/sample.xoo").key()).isEqualTo("com.foo.project:src/sample.xoo"); DefaultInputFile inputfile = (DefaultInputFile) result.inputFile("src/sample.xoo"); - assertThat(result.getReportReader().readComponent(inputfile.scannerId()).getPath()).isEqualTo("src/sample.xoo"); + assertThat(result.getReportReader().readComponent(inputfile.scannerId()).getProjectRelativePath()).isEqualTo("src/sample.xoo"); assertThat(result.getReportReader().readMetadata().getDeprecatedBranch()).isEqualTo("branch"); @@ -125,14 +125,11 @@ public class DeprecatedBranchMediumTest { .execute(); assertThat(result.inputFiles()).hasSize(1); - assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA:src/sample.xoo"); + assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA/src/sample.xoo"); // no branch in the report DefaultInputFile inputfile = (DefaultInputFile) result.inputFile("moduleA/src/sample.xoo"); - assertThat(result.getReportReader().readComponent(inputfile.scannerId()).getPath()).isEqualTo("src/sample.xoo"); - - // no branch in InputModule's key or in report - assertThat(result.getReportComponent("com.foo.project:moduleA").getKey()).isEqualTo("com.foo.project:moduleA"); + assertThat(result.getReportReader().readComponent(inputfile.scannerId()).getProjectRelativePath()).isEqualTo("moduleA/src/sample.xoo"); assertThat(result.getReportReader().readMetadata().getDeprecatedBranch()).isEqualTo("branch"); @@ -145,7 +142,7 @@ public class DeprecatedBranchMediumTest { .execute(); assertThat(result.inputFiles()).hasSize(1); - assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA:src/sample.xoo"); + assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA/src/sample.xoo"); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/coverage/CoverageMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/coverage/CoverageMediumTest.java index f230f2b8bf8..1417262c3f9 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/coverage/CoverageMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/coverage/CoverageMediumTest.java @@ -330,14 +330,14 @@ public class CoverageMediumTest { Map<String, List<org.sonar.scanner.protocol.output.ScannerReport.Measure>> allMeasures = result.allMeasures(); - assertThat(allMeasures.get("com.foo.project:module1:src/sample1.xoo")).extracting("metricKey", "intValue.value") + assertThat(allMeasures.get("com.foo.project:module1/src/sample1.xoo")).extracting("metricKey", "intValue.value") .contains(tuple(CoreMetrics.LINES_TO_COVER_KEY, 2), tuple(CoreMetrics.UNCOVERED_LINES_KEY, 2)); - assertThat(allMeasures.get("com.foo.project:module1:src/sample1.xoo")).extracting("metricKey").doesNotContain(CoreMetrics.CONDITIONS_TO_COVER_KEY, + assertThat(allMeasures.get("com.foo.project:module1/src/sample1.xoo")).extracting("metricKey").doesNotContain(CoreMetrics.CONDITIONS_TO_COVER_KEY, CoreMetrics.UNCOVERED_CONDITIONS_KEY); - assertThat(allMeasures.get("com.foo.project:module1:src/sample2.xoo")).extracting("metricKey").doesNotContain(CoreMetrics.LINES_TO_COVER_KEY, + assertThat(allMeasures.get("com.foo.project:module1/src/sample2.xoo")).extracting("metricKey").doesNotContain(CoreMetrics.LINES_TO_COVER_KEY, CoreMetrics.CONDITIONS_TO_COVER_KEY, CoreMetrics.UNCOVERED_CONDITIONS_KEY, CoreMetrics.UNCOVERED_LINES_KEY); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java index 6ce98f526ac..e31dfbb1b4e 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/cpd/CpdMediumTest.java @@ -125,7 +125,7 @@ public class CpdMediumTest { assertThat(cloneGroupFile1.getOriginPosition().getStartLine()).isEqualTo(1); assertThat(cloneGroupFile1.getOriginPosition().getEndLine()).isEqualTo(17); assertThat(cloneGroupFile1.getDuplicateList()).hasSize(1); - assertThat(cloneGroupFile1.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent((inputFile2).key()).getRef()); + assertThat(cloneGroupFile1.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent(inputFile2).getRef()); List<ScannerReport.Duplication> duplicationGroupsFile2 = result.duplicationsFor(inputFile2); assertThat(duplicationGroupsFile2).hasSize(1); @@ -134,7 +134,7 @@ public class CpdMediumTest { assertThat(cloneGroupFile2.getOriginPosition().getStartLine()).isEqualTo(1); assertThat(cloneGroupFile2.getOriginPosition().getEndLine()).isEqualTo(17); assertThat(cloneGroupFile2.getDuplicateList()).hasSize(1); - assertThat(cloneGroupFile2.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent((inputFile1).key()).getRef()); + assertThat(cloneGroupFile2.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent(inputFile1).getRef()); assertThat(result.duplicationBlocksFor(inputFile1)).isEmpty(); } @@ -177,7 +177,7 @@ public class CpdMediumTest { assertThat(cloneGroupFile1.getOriginPosition().getStartLine()).isEqualTo(1); assertThat(cloneGroupFile1.getOriginPosition().getEndLine()).isEqualTo(17); assertThat(cloneGroupFile1.getDuplicateList()).hasSize(1); - assertThat(cloneGroupFile1.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent((inputFile2).key()).getRef()); + assertThat(cloneGroupFile1.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent(inputFile2).getRef()); List<ScannerReport.Duplication> duplicationGroupsFile2 = result.duplicationsFor(inputFile2); assertThat(duplicationGroupsFile2).hasSize(1); @@ -186,7 +186,7 @@ public class CpdMediumTest { assertThat(cloneGroupFile2.getOriginPosition().getStartLine()).isEqualTo(1); assertThat(cloneGroupFile2.getOriginPosition().getEndLine()).isEqualTo(17); assertThat(cloneGroupFile2.getDuplicateList()).hasSize(1); - assertThat(cloneGroupFile2.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent((inputFile1).key()).getRef()); + assertThat(cloneGroupFile2.getDuplicate(0).getOtherFileRef()).isEqualTo(result.getReportComponent(inputFile1).getRef()); assertThat(result.duplicationBlocksFor(inputFile1)).isEmpty(); } 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 a8e84a6faf2..705692a6777 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 @@ -25,7 +25,6 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.Random; import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; @@ -34,10 +33,8 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.scanner.fs.InputProject; import org.sonar.api.utils.MessageException; import org.sonar.api.utils.System2; import org.sonar.scanner.mediumtest.AnalysisResult; @@ -112,19 +109,15 @@ public class FileSystemMediumTest { int ref = result.getReportReader().readMetadata().getRootComponentRef(); assertThat(result.getReportReader().readComponent(ref).getName()).isEmpty(); assertThat(result.inputFiles()).hasSize(1); - assertThat(result.inputDirs()).hasSize(1); DefaultInputFile file = (DefaultInputFile) result.inputFile("src/sample.xoo"); - InputDir dir = result.inputDir("src"); assertThat(file.type()).isEqualTo(InputFile.Type.MAIN); assertThat(file.relativePath()).isEqualTo("src/sample.xoo"); assertThat(file.language()).isEqualTo("xoo"); - assertThat(dir.relativePath()).isEqualTo("src"); - // file and dirs were published, since language matched xoo + // file was published, since language matched xoo assertThat(file.isPublished()).isTrue(); - assertThat(result.getReportComponent(dir.key())).isNotNull(); - assertThat(result.getReportComponent(file.key())).isNotNull(); + assertThat(result.getReportComponent(file.scannerId())).isNotNull(); } @Test @@ -290,7 +283,11 @@ public class FileSystemMediumTest { assertThat(logs.getAllAsString()).doesNotContain("'src/main/sample.java' generated metadata"); assertThat(logs.getAllAsString()).doesNotContain("'src/test/sample.java' generated metadata"); DefaultInputFile javaInputFile = (DefaultInputFile) result.inputFile("src/main/sample.java"); - assertThat(result.getReportComponent(javaInputFile.key())).isNull(); + + thrown.expect(IllegalStateException.class); + thrown.expectMessage("Unable to find report for component"); + + result.getReportComponent(javaInputFile); } @Test @@ -317,7 +314,7 @@ public class FileSystemMediumTest { assertThat(logs.getAllAsString()).contains("'src" + File.separator + "sample.unknown' indexed with language 'null'"); assertThat(logs.getAllAsString()).contains("'src/sample.unknown' generated metadata"); DefaultInputFile inputFile = (DefaultInputFile) result.inputFile("src/sample.unknown"); - assertThat(result.getReportComponent(inputFile.key())).isNotNull(); + assertThat(result.getReportComponent(inputFile)).isNotNull(); } @Test @@ -397,54 +394,9 @@ public class FileSystemMediumTest { .execute(); DefaultInputFile file = (DefaultInputFile) result.inputFile("src/sample.xoo"); - InputDir dir = result.inputDir("src"); assertThat(file.isPublished()).isTrue(); - assertThat(result.getReportComponent(dir.key())).isNotNull(); - assertThat(result.getReportComponent(file.key())).isNotNull(); - } - - @Test - public void publishDirsWithIssues() throws IOException { - tester - .addRules(new XooRulesDefinition()) - .addActiveRule("xoo", "OneIssuePerDirectory", null, "OneIssuePerDirectory", "MAJOR", null, "xoo"); - - builder = ImmutableMap.<String, String>builder() - .put("sonar.task", "scan") - .put("sonar.verbose", "true") - .put("sonar.projectBaseDir", baseDir.getAbsolutePath()) - .put("sonar.projectKey", "com.foo.project") - .put("sonar.projectVersion", "1.0-SNAPSHOT") - .put("sonar.projectDescription", "Description of Foo Project"); - - Path unknownRelative = Paths.get("src", "unknown", "file.notanalyzed"); - Path unknown = baseDir.toPath().resolve(unknownRelative); - Files.createDirectories(unknown.getParent()); - Files.write(unknown, "dummy content".getBytes()); - - Path emptyDirRelative = Paths.get("src", "emptydir"); - Files.createDirectories(emptyDirRelative); - - AnalysisResult result = tester.newAnalysis() - .properties(builder - .put("sonar.sources", "src") - .build()) - .execute(); - - DefaultInputFile unknownInputFile = (DefaultInputFile) result.inputFile("src/unknown/file.notanalyzed"); - InputProject project = result.project(); - - assertThat(unknownInputFile.isPublished()).isFalse(); - assertThat(result.getReportComponent(project.key())).isNotNull(); - - // no issues on empty dir - InputDir emptyInputDir = result.inputDir(emptyDirRelative.toString()); - assertThat(emptyInputDir).isNull(); - - // no issues on parent dir - InputDir parentInputDir = result.inputDir(unknownRelative.getParent().getParent().toString()); - assertThat(parentInputDir).isNull(); + assertThat(result.getReportComponent(file)).isNotNull(); } @Test @@ -462,10 +414,8 @@ public class FileSystemMediumTest { .execute(); assertThat(result.inputFiles()).hasSize(1); - assertThat(result.inputDirs()).hasSize(1); assertThat(result.inputFile("src/sample.xoo").type()).isEqualTo(InputFile.Type.MAIN); assertThat(result.inputFile("src/sample.xoo").relativePath()).isEqualTo("src/sample.xoo"); - assertThat(result.inputDir("src").relativePath()).isEqualTo("src"); } @Test @@ -487,7 +437,6 @@ public class FileSystemMediumTest { .execute(); assertThat(result.inputFiles()).hasSize(100); - assertThat(result.inputDirs()).hasSize(1); } @Test @@ -540,7 +489,6 @@ public class FileSystemMediumTest { .execute(); assertThat(result.inputFiles()).hasSize(4); - assertThat(result.inputDirs()).hasSize(3); } @Test @@ -680,7 +628,6 @@ public class FileSystemMediumTest { .execute(); assertThat(result.inputFiles()).hasSize(4); - assertThat(result.inputDirs()).hasSize(4); } @Test @@ -727,7 +674,6 @@ public class FileSystemMediumTest { .execute(); assertThat(result.inputFiles()).hasSize(4); - assertThat(result.inputDirs()).hasSize(3); } @Test diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issues/IssuesOnDirMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issues/IssuesOnDirMediumTest.java index ed76bba6c25..e09f0b42967 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issues/IssuesOnDirMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issues/IssuesOnDirMediumTest.java @@ -70,7 +70,6 @@ public class IssuesOnDirMediumTest { .build()) .execute(); - assertThat(result.issuesFor(result.inputDir("src"))).hasSize(0); assertThat(result.issuesFor(result.project())).hasSize(2); } @@ -97,7 +96,6 @@ public class IssuesOnDirMediumTest { .build()) .execute(); - assertThat(result.issuesFor(result.inputDir(""))).hasSize(0); assertThat(result.issuesFor(result.project())).hasSize(2); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issues/IssuesOnModuleMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issues/IssuesOnModuleMediumTest.java index 48561c72e0a..5c8fa0d92e1 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issues/IssuesOnModuleMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issues/IssuesOnModuleMediumTest.java @@ -67,7 +67,7 @@ public class IssuesOnModuleMediumTest { .build()) .execute(); - assertThat(result.issuesFor(result.getReportComponent("com.foo.project"))).hasSize(1); + assertThat(result.issuesFor(result.project())).hasSize(1); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issuesmode/IssueModeAndReportsMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issuesmode/IssueModeAndReportsMediumTest.java index cb29125779c..2e58d577288 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issuesmode/IssueModeAndReportsMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/issuesmode/IssueModeAndReportsMediumTest.java @@ -147,7 +147,7 @@ public class IssueModeAndReportsMediumTest { String logs = StringUtils.join(logTester.logs(LoggerLevel.INFO), "\n"); assertThat(logs).contains("Performing issue tracking"); - assertThat(logs).contains("6/6 components tracked"); + assertThat(logs).contains("4/4 components tracked"); // assert that original fields of a matched issue are kept assertThat(result.trackedIssues()).haveExactly(1, new Condition<TrackedIssue>() { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/scm/ScmMediumTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/scm/ScmMediumTest.java index 5b42f3e016c..94f6030b3e6 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/scm/ScmMediumTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/scm/ScmMediumTest.java @@ -116,10 +116,9 @@ public class ScmMediumTest { ScannerReportReader reader = new ScannerReportReader(reportDir); Component project = reader.readComponent(reader.readMetadata().getRootComponentRef()); - Component dir = reader.readComponent(project.getChildRef(0)); - for (Integer fileRef : dir.getChildRefList()) { + for (Integer fileRef : project.getChildRefList()) { Component file = reader.readComponent(fileRef); - if (file.getPath().equals(path)) { + if (file.getProjectRelativePath().equals(path)) { return reader.readChangesets(file.getRef()); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/DefaultPostJobContextTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/DefaultPostJobContextTest.java index 245fe2cefc1..3d45aa214d8 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/DefaultPostJobContextTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/postjob/DefaultPostJobContextTest.java @@ -87,7 +87,7 @@ public class DefaultPostJobContextTest { assertThat(issue.inputComponent()).isNull(); String moduleKey = "foo"; - componentStore.put(new TestInputFileBuilder(moduleKey, "src/Foo.php").build()); + componentStore.put(moduleKey, new TestInputFileBuilder(moduleKey, "src/Foo.php").build()); assertThat(issue.inputComponent()).isNotNull(); } 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 4b27f676ae9..e13bc24a9d0 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 @@ -21,12 +21,8 @@ package org.sonar.scanner.report; import java.io.File; import java.io.IOException; -import java.nio.file.Files; import java.nio.file.Path; -import java.util.Arrays; import java.util.Collections; -import java.util.HashMap; -import java.util.Map; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -35,10 +31,8 @@ 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.DefaultInputDir; import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.fs.internal.DefaultInputModule; -import org.sonar.api.batch.fs.internal.InputModuleHierarchy; +import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; import org.sonar.api.utils.DateUtils; import org.sonar.scanner.ProjectAnalysisInfo; @@ -49,24 +43,17 @@ import org.sonar.scanner.protocol.output.ScannerReport.Component.FileStatus; import org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType; import org.sonar.scanner.protocol.output.ScannerReportReader; import org.sonar.scanner.protocol.output.ScannerReportWriter; -import org.sonar.scanner.scan.DefaultComponentTree; -import org.sonar.scanner.scan.DefaultInputModuleHierarchy; import org.sonar.scanner.scan.branch.BranchConfiguration; -import org.sonar.scanner.scan.branch.BranchType; +import org.sonar.scanner.scan.filesystem.InputComponentStore; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.sonar.api.batch.fs.internal.TestInputFileBuilder.newDefaultInputDir; -import static org.sonar.api.batch.fs.internal.TestInputFileBuilder.newDefaultInputFile; -import static org.sonar.api.batch.fs.internal.TestInputFileBuilder.newDefaultInputModule; public class ComponentsPublisherTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); - private DefaultComponentTree tree; - private InputModuleHierarchy moduleHierarchy; private File outputDir; private ScannerReportWriter writer; private ScannerReportReader reader; @@ -75,7 +62,6 @@ public class ComponentsPublisherTest { @Before public void setUp() throws IOException { branchConfiguration = mock(BranchConfiguration.class); - tree = new DefaultComponentTree(); outputDir = temp.newFolder(); writer = new ScannerReportWriter(outputDir); reader = new ScannerReportReader(outputDir); @@ -97,7 +83,9 @@ public class ComponentsPublisherTest { .setDescription("Root description") .setBaseDir(temp.newFolder()) .setWorkDir(temp.newFolder()); - DefaultInputModule root = new DefaultInputModule(rootDef, 1); + DefaultInputProject project = new DefaultInputProject(rootDef, 1); + + InputComponentStore store = new InputComponentStore(project, branchConfiguration); Path moduleBaseDir = temp.newFolder().toPath(); ProjectDefinition module1Def = ProjectDefinition.create() @@ -108,43 +96,25 @@ public class ComponentsPublisherTest { .setWorkDir(temp.newFolder()); rootDef.addSubProject(module1Def); - DefaultInputModule module1 = new DefaultInputModule(module1Def, 2); - - moduleHierarchy = mock(InputModuleHierarchy.class); - when(moduleHierarchy.root()).thenReturn(root); - when(moduleHierarchy.children(root)).thenReturn(Collections.singleton(module1)); - when(moduleHierarchy.parent(module1)).thenReturn(root); - tree.index(module1, root); - - DefaultInputDir dir = new DefaultInputDir("module1", "src", 3) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir, module1); - - DefaultInputDir dir2 = new DefaultInputDir("module1", "src2", 17) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir2, module1); + DefaultInputFile file = new TestInputFileBuilder("foo", "module1/src/Foo.java", 4).setLines(2).setStatus(InputFile.Status.SAME).build(); + store.put("module1", file); - DefaultInputFile file = new TestInputFileBuilder("module1", "src/Foo.java", 4).setLines(2).setStatus(InputFile.Status.SAME).build(); - tree.index(file, dir); + DefaultInputFile file18 = new TestInputFileBuilder("foo", "module1/src2/Foo.java", 18).setLines(2).setStatus(InputFile.Status.SAME).build(); + store.put("module1", file18); - DefaultInputFile file18 = new TestInputFileBuilder("module1", "src2/Foo.java", 18).setLines(2).setStatus(InputFile.Status.SAME).build(); - tree.index(file18, dir2); + DefaultInputFile file2 = new TestInputFileBuilder("foo", "module1/src/Foo2.java", 5).setPublish(false).setLines(2).build(); + store.put("module1", file2); - DefaultInputFile file2 = new TestInputFileBuilder("module1", "src/Foo2.java", 5).setPublish(false).setLines(2).build(); - tree.index(file2, dir); + DefaultInputFile fileWithoutLang = new TestInputFileBuilder("foo", "module1/src/make", 6).setLines(10).setStatus(InputFile.Status.CHANGED).build(); + store.put("module1", fileWithoutLang); - DefaultInputFile fileWithoutLang = new TestInputFileBuilder("module1", "src/make", 6).setLines(10).setStatus(InputFile.Status.CHANGED).build(); - tree.index(fileWithoutLang, dir); + DefaultInputFile testFile = new TestInputFileBuilder("foo", "module1/test/FooTest.java", 7).setType(Type.TEST).setStatus(InputFile.Status.ADDED).setLines(4).build(); + store.put("module1", testFile); - DefaultInputFile testFile = new TestInputFileBuilder("module1", "test/FooTest.java", 7).setType(Type.TEST).setStatus(InputFile.Status.ADDED).setLines(4).build(); - tree.index(testFile, dir); - - ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree, branchConfiguration); + ComponentsPublisher publisher = new ComponentsPublisher(project, store); publisher.publish(writer); assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 2)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isTrue(); assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 4)).isTrue(); assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isTrue(); assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isTrue(); @@ -161,11 +131,6 @@ public class ComponentsPublisherTest { assertThat(rootProtobuf.getVersion()).isEqualTo("1.0"); assertThat(rootProtobuf.getLinkCount()).isEqualTo(0); - Component module1Protobuf = reader.readComponent(2); - assertThat(module1Protobuf.getKey()).isEqualTo("module1"); - assertThat(module1Protobuf.getDescription()).isEqualTo("Module description"); - assertThat(module1Protobuf.getVersion()).isEqualTo("1.0"); - assertThat(reader.readComponent(4).getStatus()).isEqualTo(FileStatus.SAME); assertThat(reader.readComponent(6).getStatus()).isEqualTo(FileStatus.CHANGED); assertThat(reader.readComponent(7).getStatus()).isEqualTo(FileStatus.ADDED); @@ -183,12 +148,11 @@ public class ComponentsPublisherTest { .setWorkDir(temp.newFolder()) .setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "my_branch"); - DefaultInputModule root = new DefaultInputModule(rootDef, 1); + DefaultInputProject project = new DefaultInputProject(rootDef, 1); - moduleHierarchy = mock(InputModuleHierarchy.class); - when(moduleHierarchy.root()).thenReturn(root); + InputComponentStore store = new InputComponentStore(project, branchConfiguration); - ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree, branchConfiguration); + ComponentsPublisher publisher = new ComponentsPublisher(project, store); publisher.publish(writer); Component rootProtobuf = reader.readComponent(1); assertThat(rootProtobuf.getKey()).isEqualTo("foo"); @@ -196,311 +160,48 @@ public class ComponentsPublisherTest { } @Test - public void should_write_relative_path_to_root_for_modules_in_flat_hierarchy() throws IOException { - Path rootBaseDir = temp.newFolder().toPath(); - Path module1BaseDir = rootBaseDir.resolve("module1"); - Path module2BaseDir = rootBaseDir.resolve("module2"); - Files.createDirectories(module1BaseDir); - Files.createDirectories(module2BaseDir); - - ProjectDefinition rootDef = ProjectDefinition.create() - .setKey("foo") - .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0") - .setName("Root project") - .setDescription("Root description") - .setBaseDir(rootBaseDir.toFile()) - .setWorkDir(temp.newFolder()); - DefaultInputModule root = new DefaultInputModule(rootDef, 1); - - ProjectDefinition module1Def = ProjectDefinition.create() - .setKey("module1k") - .setName("Module1") - .setDescription("Module description") - .setBaseDir(module1BaseDir.toFile()) - .setWorkDir(temp.newFolder()); - rootDef.addSubProject(module1Def); - DefaultInputModule module1 = new DefaultInputModule(module1Def, 2); - - ProjectDefinition module2Def = ProjectDefinition.create() - .setKey("module2k") - .setName("Module2") - .setDescription("Module description") - .setBaseDir(module2BaseDir.toFile()) - .setWorkDir(temp.newFolder()); - module1Def.addSubProject(module2Def); - DefaultInputModule module2 = new DefaultInputModule(module2Def, 3); - - Map<DefaultInputModule, DefaultInputModule> modules = new HashMap<>(); - modules.put(module2, module1); - modules.put(module1, root); - moduleHierarchy = new DefaultInputModuleHierarchy(root, modules); - tree.index(module2, module1); - tree.index(module1, root); - ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree, branchConfiguration); - publisher.publish(writer); - - assertThat(reader.readComponent(2).getProjectRelativePath()).isEqualTo("module1"); - assertThat(reader.readComponent(3).getProjectRelativePath()).isEqualTo("module2"); - } - - @Test - public void should_skip_dir_without_published_files() throws IOException { - ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class); - when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12")); - - Path moduleBaseDir = temp.newFolder().toPath(); - ProjectDefinition rootDef = ProjectDefinition.create() - .setKey("foo") - .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0") - .setName("Root project") - .setDescription("Root description") - .setBaseDir(moduleBaseDir.toFile()) - .setWorkDir(temp.newFolder()); - DefaultInputModule root = new DefaultInputModule(rootDef, 1); - - moduleHierarchy = mock(InputModuleHierarchy.class); - when(moduleHierarchy.root()).thenReturn(root); - when(moduleHierarchy.children(root)).thenReturn(Collections.emptyList()); - - // dir with files - DefaultInputDir dir = new DefaultInputDir("module1", "src", 2) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir, root); - - // dir without files and issues - DefaultInputDir dir2 = new DefaultInputDir("module1", "src2", 3) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir2, root); - - // dir without files but has issues - DefaultInputDir dir3 = new DefaultInputDir("module1", "src3", 4) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir3, root); - writeIssue(4); - - DefaultInputFile file = new TestInputFileBuilder("module1", "src/Foo.java", 5).setLines(2).setStatus(InputFile.Status.SAME).build(); - tree.index(file, dir); - - DefaultInputFile file2 = new TestInputFileBuilder("module1", "src2/Foo2.java", 6).setPublish(false).setLines(2).build(); - tree.index(file2, dir2); - - DefaultInputFile file3 = new TestInputFileBuilder("module1", "src2/Foo3.java", 7).setPublish(false).setLines(2).build(); - tree.index(file3, dir3); - - ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree, branchConfiguration); - publisher.publish(writer); - - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 2)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 5)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 4)).isTrue(); - - // file was not marked for publishing and directory doesn't contain issues, so directory won't be included as well - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isFalse(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isFalse(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isFalse(); - } - - @Test - public void should_skip_empty_modules_for_short_living_branches() throws IOException { - ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class); - when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12")); - - ProjectDefinition rootDef = ProjectDefinition.create() - .setKey("foo") - .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0") - .setName("Root project") - .setDescription("Root description") - .setBaseDir(temp.newFolder()) - .setWorkDir(temp.newFolder()); - DefaultInputModule root = new DefaultInputModule(rootDef, 1); - - ProjectDefinition emptyModuleDef = ProjectDefinition.create() - .setKey("modEmpty") - .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0") - .setName("Empty module") - .setDescription("Empty module") - .setBaseDir(temp.newFolder()) - .setWorkDir(temp.newFolder()); - DefaultInputModule emptyModule = new DefaultInputModule(emptyModuleDef, 2); - - ProjectDefinition notEmptyModuleDef = ProjectDefinition.create() - .setKey("modNotEmpty") - .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0") - .setName("Module") - .setDescription("Module") - .setBaseDir(temp.newFolder()) - .setWorkDir(temp.newFolder()); - DefaultInputModule notEmptyModule = new DefaultInputModule(notEmptyModuleDef, 3); - - moduleHierarchy = mock(InputModuleHierarchy.class); - when(moduleHierarchy.root()).thenReturn(root); - when(moduleHierarchy.isRoot(root)).thenReturn(true); - when(moduleHierarchy.children(root)).thenReturn(Arrays.asList(emptyModule, notEmptyModule)); - when(moduleHierarchy.children(emptyModule)).thenReturn(Collections.emptyList()); - when(moduleHierarchy.children(notEmptyModule)).thenReturn(Collections.emptyList()); - when(branchConfiguration.branchType()).thenReturn(BranchType.SHORT); - - // dir with files - DefaultInputDir dir = new DefaultInputDir("modNotEmpty", "src", 4); - tree.index(dir, notEmptyModule); - - // Only an unchanged file, so module should also be skipped - DefaultInputFile file = new TestInputFileBuilder("modNotEmpty", "src/Foo.java", 5).setLines(2).setStatus(InputFile.Status.SAME).build(); - tree.index(file, dir); - - ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree, branchConfiguration); - publisher.publish(writer); - - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 2)).isFalse(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isFalse(); - } - - @Test - public void do_not_skip_unchanged_components_in_short_branches() throws IOException { - when(branchConfiguration.isShortOrPullRequest()).thenReturn(true); - ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class); - when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12")); - - Path moduleBaseDir = temp.newFolder().toPath(); - ProjectDefinition rootDef = ProjectDefinition.create() - .setKey("foo") - .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0") - .setName("Root project") - .setDescription("Root description") - .setBaseDir(moduleBaseDir.toFile()) - .setWorkDir(temp.newFolder()); - DefaultInputModule root = new DefaultInputModule(rootDef, 1); - - moduleHierarchy = mock(InputModuleHierarchy.class); - when(moduleHierarchy.root()).thenReturn(root); - when(moduleHierarchy.children(root)).thenReturn(Collections.emptyList()); - - // dir with changed files - DefaultInputDir dir = new DefaultInputDir("module1", "src", 2) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir, root); - - // dir without changed files or issues - DefaultInputDir dir2 = new DefaultInputDir("module1", "src2", 3) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir2, root); - - // dir without changed files but has issues - DefaultInputDir dir3 = new DefaultInputDir("module1", "src3", 4) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir3, root); - writeIssue(4); - - DefaultInputFile file = new TestInputFileBuilder("module1", "src/Foo.java", 5) - .setLines(2) - .setPublish(true) - .setStatus(InputFile.Status.ADDED) - .build(); - tree.index(file, dir); - - DefaultInputFile file2 = new TestInputFileBuilder("module1", "src2/Foo2.java", 6) - .setPublish(true) - .setStatus(InputFile.Status.SAME) - .setLines(2) - .build(); - tree.index(file2, dir2); - - DefaultInputFile file3 = new TestInputFileBuilder("module1", "src3/Foo3.java", 7) - .setPublish(true) - .setStatus(InputFile.Status.SAME) - .setLines(2) - .build(); - tree.index(file3, dir3); - - ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree, branchConfiguration); - publisher.publish(writer); - - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 2)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 4)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 5)).isTrue(); - - // do not skip, needed for computing overall coverage - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isTrue(); - } - - @Test - public void do_not_skip_unchanged_components_in_pull_requests() throws IOException { + public void publish_unchanged_components_even_in_short_branches() throws IOException { when(branchConfiguration.isShortOrPullRequest()).thenReturn(true); ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class); when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12")); - Path moduleBaseDir = temp.newFolder().toPath(); + Path baseDir = temp.newFolder().toPath(); ProjectDefinition rootDef = ProjectDefinition.create() .setKey("foo") .setProperty(CoreProperties.PROJECT_VERSION_PROPERTY, "1.0") .setName("Root project") .setDescription("Root description") - .setBaseDir(moduleBaseDir.toFile()) + .setBaseDir(baseDir.toFile()) .setWorkDir(temp.newFolder()); - DefaultInputModule root = new DefaultInputModule(rootDef, 1); - - moduleHierarchy = mock(InputModuleHierarchy.class); - when(moduleHierarchy.root()).thenReturn(root); - when(moduleHierarchy.children(root)).thenReturn(Collections.emptyList()); - - // dir with changed files - DefaultInputDir dir = new DefaultInputDir("module1", "src", 2) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir, root); - - // dir without changed files or issues - DefaultInputDir dir2 = new DefaultInputDir("module1", "src2", 3) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir2, root); + DefaultInputProject project = new DefaultInputProject(rootDef, 1); - // dir without changed files but has issues - DefaultInputDir dir3 = new DefaultInputDir("module1", "src3", 4) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir3, root); - writeIssue(4); + InputComponentStore store = new InputComponentStore(project, branchConfiguration); - DefaultInputFile file = new TestInputFileBuilder("module1", "src/Foo.java", 5) + DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.java", 5) .setLines(2) .setPublish(true) .setStatus(InputFile.Status.ADDED) .build(); - tree.index(file, dir); + store.put("foo", file); - DefaultInputFile file2 = new TestInputFileBuilder("module1", "src2/Foo2.java", 6) + DefaultInputFile file2 = new TestInputFileBuilder("foo", "src2/Foo2.java", 6) .setPublish(true) .setStatus(InputFile.Status.SAME) .setLines(2) .build(); - tree.index(file2, dir2); + store.put("foo", file2); - DefaultInputFile file3 = new TestInputFileBuilder("module1", "src3/Foo3.java", 7) - .setPublish(true) - .setStatus(InputFile.Status.SAME) - .setLines(2) - .build(); - tree.index(file3, dir3); - - ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree, branchConfiguration); + ComponentsPublisher publisher = new ComponentsPublisher(project, store); publisher.publish(writer); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 2)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 4)).isTrue(); assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 5)).isTrue(); // do not skip, needed for computing overall coverage - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isTrue(); assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isTrue(); } @Test - public void add_components_without_version_and_name() throws IOException { + public void publish_project_without_version_and_name() throws IOException { ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class); when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12")); @@ -509,47 +210,13 @@ public class ComponentsPublisherTest { .setDescription("Root description") .setBaseDir(temp.newFolder()) .setWorkDir(temp.newFolder()); - DefaultInputModule root = new DefaultInputModule(rootDef, 1); - - Path moduleBaseDir = temp.newFolder().toPath(); - ProjectDefinition module1Def = ProjectDefinition.create() - .setKey("module1") - .setDescription("Module description") - .setBaseDir(moduleBaseDir.toFile()) - .setWorkDir(temp.newFolder()); - rootDef.addSubProject(module1Def); - DefaultInputModule module1 = new DefaultInputModule(module1Def, 2); - - moduleHierarchy = mock(InputModuleHierarchy.class); - when(moduleHierarchy.root()).thenReturn(root); - when(moduleHierarchy.children(root)).thenReturn(Collections.singleton(module1)); - tree.index(module1, root); - - DefaultInputDir dir = new DefaultInputDir("module1", "src", 3) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir, module1); - - DefaultInputFile file = new TestInputFileBuilder("module1", "src/Foo.java", 4).setLines(2).setStatus(InputFile.Status.SAME).build(); - tree.index(file, dir); - - DefaultInputFile fileWithoutLang = new TestInputFileBuilder("module1", "src/make", 5).setLines(10).setStatus(InputFile.Status.SAME).build(); - tree.index(fileWithoutLang, dir); + DefaultInputProject project = new DefaultInputProject(rootDef, 1); - DefaultInputFile testFile = new TestInputFileBuilder("module1", "test/FooTest.java", 6).setType(Type.TEST).setStatus(InputFile.Status.SAME).setLines(4).build(); - tree.index(testFile, dir); - - ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree, branchConfiguration); + InputComponentStore store = new InputComponentStore(project, branchConfiguration); + ComponentsPublisher publisher = new ComponentsPublisher(project, store); publisher.publish(writer); assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 2)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 3)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 4)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 5)).isTrue(); - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 6)).isTrue(); - - // no such reference - assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 7)).isFalse(); ScannerReportReader reader = new ScannerReportReader(outputDir); Component rootProtobuf = reader.readComponent(1); @@ -558,16 +225,10 @@ public class ComponentsPublisherTest { assertThat(rootProtobuf.getDescription()).isEqualTo("Root description"); assertThat(rootProtobuf.getVersion()).isEqualTo(""); assertThat(rootProtobuf.getLinkCount()).isEqualTo(0); - - Component module1Protobuf = reader.readComponent(2); - assertThat(module1Protobuf.getKey()).isEqualTo("module1"); - assertThat(module1Protobuf.getName()).isEqualTo(""); - assertThat(module1Protobuf.getDescription()).isEqualTo("Module description"); - assertThat(module1Protobuf.getVersion()).isEqualTo(""); } @Test - public void add_components_with_links_and_branch() throws Exception { + public void publish_project_with_links_and_branch() throws Exception { ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class); when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12")); @@ -577,156 +238,23 @@ public class ComponentsPublisherTest { .setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "my_branch") .setName("Root project") .setProperty(CoreProperties.LINKS_HOME_PAGE, "http://home") + .setProperty(CoreProperties.LINKS_CI, "http://ci") .setDescription("Root description") .setBaseDir(temp.newFolder()) .setWorkDir(temp.newFolder()); - DefaultInputModule root = new DefaultInputModule(rootDef, 1); - - Path moduleBaseDir = temp.newFolder().toPath(); - ProjectDefinition module1Def = ProjectDefinition.create() - .setKey("module1") - .setName("Module1") - .setProperty(CoreProperties.LINKS_CI, "http://ci") - .setDescription("Module description") - .setBaseDir(moduleBaseDir.toFile()) - .setWorkDir(temp.newFolder()); - rootDef.addSubProject(module1Def); - DefaultInputModule module1 = new DefaultInputModule(module1Def, 2); - - moduleHierarchy = mock(InputModuleHierarchy.class); - when(moduleHierarchy.root()).thenReturn(root); - when(moduleHierarchy.children(root)).thenReturn(Collections.singleton(module1)); - when(moduleHierarchy.parent(module1)).thenReturn(root); - tree.index(module1, root); + DefaultInputProject project = new DefaultInputProject(rootDef, 1); - DefaultInputDir dir = new DefaultInputDir("module1", "src", 3) - .setModuleBaseDir(moduleBaseDir); - tree.index(dir, module1); - - DefaultInputFile file = new TestInputFileBuilder("module1", "src/Foo.java", 4).setLines(2).setStatus(InputFile.Status.SAME).build(); - tree.index(file, dir); - - ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree, branchConfiguration); + InputComponentStore store = new InputComponentStore(project, branchConfiguration); + ComponentsPublisher publisher = new ComponentsPublisher(project, store); publisher.publish(writer); ScannerReportReader reader = new ScannerReportReader(outputDir); Component rootProtobuf = reader.readComponent(1); assertThat(rootProtobuf.getVersion()).isEqualTo("1.0"); - assertThat(rootProtobuf.getLinkCount()).isEqualTo(1); + assertThat(rootProtobuf.getLinkCount()).isEqualTo(2); assertThat(rootProtobuf.getLink(0).getType()).isEqualTo(ComponentLinkType.HOME); assertThat(rootProtobuf.getLink(0).getHref()).isEqualTo("http://home"); - - Component module1Protobuf = reader.readComponent(2); - assertThat(module1Protobuf.getVersion()).isEqualTo("1.0"); - assertThat(module1Protobuf.getLinkCount()).isEqualTo(1); - assertThat(module1Protobuf.getLink(0).getType()).isEqualTo(ComponentLinkType.CI); - assertThat(module1Protobuf.getLink(0).getHref()).isEqualTo("http://ci"); - } - - @Test - public void add_components_with_correct_project_relative_path() throws Exception { - Map<DefaultInputModule, DefaultInputModule> parents = new HashMap<>(); - - DefaultInputModule root = newDefaultInputModule("foo", temp.newFolder()); - - DefaultInputFile file = newDefaultInputFile(root.getBaseDir(), root, "Foo.java"); - tree.index(file, root); - - DefaultInputDir dir1 = newDefaultInputDir(root, "dir1"); - tree.index(dir1, root); - - DefaultInputFile dir1_file = newDefaultInputFile(root.getBaseDir(), root, "dir1/Foo.java"); - tree.index(dir1_file, dir1); - - DefaultInputDir dir1_dir1 = newDefaultInputDir(root, "dir1/dir1"); - tree.index(dir1_dir1, dir1); - - DefaultInputFile dir1_dir1_file = newDefaultInputFile(root.getBaseDir(), root, "dir1/dir1/Foo.java"); - tree.index(dir1_dir1_file, dir1_dir1); - - // module in root - - DefaultInputModule mod1 = newDefaultInputModule(root, "mod1"); - parents.put(mod1, root); - tree.index(mod1, root); - - DefaultInputFile mod1_file = newDefaultInputFile(root.getBaseDir(), mod1, "Foo.java"); - tree.index(mod1_file, mod1); - - DefaultInputDir mod1_dir2 = newDefaultInputDir(mod1, "dir2"); - tree.index(mod1_dir2, mod1); - - DefaultInputFile mod1_dir2_file = newDefaultInputFile(root.getBaseDir(), mod1, "dir2/Foo.java"); - tree.index(mod1_dir2_file, mod1_dir2); - - // module in module - - DefaultInputModule mod1_mod2 = newDefaultInputModule(mod1, "mod2"); - parents.put(mod1_mod2, mod1); - tree.index(mod1_mod2, mod1); - - DefaultInputFile mod1_mod2_file = newDefaultInputFile(root.getBaseDir(), mod1_mod2, "Foo.java"); - tree.index(mod1_mod2_file, mod1_mod2); - - DefaultInputDir mod1_mod2_dir = newDefaultInputDir(mod1_mod2, "dir"); - tree.index(mod1_mod2_dir, mod1_mod2); - - DefaultInputFile mod1_mod2_dir_file = newDefaultInputFile(root.getBaseDir(), mod1_mod2, "dir/Foo.java"); - tree.index(mod1_mod2_dir_file, mod1_mod2_dir); - - moduleHierarchy = new DefaultInputModuleHierarchy(root, parents); - - ComponentsPublisher publisher = new ComponentsPublisher(moduleHierarchy, tree, branchConfiguration); - publisher.publish(writer); - - ScannerReportReader reader = new ScannerReportReader(outputDir); - - // project root - assertThat(reader.readComponent(root.scannerId()).getPath()).isEmpty(); - assertThat(reader.readComponent(root.scannerId()).getProjectRelativePath()).isEmpty(); - - // file in root - assertThat(reader.readComponent(file.scannerId()).getPath()).isEqualTo("Foo.java"); - assertThat(reader.readComponent(file.scannerId()).getProjectRelativePath()).isEqualTo("Foo.java"); - - // dir in root - assertThat(reader.readComponent(dir1.scannerId()).getPath()).isEqualTo("dir1"); - assertThat(reader.readComponent(dir1.scannerId()).getProjectRelativePath()).isEqualTo("dir1"); - - // file in dir in root - assertThat(reader.readComponent(dir1_file.scannerId()).getPath()).isEqualTo("dir1/Foo.java"); - assertThat(reader.readComponent(dir1_file.scannerId()).getProjectRelativePath()).isEqualTo("dir1/Foo.java"); - - // dir in dir in root - assertThat(reader.readComponent(dir1_dir1.scannerId()).getPath()).isEqualTo("dir1/dir1"); - assertThat(reader.readComponent(dir1_dir1.scannerId()).getProjectRelativePath()).isEqualTo("dir1/dir1"); - - // module in root - assertThat(reader.readComponent(mod1.scannerId()).getPath()).isEqualTo("mod1"); - assertThat(reader.readComponent(mod1.scannerId()).getProjectRelativePath()).isEqualTo("mod1"); - - // dir in module in root - assertThat(reader.readComponent(mod1_dir2.scannerId()).getPath()).isEqualTo("dir2"); - assertThat(reader.readComponent(mod1_dir2.scannerId()).getProjectRelativePath()).isEqualTo("mod1/dir2"); - - // file in dir in module in root - assertThat(reader.readComponent(mod1_dir2_file.scannerId()).getPath()).isEqualTo("dir2/Foo.java"); - assertThat(reader.readComponent(mod1_dir2_file.scannerId()).getProjectRelativePath()).isEqualTo("mod1/dir2/Foo.java"); - - // module in module - assertThat(reader.readComponent(mod1_mod2.scannerId()).getPath()).isEqualTo("mod2"); - assertThat(reader.readComponent(mod1_mod2.scannerId()).getProjectRelativePath()).isEqualTo("mod1/mod2"); - - // file in module in module - assertThat(reader.readComponent(mod1_mod2_file.scannerId()).getPath()).isEqualTo("Foo.java"); - assertThat(reader.readComponent(mod1_mod2_file.scannerId()).getProjectRelativePath()).isEqualTo("mod1/mod2/Foo.java"); - - // dir in module in module - assertThat(reader.readComponent(mod1_mod2_dir.scannerId()).getPath()).isEqualTo("dir"); - assertThat(reader.readComponent(mod1_mod2_dir.scannerId()).getProjectRelativePath()).isEqualTo("mod1/mod2/dir"); - - // file in dir in module in module - assertThat(reader.readComponent(mod1_mod2_dir_file.scannerId()).getPath()).isEqualTo("dir/Foo.java"); - assertThat(reader.readComponent(mod1_mod2_dir_file.scannerId()).getProjectRelativePath()).isEqualTo("mod1/mod2/dir/Foo.java"); + assertThat(rootProtobuf.getLink(1).getType()).isEqualTo(ComponentLinkType.CI); + assertThat(rootProtobuf.getLink(1).getHref()).isEqualTo("http://ci"); } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/CoveragePublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/CoveragePublisherTest.java index d2cc7ac178d..93663f7f57b 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/CoveragePublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/CoveragePublisherTest.java @@ -59,7 +59,7 @@ public class CoveragePublisherTest { inputFile = new TestInputFileBuilder(moduleKey, "src/Foo.php").setLines(5).build(); DefaultInputProject rootModule = TestInputFileBuilder.newDefaultInputProject(moduleKey, temp.newFolder()); InputComponentStore componentCache = new InputComponentStore(rootModule, mock(BranchConfiguration.class)); - componentCache.put(inputFile); + componentCache.put(moduleKey, inputFile); measureCache = mock(MeasureCache.class); when(measureCache.byMetric(anyString(), anyString())).thenReturn(null); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MeasuresPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MeasuresPublisherTest.java index 08cb3b61e3c..7583eac0971 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MeasuresPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/MeasuresPublisherTest.java @@ -70,7 +70,7 @@ public class MeasuresPublisherTest { project = TestInputFileBuilder.newDefaultInputProject(projectKey, temp.newFolder()); inputFile = new TestInputFileBuilder(projectKey, "src/Foo.php").setPublish(true).build(); InputComponentStore componentCache = new InputComponentStore(project, mock(BranchConfiguration.class)); - componentCache.put(inputFile); + componentCache.put(projectKey, inputFile); measureCache = mock(MeasureCache.class); when(measureCache.byComponentKey(anyString())).thenReturn(Collections.<DefaultMeasure<?>>emptyList()); publisher = new MeasuresPublisher(componentCache, measureCache, mock(TestPlanBuilder.class)); 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 f6840f3b175..da37398e387 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 @@ -59,7 +59,7 @@ public class SourcePublisherTest { DefaultInputProject rootProject = TestInputFileBuilder.newDefaultInputProject(moduleKey, baseDir); InputComponentStore componentStore = new InputComponentStore(rootProject, mock(BranchConfiguration.class)); - componentStore.put(inputFile); + componentStore.put(moduleKey, inputFile); publisher = new SourcePublisher(componentStore); File outputDir = temp.newFolder(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DefaultComponentTreeTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DefaultComponentTreeTest.java deleted file mode 100644 index 930f5ee8086..00000000000 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/DefaultComponentTreeTest.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.scan; - -import java.io.IOException; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.batch.fs.internal.DefaultInputModule; -import org.sonar.api.batch.fs.internal.DefaultInputProject; - -import static org.assertj.core.api.Assertions.assertThat; - -public class DefaultComponentTreeTest { - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - private DefaultComponentTree tree; - - @Before - public void setUp() { - tree = new DefaultComponentTree(); - } - - @Test - public void test() throws IOException { - DefaultInputProject root = new DefaultInputProject(ProjectDefinition.create().setKey("root").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); - DefaultInputModule mod1 = new DefaultInputModule(ProjectDefinition.create().setKey("mod1").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); - DefaultInputModule mod2 = new DefaultInputModule(ProjectDefinition.create().setKey("mod2").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); - DefaultInputModule mod3 = new DefaultInputModule(ProjectDefinition.create().setKey("mod3").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); - DefaultInputModule mod4 = new DefaultInputModule(ProjectDefinition.create().setKey("mod4").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); - - tree.index(mod1, root); - tree.index(mod2, mod1); - tree.index(mod3, root); - tree.index(mod4, root); - - assertThat(tree.getChildren(root)).containsOnly(mod1, mod3, mod4); - assertThat(tree.getChildren(mod4)).isEmpty(); - assertThat(tree.getChildren(mod1)).containsOnly(mod2); - - assertThat(tree.getParent(mod4)).isEqualTo(root); - assertThat(tree.getParent(mod2)).isEqualTo(mod1); - assertThat(tree.getParent(mod1)).isEqualTo(root); - assertThat(tree.getParent(root)).isNull(); - } -} 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 2f54bfea404..2c2b51fd0ca 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 @@ -33,15 +33,13 @@ import static org.mockito.Mockito.when; public class ModuleIndexerTest { private ModuleIndexer indexer; - private DefaultComponentTree tree; private DefaultInputModuleHierarchy moduleHierarchy; private InputComponentStore componentStore; public void createIndexer(DefaultInputProject rootProject) { componentStore = new InputComponentStore(rootProject, mock(BranchConfiguration.class)); - tree = new DefaultComponentTree(); moduleHierarchy = mock(DefaultInputModuleHierarchy.class); - indexer = new ModuleIndexer(tree, componentStore, moduleHierarchy); + indexer = new ModuleIndexer(componentStore, moduleHierarchy); } @Test @@ -80,6 +78,5 @@ public class ModuleIndexerTest { DefaultInputModule rootModule = moduleHierarchy.root(); assertThat(rootModule).isNotNull(); assertThat(moduleHierarchy.children(rootModule)).hasSize(3); - assertThat(tree.getChildren(rootModule)).hasSize(3); } } 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 5170bf0745e..a9d30ff3bed 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 @@ -60,15 +60,15 @@ public class InputComponentStoreTest { DefaultInputProject rootProject = TestInputFileBuilder.newDefaultInputProject(rootDef); DefaultInputModule subModule = TestInputFileBuilder.newDefaultInputModule(moduleDef); - InputComponentStore cache = new InputComponentStore(rootProject, mock(BranchConfiguration.class)); - cache.put(subModule); + InputComponentStore store = new InputComponentStore(rootProject, mock(BranchConfiguration.class)); + store.put(subModule); DefaultInputFile fooFile = new TestInputFileBuilder(rootModuleKey, "src/main/java/Foo.java") .setModuleBaseDir(rootBaseDir.toPath()) .setPublish(true) .build(); - cache.put(fooFile); - cache.put(new TestInputFileBuilder(subModuleKey, "src/main/java/Bar.java") + store.put(rootProject.key(), fooFile); + store.put(subModuleKey, new TestInputFileBuilder(rootModuleKey, "src/main/java/Bar.java") .setLanguage("bla") .setPublish(false) .setType(Type.MAIN) @@ -78,28 +78,20 @@ public class InputComponentStoreTest { .setModuleBaseDir(temp.newFolder().toPath()) .build()); - DefaultInputFile loadedFile = (DefaultInputFile) cache.getFile(subModuleKey, "src/main/java/Bar.java"); + DefaultInputFile loadedFile = (DefaultInputFile) store.getFile(subModuleKey, "src/main/java/Bar.java"); assertThat(loadedFile.relativePath()).isEqualTo("src/main/java/Bar.java"); assertThat(loadedFile.charset()).isEqualTo(StandardCharsets.UTF_8); - assertThat(cache.filesByModule(rootModuleKey)).hasSize(1); - assertThat(cache.filesByModule(subModuleKey)).hasSize(1); - assertThat(cache.allFiles()).hasSize(2); - for (InputPath inputPath : cache.allFiles()) { + assertThat(store.filesByModule(rootModuleKey)).hasSize(1); + assertThat(store.filesByModule(subModuleKey)).hasSize(1); + assertThat(store.allFiles()).hasSize(2); + for (InputPath inputPath : store.allFiles()) { assertThat(inputPath.relativePath()).startsWith("src/main/java/"); } List<InputFile> toPublish = new LinkedList<>(); - cache.allFilesToPublish().forEach(toPublish::add); + store.allFilesToPublish().forEach(toPublish::add); assertThat(toPublish).containsExactly(fooFile); - - cache.remove(fooFile); - assertThat(cache.allFiles()).hasSize(1); - - cache.removeModule(rootModuleKey); - assertThat(cache.filesByModule(rootModuleKey)).hasSize(0); - assertThat(cache.filesByModule(subModuleKey)).hasSize(1); - assertThat(cache.allFiles()).hasSize(1); } static class InputComponentStoreTester extends InputComponentStore { @@ -111,7 +103,7 @@ public class InputComponentStoreTest { DefaultInputFile file = new TestInputFileBuilder(moduleKey, relpath) .setLanguage(language) .build(); - put(file); + put(moduleKey, file); return file; } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java index 32ba8fbc390..840cd7d9257 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/filesystem/InputFileBuilderTest.java @@ -53,21 +53,17 @@ public class InputFileBuilderTest { public void setUp() throws IOException { baseDir = temp.newFolder().toPath(); workDir = temp.newFolder().toPath(); - DefaultInputModule root = new DefaultInputModule(ProjectDefinition.create() + DefaultInputProject project = new DefaultInputProject(ProjectDefinition.create() .setBaseDir(baseDir.toFile()) .setWorkDir(workDir.toFile()) + .setProperty(CoreProperties.ENCODING_PROPERTY, StandardCharsets.UTF_8.name()) .setKey("root"), 0); Path moduleBaseDir = baseDir.resolve("module1"); Files.createDirectories(moduleBaseDir); - DefaultInputProject project = new DefaultInputProject(ProjectDefinition.create() - .setBaseDir(baseDir.toFile()) - .setWorkDir(workDir.toFile()) - .setProperty(CoreProperties.ENCODING_PROPERTY, StandardCharsets.UTF_8.name()) - .setKey("module1"), 0); DefaultInputModule module = new DefaultInputModule(ProjectDefinition.create() .setBaseDir(moduleBaseDir.toFile()) .setWorkDir(workDir.toFile()) - .setKey("module1"), 0); + .setKey("module1"), 1); MetadataGenerator metadataGenerator = mock(MetadataGenerator.class); ScannerComponentIdGenerator idGenerator = new ScannerComponentIdGenerator(); @@ -82,11 +78,10 @@ public class InputFileBuilderTest { Path filePath = baseDir.resolve("module1/src/File1.xoo"); DefaultInputFile inputFile = builder.create(Type.MAIN, filePath, null); - assertThat(inputFile.moduleKey()).isEqualTo("module1"); assertThat(inputFile.absolutePath()).isEqualTo(filePath.toString().replaceAll("\\\\", "/")); assertThat(inputFile.relativePath()).isEqualTo("src/File1.xoo"); assertThat(inputFile.path()).isEqualTo(filePath); - assertThat(inputFile.key()).isEqualTo("module1:src/File1.xoo"); + assertThat(inputFile.key()).isEqualTo("root:module1/src/File1.xoo"); assertThat(inputFile.isPublished()).isFalse(); sensorStrategy.setGlobal(true); 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 dcd265010d1..523d875470d 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 @@ -114,7 +114,9 @@ public class ModuleInputComponentStoreTest { } private ModuleInputComponentStore newModuleInputComponentStore() { - return new ModuleInputComponentStore(mock(InputModule.class), componentStore, mock(SensorStrategy.class)); + InputModule module = mock(InputModule.class); + when(module.key()).thenReturn("moduleKey"); + return new ModuleInputComponentStore(module, componentStore, mock(SensorStrategy.class)); } @Test @@ -132,9 +134,6 @@ public class ModuleInputComponentStoreTest { store.inputFile(relativePath); verify(inputComponentStore).getFile(any(String.class), eq(relativePath)); - store.inputDir(relativePath); - verify(inputComponentStore).getDir(any(String.class), eq(relativePath)); - store.languages(); verify(inputComponentStore).getLanguages(any(String.class)); } @@ -154,9 +153,6 @@ public class ModuleInputComponentStoreTest { store.inputFile(relativePath); verify(inputComponentStore).getFile(relativePath); - store.inputDir(relativePath); - verify(inputComponentStore).getDir(relativePath); - store.languages(); verify(inputComponentStore).getLanguages(); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java index d813051b339..36f422d95bf 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/report/JSONReportTest.java @@ -23,7 +23,6 @@ import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.text.SimpleDateFormat; -import java.util.Arrays; import java.util.Collections; import java.util.TimeZone; import org.apache.commons.io.IOUtils; @@ -34,9 +33,7 @@ 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.DefaultFileSystem; -import org.sonar.api.batch.fs.internal.DefaultInputDir; import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.batch.fs.internal.InputModuleHierarchy; import org.sonar.api.batch.fs.internal.TestInputFileBuilder; @@ -48,7 +45,6 @@ import org.sonar.api.platform.Server; import org.sonar.api.rule.RuleKey; import org.sonar.scanner.issue.IssueCache; import org.sonar.scanner.issue.tracking.TrackedIssue; -import org.sonar.scanner.scan.DefaultComponentTree; import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.filesystem.InputComponentStore; @@ -81,39 +77,20 @@ public class JSONReportTest { SIMPLE_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT+02:00")); when(server.getVersion()).thenReturn("3.6"); - DefaultComponentTree inputComponentTree = new DefaultComponentTree(); ProjectDefinition def = ProjectDefinition.create().setBaseDir(projectBaseDir).setWorkDir(temp.newFolder()).setKey("struts"); DefaultInputProject project = new DefaultInputProject(def, 1); - DefaultInputModule rootModule = new DefaultInputModule(def, 1); InputComponentStore inputComponentStore = new InputComponentStore(project, mock(BranchConfiguration.class)); - DefaultInputModule moduleA = new DefaultInputModule(ProjectDefinition.create().setKey("struts-core").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); - inputComponentTree.index(moduleA, rootModule); - DefaultInputModule moduleB = new DefaultInputModule(ProjectDefinition.create().setKey("struts-ui").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); - inputComponentTree.index(moduleB, rootModule); - - DefaultInputDir inputDir = new DefaultInputDir("struts", "src/main/java/org/apache/struts", TestInputFileBuilder.nextBatchId()) - .setModuleBaseDir(projectBaseDir.toPath()); DefaultInputFile inputFile = new TestInputFileBuilder("struts", "src/main/java/org/apache/struts/Action.java") .setModuleBaseDir(projectBaseDir.toPath()).build(); inputFile.setStatus(InputFile.Status.CHANGED); inputFile.setPublished(true); - inputComponentStore.put(inputFile); - inputComponentStore.put(inputDir); - - inputComponentTree.index(inputDir, rootModule); - inputComponentTree.index(inputFile, inputDir); - - when(moduleHierarchy.children(rootModule)).thenReturn(Arrays.asList(moduleA, moduleB)); - when(moduleHierarchy.parent(moduleA)).thenReturn(rootModule); - when(moduleHierarchy.parent(moduleB)).thenReturn(rootModule); - when(moduleHierarchy.relativePath(moduleA)).thenReturn("core"); - when(moduleHierarchy.relativePath(moduleB)).thenReturn("ui"); + inputComponentStore.put("struts", inputFile); RulesBuilder builder = new RulesBuilder(); builder.add(RuleKey.of("squid", "AvoidCycles")).setName("Avoid Cycles"); rules = builder.build(); - jsonReport = new JSONReport(moduleHierarchy, settings.asConfig(), fs, server, rules, issueCache, rootModule, inputComponentStore, inputComponentTree); + jsonReport = new JSONReport(settings.asConfig(), fs, server, rules, issueCache, project, inputComponentStore); } @Test diff --git a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/scan/report/JSONReportTest/report-without-resolved-issues.json b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/scan/report/JSONReportTest/report-without-resolved-issues.json index b5af45efe6c..8238d2eae85 100644 --- a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/scan/report/JSONReportTest/report-without-resolved-issues.json +++ b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/scan/report/JSONReportTest/report-without-resolved-issues.json @@ -4,23 +4,9 @@ "components": [ {"key": "struts"}, { - "key": "struts-core", - "path": "core" - }, - { - "key": "struts-ui", - "path": "ui" - }, - { "key": "struts:src/main/java/org/apache/struts/Action.java", "path": "src/main/java/org/apache/struts/Action.java", - "moduleKey": "struts", "status": "CHANGED" - }, - { - "key": "struts:src/main/java/org/apache/struts", - "path": "src/main/java/org/apache/struts", - "moduleKey": "struts" } ], "rules": [], diff --git a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/scan/report/JSONReportTest/report.json b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/scan/report/JSONReportTest/report.json index a78fbca71e5..f80eadc890c 100644 --- a/sonar-scanner-engine/src/test/resources/org/sonar/scanner/scan/report/JSONReportTest/report.json +++ b/sonar-scanner-engine/src/test/resources/org/sonar/scanner/scan/report/JSONReportTest/report.json @@ -24,23 +24,9 @@ "key": "struts" }, { - "key": "struts-core", - "path": "core" - }, - { - "key": "struts-ui", - "path": "ui" - }, - { "key": "struts:src/main/java/org/apache/struts/Action.java", "path": "src/main/java/org/apache/struts/Action.java", - "moduleKey": "struts", "status": "CHANGED" - }, - { - "key": "struts:src/main/java/org/apache/struts", - "path": "src/main/java/org/apache/struts", - "moduleKey": "struts" } ], "rules": [ |