Search in sources :

Example 1 with DefaultMeasure

use of org.sonar.api.batch.sensor.measure.internal.DefaultMeasure in project sonarqube by SonarSource.

the class CoveragePublisherTest method publishCoverage.

@Test
public void publishCoverage() throws Exception {
    DefaultMeasure<String> utLineHits = new DefaultMeasure<String>().forMetric(CoreMetrics.COVERAGE_LINE_HITS_DATA).withValue("2=1;3=1;5=0;6=3");
    when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY)).thenReturn((DefaultMeasure) utLineHits);
    DefaultMeasure<String> conditionsByLine = new DefaultMeasure<String>().forMetric(CoreMetrics.CONDITIONS_BY_LINE).withValue("3=4");
    when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.CONDITIONS_BY_LINE_KEY)).thenReturn((DefaultMeasure) conditionsByLine);
    DefaultMeasure<String> coveredConditionsByUts = new DefaultMeasure<String>().forMetric(CoreMetrics.COVERED_CONDITIONS_BY_LINE).withValue("3=2");
    when(measureCache.byMetric("foo:src/Foo.php", CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY)).thenReturn((DefaultMeasure) coveredConditionsByUts);
    File outputDir = temp.newFolder();
    ScannerReportWriter writer = new ScannerReportWriter(outputDir);
    publisher.publish(writer);
    try (CloseableIterator<LineCoverage> it = new ScannerReportReader(outputDir).readComponentCoverage(inputFile.batchId())) {
        assertThat(it.next()).isEqualTo(LineCoverage.newBuilder().setLine(2).setHits(true).build());
        assertThat(it.next()).isEqualTo(LineCoverage.newBuilder().setLine(3).setHits(true).setConditions(4).setCoveredConditions(2).build());
        assertThat(it.next()).isEqualTo(LineCoverage.newBuilder().setLine(5).setHits(false).build());
    }
}
Also used : ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) LineCoverage(org.sonar.scanner.protocol.output.ScannerReport.LineCoverage) Matchers.anyString(org.mockito.Matchers.anyString) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) Test(org.junit.Test)

Example 2 with DefaultMeasure

use of org.sonar.api.batch.sensor.measure.internal.DefaultMeasure in project sonarqube by SonarSource.

the class MeasuresPublisherTest method publishMeasures.

@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(inputModule.batchId())).hasSize(0);
    try (CloseableIterator<ScannerReport.Measure> componentMeasures = reader.readComponentMeasures(inputFile.batchId())) {
        assertThat(componentMeasures).hasSize(2);
    }
}
Also used : ScannerReportReader(org.sonar.scanner.protocol.output.ScannerReportReader) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 3 with DefaultMeasure

use of org.sonar.api.batch.sensor.measure.internal.DefaultMeasure in project sonarqube by SonarSource.

the class DefaultFileLinesContextTest method shouldSave.

@Test
public void shouldSave() {
    fileLineMeasures.setIntValue(HITS_METRIC_KEY, 1, 2);
    fileLineMeasures.setIntValue(HITS_METRIC_KEY, 3, 0);
    fileLineMeasures.save();
    assertThat(fileLineMeasures).hasToString("DefaultFileLinesContext{{hits={1=2, 3=0}}}");
    ArgumentCaptor<DefaultMeasure> captor = ArgumentCaptor.forClass(DefaultMeasure.class);
    verify(sensorStorage).store(captor.capture());
    DefaultMeasure measure = captor.getValue();
    assertThat(measure.inputComponent()).isEqualTo(file);
    assertThat(measure.metric().key()).isEqualTo(HITS_METRIC_KEY);
    assertThat(measure.value()).isEqualTo("1=2;3=0");
}
Also used : DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) Test(org.junit.Test)

Example 4 with DefaultMeasure

use of org.sonar.api.batch.sensor.measure.internal.DefaultMeasure in project sonarqube by SonarSource.

the class TestExecutionPublisher method updateTestExecutionFromTestPlan.

private void updateTestExecutionFromTestPlan(final InputFile inputFile, ScannerReportWriter writer) {
    final DefaultTestPlan testPlan = testPlanBuilder.getTestPlanByFile(inputFile);
    if (testPlan == null || !testPlan.testCases().iterator().hasNext()) {
        return;
    }
    long nonSkippedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() != Status.SKIPPED).count();
    appendMeasure(inputFile, writer, new DefaultMeasure<Integer>().forMetric(TESTS).withValue((int) nonSkippedTests));
    long executionTime = StreamSupport.stream(testPlan.testCases().spliterator(), false).map(DefaultTestCase::durationInMs).filter(Objects::nonNull).mapToLong(Long::longValue).sum();
    appendMeasure(inputFile, writer, new DefaultMeasure<Long>().forMetric(TEST_EXECUTION_TIME).withValue(executionTime));
    long errorTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.ERROR).count();
    appendMeasure(inputFile, writer, new DefaultMeasure<Integer>().forMetric(TEST_ERRORS).withValue((int) errorTests));
    long skippedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.SKIPPED).count();
    appendMeasure(inputFile, writer, new DefaultMeasure<Integer>().forMetric(SKIPPED_TESTS).withValue((int) skippedTests));
    long failedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.FAILURE).count();
    appendMeasure(inputFile, writer, new DefaultMeasure<Integer>().forMetric(TEST_FAILURES).withValue((int) failedTests));
}
Also used : DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputFile(org.sonar.api.batch.fs.InputFile) InputComponentStore(org.sonar.scanner.scan.filesystem.InputComponentStore) InputComponent(org.sonar.api.batch.fs.InputComponent) TEST_EXECUTION_TIME(org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME) Status(org.sonar.scanner.deprecated.test.DefaultTestCase.Status) TestPlanBuilder(org.sonar.scanner.deprecated.test.TestPlanBuilder) DefaultTestPlan(org.sonar.scanner.deprecated.test.DefaultTestPlan) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) SKIPPED_TESTS(org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS) TEST_ERRORS(org.sonar.api.measures.CoreMetrics.TEST_ERRORS) TESTS(org.sonar.api.measures.CoreMetrics.TESTS) DefaultSensorStorage.toReportMeasure(org.sonar.scanner.sensor.DefaultSensorStorage.toReportMeasure) Objects(java.util.Objects) DefaultTestCase(org.sonar.scanner.deprecated.test.DefaultTestCase) TEST_FAILURES(org.sonar.api.measures.CoreMetrics.TEST_FAILURES) StreamSupport(java.util.stream.StreamSupport) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) DefaultTestPlan(org.sonar.scanner.deprecated.test.DefaultTestPlan) Objects(java.util.Objects)

Example 5 with DefaultMeasure

use of org.sonar.api.batch.sensor.measure.internal.DefaultMeasure in project sonarqube by SonarSource.

the class DefaultSensorStorageTest method should_save_project_measure.

@Test
public void should_save_project_measure() throws IOException {
    String projectKey = "myProject";
    DefaultInputModule module = new DefaultInputModule(ProjectDefinition.create().setKey(projectKey).setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
    underTest.store(new DefaultMeasure().on(module).forMetric(CoreMetrics.NCLOC).withValue(10));
    ScannerReport.Measure m = reportReader.readComponentMeasures(module.scannerId()).next();
    assertThat(m.getIntValue().getValue()).isEqualTo(10);
    assertThat(m.getMetricKey()).isEqualTo(CoreMetrics.NCLOC_KEY);
}
Also used : DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) Test(org.junit.Test)

Aggregations

DefaultMeasure (org.sonar.api.batch.sensor.measure.internal.DefaultMeasure)15 Test (org.junit.Test)10 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)8 InputComponent (org.sonar.api.batch.fs.InputComponent)5 InputFile (org.sonar.api.batch.fs.InputFile)5 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)5 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)4 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)4 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)4 ScannerReportWriter (org.sonar.scanner.protocol.output.ScannerReportWriter)3 Serializable (java.io.Serializable)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 StreamSupport (java.util.stream.StreamSupport)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Metric (org.sonar.api.batch.measure.Metric)2 ScannerReportReader (org.sonar.scanner.protocol.output.ScannerReportReader)2 Iterables (com.google.common.collect.Iterables)1 File (java.io.File)1 Math.max (java.lang.Math.max)1