From bf1388308dfd71f60e41dcd89eebc5cef710bff3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 26 Jun 2015 19:16:14 +0200 Subject: [PATCH] SONAR-6645 remove computation of new coverage measures from Batch --- .../batch/bootstrap/BatchComponents.java | 8 - .../AbstractNewCoverageFileAnalyzer.java | 262 --------------- .../batch/compute/NewCoverageAggregator.java | 99 ------ .../compute/NewCoverageFileAnalyzer.java | 73 ----- .../compute/NewItCoverageFileAnalyzer.java | 68 ---- .../NewOverallCoverageFileAnalyzer.java | 68 ---- .../compute/NewCoverageAggregatorTest.java | 90 ------ .../compute/NewCoverageFileAnalyzerTest.java | 297 ------------------ 8 files changed, 965 deletions(-) delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/compute/AbstractNewCoverageFileAnalyzer.java delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/compute/NewCoverageAggregator.java delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/compute/NewCoverageFileAnalyzer.java delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/compute/NewItCoverageFileAnalyzer.java delete mode 100644 sonar-batch/src/main/java/org/sonar/batch/compute/NewOverallCoverageFileAnalyzer.java delete mode 100644 sonar-batch/src/test/java/org/sonar/batch/compute/NewCoverageAggregatorTest.java delete mode 100644 sonar-batch/src/test/java/org/sonar/batch/compute/NewCoverageFileAnalyzerTest.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java index e4dadf1c309..d40e1c223f7 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/BatchComponents.java @@ -32,10 +32,6 @@ import org.sonar.batch.compute.ItBranchCoverageDecorator; import org.sonar.batch.compute.ItCoverageDecorator; import org.sonar.batch.compute.ItLineCoverageDecorator; import org.sonar.batch.compute.LineCoverageDecorator; -import org.sonar.batch.compute.NewCoverageAggregator; -import org.sonar.batch.compute.NewCoverageFileAnalyzer; -import org.sonar.batch.compute.NewItCoverageFileAnalyzer; -import org.sonar.batch.compute.NewOverallCoverageFileAnalyzer; import org.sonar.batch.compute.OverallBranchCoverageDecorator; import org.sonar.batch.compute.OverallCoverageDecorator; import org.sonar.batch.compute.OverallLineCoverageDecorator; @@ -115,10 +111,6 @@ public class BatchComponents { CommentDensityDecorator.class, DirectoriesDecorator.class, FilesDecorator.class, - NewCoverageFileAnalyzer.class, - NewItCoverageFileAnalyzer.class, - NewOverallCoverageFileAnalyzer.class, - NewCoverageAggregator.class, TimeMachineConfiguration.class ); components.addAll(CorePropertyDefinitions.all()); diff --git a/sonar-batch/src/main/java/org/sonar/batch/compute/AbstractNewCoverageFileAnalyzer.java b/sonar-batch/src/main/java/org/sonar/batch/compute/AbstractNewCoverageFileAnalyzer.java deleted file mode 100644 index 6f17ffc0341..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/compute/AbstractNewCoverageFileAnalyzer.java +++ /dev/null @@ -1,262 +0,0 @@ -/* - * 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.batch.compute; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Date; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.annotation.Nullable; -import org.apache.commons.lang.ObjectUtils; -import org.sonar.api.batch.Decorator; -import org.sonar.api.batch.DecoratorBarriers; -import org.sonar.api.batch.DecoratorContext; -import org.sonar.api.batch.DependedUpon; -import org.sonar.api.batch.DependsUpon; -import org.sonar.api.batch.RequiresDB; -import org.sonar.api.measures.Measure; -import org.sonar.api.measures.Metric; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.Qualifiers; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.Scopes; -import org.sonar.api.utils.KeyValueFormat; -import org.sonar.batch.components.Period; -import org.sonar.batch.components.TimeMachineConfiguration; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReport.Changesets.Changeset; -import org.sonar.batch.protocol.output.BatchReportReader; -import org.sonar.batch.report.ReportPublisher; - -/** - * @since 2.7 - */ -@RequiresDB -@DependedUpon(DecoratorBarriers.END_OF_TIME_MACHINE) -public abstract class AbstractNewCoverageFileAnalyzer implements Decorator { - - private final List structs; - private final ReportPublisher publishReportJob; - private final BatchComponentCache resourceCache; - - public AbstractNewCoverageFileAnalyzer(TimeMachineConfiguration timeMachineConfiguration, ReportPublisher publishReportJob, BatchComponentCache resourceCache) { - this(new ArrayList(), publishReportJob, resourceCache); - for (Period period : timeMachineConfiguration.periods()) { - structs.add(new PeriodStruct(period.getIndex(), period.getDate())); - } - } - - AbstractNewCoverageFileAnalyzer(List structs, ReportPublisher publishReportJob, BatchComponentCache resourceCache) { - this.resourceCache = resourceCache; - this.publishReportJob = publishReportJob; - this.structs = structs; - } - - public abstract Metric getCoverageLineHitsDataMetric(); - - public abstract Metric getConditionsByLineMetric(); - - public abstract Metric getCoveredConditionsByLineMetric(); - - public abstract Metric getNewLinesToCoverMetric(); - - public abstract Metric getNewUncoveredLinesMetric(); - - public abstract Metric getNewConditionsToCoverMetric(); - - public abstract Metric getNewUncoveredConditionsMetric(); - - @Override - public boolean shouldExecuteOnProject(Project project) { - return !structs.isEmpty(); - } - - private boolean shouldDecorate(Resource resource) { - return Scopes.isFile(resource) && !Qualifiers.UNIT_TEST_FILE.equals(resource.getQualifier()); - } - - @DependsUpon - public List dependsOnMetrics() { - - return Arrays.asList(getCoverageLineHitsDataMetric(), getConditionsByLineMetric(), getCoveredConditionsByLineMetric()); - } - - @DependedUpon - public List generatesNewCoverageMetrics() { - return Arrays.asList(getNewLinesToCoverMetric(), getNewUncoveredLinesMetric(), getNewConditionsToCoverMetric(), getNewUncoveredConditionsMetric()); - } - - @Override - public void decorate(Resource resource, DecoratorContext context) { - if (shouldDecorate(resource)) { - doDecorate(context); - } - } - - void doDecorate(DecoratorContext context) { - if (parse(context)) { - compute(context); - } - } - - private boolean parse(DecoratorContext context) { - BatchReportReader reader = new BatchReportReader(publishReportJob.getReportDir()); - BatchReport.Changesets componentScm = reader.readChangesets(resourceCache.get(context.getResource()).batchId()); - Measure hitsByLineMeasure = context.getMeasure(getCoverageLineHitsDataMetric()); - - if (componentScm != null && hitsByLineMeasure != null && hitsByLineMeasure.hasData()) { - Map hitsByLine = parseCountByLine(hitsByLineMeasure); - Map conditionsByLine = parseCountByLine(context.getMeasure(getConditionsByLineMetric())); - Map coveredConditionsByLine = parseCountByLine(context.getMeasure(getCoveredConditionsByLineMetric())); - - reset(); - - for (Map.Entry entry : hitsByLine.entrySet()) { - int lineId = entry.getKey(); - int hits = entry.getValue(); - int conditions = (Integer) ObjectUtils.defaultIfNull(conditionsByLine.get(lineId), 0); - int coveredConditions = (Integer) ObjectUtils.defaultIfNull(coveredConditionsByLine.get(lineId), 0); - Changeset changeset = componentScm.getChangeset(componentScm.getChangesetIndexByLine(lineId - 1)); - Date date = changeset.hasDate() ? new Date(changeset.getDate()) : null; - for (PeriodStruct struct : structs) { - struct.analyze(date, hits, conditions, coveredConditions); - } - } - - return true; - } - return false; - } - - private void reset() { - for (PeriodStruct struct : structs) { - struct.reset(); - } - } - - private void compute(DecoratorContext context) { - Measure newLines = new Measure(getNewLinesToCoverMetric()); - Measure newUncoveredLines = new Measure(getNewUncoveredLinesMetric()); - Measure newConditions = new Measure(getNewConditionsToCoverMetric()); - Measure newUncoveredConditions = new Measure(getNewUncoveredConditionsMetric()); - - for (PeriodStruct struct : structs) { - if (struct.hasNewCode()) { - newLines.setVariation(struct.index, (double) struct.getNewLines()); - newUncoveredLines.setVariation(struct.index, (double) (struct.getNewLines() - struct.getNewCoveredLines())); - newConditions.setVariation(struct.index, (double) struct.getNewConditions()); - newUncoveredConditions.setVariation(struct.index, (double) struct.getNewConditions() - struct.getNewCoveredConditions()); - } - } - - context.saveMeasure(newLines); - context.saveMeasure(newUncoveredLines); - context.saveMeasure(newConditions); - context.saveMeasure(newUncoveredConditions); - } - - private static Map parseCountByLine(@Nullable Measure measure) { - if (measure != null && measure.hasData()) { - return KeyValueFormat.parseIntInt(measure.getData()); - } - return new HashMap<>(); - } - - public static final class PeriodStruct { - int index; - Date date; - Integer newLines; - Integer newCoveredLines; - Integer newConditions; - Integer newCoveredConditions; - - PeriodStruct(int index, @Nullable Date date) { - this.index = index; - this.date = date; - } - - void reset() { - newLines = null; - newCoveredLines = null; - newConditions = null; - newCoveredConditions = null; - } - - void analyze(@Nullable Date lineDate, int hits, int conditions, int coveredConditions) { - if (lineDate == null) { - // TODO warning - - } else if (date == null || lineDate.after(date)) { - // TODO test if string comparison is faster or not - addLine(hits > 0); - addConditions(conditions, coveredConditions); - } - } - - void addLine(boolean covered) { - if (newLines == null) { - newLines = 0; - } - newLines += 1; - if (covered) { - if (newCoveredLines == null) { - newCoveredLines = 0; - } - newCoveredLines += 1; - } - } - - void addConditions(int count, int countCovered) { - if (newConditions == null) { - newConditions = 0; - } - newConditions += count; - if (count > 0) { - if (newCoveredConditions == null) { - newCoveredConditions = 0; - } - newCoveredConditions += countCovered; - } - } - - boolean hasNewCode() { - return newLines != null; - } - - public int getNewLines() { - return newLines != null ? newLines : 0; - } - - public int getNewCoveredLines() { - return newCoveredLines != null ? newCoveredLines : 0; - } - - public int getNewConditions() { - return newConditions != null ? newConditions : 0; - } - - public int getNewCoveredConditions() { - return newCoveredConditions != null ? newCoveredConditions : 0; - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/compute/NewCoverageAggregator.java b/sonar-batch/src/main/java/org/sonar/batch/compute/NewCoverageAggregator.java deleted file mode 100644 index 1923d5211a6..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/compute/NewCoverageAggregator.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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.batch.compute; - -import java.util.Arrays; -import java.util.List; -import org.apache.commons.lang.ArrayUtils; -import org.sonar.api.batch.Decorator; -import org.sonar.api.batch.DecoratorBarriers; -import org.sonar.api.batch.DecoratorContext; -import org.sonar.api.batch.DependedUpon; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; -import org.sonar.api.measures.Metric; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.Qualifiers; -import org.sonar.api.resources.Resource; -import org.sonar.api.resources.Scopes; - -@DependedUpon(DecoratorBarriers.END_OF_TIME_MACHINE) -public final class NewCoverageAggregator implements Decorator { - - @Override - public boolean shouldExecuteOnProject(Project project) { - return true; - } - - @DependedUpon - public List generatesNewCoverageMetrics() { - return Arrays.asList( - CoreMetrics.NEW_LINES_TO_COVER, CoreMetrics.NEW_UNCOVERED_LINES, CoreMetrics.NEW_CONDITIONS_TO_COVER, CoreMetrics.NEW_UNCOVERED_CONDITIONS, - CoreMetrics.NEW_IT_LINES_TO_COVER, CoreMetrics.NEW_IT_UNCOVERED_LINES, CoreMetrics.NEW_IT_CONDITIONS_TO_COVER, CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS, - CoreMetrics.NEW_OVERALL_LINES_TO_COVER, CoreMetrics.NEW_OVERALL_UNCOVERED_LINES, CoreMetrics.NEW_OVERALL_CONDITIONS_TO_COVER, CoreMetrics.NEW_OVERALL_UNCOVERED_CONDITIONS); - } - - @Override - public void decorate(Resource resource, DecoratorContext context) { - if (shouldDecorate(resource)) { - int maxPeriods = Qualifiers.isView(resource, true) ? 3 : 5; - aggregate(context, CoreMetrics.NEW_LINES_TO_COVER, maxPeriods); - aggregate(context, CoreMetrics.NEW_UNCOVERED_LINES, maxPeriods); - aggregate(context, CoreMetrics.NEW_CONDITIONS_TO_COVER, maxPeriods); - aggregate(context, CoreMetrics.NEW_UNCOVERED_CONDITIONS, maxPeriods); - aggregate(context, CoreMetrics.NEW_IT_LINES_TO_COVER, maxPeriods); - aggregate(context, CoreMetrics.NEW_IT_UNCOVERED_LINES, maxPeriods); - aggregate(context, CoreMetrics.NEW_IT_CONDITIONS_TO_COVER, maxPeriods); - aggregate(context, CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS, maxPeriods); - aggregate(context, CoreMetrics.NEW_OVERALL_LINES_TO_COVER, maxPeriods); - aggregate(context, CoreMetrics.NEW_OVERALL_UNCOVERED_LINES, maxPeriods); - aggregate(context, CoreMetrics.NEW_OVERALL_CONDITIONS_TO_COVER, maxPeriods); - aggregate(context, CoreMetrics.NEW_OVERALL_UNCOVERED_CONDITIONS, maxPeriods); - } - } - - void aggregate(DecoratorContext context, Metric metric, int maxPeriods) { - int[] variations = {0, 0, 0, 0, 0}; - boolean[] hasValues = {false, false, false, false, false}; - for (Measure child : context.getChildrenMeasures(metric)) { - for (int indexPeriod = 1; indexPeriod <= maxPeriods; indexPeriod++) { - Double variation = child.getVariation(indexPeriod); - if (variation != null) { - variations[indexPeriod - 1] = variations[indexPeriod - 1] + variation.intValue(); - hasValues[indexPeriod - 1] = true; - } - } - } - - if (ArrayUtils.contains(hasValues, true)) { - Measure measure = new Measure(metric); - for (int index = 0; index < 5; index++) { - if (hasValues[index]) { - measure.setVariation(index + 1, (double) variations[index]); - } - } - context.saveMeasure(measure); - } - } - - boolean shouldDecorate(Resource resource) { - return Scopes.isHigherThan(resource, Scopes.FILE); - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/compute/NewCoverageFileAnalyzer.java b/sonar-batch/src/main/java/org/sonar/batch/compute/NewCoverageFileAnalyzer.java deleted file mode 100644 index ba36d5b782b..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/compute/NewCoverageFileAnalyzer.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * 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.batch.compute; - -import java.util.List; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Metric; -import org.sonar.batch.components.TimeMachineConfiguration; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.report.ReportPublisher; - -public class NewCoverageFileAnalyzer extends AbstractNewCoverageFileAnalyzer { - - public NewCoverageFileAnalyzer(TimeMachineConfiguration timeMachineConfiguration, ReportPublisher publishReportJob, BatchComponentCache resourceCache) { - super(timeMachineConfiguration, publishReportJob, resourceCache); - } - - NewCoverageFileAnalyzer(List structs, ReportPublisher publishReportJob, BatchComponentCache resourceCache) { - super(structs, publishReportJob, resourceCache); - } - - @Override - public Metric getCoverageLineHitsDataMetric() { - return CoreMetrics.COVERAGE_LINE_HITS_DATA; - } - - @Override - public Metric getConditionsByLineMetric() { - return CoreMetrics.CONDITIONS_BY_LINE; - } - - @Override - public Metric getCoveredConditionsByLineMetric() { - return CoreMetrics.COVERED_CONDITIONS_BY_LINE; - } - - @Override - public Metric getNewLinesToCoverMetric() { - return CoreMetrics.NEW_LINES_TO_COVER; - } - - @Override - public Metric getNewUncoveredLinesMetric() { - return CoreMetrics.NEW_UNCOVERED_LINES; - } - - @Override - public Metric getNewConditionsToCoverMetric() { - return CoreMetrics.NEW_CONDITIONS_TO_COVER; - } - - @Override - public Metric getNewUncoveredConditionsMetric() { - return CoreMetrics.NEW_UNCOVERED_CONDITIONS; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/compute/NewItCoverageFileAnalyzer.java b/sonar-batch/src/main/java/org/sonar/batch/compute/NewItCoverageFileAnalyzer.java deleted file mode 100644 index 48d62305706..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/compute/NewItCoverageFileAnalyzer.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.batch.compute; - -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Metric; -import org.sonar.batch.components.TimeMachineConfiguration; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.report.ReportPublisher; - -public class NewItCoverageFileAnalyzer extends AbstractNewCoverageFileAnalyzer { - - public NewItCoverageFileAnalyzer(TimeMachineConfiguration timeMachineConfiguration, ReportPublisher publishReportJob, BatchComponentCache resourceCache) { - super(timeMachineConfiguration, publishReportJob, resourceCache); - } - - @Override - public Metric getCoverageLineHitsDataMetric() { - return CoreMetrics.IT_COVERAGE_LINE_HITS_DATA; - } - - @Override - public Metric getConditionsByLineMetric() { - return CoreMetrics.IT_CONDITIONS_BY_LINE; - } - - @Override - public Metric getCoveredConditionsByLineMetric() { - return CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE; - } - - @Override - public Metric getNewLinesToCoverMetric() { - return CoreMetrics.NEW_IT_LINES_TO_COVER; - } - - @Override - public Metric getNewUncoveredLinesMetric() { - return CoreMetrics.NEW_IT_UNCOVERED_LINES; - } - - @Override - public Metric getNewConditionsToCoverMetric() { - return CoreMetrics.NEW_IT_CONDITIONS_TO_COVER; - } - - @Override - public Metric getNewUncoveredConditionsMetric() { - return CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS; - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/compute/NewOverallCoverageFileAnalyzer.java b/sonar-batch/src/main/java/org/sonar/batch/compute/NewOverallCoverageFileAnalyzer.java deleted file mode 100644 index 1c2688c6243..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/compute/NewOverallCoverageFileAnalyzer.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.batch.compute; - -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Metric; -import org.sonar.batch.components.TimeMachineConfiguration; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.report.ReportPublisher; - -public class NewOverallCoverageFileAnalyzer extends AbstractNewCoverageFileAnalyzer { - - public NewOverallCoverageFileAnalyzer(TimeMachineConfiguration timeMachineConfiguration, ReportPublisher publishReportJob, BatchComponentCache resourceCache) { - super(timeMachineConfiguration, publishReportJob, resourceCache); - } - - @Override - public Metric getCoverageLineHitsDataMetric() { - return CoreMetrics.OVERALL_COVERAGE_LINE_HITS_DATA; - } - - @Override - public Metric getConditionsByLineMetric() { - return CoreMetrics.OVERALL_CONDITIONS_BY_LINE; - } - - @Override - public Metric getCoveredConditionsByLineMetric() { - return CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE; - } - - @Override - public Metric getNewLinesToCoverMetric() { - return CoreMetrics.NEW_OVERALL_LINES_TO_COVER; - } - - @Override - public Metric getNewUncoveredLinesMetric() { - return CoreMetrics.NEW_OVERALL_UNCOVERED_LINES; - } - - @Override - public Metric getNewConditionsToCoverMetric() { - return CoreMetrics.NEW_OVERALL_CONDITIONS_TO_COVER; - } - - @Override - public Metric getNewUncoveredConditionsMetric() { - return CoreMetrics.NEW_OVERALL_UNCOVERED_CONDITIONS; - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/compute/NewCoverageAggregatorTest.java b/sonar-batch/src/test/java/org/sonar/batch/compute/NewCoverageAggregatorTest.java deleted file mode 100644 index 7951e3a7add..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/compute/NewCoverageAggregatorTest.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * 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.batch.compute; - -import java.util.Arrays; -import java.util.Collections; -import org.junit.Test; -import org.mockito.ArgumentMatcher; -import org.mockito.Matchers; -import org.sonar.api.batch.DecoratorContext; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; - -import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class NewCoverageAggregatorTest { - - @Test - public void shouldNotSaveDataWhenNoMeasures() { - NewCoverageAggregator aggregator = new NewCoverageAggregator(); - DecoratorContext context = mock(DecoratorContext.class); - when(context.getChildrenMeasures(CoreMetrics.NEW_LINES_TO_COVER)).thenReturn(Collections.emptyList()); - - aggregator.aggregate(context, CoreMetrics.NEW_LINES_TO_COVER, 3); - - verify(context, never()).saveMeasure(Matchers.anyObject()); - } - - @Test - public void shouldNotsetZeroWhenNoValueOnPeriod() { - NewCoverageAggregator aggregator = new NewCoverageAggregator(); - DecoratorContext context = mock(DecoratorContext.class); - when(context.getChildrenMeasures(CoreMetrics.NEW_LINES_TO_COVER)).thenReturn(Arrays.asList(newMeasure(null, 3.0, 2.0), newMeasure(null, 13.0, null))); - - aggregator.aggregate(context, CoreMetrics.NEW_LINES_TO_COVER, 3); - - verify(context).saveMeasure(argThat(new ArgumentMatcher() { - @Override - public boolean matches(Object o) { - Measure m = (Measure) o; - return m.getVariation1() == null; - } - })); - } - - @Test - public void shouldSumValues() { - NewCoverageAggregator aggregator = new NewCoverageAggregator(); - DecoratorContext context = mock(DecoratorContext.class); - when(context.getChildrenMeasures(CoreMetrics.NEW_LINES_TO_COVER)).thenReturn(Arrays.asList(newMeasure(null, 3.0, 2.0), newMeasure(null, 13.0, null))); - - aggregator.aggregate(context, CoreMetrics.NEW_LINES_TO_COVER, 3); - - verify(context).saveMeasure(argThat(new ArgumentMatcher() { - @Override - public boolean matches(Object o) { - Measure m = (Measure) o; - return m.getVariation2() == 16.0 && m.getVariation3() == 2.0; - } - })); - } - - private Measure newMeasure(Double variation1, Double variation2, Double variation3) { - return new Measure(CoreMetrics.NEW_LINES_TO_COVER) - .setVariation1(variation1) - .setVariation2(variation2) - .setVariation3(variation3); - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/compute/NewCoverageFileAnalyzerTest.java b/sonar-batch/src/test/java/org/sonar/batch/compute/NewCoverageFileAnalyzerTest.java deleted file mode 100644 index 6deb673fa50..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/compute/NewCoverageFileAnalyzerTest.java +++ /dev/null @@ -1,297 +0,0 @@ -/* - * 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.batch.compute; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.mockito.ArgumentMatcher; -import org.sonar.api.batch.DecoratorContext; -import org.sonar.api.measures.CoreMetrics; -import org.sonar.api.measures.Measure; -import org.sonar.api.measures.Metric; -import org.sonar.api.resources.File; -import org.sonar.api.resources.Resource; -import org.sonar.api.utils.DateUtils; -import org.sonar.batch.index.BatchComponentCache; -import org.sonar.batch.protocol.output.BatchReport; -import org.sonar.batch.protocol.output.BatchReport.Changesets.Changeset; -import org.sonar.batch.protocol.output.BatchReportWriter; -import org.sonar.batch.report.ReportPublisher; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class NewCoverageFileAnalyzerTest { - - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - private DecoratorContext context; - private NewCoverageFileAnalyzer decorator; - private BatchReportWriter writer; - - @Before - public void prepare() throws Exception { - context = mock(DecoratorContext.class); - Resource f = File.create("src/Foo.java").setEffectiveKey("foo:src/Foo.java"); - when(context.getResource()).thenReturn(f); - BatchComponentCache cache = new BatchComponentCache(); - cache.add(f, null); - List structs = Arrays.asList( - new AbstractNewCoverageFileAnalyzer.PeriodStruct(1, newDate("2009-12-25")), - new AbstractNewCoverageFileAnalyzer.PeriodStruct(3, newDate("2011-02-18"))); - ReportPublisher publishReportJob = mock(ReportPublisher.class); - java.io.File reportBaseDir = temp.newFolder(); - when(publishReportJob.getReportDir()).thenReturn(reportBaseDir); - writer = new BatchReportWriter(reportBaseDir); - decorator = new NewCoverageFileAnalyzer(structs, publishReportJob, cache); - - } - - @Test - public void shouldDoNothingIfNoScmData() { - when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)) - .thenReturn(new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "1=10")); - - decorator.doDecorate(context); - verify(context, never()).saveMeasure(any(Measure.class)); - } - - @Test - public void shouldDoNothingIfNoCoverageData() { - writer.writeComponentChangesets(BatchReport.Changesets.newBuilder() - .setComponentRef(1) - .addChangeset(Changeset.newBuilder() - .setDate(DateUtils.parseDateTime("2008-05-18T00:00:00+0000").getTime()) - .build()) - .addChangesetIndexByLine(0) - .build()); - - decorator.doDecorate(context); - - verify(context, never()).saveMeasure(any(Measure.class)); - } - - @Test - public void shouldGetNewLines() { - when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)).thenReturn( - new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "10=2;11=3")); - writer.writeComponentChangesets(BatchReport.Changesets.newBuilder() - .setComponentRef(1) - .addChangeset(Changeset.newBuilder() - .build()) - .addChangeset(Changeset.newBuilder() - .setDate(DateUtils.parseDateTime("2007-01-15T00:00:00+0000").getTime()) - .build()) - .addChangeset(Changeset.newBuilder() - .setDate(DateUtils.parseDateTime("2011-01-01T00:00:00+0000").getTime()) - .build()) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(1) - .addChangesetIndexByLine(2) - .build()); - - decorator.doDecorate(context); - - // line 11 has been updated after date1 (2009-12-25). This line is covered. - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 1, 1.0))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 1, 0.0))); - - // no line have been updated after date3 (2011-02-18) - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 3, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 3, null))); - - // no data on other periods - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 2, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 4, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 5, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 2, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 4, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 5, null))); - } - - @Test - public void shouldGetNewConditions() { - when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)).thenReturn( - new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "10=2;11=3")); - when(context.getMeasure(CoreMetrics.CONDITIONS_BY_LINE)).thenReturn( - new Measure(CoreMetrics.CONDITIONS_BY_LINE, "11=4")); - when(context.getMeasure(CoreMetrics.COVERED_CONDITIONS_BY_LINE)).thenReturn( - new Measure(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "11=1")); - writer.writeComponentChangesets(BatchReport.Changesets.newBuilder() - .setComponentRef(1) - .addChangeset(Changeset.newBuilder() - .build()) - .addChangeset(Changeset.newBuilder() - .setDate(DateUtils.parseDateTime("2007-01-15T00:00:00+0000").getTime()) - .build()) - .addChangeset(Changeset.newBuilder() - .setDate(DateUtils.parseDateTime("2011-01-01T00:00:00+0000").getTime()) - .build()) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(1) - .addChangesetIndexByLine(2) - .build()); - - decorator.doDecorate(context); - - // line 11 has been updated after date1 (2009-12-25). This line has 1 covered condition amongst 4 - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 1, 4.0))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 1, 3.0))); - - // no line have been updated after date3 (2011-02-18) - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 3, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 3, null))); - - // no data on other periods - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 2, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 4, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 5, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 2, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 4, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 5, null))); - } - - @Test - public void shouldNotGetNewConditionsWhenNewLineHasNoConditions() { - when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)).thenReturn( - new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "10=2;11=3")); - when(context.getMeasure(CoreMetrics.CONDITIONS_BY_LINE)).thenReturn( - new Measure(CoreMetrics.CONDITIONS_BY_LINE, "10=1")); - when(context.getMeasure(CoreMetrics.COVERED_CONDITIONS_BY_LINE)).thenReturn( - new Measure(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "10=1")); - writer.writeComponentChangesets(BatchReport.Changesets.newBuilder() - .setComponentRef(1) - .addChangeset(Changeset.newBuilder() - .build()) - .addChangeset(Changeset.newBuilder() - .setDate(DateUtils.parseDateTime("2007-01-15T00:00:00+0000").getTime()) - .build()) - .addChangeset(Changeset.newBuilder() - .setDate(DateUtils.parseDateTime("2011-01-01T00:00:00+0000").getTime()) - .build()) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(1) - .addChangesetIndexByLine(2) - .build()); - - decorator.doDecorate(context); - - // line 11 has been updated after date1 (2009-12-25) but it has no conditions - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 1, 0.0))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 1, 0.0))); - } - - @Test - public void shouldLeaveNullValueWhenNothingHasChanged() { - - when(context.getMeasure(CoreMetrics.COVERAGE_LINE_HITS_DATA)).thenReturn( - new Measure(CoreMetrics.COVERAGE_LINE_HITS_DATA, "2=1;3=1")); - when(context.getMeasure(CoreMetrics.CONDITIONS_BY_LINE)).thenReturn( - new Measure(CoreMetrics.CONDITIONS_BY_LINE, "2=1")); - when(context.getMeasure(CoreMetrics.COVERED_CONDITIONS_BY_LINE)).thenReturn( - new Measure(CoreMetrics.COVERED_CONDITIONS_BY_LINE, "2=1")); - writer.writeComponentChangesets(BatchReport.Changesets.newBuilder() - .setComponentRef(1) - .addChangeset(Changeset.newBuilder() - .setDate(DateUtils.parseDateTime("2008-08-02T13:56:37+0200").getTime()) - .build()) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .addChangesetIndexByLine(0) - .build()); - - decorator.doDecorate(context); - - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 1, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 1, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 1, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 1, null))); - - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_LINES_TO_COVER, 3, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_LINES, 3, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_CONDITIONS_TO_COVER, 3, null))); - verify(context).saveMeasure(argThat(new VariationMatcher(CoreMetrics.NEW_UNCOVERED_CONDITIONS, 3, null))); - } - - static class VariationMatcher extends ArgumentMatcher { - Metric metric; - int index; - Double variation; - - VariationMatcher(Metric metric, int index, Double variation) { - this.metric = metric; - this.index = index; - this.variation = variation; - } - - @Override - public boolean matches(Object o) { - Measure m = (Measure) o; - if (m.getMetric().equals(metric)) { - if ((variation == null && m.getVariation(index) == null) || - (variation != null && variation.equals(m.getVariation(index)))) { - return true; - } - } - return false; - } - } - - private Date newDate(String s) throws ParseException { - return new SimpleDateFormat(DateUtils.DATE_FORMAT).parse(s); - } -} -- 2.39.5