2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.core.sensors;
22 import org.sonar.api.batch.Decorator;
23 import org.sonar.api.batch.DecoratorContext;
24 import org.sonar.api.batch.DependedUpon;
25 import org.sonar.api.measures.Measure;
26 import org.sonar.api.measures.Metric;
27 import org.sonar.api.resources.Project;
28 import org.sonar.api.resources.Resource;
29 import org.sonar.api.resources.ResourceUtils;
31 import java.util.Arrays;
32 import java.util.Collection;
34 public abstract class AbstractCoverageDecorator implements Decorator {
36 public boolean shouldExecuteOnProject(Project project) {
37 return project.getAnalysisType().isDynamic(true);
41 public Collection<Metric> generatedMetrics() {
42 return Arrays.asList(getGeneratedMetric(), getGeneratedMetricForNewCode());
45 public void decorate(final Resource resource, final DecoratorContext context) {
46 if (shouldDecorate(resource, context)) {
47 computeMeasure(context);
48 computeMeasureForNewCode(context);
52 protected boolean shouldDecorate(final Resource resource, final DecoratorContext context) {
53 return context.getMeasure(getGeneratedMetric()) == null && !ResourceUtils.isUnitTestClass(resource);
56 private void computeMeasure(DecoratorContext context) {
57 Long elements = countElements(context);
58 if (elements != null && elements > 0L) {
59 Long coveredElements = countCoveredElements(context);
60 context.saveMeasure(getGeneratedMetric(), calculateCoverage(coveredElements, elements));
64 private void computeMeasureForNewCode(DecoratorContext context) {
65 Measure measure = new Measure(getGeneratedMetricForNewCode());
66 boolean hasValue = false;
67 /* TODO remove this magic number */
68 for (int periodIndex = 1; periodIndex <= 5; periodIndex++) {
69 Long elements = countElementsForNewCode(context, periodIndex);
71 if (elements != null && elements > 0L) {
72 long coveredElements = countCoveredElementsForNewCode(context, periodIndex);
73 measure.setVariation(periodIndex, calculateCoverage(coveredElements, elements));
78 context.saveMeasure(measure);
82 private double calculateCoverage(final long coveredLines, final long lines) {
83 return (100.0 * coveredLines) / lines;
86 protected abstract Metric getGeneratedMetric();
88 protected abstract Long countElements(DecoratorContext context);
90 protected abstract long countCoveredElements(DecoratorContext context);
92 protected abstract Metric getGeneratedMetricForNewCode();
94 protected abstract Long countElementsForNewCode(DecoratorContext context, int periodIndex);
96 protected abstract long countCoveredElementsForNewCode(DecoratorContext context, int periodIndex);