diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-08-27 11:45:48 +0200 |
---|---|---|
committer | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2015-08-29 15:58:42 +0200 |
commit | a85ef83554c61283f8ad965d092c6d415950951b (patch) | |
tree | ad7b1716164f70ae1d16066b9089aebb5611de47 /server | |
parent | 2a5ac824b74b0ebca1b6ebcb3267b9c12c67d5d0 (diff) | |
download | sonarqube-a85ef83554c61283f8ad965d092c6d415950951b.tar.gz sonarqube-a85ef83554c61283f8ad965d092c6d415950951b.zip |
change NewCoverageVariationSumFormula into VariationSumFormula
make it more generic to be used for other NEW_* metrics
Diffstat (limited to 'server')
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/computation/formula/VariationSumFormula.java (renamed from server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/NewCoverageVariationSumFormula.java) | 32 | ||||
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/CoverageUtils.java | 16 | ||||
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/LinesAndConditionsWithUncoveredVariationCounter.java | 5 | ||||
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/computation/period/PeriodPredicates.java | 47 | ||||
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputeQProfileMeasureStep.java | 6 | ||||
-rw-r--r-- | server/sonar-server/src/main/java/org/sonar/server/computation/step/NewCoverageMeasuresStep.java | 25 | ||||
-rw-r--r-- | server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodPredicatesTest.java | 45 |
7 files changed, 130 insertions, 46 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/NewCoverageVariationSumFormula.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/VariationSumFormula.java index 6bcf790a0c8..cf066b754a6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/NewCoverageVariationSumFormula.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/VariationSumFormula.java @@ -17,36 +17,36 @@ * 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.formula.coverage; +package org.sonar.server.computation.formula; import com.google.common.base.Optional; +import com.google.common.base.Predicate; import org.sonar.server.computation.component.CrawlerDepthLimit; -import org.sonar.server.computation.formula.Counter; -import org.sonar.server.computation.formula.CounterInitializationContext; -import org.sonar.server.computation.formula.CreateMeasureContext; -import org.sonar.server.computation.formula.Formula; import org.sonar.server.computation.formula.counter.DoubleVariationValue; import org.sonar.server.computation.measure.Measure; import org.sonar.server.computation.measure.MeasureVariations; import org.sonar.server.computation.period.Period; +import static com.google.common.collect.FluentIterable.from; import static java.util.Objects.requireNonNull; -import static org.sonar.server.computation.formula.coverage.CoverageUtils.supportedPeriods; import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; /** - * A Formula which aggregates a new coverage measure by simply making the sums of its variations. + * A Formula which aggregates variations of a specific metric by simply making the sums of its variations. It supports + * make the sum of only specific periods. */ -public class NewCoverageVariationSumFormula implements Formula<NewCoverageVariationSumFormula.VariationSumCounter> { +public class VariationSumFormula implements Formula<VariationSumFormula.VariationSumCounter> { private final String metricKey; + private final Predicate<Period> supportedPeriods; - public NewCoverageVariationSumFormula(String metricKey) { + public VariationSumFormula(String metricKey, Predicate<Period> supportedPeriods) { + this.supportedPeriods = supportedPeriods; this.metricKey = requireNonNull(metricKey, "Metric key cannot be null"); } @Override public VariationSumCounter createNewCounter() { - return new VariationSumCounter(metricKey); + return new VariationSumCounter(metricKey, supportedPeriods); } @Override @@ -61,9 +61,9 @@ public class NewCoverageVariationSumFormula implements Formula<NewCoverageVariat return Optional.of(newMeasureBuilder().setVariations(variations.build()).createNoValue()); } - private static MeasureVariations.Builder createAndPopulateBuilder(DoubleVariationValue.Array array, CreateMeasureContext context) { + private MeasureVariations.Builder createAndPopulateBuilder(DoubleVariationValue.Array array, CreateMeasureContext context) { MeasureVariations.Builder builder = MeasureVariations.newMeasureVariationsBuilder(); - for (Period period : supportedPeriods(context)) { + for (Period period : from(context.getPeriods()).filter(supportedPeriods)) { DoubleVariationValue elements = array.get(period); if (elements.isSet()) { builder.setVariation(period, elements.getValue()); @@ -74,15 +74,17 @@ public class NewCoverageVariationSumFormula implements Formula<NewCoverageVariat @Override public String[] getOutputMetricKeys() { - return new String[] { metricKey }; + return new String[] {metricKey}; } public static final class VariationSumCounter implements Counter<VariationSumCounter> { private final DoubleVariationValue.Array array = DoubleVariationValue.newArray(); private final String metricKey; + private final Predicate<Period> supportedPeriods; - private VariationSumCounter(String metricKey) { + private VariationSumCounter(String metricKey, Predicate<Period> supportedPeriods) { this.metricKey = metricKey; + this.supportedPeriods = supportedPeriods; } @Override @@ -97,7 +99,7 @@ public class NewCoverageVariationSumFormula implements Formula<NewCoverageVariat return; } MeasureVariations variations = measure.get().getVariations(); - for (Period period : supportedPeriods(context)) { + for (Period period : from(context.getPeriods()).filter(supportedPeriods)) { if (variations.hasVariation(period.getIndex())) { double variation = variations.getVariation(period.getIndex()); if (variation > 0) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/CoverageUtils.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/CoverageUtils.java index 58e4ce1efa3..fe84439c83d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/CoverageUtils.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/CoverageUtils.java @@ -20,9 +20,6 @@ package org.sonar.server.computation.formula.coverage; import com.google.common.base.Optional; -import com.google.common.base.Predicate; -import java.util.List; -import javax.annotation.Nonnull; import org.sonar.server.computation.component.Component; import org.sonar.server.computation.formula.CounterInitializationContext; import org.sonar.server.computation.formula.CreateMeasureContext; @@ -32,6 +29,7 @@ import org.sonar.server.computation.period.Period; import static com.google.common.collect.FluentIterable.from; import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; +import static org.sonar.server.computation.period.PeriodPredicates.viewsRestrictedPeriods; public final class CoverageUtils { private static final Measure DEFAULT_MEASURE = newMeasureBuilder().create(0L); @@ -87,19 +85,11 @@ public final class CoverageUtils { return supportedPeriods(context.getLeaf().getType(), context.getPeriods()); } - private static Iterable<Period> supportedPeriods(Component.Type type, List<Period> periods) { + private static Iterable<Period> supportedPeriods(Component.Type type, Iterable<Period> periods) { if (type.isReportType()) { return periods; } - return from(periods).filter(ViewsSupportedPeriods.INSTANCE); + return from(periods).filter(viewsRestrictedPeriods()); } - private enum ViewsSupportedPeriods implements Predicate<Period> { - INSTANCE; - - @Override - public boolean apply(@Nonnull Period input) { - return input.getIndex() < 4; - } - } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/LinesAndConditionsWithUncoveredVariationCounter.java b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/LinesAndConditionsWithUncoveredVariationCounter.java index 96fc9422021..946ddaf7dd9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/LinesAndConditionsWithUncoveredVariationCounter.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/formula/coverage/LinesAndConditionsWithUncoveredVariationCounter.java @@ -25,6 +25,7 @@ import org.sonar.server.computation.measure.Measure; import org.sonar.server.computation.measure.MeasureVariations; import org.sonar.server.computation.period.Period; +import static org.sonar.server.computation.formula.coverage.CoverageUtils.getLongVariation; import static org.sonar.server.computation.formula.coverage.CoverageUtils.supportedPeriods; public final class LinesAndConditionsWithUncoveredVariationCounter extends ElementsAndCoveredElementsVariationCounter { @@ -49,11 +50,11 @@ public final class LinesAndConditionsWithUncoveredVariationCounter extends Eleme if (!newLines.hasVariation(period.getIndex())) { continue; } - long elements = (long) newLines.getVariation(period.getIndex()) + CoverageUtils.getLongVariation(newConditions, period); + long elements = (long) newLines.getVariation(period.getIndex()) + getLongVariation(newConditions, period); this.elements.increment(period, elements); coveredElements.increment( period, - elements - CoverageUtils.getLongVariation(uncoveredConditions, period) - CoverageUtils.getLongVariation(uncoveredLines, period)); + elements - getLongVariation(uncoveredConditions, period) - getLongVariation(uncoveredLines, period)); } } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/period/PeriodPredicates.java b/server/sonar-server/src/main/java/org/sonar/server/computation/period/PeriodPredicates.java new file mode 100644 index 00000000000..d5806e44901 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/period/PeriodPredicates.java @@ -0,0 +1,47 @@ +/* + * 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.period; + +import com.google.common.base.Predicate; +import javax.annotation.Nonnull; + +public final class PeriodPredicates { + private PeriodPredicates() { + // prevents instantiation + } + + /** + * Since Periods 4 and 5 can be customized per project and/or per view/subview, aggregating variation on these periods + * for NEW_* metrics will only generate garbage data which will make no sense. These Periods should be ignored + * when processing views/subviews. + */ + public static Predicate<Period> viewsRestrictedPeriods() { + return ViewsSupportedPeriods.INSTANCE; + } + + private enum ViewsSupportedPeriods implements Predicate<Period> { + INSTANCE; + + @Override + public boolean apply(@Nonnull Period input) { + return input.getIndex() < 4; + } + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputeQProfileMeasureStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputeQProfileMeasureStep.java index e92741f0653..12806b26c39 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputeQProfileMeasureStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputeQProfileMeasureStep.java @@ -57,15 +57,15 @@ public class ComputeQProfileMeasureStep implements ComputationStep { @Override public void execute() { Metric qProfilesMetric = metricRepository.getByKey(CoreMetrics.QUALITY_PROFILES_KEY); - new PathAwareCrawler<>(new NewCoverageAggregationComponentCrawler(qProfilesMetric)) + new PathAwareCrawler<>(new NewCoverageAggregationComponentVisitor(qProfilesMetric)) .visit(treeRootHolder.getRoot()); } - private class NewCoverageAggregationComponentCrawler extends PathAwareVisitorAdapter<QProfiles> { + private class NewCoverageAggregationComponentVisitor extends PathAwareVisitorAdapter<QProfiles> { private final Metric qProfilesMetric; - public NewCoverageAggregationComponentCrawler(Metric qProfilesMetric) { + public NewCoverageAggregationComponentVisitor(Metric qProfilesMetric) { super(CrawlerDepthLimit.MODULE, POST_ORDER, new SimpleStackElementFactory<QProfiles>() { @Override public QProfiles createForAny(Component component) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/NewCoverageMeasuresStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/NewCoverageMeasuresStep.java index d503f5077d5..eee963c6af0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/NewCoverageMeasuresStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/NewCoverageMeasuresStep.java @@ -41,7 +41,7 @@ import org.sonar.server.computation.formula.CounterInitializationContext; import org.sonar.server.computation.formula.CreateMeasureContext; import org.sonar.server.computation.formula.Formula; import org.sonar.server.computation.formula.FormulaExecutorComponentVisitor; -import org.sonar.server.computation.formula.coverage.NewCoverageVariationSumFormula; +import org.sonar.server.computation.formula.VariationSumFormula; import org.sonar.server.computation.formula.counter.IntVariationValue; import org.sonar.server.computation.formula.coverage.LinesAndConditionsWithUncoveredMetricKeys; import org.sonar.server.computation.formula.coverage.LinesAndConditionsWithUncoveredVariationFormula; @@ -56,6 +56,7 @@ import org.sonar.server.computation.period.Period; import org.sonar.server.computation.period.PeriodsHolder; import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; +import static org.sonar.server.computation.period.PeriodPredicates.viewsRestrictedPeriods; /** * Computes measures related to the New Coverage. These measures do not have values, only variations. @@ -63,8 +64,7 @@ import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; public class NewCoverageMeasuresStep implements ComputationStep { private static final List<Formula> FORMULAS = ImmutableList.<Formula>of( // UT coverage - new NewCoverageFormula() - , + new NewCoverageFormula(), new NewBranchCoverageFormula(), new NewLineCoverageFormula(), // IT File coverage @@ -74,8 +74,7 @@ public class NewCoverageMeasuresStep implements ComputationStep { // Overall coverage new NewOverallCodeCoverageFormula(), new NewOverallBranchCoverageFormula(), - new NewOverallLineCoverageFormula() - ); + new NewOverallLineCoverageFormula()); private final TreeRootHolder treeRootHolder; private final PeriodsHolder periodsHolder; @@ -132,7 +131,7 @@ public class NewCoverageMeasuresStep implements ComputationStep { private static final NewCoverageOutputMetricKeys OUTPUT_METRIC_KEYS = new NewCoverageOutputMetricKeys( CoreMetrics.NEW_LINES_TO_COVER_KEY, CoreMetrics.NEW_UNCOVERED_LINES_KEY, CoreMetrics.NEW_CONDITIONS_TO_COVER_KEY, CoreMetrics.NEW_UNCOVERED_CONDITIONS_KEY); - private static final Iterable<Formula<?>> VIEWS_FORMULAS = intSumFormulas(OUTPUT_METRIC_KEYS); + private static final Iterable<Formula<?>> VIEWS_FORMULAS = variationSumFormulas(OUTPUT_METRIC_KEYS); private NewLinesAndConditionsCoverageFormula(BatchReportReader batchReportReader) { super(batchReportReader, @@ -180,7 +179,7 @@ public class NewCoverageMeasuresStep implements ComputationStep { private static final NewCoverageOutputMetricKeys OUTPUT_METRIC_KEYS = new NewCoverageOutputMetricKeys( CoreMetrics.NEW_IT_LINES_TO_COVER_KEY, CoreMetrics.NEW_IT_UNCOVERED_LINES_KEY, CoreMetrics.NEW_IT_CONDITIONS_TO_COVER_KEY, CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS_KEY); - private static final Iterable<Formula<?>> VIEWS_FORMULAS = intSumFormulas(OUTPUT_METRIC_KEYS); + private static final Iterable<Formula<?>> VIEWS_FORMULAS = variationSumFormulas(OUTPUT_METRIC_KEYS); private NewItLinesAndConditionsCoverageFormula(BatchReportReader batchReportReader) { super(batchReportReader, @@ -229,7 +228,7 @@ public class NewCoverageMeasuresStep implements ComputationStep { private static final NewCoverageOutputMetricKeys OUTPUT_METRIC_KEYS = new NewCoverageOutputMetricKeys( CoreMetrics.NEW_OVERALL_LINES_TO_COVER_KEY, CoreMetrics.NEW_OVERALL_UNCOVERED_LINES_KEY, CoreMetrics.NEW_OVERALL_CONDITIONS_TO_COVER_KEY, CoreMetrics.NEW_OVERALL_UNCOVERED_CONDITIONS_KEY); - private static final Iterable<Formula<?>> VIEWS_FORMULAS = intSumFormulas(OUTPUT_METRIC_KEYS); + private static final Iterable<Formula<?>> VIEWS_FORMULAS = variationSumFormulas(OUTPUT_METRIC_KEYS); private NewOverallLinesAndConditionsCoverageFormula(BatchReportReader batchReportReader) { super(batchReportReader, @@ -278,12 +277,12 @@ public class NewCoverageMeasuresStep implements ComputationStep { * Creates a List of {@link org.sonar.server.computation.formula.SumFormula.IntSumFormula} for each * metric key of the specified {@link NewCoverageOutputMetricKeys} instance. */ - private static Iterable<Formula<?>> intSumFormulas(NewCoverageOutputMetricKeys outputMetricKeys) { + private static Iterable<Formula<?>> variationSumFormulas(NewCoverageOutputMetricKeys outputMetricKeys) { return ImmutableList.<Formula<?>>of( - new NewCoverageVariationSumFormula(outputMetricKeys.getNewLinesToCover()), - new NewCoverageVariationSumFormula(outputMetricKeys.getNewUncoveredLines()), - new NewCoverageVariationSumFormula(outputMetricKeys.getNewConditionsToCover()), - new NewCoverageVariationSumFormula(outputMetricKeys.getNewUncoveredConditions())); + new VariationSumFormula(outputMetricKeys.getNewLinesToCover(), viewsRestrictedPeriods()), + new VariationSumFormula(outputMetricKeys.getNewUncoveredLines(), viewsRestrictedPeriods()), + new VariationSumFormula(outputMetricKeys.getNewConditionsToCover(), viewsRestrictedPeriods()), + new VariationSumFormula(outputMetricKeys.getNewUncoveredConditions(), viewsRestrictedPeriods())); } public static class NewLinesAndConditionsFormula implements Formula<NewCoverageCounter> { diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodPredicatesTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodPredicatesTest.java new file mode 100644 index 00000000000..103494f1f71 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/period/PeriodPredicatesTest.java @@ -0,0 +1,45 @@ +/* + * 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.period; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class PeriodPredicatesTest { + + @Test(expected = NullPointerException.class) + public void viewsRestrictedPeriods_throws_NPE_if_Period_is_null() { + PeriodPredicates.viewsRestrictedPeriods().apply(null); + } + + @Test + public void viewsRestrictedPeriods() { + assertThat(PeriodPredicates.viewsRestrictedPeriods().apply(createPeriod(1))).isTrue(); + assertThat(PeriodPredicates.viewsRestrictedPeriods().apply(createPeriod(2))).isTrue(); + assertThat(PeriodPredicates.viewsRestrictedPeriods().apply(createPeriod(3))).isTrue(); + assertThat(PeriodPredicates.viewsRestrictedPeriods().apply(createPeriod(4))).isFalse(); + assertThat(PeriodPredicates.viewsRestrictedPeriods().apply(createPeriod(5))).isFalse(); + } + + private Period createPeriod(int index) { + return new Period(index, "don't care", null, 1l, 1l); + } +} |