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());
}
}
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);
}
}
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");
}
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));
}
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);
}
Aggregations