@Override public ScannerReport.Metadata readMetadata() { ensureInitialized(); if (this.metadata == null) { this.metadata = delegate.readMetadata(); } return this.metadata; }
@Test(expected = IllegalStateException.class) public void fail_if_missing_metadata_file() { underTest.readMetadata(); }
@Test public void write_project_basedir() throws Exception { String path = "some/dir"; Path relativePathFromScmRoot = Paths.get(path); when(scmProvider.relativePathFromScmRoot(any(Path.class))).thenReturn(relativePathFromScmRoot); File outputDir = temp.newFolder(); underTest.publish(new ScannerReportWriter(outputDir)); ScannerReportReader reader = new ScannerReportReader(outputDir); ScannerReport.Metadata metadata = reader.readMetadata(); assertThat(metadata.getRelativePathFromScmRoot()).isEqualTo(path); }
@Test public void write_long_lived_branch_info() throws Exception { String branchName = "long-lived"; when(branches.branchName()).thenReturn(branchName); when(branches.branchType()).thenReturn(BranchType.LONG); File outputDir = temp.newFolder(); underTest.publish(new ScannerReportWriter(outputDir)); ScannerReportReader reader = new ScannerReportReader(outputDir); ScannerReport.Metadata metadata = reader.readMetadata(); assertThat(metadata.getBranchName()).isEqualTo(branchName); assertThat(metadata.getBranchType()).isEqualTo(ScannerReport.Metadata.BranchType.LONG); }
@Test public void write_project_organization() throws Exception { when(properties.organizationKey()).thenReturn(Optional.of("SonarSource")); File outputDir = temp.newFolder(); ScannerReportWriter writer = new ScannerReportWriter(outputDir); underTest.publish(writer); ScannerReportReader reader = new ScannerReportReader(outputDir); ScannerReport.Metadata metadata = reader.readMetadata(); assertThat(metadata.getOrganizationKey()).isEqualTo("SonarSource"); }
@Test public void write_revision_id() throws Exception { String revisionId = "some-sha1"; when(scmProvider.revisionId(any(Path.class))).thenReturn(revisionId); File outputDir = temp.newFolder(); underTest.publish(new ScannerReportWriter(outputDir)); ScannerReportReader reader = new ScannerReportReader(outputDir); ScannerReport.Metadata metadata = reader.readMetadata(); assertThat(metadata.getScmRevisionId()).isEqualTo(revisionId); }
@Test public void write_short_lived_branch_info() throws Exception { String branchName = "feature"; String branchTarget = "short-lived"; when(branches.branchName()).thenReturn(branchName); when(branches.longLivingSonarReferenceBranch()).thenReturn(branchTarget); File outputDir = temp.newFolder(); underTest.publish(new ScannerReportWriter(outputDir)); ScannerReportReader reader = new ScannerReportReader(outputDir); ScannerReport.Metadata metadata = reader.readMetadata(); assertThat(metadata.getBranchName()).isEqualTo(branchName); assertThat(metadata.getBranchType()).isEqualTo(ScannerReport.Metadata.BranchType.SHORT); assertThat(metadata.getMergeBranchName()).isEqualTo(branchTarget); }
private void loadReport(File file) { reader = new ScannerReportReader(file); metadata = reader.readMetadata(); updateTitle(); loadComponents(); updateActiveRules(); updateAdHocRules(); updateQualityProfiles(); updatePlugins(); }
@Test public void verify_metadata() { String branchName = "feature"; String branchTarget = "branch-1.x"; AnalysisResult result = getResult(tester .setBranchName(branchName) .setBranchTarget(branchTarget) .setLongLivingSonarReferenceBranch(branchTarget) .setBranchType(BranchType.SHORT)); ScannerReport.Metadata metadata = result.getReportReader().readMetadata(); assertThat(metadata.getBranchName()).isEqualTo(branchName); assertThat(metadata.getBranchType()).isEqualTo(ScannerReport.Metadata.BranchType.SHORT); assertThat(metadata.getMergeBranchName()).isEqualTo(branchTarget); }
@Test public void write_project_branch() throws Exception { when(cpdSettings.isCrossProjectDuplicationEnabled()).thenReturn(false); ProjectDefinition projectDef = ProjectDefinition.create() .setKey("foo") .setProperty(CoreProperties.PROJECT_BRANCH_PROPERTY, "myBranch"); createPublisher(projectDef); File outputDir = temp.newFolder(); ScannerReportWriter writer = new ScannerReportWriter(outputDir); underTest.publish(writer); ScannerReportReader reader = new ScannerReportReader(outputDir); ScannerReport.Metadata metadata = reader.readMetadata(); assertThat(metadata.getAnalysisDate()).isEqualTo(1234567L); assertThat(metadata.getProjectKey()).isEqualTo("root"); assertThat(metadata.getDeprecatedBranch()).isEqualTo("myBranch"); assertThat(metadata.getCrossProjectDuplicationActivated()).isFalse(); }
@Test public void read_metadata() { ScannerReportWriter writer = new ScannerReportWriter(dir); ScannerReport.Metadata.Builder metadata = ScannerReport.Metadata.newBuilder() .setAnalysisDate(15000000L) .setProjectKey("PROJECT_A") .setRootComponentRef(1) .setCrossProjectDuplicationActivated(true); writer.writeMetadata(metadata.build()); ScannerReport.Metadata readMetadata = underTest.readMetadata(); assertThat(readMetadata.getAnalysisDate()).isEqualTo(15000000L); assertThat(readMetadata.getProjectKey()).isEqualTo("PROJECT_A"); assertThat(readMetadata.getRootComponentRef()).isEqualTo(1); assertThat(readMetadata.getCrossProjectDuplicationActivated()).isTrue(); }
@Test public void scanProjectWithoutProjectName() throws IOException { File srcDir = new File(baseDir, "src"); srcDir.mkdir(); File xooFile = new File(srcDir, "sample.xoo"); FileUtils.write(xooFile, "Sample xoo\ncontent", StandardCharsets.UTF_8); AnalysisResult result = tester.newAnalysis() .properties(builder .put("sonar.sources", "src") .build()) .execute(); int ref = result.getReportReader().readMetadata().getRootComponentRef(); assertThat(result.getReportReader().readComponent(ref).getName()).isEmpty(); assertThat(result.inputFiles()).hasSize(1); DefaultInputFile file = (DefaultInputFile) result.inputFile("src/sample.xoo"); assertThat(file.type()).isEqualTo(InputFile.Type.MAIN); assertThat(file.relativePath()).isEqualTo("src/sample.xoo"); assertThat(file.language()).isEqualTo("xoo"); // file was published, since language matched xoo assertThat(file.isPublished()).isTrue(); assertThat(result.getReportComponent(file.scannerId())).isNotNull(); }
@Test public void scanMultiModuleWithBranch() throws IOException { Path srcDir = baseDir.toPath().resolve("moduleA").resolve("src"); Files.createDirectories(srcDir); File xooFile = new File(srcDir.toFile(), "sample.xoo"); FileUtils.write(xooFile, "Sample xoo\ncontent"); AnalysisResult result = tester.newAnalysis() .properties(ImmutableMap.<String, String>builder() .putAll(commonProps) .put("sonar.branch", "branch") .put("sonar.modules", "moduleA") .build()) .execute(); assertThat(result.inputFiles()).hasSize(1); 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()).getProjectRelativePath()).isEqualTo("moduleA/src/sample.xoo"); assertThat(result.getReportReader().readMetadata().getDeprecatedBranch()).isEqualTo("branch"); result = tester.newAnalysis() .properties(ImmutableMap.<String, String>builder() .putAll(commonProps) .put("sonar.branch", "") .put("sonar.modules", "moduleA") .build()) .execute(); assertThat(result.inputFiles()).hasSize(1); assertThat(result.inputFile("moduleA/src/sample.xoo").key()).isEqualTo("com.foo.project:moduleA/src/sample.xoo"); }
@Test public void should_not_crash_when_scm_provider_does_not_support_revisionId() throws IOException { String relativePathFromScmRoot = "some/path"; ScmProvider fakeScmProvider = new ScmProvider() { @Override public String key() { return "foo"; } @Override public Path relativePathFromScmRoot(Path path) { return Paths.get(relativePathFromScmRoot); } }; when(scmConfiguration.provider()).thenReturn(fakeScmProvider); File outputDir = temp.newFolder(); underTest.publish(new ScannerReportWriter(outputDir)); ScannerReportReader reader = new ScannerReportReader(outputDir); ScannerReport.Metadata metadata = reader.readMetadata(); assertThat(metadata.getRelativePathFromScmRoot()).isEqualTo(relativePathFromScmRoot); } }
@Test public void should_not_crash_when_scm_provider_does_not_support_relativePathFromScmRoot() throws IOException { String revisionId = "some-sha1"; ScmProvider fakeScmProvider = new ScmProvider() { @Override public String key() { return "foo"; } @Override public String revisionId(Path path) { return revisionId; } }; when(scmConfiguration.provider()).thenReturn(fakeScmProvider); File outputDir = temp.newFolder(); underTest.publish(new ScannerReportWriter(outputDir)); ScannerReportReader reader = new ScannerReportReader(outputDir); ScannerReport.Metadata metadata = reader.readMetadata(); assertThat(metadata.getScmRevisionId()).isEqualTo(revisionId); }
@Test public void scanProjectWithBranch() throws IOException { File srcDir = new File(baseDir, "src"); srcDir.mkdir(); File xooFile = new File(srcDir, "sample.xoo"); FileUtils.write(xooFile, "Sample xoo\ncontent"); AnalysisResult result = tester.newAnalysis() .properties(ImmutableMap.<String, String>builder() .putAll(commonProps) .put("sonar.branch", "branch") .build()) .execute(); assertThat(result.inputFiles()).hasSize(1); 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()).getProjectRelativePath()).isEqualTo("src/sample.xoo"); assertThat(result.getReportReader().readMetadata().getDeprecatedBranch()).isEqualTo("branch"); result = tester.newAnalysis() .properties(ImmutableMap.<String, String>builder() .putAll(commonProps) .put("sonar.branch", "") .build()) .execute(); assertThat(result.inputFiles()).hasSize(1); assertThat(result.inputFile("src/sample.xoo").key()).isEqualTo("com.foo.project:src/sample.xoo"); }
private ScannerReport.Changesets getChangesets(File baseDir, String path) { File reportDir = new File(baseDir, ".sonar/scanner-report"); ScannerReportReader reader = new ScannerReportReader(reportDir); Component project = reader.readComponent(reader.readMetadata().getRootComponentRef()); for (Integer fileRef : project.getChildRefList()) { Component file = reader.readComponent(fileRef); if (file.getProjectRelativePath().equals(path)) { return reader.readChangesets(file.getRef()); } } return null; }
@Test public void write_metadata() throws Exception { Date date = new Date(); when(qProfiles.findAll()).thenReturn(asList(new QProfile("q1", "Q1", "java", date))); when(pluginRepository.getPluginsByKey()).thenReturn(ImmutableMap.of( "java", new ScannerPlugin("java", 12345L, null), "php", new ScannerPlugin("php", 45678L, null))); File outputDir = temp.newFolder(); ScannerReportWriter writer = new ScannerReportWriter(outputDir); underTest.publish(writer); ScannerReportReader reader = new ScannerReportReader(outputDir); ScannerReport.Metadata metadata = reader.readMetadata(); assertThat(metadata.getAnalysisDate()).isEqualTo(1234567L); assertThat(metadata.getProjectKey()).isEqualTo("root"); assertThat(metadata.getModulesProjectRelativePathByKeyMap()).containsOnly(entry("module", "modulePath"), entry("root", "")); assertThat(metadata.getQprofilesPerLanguageMap()).containsOnly(entry("java", org.sonar.scanner.protocol.output.ScannerReport.Metadata.QProfile.newBuilder() .setKey("q1") .setName("Q1") .setLanguage("java") .setRulesUpdatedAt(date.getTime()) .build())); assertThat(metadata.getPluginsByKey()).containsOnly(entry("java", org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin.newBuilder() .setKey("java") .setUpdatedAt(12345) .build()), entry("php", org.sonar.scanner.protocol.output.ScannerReport.Metadata.Plugin.newBuilder() .setKey("php") .setUpdatedAt(45678) .build())); }
@Override public ScannerReport.Metadata readMetadata() { ensureInitialized(); if (this.metadata == null) { this.metadata = delegate.readMetadata(); } return this.metadata; }
private void loadReport(File file) { reader = new ScannerReportReader(file); metadata = reader.readMetadata(); updateTitle(); loadComponents(); updateActiveRules(); updateAdHocRules(); updateQualityProfiles(); updatePlugins(); }