Search in sources :

Example 1 with DefaultSignificantCode

use of org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode in project sonarqube by SonarSource.

the class DefaultSensorStorageTest method duplicateSignificantCode.

@Test(expected = UnsupportedOperationException.class)
public void duplicateSignificantCode() throws Exception {
    InputFile inputFile = new TestInputFileBuilder("foo", "src/Foo.java").setModuleBaseDir(temp.newFolder().toPath()).build();
    DefaultSignificantCode h = new DefaultSignificantCode(null).onFile(inputFile);
    underTest.store(h);
    underTest.store(h);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultSignificantCode(org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.Test)

Example 2 with DefaultSignificantCode

use of org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode in project sonarqube by SonarSource.

the class DefaultSensorStorageTest method should_skip_significant_code_on_pull_request_when_file_status_is_SAME.

@Test
public void should_skip_significant_code_on_pull_request_when_file_status_is_SAME() {
    DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setStatus(InputFile.Status.SAME).setContents("foo").build();
    when(branchConfiguration.isPullRequest()).thenReturn(true);
    underTest.store(new DefaultSignificantCode().onFile(file).addRange(file.selectLine(1)));
    assertThat(reportWriter.hasComponentData(FileStructure.Domain.SGNIFICANT_CODE, file.scannerId())).isFalse();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultSignificantCode(org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode) Test(org.junit.Test)

Example 3 with DefaultSignificantCode

use of org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode in project sonarqube by SonarSource.

the class DefaultSensorStorage method store.

@Override
public void store(NewSignificantCode newSignificantCode) {
    DefaultSignificantCode significantCode = (DefaultSignificantCode) newSignificantCode;
    ScannerReportWriter writer = reportPublisher.getWriter();
    DefaultInputFile inputFile = (DefaultInputFile) significantCode.inputFile();
    if (shouldSkipStorage(inputFile)) {
        return;
    }
    inputFile.setPublished(true);
    int componentRef = inputFile.scannerId();
    if (writer.hasComponentData(FileStructure.Domain.SGNIFICANT_CODE, componentRef)) {
        throw new UnsupportedOperationException("Trying to save significant code information twice for the same file is not supported: " + significantCode.inputFile());
    }
    List<ScannerReport.LineSgnificantCode> protobuf = significantCode.significantCodePerLine().values().stream().map(range -> ScannerReport.LineSgnificantCode.newBuilder().setLine(range.start().line()).setStartOffset(range.start().lineOffset()).setEndOffset(range.end().lineOffset()).build()).collect(Collectors.toList());
    writer.writeComponentSignificantCode(componentRef, protobuf);
}
Also used : Metric(org.sonar.api.batch.measure.Metric) ScannerMetrics(org.sonar.core.metric.ScannerMetrics) Block(org.sonar.duplications.block.Block) Measure(org.sonar.api.batch.sensor.measure.Measure) COMMENT_LINES_DATA_KEY(org.sonar.api.measures.CoreMetrics.COMMENT_LINES_DATA_KEY) PmdBlockChunker(org.sonar.duplications.internal.pmd.PmdBlockChunker) NewSignificantCode(org.sonar.api.batch.sensor.code.NewSignificantCode) Loggers(org.sonar.api.utils.log.Loggers) NewCpdTokens(org.sonar.api.batch.sensor.cpd.NewCpdTokens) Map(java.util.Map) Configuration(org.sonar.api.config.Configuration) MetricFinder(org.sonar.api.batch.measure.MetricFinder) InputComponent(org.sonar.api.batch.fs.InputComponent) CloseableIterator(org.sonar.core.util.CloseableIterator) Set(java.util.Set) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) Collectors(java.util.stream.Collectors) CoreMetrics(org.sonar.api.measures.CoreMetrics) Serializable(java.io.Serializable) List(java.util.List) SensorStorage(org.sonar.api.batch.sensor.internal.SensorStorage) ReportPublisher(org.sonar.scanner.report.ReportPublisher) DefaultSymbolTable(org.sonar.api.batch.sensor.symbol.internal.DefaultSymbolTable) PUBLIC_DOCUMENTED_API_DENSITY_KEY(org.sonar.api.measures.CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY_KEY) Math.max(java.lang.Math.max) DefaultHighlighting(org.sonar.api.batch.sensor.highlighting.internal.DefaultHighlighting) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) ContextPropertiesCache(org.sonar.scanner.repository.ContextPropertiesCache) SortedMap(java.util.SortedMap) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) TEST_SUCCESS_DENSITY_KEY(org.sonar.api.measures.CoreMetrics.TEST_SUCCESS_DENSITY_KEY) InputFile(org.sonar.api.batch.fs.InputFile) IssuePublisher(org.sonar.scanner.issue.IssuePublisher) BranchConfiguration(org.sonar.scanner.scan.branch.BranchConfiguration) SonarCpdBlockIndex(org.sonar.scanner.cpd.index.SonarCpdBlockIndex) Constants(org.sonar.scanner.protocol.Constants) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) ExternalIssue(org.sonar.api.batch.sensor.issue.ExternalIssue) HashSet(java.util.HashSet) ScannerReportUtils(org.sonar.scanner.report.ScannerReportUtils) AdHocRule(org.sonar.api.batch.sensor.rule.AdHocRule) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) TextRange(org.sonar.api.batch.fs.TextRange) NewSymbolTable(org.sonar.api.batch.sensor.symbol.NewSymbolTable) LINES_KEY(org.sonar.api.measures.CoreMetrics.LINES_KEY) DefaultSignificantCode(org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode) Issue(org.sonar.api.batch.sensor.issue.Issue) FileStructure(org.sonar.scanner.protocol.output.FileStructure) AnalysisError(org.sonar.api.batch.sensor.error.AnalysisError) DefaultCoverage(org.sonar.api.batch.sensor.coverage.internal.DefaultCoverage) NewHighlighting(org.sonar.api.batch.sensor.highlighting.NewHighlighting) Collectors.toList(java.util.stream.Collectors.toList) TreeMap(java.util.TreeMap) InputDir(org.sonar.api.batch.fs.InputDir) NewCoverage(org.sonar.api.batch.sensor.coverage.NewCoverage) KeyValueFormat(org.sonar.api.utils.KeyValueFormat) DefaultCpdTokens(org.sonar.api.batch.sensor.cpd.internal.DefaultCpdTokens) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultSignificantCode(org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter)

Example 4 with DefaultSignificantCode

use of org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode in project sonarqube by SonarSource.

the class DefaultSensorStorageTest method should_save_significant_code.

@Test
public void should_save_significant_code() {
    DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setContents("foo").build();
    underTest.store(new DefaultSignificantCode().onFile(file).addRange(file.selectLine(1)));
    assertThat(reportWriter.hasComponentData(FileStructure.Domain.SGNIFICANT_CODE, file.scannerId())).isTrue();
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultSignificantCode(org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode) Test(org.junit.Test)

Example 5 with DefaultSignificantCode

use of org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode in project sonarqube by SonarSource.

the class InMemorySensorStorage method store.

@Override
public void store(NewSignificantCode newSignificantCode) {
    DefaultSignificantCode significantCode = (DefaultSignificantCode) newSignificantCode;
    String fileKey = significantCode.inputFile().key();
    // Emulate duplicate storage check
    if (significantCodePerComponent.containsKey(fileKey)) {
        throw new UnsupportedOperationException("Trying to save significant code information twice for the same file is not supported: " + significantCode.inputFile());
    }
    significantCodePerComponent.put(fileKey, significantCode);
}
Also used : DefaultSignificantCode(org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode)

Aggregations

DefaultSignificantCode (org.sonar.api.batch.sensor.code.internal.DefaultSignificantCode)5 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)4 Test (org.junit.Test)3 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)3 InputFile (org.sonar.api.batch.fs.InputFile)2 Serializable (java.io.Serializable)1 Math.max (java.lang.Math.max)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1 Nullable (javax.annotation.Nullable)1 InputComponent (org.sonar.api.batch.fs.InputComponent)1 InputDir (org.sonar.api.batch.fs.InputDir)1 TextRange (org.sonar.api.batch.fs.TextRange)1 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1