@Override public void publish(ScannerReportWriter writer) { final ScannerReport.Measure.Builder builder = ScannerReport.Measure.newBuilder(); for (final InputComponent c : componentStore.all()) { DefaultInputComponent component = (DefaultInputComponent) c; if (component.isFile()) { DefaultInputFile file = (DefaultInputFile) component; // Recompute all coverage measures from line data to take into account the possible merge of several reports updateCoverageFromLineData(file); // Recompute test execution measures from MutableTestPlan to take into account the possible merge of several reports updateTestExecutionFromTestPlan(file); } Iterable<DefaultMeasure<?>> scannerMeasures = measureCache.byComponentKey(component.key()); if (scannerMeasures.iterator().hasNext()) { writer.writeComponentMeasures(component.scannerId(), StreamSupport.stream(scannerMeasures.spliterator(), false) .map(input -> { if (input.value() == null) { throw new IllegalArgumentException( String.format("Measure on metric '%s' and component '%s' has no value, but it's not allowed", input.metric().key(), component.key())); } builder.clear(); builder.setMetricKey(input.metric().key()); setValueAccordingToType(builder, input); return builder.build(); }).collect(Collectors.toList())); } } }
@Before public void prepare() throws IOException { String projectKey = "foo"; project = TestInputFileBuilder.newDefaultInputProject(projectKey, temp.newFolder()); inputFile = new TestInputFileBuilder(projectKey, "src/Foo.php").setPublish(true).build(); InputComponentStore componentCache = new InputComponentStore(mock(BranchConfiguration.class)); componentCache.put(projectKey, inputFile); measureCache = mock(MeasureCache.class); when(measureCache.byComponentKey(anyString())).thenReturn(Collections.<DefaultMeasure<?>>emptyList()); publisher = new MeasuresPublisher(componentCache, measureCache, mock(TestPlanBuilder.class)); outputDir = temp.newFolder(); writer = new ScannerReportWriter(outputDir); }
@Test public void fail_with_IAE_when_measure_has_no_value() throws Exception { DefaultMeasure<Integer> measure = new DefaultMeasure<Integer>().forMetric(CoreMetrics.LINES_TO_COVER); when(measureCache.byComponentKey(inputFile.key())).thenReturn(Collections.singletonList(measure)); try { publisher.publish(writer); fail(); } catch (RuntimeException e) { assertThat(ExceptionUtils.getFullStackTrace(e)).contains("Measure on metric 'lines_to_cover' and component 'foo:src/Foo.php' has no value, but it's not allowed"); } }
@Override public void publish(ScannerReportWriter writer) { final ScannerReport.Measure.Builder builder = ScannerReport.Measure.newBuilder(); for (final InputComponent c : componentStore.all()) { DefaultInputComponent component = (DefaultInputComponent) c; if (component.isFile()) { DefaultInputFile file = (DefaultInputFile) component; // Recompute all coverage measures from line data to take into account the possible merge of several reports updateCoverageFromLineData(file); // Recompute test execution measures from MutableTestPlan to take into account the possible merge of several reports updateTestExecutionFromTestPlan(file); } Iterable<DefaultMeasure<?>> scannerMeasures = measureCache.byComponentKey(component.key()); if (scannerMeasures.iterator().hasNext()) { writer.writeComponentMeasures(component.scannerId(), StreamSupport.stream(scannerMeasures.spliterator(), false) .map(input -> { if (input.value() == null) { throw new IllegalArgumentException( String.format("Measure on metric '%s' and component '%s' has no value, but it's not allowed", input.metric().key(), component.key())); } builder.clear(); builder.setMetricKey(input.metric().key()); setValueAccordingToType(builder, input); return builder.build(); }).collect(Collectors.toList())); } } }
@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); } }