001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.plugins.core.sensors;
021
022import org.sonar.api.batch.DecoratorContext;
023import org.sonar.api.batch.DependsUpon;
024import org.sonar.api.measures.CoreMetrics;
025import org.sonar.api.measures.MeasureUtils;
026import org.sonar.api.measures.Metric;
027
028import java.util.Arrays;
029import java.util.Collection;
030
031public final class CoverageDecorator extends AbstractCoverageDecorator {
032
033  @DependsUpon
034  public Collection<Metric> usedMetrics() {
035    return Arrays.asList(CoreMetrics.LINES_TO_COVER, CoreMetrics.UNCOVERED_LINES, CoreMetrics.NEW_LINES_TO_COVER,
036        CoreMetrics.NEW_UNCOVERED_LINES, CoreMetrics.CONDITIONS_TO_COVER, CoreMetrics.UNCOVERED_CONDITIONS,
037        CoreMetrics.NEW_CONDITIONS_TO_COVER, CoreMetrics.NEW_UNCOVERED_CONDITIONS);
038  }
039
040  @Override
041  protected Metric getGeneratedMetric() {
042    return CoreMetrics.COVERAGE;
043  }
044
045  @Override
046  protected Long countElements(DecoratorContext context) {
047    long lines = MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.LINES_TO_COVER), 0L);
048    long conditions = MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.CONDITIONS_TO_COVER), 0L);
049    return lines + conditions;
050  }
051
052  @Override
053  protected long countCoveredElements(DecoratorContext context) {
054    long uncoveredLines = MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.UNCOVERED_LINES), 0L);
055    long lines = MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.LINES_TO_COVER), 0L);
056    long uncoveredConditions = MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.UNCOVERED_CONDITIONS), 0L);
057    long conditions = MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.CONDITIONS_TO_COVER), 0L);
058    return lines + conditions - uncoveredConditions - uncoveredLines;
059  }
060
061
062  @Override
063  protected Metric getGeneratedMetricForNewCode() {
064    return CoreMetrics.NEW_COVERAGE;
065  }
066
067  @Override
068  protected Long countElementsForNewCode(DecoratorContext context, int periodIndex) {
069    Long newLinesToCover = MeasureUtils.getVariationAsLong(context.getMeasure(CoreMetrics.NEW_LINES_TO_COVER), periodIndex);
070    if (newLinesToCover != null) {
071      long newConditionsToCover = MeasureUtils.getVariationAsLong(context.getMeasure(CoreMetrics.NEW_CONDITIONS_TO_COVER), periodIndex, 0L);
072      return newLinesToCover + newConditionsToCover;
073    }
074    return null;
075  }
076
077  @Override
078  protected long countCoveredElementsForNewCode(DecoratorContext context, int periodIndex) {
079    long newLines = MeasureUtils.getVariationAsLong(context.getMeasure(CoreMetrics.NEW_LINES_TO_COVER), periodIndex, 0L);
080    long newUncoveredLines = MeasureUtils.getVariationAsLong(context.getMeasure(CoreMetrics.NEW_UNCOVERED_LINES), periodIndex, 0L);
081    long newUncoveredConditions = MeasureUtils.getVariationAsLong(context.getMeasure(CoreMetrics.NEW_UNCOVERED_CONDITIONS), periodIndex, 0L);
082    long newConditions = MeasureUtils.getVariationAsLong(context.getMeasure(CoreMetrics.NEW_CONDITIONS_TO_COVER), periodIndex, 0L);
083    return newLines + newConditions - newUncoveredConditions - newUncoveredLines;
084  }
085}