001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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.Decorator;
023    import org.sonar.api.batch.DecoratorContext;
024    import org.sonar.api.batch.DependedUpon;
025    import org.sonar.api.measures.Measure;
026    import org.sonar.api.measures.Metric;
027    import org.sonar.api.resources.Project;
028    import org.sonar.api.resources.Resource;
029    import org.sonar.api.resources.ResourceUtils;
030    
031    import java.util.Arrays;
032    import java.util.Collection;
033    
034    public abstract class AbstractCoverageDecorator implements Decorator {
035    
036      public boolean shouldExecuteOnProject(Project project) {
037        return project.getAnalysisType().isDynamic(true);
038      }
039    
040      @DependedUpon
041      public Collection<Metric> generatedMetrics() {
042        return Arrays.asList(getGeneratedMetric(), getGeneratedMetricForNewCode());
043      }
044    
045      public void decorate(final Resource resource, final DecoratorContext context) {
046        if (shouldDecorate(resource, context)) {
047          computeMeasure(context);
048          computeMeasureForNewCode(context);
049        }
050      }
051    
052      protected boolean shouldDecorate(final Resource resource, final DecoratorContext context) {
053        return !ResourceUtils.isUnitTestClass(resource);
054      }
055    
056      private void computeMeasure(DecoratorContext context) {
057        if (context.getMeasure(getGeneratedMetric()) == null) {
058          Long elements = countElements(context);
059          if (elements != null && elements > 0L) {
060            Long coveredElements = countCoveredElements(context);
061            context.saveMeasure(getGeneratedMetric(), calculateCoverage(coveredElements, elements));
062          }
063        }
064      }
065    
066      private void computeMeasureForNewCode(DecoratorContext context) {
067        if (context.getMeasure(getGeneratedMetricForNewCode()) == null) {
068          Measure measure = new Measure(getGeneratedMetricForNewCode());
069          boolean hasValue = false;
070          /* TODO remove this magic number */
071          for (int periodIndex = 1; periodIndex <= 5; periodIndex++) {
072            Long elements = countElementsForNewCode(context, periodIndex);
073    
074            if (elements != null && elements > 0L) {
075              long coveredElements = countCoveredElementsForNewCode(context, periodIndex);
076              measure.setVariation(periodIndex, calculateCoverage(coveredElements, elements));
077              hasValue = true;
078            }
079          }
080          if (hasValue) {
081            context.saveMeasure(measure);
082          }
083        }
084      }
085    
086      private double calculateCoverage(final long coveredLines, final long lines) {
087        return (100.0 * coveredLines) / lines;
088      }
089    
090      protected abstract Metric getGeneratedMetric();
091    
092      protected abstract Long countElements(DecoratorContext context);
093    
094      protected abstract long countCoveredElements(DecoratorContext context);
095    
096      protected abstract Metric getGeneratedMetricForNewCode();
097    
098      protected abstract Long countElementsForNewCode(DecoratorContext context, int periodIndex);
099    
100      protected abstract long countCoveredElementsForNewCode(DecoratorContext context, int periodIndex);
101    }