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.List;
030
031public final class LineCoverageDecorator extends AbstractCoverageDecorator {
032
033  @DependsUpon
034  public List<Metric> dependsUponMetrics() {
035    return Arrays.asList(CoreMetrics.UNCOVERED_LINES, CoreMetrics.LINES_TO_COVER, CoreMetrics.NEW_UNCOVERED_LINES,
036        CoreMetrics.NEW_LINES_TO_COVER);
037  }
038
039  @Override
040  protected Metric getGeneratedMetric() {
041    return CoreMetrics.LINE_COVERAGE;
042  }
043
044  @Override
045  protected Long countElements(DecoratorContext context) {
046    return MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.LINES_TO_COVER), 0L);
047  }
048
049  @Override
050  protected long countCoveredElements(DecoratorContext context) {
051    long uncoveredLines = MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.UNCOVERED_LINES), 0L);
052    long lines = MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.LINES_TO_COVER), 0L);
053    return lines - uncoveredLines;
054  }
055
056
057  @Override
058  protected Metric getGeneratedMetricForNewCode() {
059    return CoreMetrics.NEW_LINE_COVERAGE;
060  }
061
062  @Override
063  protected Long countElementsForNewCode(DecoratorContext context, int periodIndex) {
064    return MeasureUtils.getVariationAsLong(context.getMeasure(CoreMetrics.NEW_LINES_TO_COVER), periodIndex);
065  }
066
067  @Override
068  protected long countCoveredElementsForNewCode(DecoratorContext context, int periodIndex) {
069    long uncoveredLines = MeasureUtils.getVariationAsLong(context.getMeasure(CoreMetrics.NEW_UNCOVERED_LINES), periodIndex, 0L);
070    long lines = MeasureUtils.getVariationAsLong(context.getMeasure(CoreMetrics.NEW_LINES_TO_COVER), periodIndex, 0L);
071    return lines - uncoveredLines;
072  }
073}