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     */
020    package org.sonar.plugins.core.sensors;
021    
022    import org.sonar.api.batch.DecoratorContext;
023    import org.sonar.api.batch.DependsUpon;
024    import org.sonar.api.measures.CoreMetrics;
025    import org.sonar.api.measures.MeasureUtils;
026    import org.sonar.api.measures.Metric;
027    
028    import java.util.Arrays;
029    import java.util.List;
030    
031    public final class ItBranchCoverageDecorator extends AbstractCoverageDecorator {
032    
033      @DependsUpon
034      public List<Metric> dependsUponMetrics() {
035        return Arrays.asList(CoreMetrics.IT_UNCOVERED_CONDITIONS, CoreMetrics.IT_CONDITIONS_TO_COVER,
036            CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS, CoreMetrics.NEW_IT_CONDITIONS_TO_COVER);
037      }
038    
039      @Override
040      protected Metric getGeneratedMetric() {
041        return CoreMetrics.IT_BRANCH_COVERAGE;
042      }
043    
044      @Override
045      protected Long countElements(DecoratorContext context) {
046        return MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.IT_CONDITIONS_TO_COVER), 0L);
047      }
048    
049      @Override
050      protected long countCoveredElements(DecoratorContext context) {
051        long uncoveredConditions = MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.IT_UNCOVERED_CONDITIONS), 0L);
052        long conditions = MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.IT_CONDITIONS_TO_COVER), 0L);
053        return conditions - uncoveredConditions;
054      }
055    
056      @Override
057      protected Metric getGeneratedMetricForNewCode() {
058        return CoreMetrics.NEW_IT_BRANCH_COVERAGE;
059      }
060    
061      @Override
062      protected Long countElementsForNewCode(DecoratorContext context, int periodIndex) {
063        return MeasureUtils.getVariationAsLong(context.getMeasure(CoreMetrics.NEW_IT_CONDITIONS_TO_COVER), periodIndex);
064      }
065    
066      @Override
067      protected long countCoveredElementsForNewCode(DecoratorContext context, int periodIndex) {
068        long uncoveredConditions = MeasureUtils.getVariationAsLong(context.getMeasure(CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS), periodIndex, 0L);
069        long conditions = MeasureUtils.getVariationAsLong(context.getMeasure(CoreMetrics.NEW_IT_CONDITIONS_TO_COVER), periodIndex, 0L);
070        return conditions - uncoveredConditions;
071      }
072    }