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.timemachine;
22 import org.apache.commons.lang.ArrayUtils;
23 import org.sonar.api.batch.Decorator;
24 import org.sonar.api.batch.DecoratorContext;
25 import org.sonar.api.batch.DependedUpon;
26 import org.sonar.api.measures.CoreMetrics;
27 import org.sonar.api.measures.Measure;
28 import org.sonar.api.measures.Metric;
29 import org.sonar.api.resources.Project;
30 import org.sonar.api.resources.Qualifiers;
31 import org.sonar.api.resources.Resource;
32 import org.sonar.api.resources.Scopes;
34 import java.util.Arrays;
35 import java.util.List;
37 public final class NewCoverageAggregator implements Decorator {
39 public boolean shouldExecuteOnProject(Project project) {
44 public List<Metric> generatesNewCoverageMetrics() {
45 return Arrays.asList(CoreMetrics.NEW_LINES_TO_COVER, CoreMetrics.NEW_UNCOVERED_LINES,
46 CoreMetrics.NEW_CONDITIONS_TO_COVER, CoreMetrics.NEW_UNCOVERED_CONDITIONS);
49 public void decorate(Resource resource, DecoratorContext context) {
50 if (shouldDecorate(resource)) {
51 int maxPeriods = (Qualifiers.isView(resource, true) ? 3 : 5);
52 aggregate(context, CoreMetrics.NEW_LINES_TO_COVER, maxPeriods);
53 aggregate(context, CoreMetrics.NEW_UNCOVERED_LINES, maxPeriods);
54 aggregate(context, CoreMetrics.NEW_CONDITIONS_TO_COVER, maxPeriods);
55 aggregate(context, CoreMetrics.NEW_UNCOVERED_CONDITIONS, maxPeriods);
59 private void aggregate(DecoratorContext context, Metric metric, int maxPeriods) {
60 int[] variations = {0,0,0,0,0};
61 boolean[] hasValues = {false,false,false,false,false};
62 for (Measure child : context.getChildrenMeasures(metric)) {
63 for (int indexPeriod=1 ; indexPeriod<=maxPeriods ; indexPeriod++) {
64 Double variation = child.getVariation(indexPeriod);
65 if (variation!=null) {
66 variations[indexPeriod-1]=variations[indexPeriod-1] + variation.intValue();
67 hasValues[indexPeriod-1]=true;
72 if (ArrayUtils.contains(hasValues, true)) {
73 Measure measure = new Measure(metric);
74 for (int index=0 ; index<5 ; index++) {
75 if (hasValues[index]) {
76 measure.setVariation(index+1, (double)variations[index]);
79 context.saveMeasure(measure);
83 private boolean shouldDecorate(Resource resource) {
84 return Scopes.isHigherThan(resource, Scopes.FILE);