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; }
@Override public CloseableIterator<ScannerReport.LineCoverage> readComponentCoverage(int fileRef) { ensureInitialized(); return delegate.readComponentCoverage(fileRef); }
@Override public CloseableIterator<ScannerReport.Duplication> readComponentDuplications(int componentRef) { ensureInitialized(); return delegate.readComponentDuplications(componentRef); }
private void loadReport(File file) { reader = new ScannerReportReader(file); metadata = reader.readMetadata(); updateTitle(); loadComponents(); updateActiveRules(); updateAdHocRules(); updateQualityProfiles(); updatePlugins(); }
private void updateCpdTextBlocks(Component component) { cpdTextBlocksEditor.setText(""); if (reader.hasCoverage(component.getRef())) { try (CloseableIterator<ScannerReport.CpdTextBlock> it = reader.readCpdTextBlocks(component.getRef())) { while (it.hasNext()) { ScannerReport.CpdTextBlock textBlock = it.next(); cpdTextBlocksEditor.getDocument().insertString(cpdTextBlocksEditor.getDocument().getEndPosition().getOffset(), textBlock + "\n", null); } } catch (Exception e) { throw new IllegalStateException("Can't read CPD text blocks for " + getNodeName(component), e); } } }
@Test public void publish_project_without_version_and_name() throws IOException { ProjectAnalysisInfo projectAnalysisInfo = mock(ProjectAnalysisInfo.class); when(projectAnalysisInfo.analysisDate()).thenReturn(DateUtils.parseDate("2012-12-12")); ProjectDefinition rootDef = ProjectDefinition.create() .setKey("foo") .setDescription("Root description") .setBaseDir(temp.newFolder()) .setWorkDir(temp.newFolder()); DefaultInputProject project = new DefaultInputProject(rootDef, 1); InputComponentStore store = new InputComponentStore(branchConfiguration); ComponentsPublisher publisher = new ComponentsPublisher(project, store); publisher.publish(writer); assertThat(writer.hasComponentData(FileStructure.Domain.COMPONENT, 1)).isTrue(); ScannerReportReader reader = new ScannerReportReader(outputDir); Component rootProtobuf = reader.readComponent(1); assertThat(rootProtobuf.getKey()).isEqualTo("foo"); assertThat(rootProtobuf.getName()).isEqualTo(""); assertThat(rootProtobuf.getDescription()).isEqualTo("Root description"); assertThat(rootProtobuf.getVersion()).isEqualTo(""); assertThat(rootProtobuf.getLinkCount()).isEqualTo(0); }
@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(); }
@Override public ScannerReport.Component readComponent(int componentRef) { ensureInitialized(); return delegate.readComponent(componentRef); }
@Override public CloseableIterator<ScannerReport.Measure> readComponentMeasures(int componentRef) { ensureInitialized(); return delegate.readComponentMeasures(componentRef); }
@Test public void read_coverage() { ScannerReportWriter writer = new ScannerReportWriter(dir); writer.writeMetadata(ScannerReport.Metadata.newBuilder() .setRootComponentRef(1) .build()); writer.writeComponent(ScannerReport.Component.newBuilder() .setRef(1).build()); writer.writeComponentCoverage(1, asList( ScannerReport.LineCoverage.newBuilder() .setLine(1) .setConditions(1) .setHits(true) .setCoveredConditions(1) .build(), ScannerReport.LineCoverage.newBuilder() .setLine(2) .setConditions(5) .setHits(false) .setCoveredConditions(4) .build())); underTest = new ScannerReportReader(dir); try (CloseableIterator<ScannerReport.LineCoverage> it = new ScannerReportReader(dir).readComponentCoverage(1)) { ScannerReport.LineCoverage coverage = it.next(); assertThat(coverage.getLine()).isEqualTo(1); assertThat(coverage.getConditions()).isEqualTo(1); assertThat(coverage.getHits()).isTrue(); assertThat(coverage.getCoveredConditions()).isEqualTo(1); } }
@Override public CloseableIterator<ScannerReport.Issue> readComponentIssues(int componentRef) { ensureInitialized(); return delegate.readComponentIssues(componentRef); }
@Test public void publishMeasures() throws Exception { DefaultMeasure<Integer> measure = new DefaultMeasure<Integer>().forMetric(CoreMetrics.LINES_TO_COVER) .withValue(2); // String value DefaultMeasure<String> stringMeasure = new DefaultMeasure<String>().forMetric(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION) .withValue("foo bar"); when(measureCache.byComponentKey(inputFile.key())).thenReturn(asList(measure, stringMeasure)); publisher.publish(writer); ScannerReportReader reader = new ScannerReportReader(outputDir); assertThat(reader.readComponentMeasures(project.scannerId())).hasSize(0); try (CloseableIterator<ScannerReport.Measure> componentMeasures = reader.readComponentMeasures(inputFile.scannerId())) { assertThat(componentMeasures).hasSize(2); } }
@Override public CloseableIterator<ScannerReport.Symbol> readComponentSymbols(int componentRef) { ensureInitialized(); return delegate.readComponentSymbols(componentRef); }
@Override public CloseableIterator<ScannerReport.ExternalIssue> readComponentExternalIssues(int componentRef) { ensureInitialized(); return delegate.readComponentExternalIssues(componentRef); }
private void ensureInitialized() { if (this.delegate == null) { this.delegate = new org.sonar.scanner.protocol.output.ScannerReportReader(batchReportDirectoryHolder.getDirectory()); } }
@Override public CloseableIterator<ScannerReport.SyntaxHighlightingRule> readComponentSyntaxHighlighting(int fileRef) { ensureInitialized(); return delegate.readComponentSyntaxHighlighting(fileRef); }
@Test public void read_duplications() { ScannerReportWriter writer = new ScannerReportWriter(dir); writer.writeMetadata(ScannerReport.Metadata.newBuilder() .setRootComponentRef(1).build()); writer.writeComponent(ScannerReport.Component.newBuilder() .setRef(1).build()); ScannerReport.Duplication duplication = ScannerReport.Duplication.newBuilder() .setOriginPosition(ScannerReport.TextRange.newBuilder() .setStartLine(1) .setEndLine(5) .build()) .addDuplicate(ScannerReport.Duplicate.newBuilder() .setOtherFileRef(2) .setRange(ScannerReport.TextRange.newBuilder() .setStartLine(6) .setEndLine(10) .build()) .build()) .build(); writer.writeComponentDuplications(1, asList(duplication)); ScannerReportReader sut = new ScannerReportReader(dir); assertThat(sut.readComponentDuplications(1)).hasSize(1); }
@Test public void read_duplication_blocks() { ScannerReportWriter writer = new ScannerReportWriter(dir); writer.writeMetadata(ScannerReport.Metadata.newBuilder() .setRootComponentRef(1).build()); writer.writeComponent(ScannerReport.Component.newBuilder() .setRef(1).build()); ScannerReport.CpdTextBlock duplicationBlock = ScannerReport.CpdTextBlock.newBuilder() .setHash("abcdefghijklmnop") .setStartLine(1) .setEndLine(2) .setStartTokenIndex(10) .setEndTokenIndex(15) .build(); writer.writeCpdTextBlocks(1, singletonList(duplicationBlock)); ScannerReportReader sut = new ScannerReportReader(dir); assertThat(sut.readCpdTextBlocks(1)).hasSize(1); }
@Test public void read_symbols() { ScannerReportWriter writer = new ScannerReportWriter(dir); writer.writeMetadata(ScannerReport.Metadata.newBuilder() .setRootComponentRef(1) .build()); writer.writeComponent(ScannerReport.Component.newBuilder() .setRef(1).build()); writer.writeComponentSymbols(1, asList(ScannerReport.Symbol.newBuilder() .setDeclaration(ScannerReport.TextRange.newBuilder() .setStartLine(1) .setStartOffset(3) .setEndLine(1) .setEndOffset(5) .build()) .addReference(ScannerReport.TextRange.newBuilder() .setStartLine(10) .setStartOffset(15) .setEndLine(11) .setEndOffset(2) .build()) .build())); underTest = new ScannerReportReader(dir); assertThat(underTest.readComponentSymbols(1)).hasSize(1); }
private void updateDuplications(Component component) { duplicationEditor.setText(""); if (reader.hasCoverage(component.getRef())) { try (CloseableIterator<ScannerReport.Duplication> it = reader.readComponentDuplications(component.getRef())) { while (it.hasNext()) { ScannerReport.Duplication dup = it.next(); duplicationEditor.getDocument().insertString(duplicationEditor.getDocument().getEndPosition().getOffset(), dup + "\n", null); } } catch (Exception e) { throw new IllegalStateException("Can't read duplications for " + getNodeName(component), e); } } }