Search in sources :

Example 11 with DefaultMeasure

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

the class MeasuresPublisher method updateTestExecutionFromTestPlan.

private void updateTestExecutionFromTestPlan(final InputFile inputFile) {
    final MutableTestPlan testPlan = testPlanBuilder.getTestPlanByFile(inputFile);
    if (testPlan == null || Iterables.isEmpty(testPlan.testCases())) {
        return;
    }
    long nonSkippedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() != Status.SKIPPED).count();
    measureCache.put(inputFile.key(), TESTS_KEY, new DefaultMeasure<Integer>().forMetric(TESTS).withValue((int) nonSkippedTests));
    long executionTime = StreamSupport.stream(testPlan.testCases().spliterator(), false).mapToLong(t -> t.durationInMs() != null ? t.durationInMs().longValue() : 0L).sum();
    measureCache.put(inputFile.key(), TEST_EXECUTION_TIME_KEY, new DefaultMeasure<Long>().forMetric(TEST_EXECUTION_TIME).withValue(executionTime));
    long errorTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.ERROR).count();
    measureCache.put(inputFile.key(), TEST_ERRORS_KEY, new DefaultMeasure<Integer>().forMetric(TEST_ERRORS).withValue((int) errorTests));
    long skippedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.SKIPPED).count();
    measureCache.put(inputFile.key(), SKIPPED_TESTS_KEY, new DefaultMeasure<Integer>().forMetric(SKIPPED_TESTS).withValue((int) skippedTests));
    long failedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.FAILURE).count();
    measureCache.put(inputFile.key(), TEST_FAILURES_KEY, 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) Metric(org.sonar.api.batch.measure.Metric) Iterables(com.google.common.collect.Iterables) InputComponentStore(org.sonar.scanner.scan.filesystem.InputComponentStore) SKIPPED_TESTS_KEY(org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS_KEY) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) SKIPPED_TESTS(org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS) LINES_TO_COVER(org.sonar.api.measures.CoreMetrics.LINES_TO_COVER) Type(org.sonar.api.batch.fs.InputFile.Type) TESTS(org.sonar.api.measures.CoreMetrics.TESTS) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) UNCOVERED_LINES_KEY(org.sonar.api.measures.CoreMetrics.UNCOVERED_LINES_KEY) TEST_ERRORS_KEY(org.sonar.api.measures.CoreMetrics.TEST_ERRORS_KEY) CONDITIONS_TO_COVER_KEY(org.sonar.api.measures.CoreMetrics.CONDITIONS_TO_COVER_KEY) TEST_FAILURES_KEY(org.sonar.api.measures.CoreMetrics.TEST_FAILURES_KEY) UNCOVERED_CONDITIONS(org.sonar.api.measures.CoreMetrics.UNCOVERED_CONDITIONS) Status(org.sonar.api.test.TestCase.Status) CONDITIONS_TO_COVER(org.sonar.api.measures.CoreMetrics.CONDITIONS_TO_COVER) StringValue(org.sonar.scanner.protocol.output.ScannerReport.Measure.StringValue) Map(java.util.Map) TEST_EXECUTION_TIME_KEY(org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME_KEY) StreamSupport(java.util.stream.StreamSupport) UNCOVERED_CONDITIONS_KEY(org.sonar.api.measures.CoreMetrics.UNCOVERED_CONDITIONS_KEY) LINES_TO_COVER_KEY(org.sonar.api.measures.CoreMetrics.LINES_TO_COVER_KEY) InputComponent(org.sonar.api.batch.fs.InputComponent) TEST_EXECUTION_TIME(org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME) TestPlanBuilder(org.sonar.scanner.deprecated.test.TestPlanBuilder) IntValue(org.sonar.scanner.protocol.output.ScannerReport.Measure.IntValue) LongValue(org.sonar.scanner.protocol.output.ScannerReport.Measure.LongValue) TEST_ERRORS(org.sonar.api.measures.CoreMetrics.TEST_ERRORS) MeasureCache(org.sonar.scanner.scan.measure.MeasureCache) Collectors(java.util.stream.Collectors) CoreMetrics(org.sonar.api.measures.CoreMetrics) Serializable(java.io.Serializable) MutableTestPlan(org.sonar.api.test.MutableTestPlan) BoolValue(org.sonar.scanner.protocol.output.ScannerReport.Measure.BoolValue) TEST_FAILURES(org.sonar.api.measures.CoreMetrics.TEST_FAILURES) TESTS_KEY(org.sonar.api.measures.CoreMetrics.TESTS_KEY) UNCOVERED_LINES(org.sonar.api.measures.CoreMetrics.UNCOVERED_LINES) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) KeyValueFormat(org.sonar.api.utils.KeyValueFormat) Collections(java.util.Collections) DoubleValue(org.sonar.scanner.protocol.output.ScannerReport.Measure.DoubleValue) MutableTestPlan(org.sonar.api.test.MutableTestPlan)

Example 12 with DefaultMeasure

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

the class DefaultSensorStorageTest method shouldSaveProjectMeasureToSensorContext.

@Test
public void shouldSaveProjectMeasureToSensorContext() {
    String projectKey = "myProject";
    DefaultInputModule module = new DefaultInputModule(projectKey);
    ArgumentCaptor<DefaultMeasure> argumentCaptor = ArgumentCaptor.forClass(DefaultMeasure.class);
    when(measureCache.put(eq(module.key()), eq(CoreMetrics.NCLOC_KEY), argumentCaptor.capture())).thenReturn(null);
    underTest.store(new DefaultMeasure().on(module).forMetric(CoreMetrics.NCLOC).withValue(10));
    DefaultMeasure m = argumentCaptor.getValue();
    assertThat(m.value()).isEqualTo(10);
    assertThat(m.metric()).isEqualTo(CoreMetrics.NCLOC);
}
Also used : DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) DefaultInputModule(org.sonar.api.batch.fs.internal.DefaultInputModule) Test(org.junit.Test)

Example 13 with DefaultMeasure

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

the class DefaultSensorStorageTest method shouldSaveFileMeasureToSensorContext.

@Test
public void shouldSaveFileMeasureToSensorContext() {
    InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
    ArgumentCaptor<DefaultMeasure> argumentCaptor = ArgumentCaptor.forClass(DefaultMeasure.class);
    when(measureCache.put(eq(file.key()), eq(CoreMetrics.NCLOC_KEY), argumentCaptor.capture())).thenReturn(null);
    underTest.store(new DefaultMeasure().on(file).forMetric(CoreMetrics.NCLOC).withValue(10));
    DefaultMeasure m = argumentCaptor.getValue();
    assertThat(m.value()).isEqualTo(10);
    assertThat(m.metric()).isEqualTo(CoreMetrics.NCLOC);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) InputFile(org.sonar.api.batch.fs.InputFile) Test(org.junit.Test)

Example 14 with DefaultMeasure

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

the class DefaultSensorStorageTest method should_not_skip_file_measures_on_pull_request_when_file_status_is_SAME.

@Test
public void should_not_skip_file_measures_on_pull_request_when_file_status_is_SAME() {
    DefaultInputFile file = new TestInputFileBuilder("foo", "src/Foo.php").setStatus(InputFile.Status.SAME).build();
    when(branchConfiguration.isPullRequest()).thenReturn(true);
    underTest.store(new DefaultMeasure().on(file).forMetric(CoreMetrics.NCLOC).withValue(10));
    ScannerReport.Measure m = reportReader.readComponentMeasures(file.scannerId()).next();
    assertThat(m.getIntValue().getValue()).isEqualTo(10);
    assertThat(m.getMetricKey()).isEqualTo(CoreMetrics.NCLOC_KEY);
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) Test(org.junit.Test)

Example 15 with DefaultMeasure

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

the class DefaultSensorStorageTest method shouldFailIfUnknownMetric.

@Test
public void shouldFailIfUnknownMetric() {
    InputFile file = new TestInputFileBuilder("foo", "src/Foo.php").build();
    assertThatThrownBy(() -> underTest.store(new DefaultMeasure().on(file).forMetric(CoreMetrics.LINES).withValue(10))).isInstanceOf(UnsupportedOperationException.class).hasMessage("Unknown metric: lines");
}
Also used : TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) 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