2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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 License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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,
50 CoreMetrics.NEW_OVERALL_LINES_TO_COVER, CoreMetrics.NEW_OVERALL_UNCOVERED_LINES, CoreMetrics.NEW_OVERALL_CONDITIONS_TO_COVER, CoreMetrics.NEW_OVERALL_UNCOVERED_CONDITIONS);
53 public void decorate(Resource resource, DecoratorContext context) {
54 if (shouldDecorate(resource)) {
55 int maxPeriods = (Qualifiers.isView(resource, true) ? 3 : 5);
56 aggregate(context, CoreMetrics.NEW_LINES_TO_COVER, maxPeriods);
57 aggregate(context, CoreMetrics.NEW_UNCOVERED_LINES, maxPeriods);
58 aggregate(context, CoreMetrics.NEW_CONDITIONS_TO_COVER, maxPeriods);
59 aggregate(context, CoreMetrics.NEW_UNCOVERED_CONDITIONS, maxPeriods);
60 aggregate(context, CoreMetrics.NEW_IT_LINES_TO_COVER, maxPeriods);
61 aggregate(context, CoreMetrics.NEW_IT_UNCOVERED_LINES, maxPeriods);
62 aggregate(context, CoreMetrics.NEW_IT_CONDITIONS_TO_COVER, maxPeriods);
63 aggregate(context, CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS, maxPeriods);
64 aggregate(context, CoreMetrics.NEW_OVERALL_LINES_TO_COVER, maxPeriods);
65 aggregate(context, CoreMetrics.NEW_OVERALL_UNCOVERED_LINES, maxPeriods);
66 aggregate(context, CoreMetrics.NEW_OVERALL_CONDITIONS_TO_COVER, maxPeriods);
67 aggregate(context, CoreMetrics.NEW_OVERALL_UNCOVERED_CONDITIONS, maxPeriods);
71 void aggregate(DecoratorContext context, Metric metric, int maxPeriods) {
72 int[] variations = {0,0,0,0,0};
73 boolean[] hasValues = {false,false,false,false,false};
74 for (Measure child : context.getChildrenMeasures(metric)) {
75 for (int indexPeriod=1 ; indexPeriod<=maxPeriods ; indexPeriod++) {
76 Double variation = child.getVariation(indexPeriod);
77 if (variation!=null) {
78 variations[indexPeriod-1]=variations[indexPeriod-1] + variation.intValue();
79 hasValues[indexPeriod-1]=true;
84 if (ArrayUtils.contains(hasValues, true)) {
85 Measure measure = new Measure(metric);
86 for (int index=0 ; index<5 ; index++) {
87 if (hasValues[index]) {
88 measure.setVariation(index+1, (double)variations[index]);
91 context.saveMeasure(measure);
95 boolean shouldDecorate(Resource resource) {
96 return Scopes.isHigherThan(resource, Scopes.FILE);