diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-03-27 16:51:39 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-03-27 17:05:48 +0100 |
commit | 6e44175ed6e81ae9989aa767439d91b9a502f5bf (patch) | |
tree | 55bba76fc85cd06fe1698daac2bc411fe5cfd767 /server | |
parent | 5446d877b4e67f2f32ac869e76d9ad02ca226773 (diff) | |
download | sonarqube-6e44175ed6e81ae9989aa767439d91b9a502f5bf.tar.gz sonarqube-6e44175ed6e81ae9989aa767439d91b9a502f5bf.zip |
SONAR-6338 Coverage in compute
Diffstat (limited to 'server')
2 files changed, 268 insertions, 0 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistCoverageStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistCoverageStep.java new file mode 100644 index 00000000000..1cdec57b2b1 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistCoverageStep.java @@ -0,0 +1,109 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.computation.step; + +import com.google.common.annotations.VisibleForTesting; +import org.sonar.api.resources.Qualifiers; +import org.sonar.batch.protocol.Constants; +import org.sonar.batch.protocol.output.BatchReport; +import org.sonar.batch.protocol.output.BatchReportReader; +import org.sonar.server.computation.ComputationContext; +import org.sonar.server.source.db.FileSourceDb; + +import javax.annotation.Nullable; + +/** + * Nothing is persist for the moment. Only Coverage are read and not persist for the moment + */ +public class PersistCoverageStep implements ComputationStep { + + // Temporary variable in order to be able to test that coverage are well computed. Will only contains data from last processed file + private FileSourceDb.Data fileSourceData; + + @Override + public String[] supportedProjectQualifiers() { + return new String[] {Qualifiers.PROJECT}; + } + + @Override + public void execute(ComputationContext context) { + int rootComponentRef = context.getReportMetadata().getRootComponentRef(); + recursivelyProcessComponent(context, rootComponentRef); + } + + private void recursivelyProcessComponent(ComputationContext context, int componentRef) { + BatchReportReader reportReader = context.getReportReader(); + BatchReport.Component component = reportReader.readComponent(componentRef); + if (component.getType().equals(Constants.ComponentType.FILE)) { + BatchReport.Coverage coverage = reportReader.readFileCoverage(componentRef); + processCoverage(component, coverage); + } + + for (Integer childRef : component.getChildRefList()) { + recursivelyProcessComponent(context, childRef); + } + } + + private void processCoverage(BatchReport.Component component, @Nullable BatchReport.Coverage coverage) { + fileSourceData = null; + if (coverage != null) { + FileSourceDb.Data.Builder dataBuilder = FileSourceDb.Data.newBuilder(); + for (int line = 0; line < coverage.getConditionsByLineCount(); line ++) { + FileSourceDb.Line.Builder lineBuilder = dataBuilder.addLinesBuilder().setLine(line); + processLineCoverage(line, lineBuilder, coverage); + } + fileSourceData = dataBuilder.build(); + } + } + + private void processLineCoverage(int line, FileSourceDb.Line.Builder lineBuilder, BatchReport.Coverage coverage){ + // Unit test + if (coverage.getUtHitsByLine(line)) { + lineBuilder.setUtLineHits(1); + } + lineBuilder.setUtConditions(coverage.getConditionsByLine(line)); + lineBuilder.setUtCoveredConditions(coverage.getUtCoveredConditionsByLine(line)); + + // Integration test + if (coverage.getItHitsByLine(line)) { + lineBuilder.setItLineHits(1); + } + lineBuilder.setItConditions(coverage.getConditionsByLine(line)); + lineBuilder.setItCoveredConditions(coverage.getItCoveredConditionsByLine(line)); + + // Overall test + if (coverage.getUtHitsByLine(line) || coverage.getItHitsByLine(line)) { + lineBuilder.setOverallLineHits(1); + } + lineBuilder.setOverallConditions(coverage.getConditionsByLine(line)); + lineBuilder.setOverallCoveredConditions(coverage.getOverallCoveredConditionsByLine(line)); + } + + @VisibleForTesting + FileSourceDb.Data getFileSourceData() { + return fileSourceData; + } + + @Override + public String getDescription() { + return "Read Coverage"; + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistCoverageStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistCoverageStepTest.java new file mode 100644 index 00000000000..e044cacc4c6 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistCoverageStepTest.java @@ -0,0 +1,159 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.computation.step; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.TemporaryFolder; +import org.sonar.batch.protocol.Constants; +import org.sonar.batch.protocol.output.BatchReport; +import org.sonar.batch.protocol.output.BatchReportReader; +import org.sonar.batch.protocol.output.BatchReportWriter; +import org.sonar.core.component.ComponentDto; +import org.sonar.server.computation.ComputationContext; +import org.sonar.server.source.db.FileSourceDb; +import org.sonar.test.DbTests; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +@Category(DbTests.class) +public class PersistCoverageStepTest extends BaseStepTest { + + private static final Integer FILE_REF = 3; + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + File reportDir; + + PersistCoverageStep step; + + @Before + public void setup() throws Exception { + reportDir = temp.newFolder(); + step = new PersistCoverageStep(); + } + + @Override + protected ComputationStep step() throws IOException { + return step; + } + + @Test + public void compute_nothing() throws Exception { + initReport(); + + step.execute(new ComputationContext(new BatchReportReader(reportDir), mock(ComponentDto.class))); + + assertThat(step.getFileSourceData()).isNull(); + } + + @Test + public void compute_coverage_from_one_line() throws Exception { + BatchReportWriter writer = initReport(); + + writer.writeFileCoverage(BatchReport.Coverage.newBuilder() + .setFileRef(FILE_REF) + .addAllConditionsByLine(Arrays.asList(10)) + .addAllUtHitsByLine(Arrays.asList(true)) + .addAllUtCoveredConditionsByLine(Arrays.asList(2)) + .addAllItHitsByLine(Arrays.asList(false)) + .addAllItCoveredConditionsByLine(Arrays.asList(3)) + .addAllOverallCoveredConditionsByLine(Arrays.asList(4)) + .build()); + + step.execute(new ComputationContext(new BatchReportReader(reportDir), mock(ComponentDto.class))); + + FileSourceDb.Data data = step.getFileSourceData(); + assertThat(data.getLines(0).getUtLineHits()).isEqualTo(1); + assertThat(data.getLines(0).getUtConditions()).isEqualTo(10); + assertThat(data.getLines(0).getUtCoveredConditions()).isEqualTo(2); + assertThat(data.getLines(0).hasItLineHits()).isFalse(); + assertThat(data.getLines(0).getItConditions()).isEqualTo(10); + assertThat(data.getLines(0).getItCoveredConditions()).isEqualTo(3); + assertThat(data.getLines(0).getOverallLineHits()).isEqualTo(1); + assertThat(data.getLines(0).getOverallConditions()).isEqualTo(10); + assertThat(data.getLines(0).getOverallCoveredConditions()).isEqualTo(4); + } + + @Test + public void compute_coverage_from_lines() throws Exception { + BatchReportWriter writer = initReport(); + + writer.writeFileCoverage(BatchReport.Coverage.newBuilder() + .setFileRef(FILE_REF) + .addAllConditionsByLine(Arrays.asList(10, 0, 4)) + .addAllUtHitsByLine(Arrays.asList(true, false, false)) + .addAllItHitsByLine(Arrays.asList(false, true, true)) + .addAllUtCoveredConditionsByLine(Arrays.asList(1, 0, 4)) + .addAllItCoveredConditionsByLine(Arrays.asList(1, 0, 5)) + .addAllOverallCoveredConditionsByLine(Arrays.asList(1, 0, 5)) + .build()); + + step.execute(new ComputationContext(new BatchReportReader(reportDir), mock(ComponentDto.class))); + + FileSourceDb.Data data = step.getFileSourceData(); + assertThat(data.getLines(0).getUtLineHits()).isEqualTo(1); + assertThat(data.getLines(0).hasItLineHits()).isFalse(); + + assertThat(data.getLines(1).hasUtLineHits()).isFalse(); + assertThat(data.getLines(1).getItLineHits()).isEqualTo(1); + + assertThat(data.getLines(2).hasUtLineHits()).isFalse(); + assertThat(data.getLines(2).getItLineHits()).isEqualTo(1); + } + + private BatchReportWriter initReport() { + BatchReportWriter writer = new BatchReportWriter(reportDir); + writer.writeMetadata(BatchReport.Metadata.newBuilder() + .setRootComponentRef(1) + .setProjectKey("PROJECT_KEY") + .setAnalysisDate(150000000L) + .build()); + + writer.writeComponent(BatchReport.Component.newBuilder() + .setRef(1) + .setType(Constants.ComponentType.PROJECT) + .setUuid("PROJECT_A") + .addChildRef(2) + .build()); + writer.writeComponent(BatchReport.Component.newBuilder() + .setRef(2) + .setType(Constants.ComponentType.MODULE) + .setUuid("BCDE") + .addChildRef(FILE_REF) + .build()); + writer.writeComponent(BatchReport.Component.newBuilder() + .setRef(FILE_REF) + .setType(Constants.ComponentType.FILE) + .setUuid("FILE_A") + .build()); + return writer; + } + +} |