2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 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.timemachine;
22 import org.apache.commons.lang.ArrayUtils;
23 import org.sonar.api.batch.Decorator;
24 import org.sonar.api.batch.DecoratorBarriers;
25 import org.sonar.api.batch.DecoratorContext;
26 import org.sonar.api.batch.DependedUpon;
27 import org.sonar.api.measures.CoreMetrics;
28 import org.sonar.api.measures.Measure;
29 import org.sonar.api.measures.Metric;
30 import org.sonar.api.resources.Project;
31 import org.sonar.api.resources.Qualifiers;
32 import org.sonar.api.resources.Resource;
33 import org.sonar.api.resources.Scopes;
35 import java.util.Arrays;
36 import java.util.List;
38 @DependedUpon(DecoratorBarriers.END_OF_TIME_MACHINE)
39 public final class NewCoverageAggregator implements Decorator {
41 public boolean shouldExecuteOnProject(Project project) {
46 public List<Metric> generatesNewCoverageMetrics() {
48 CoreMetrics.NEW_LINES_TO_COVER, CoreMetrics.NEW_UNCOVERED_LINES, CoreMetrics.NEW_CONDITIONS_TO_COVER, CoreMetrics.NEW_UNCOVERED_CONDITIONS,
49 CoreMetrics.NEW_IT_LINES_TO_COVER, CoreMetrics.NEW_IT_UNCOVERED_LINES, CoreMetrics.NEW_IT_CONDITIONS_TO_COVER, CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS);
52 public void decorate(Resource resource, DecoratorContext context) {
53 if (shouldDecorate(resource)) {
54 int maxPeriods = (Qualifiers.isView(resource, true) ? 3 : 5);
55 aggregate(context, CoreMetrics.NEW_LINES_TO_COVER, maxPeriods);
56 aggregate(context, CoreMetrics.NEW_UNCOVERED_LINES, maxPeriods);
57 aggregate(context, CoreMetrics.NEW_CONDITIONS_TO_COVER, maxPeriods);
58 aggregate(context, CoreMetrics.NEW_UNCOVERED_CONDITIONS, maxPeriods);
59 aggregate(context, CoreMetrics.NEW_IT_LINES_TO_COVER, maxPeriods);
60 aggregate(context, CoreMetrics.NEW_IT_UNCOVERED_LINES, maxPeriods);
61 aggregate(context, CoreMetrics.NEW_IT_CONDITIONS_TO_COVER, maxPeriods);
62 aggregate(context, CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS, maxPeriods);
66 void aggregate(DecoratorContext context, Metric metric, int maxPeriods) {
67 int[] variations = {0,0,0,0,0};
68 boolean[] hasValues = {false,false,false,false,false};
69 for (Measure child : context.getChildrenMeasures(metric)) {
70 for (int indexPeriod=1 ; indexPeriod<=maxPeriods ; indexPeriod++) {
71 Double variation = child.getVariation(indexPeriod);
72 if (variation!=null) {
73 variations[indexPeriod-1]=variations[indexPeriod-1] + variation.intValue();
74 hasValues[indexPeriod-1]=true;
79 if (ArrayUtils.contains(hasValues, true)) {
80 Measure measure = new Measure(metric);
81 for (int index=0 ; index<5 ; index++) {
82 if (hasValues[index]) {
83 measure.setVariation(index+1, (double)variations[index]);
86 context.saveMeasure(measure);
90 boolean shouldDecorate(Resource resource) {
91 return Scopes.isHigherThan(resource, Scopes.FILE);