]> source.dussan.org Git - sonarqube.git/blob
e236c6298ed34fb305573796f6ef43d2dffb8a18
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.plugins.core.sensors;
21
22 import com.google.common.annotations.VisibleForTesting;
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableList.Builder;
25 import com.google.common.collect.ImmutableSet;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.sonar.api.CoreProperties;
29 import org.sonar.api.config.Settings;
30 import org.sonar.api.measures.Measure;
31 import org.sonar.api.measures.Metric;
32 import org.sonar.api.resources.Resource;
33 import org.sonar.api.utils.WildcardPattern;
34 import org.sonar.core.measure.MeasurementFilter;
35
36 import java.util.Collection;
37 import java.util.Iterator;
38
39 public class CoverageMeasurementFilter implements MeasurementFilter {
40
41   private static final Logger LOG = LoggerFactory.getLogger(CoverageMeasurementFilter.class);
42
43   private final Settings settings;
44   private final ImmutableSet<Metric> coverageMetrics;
45   private Collection<WildcardPattern> resourcePatterns;
46
47   public CoverageMeasurementFilter(Settings settings,
48     CoverageDecorator coverageDecorator,
49     LineCoverageDecorator lineCoverageDecorator,
50     BranchCoverageDecorator branchCoverageDecorator) {
51     this.settings = settings;
52     this.coverageMetrics = ImmutableSet.<Metric>builder()
53       .addAll(coverageDecorator.generatedMetrics())
54       .addAll(coverageDecorator.usedMetrics())
55       .addAll(lineCoverageDecorator.generatedMetrics())
56       .addAll(lineCoverageDecorator.dependsUponMetrics())
57       .addAll(branchCoverageDecorator.generatedMetrics())
58       .addAll(branchCoverageDecorator.dependsUponMetrics())
59       .build();
60
61     initPatterns();
62   }
63
64   @Override
65   public boolean accept(Resource resource, Measure measure) {
66     if (isCoverageMetric(measure.getMetric())) {
67       return !hasMatchingPattern(resource);
68     } else {
69       return true;
70     }
71   }
72
73   private boolean isCoverageMetric(Metric metric) {
74     return this.coverageMetrics.contains(metric);
75   }
76
77   private boolean hasMatchingPattern(Resource resource) {
78     boolean found = false;
79     Iterator<WildcardPattern> iterator = resourcePatterns.iterator();
80     while (!found && iterator.hasNext()) {
81       found = resource.matchFilePattern(iterator.next().toString());
82     }
83     return found;
84   }
85
86   @VisibleForTesting
87   final void initPatterns() {
88     Builder<WildcardPattern> builder = ImmutableList.builder();
89     for (String pattern : settings.getStringArray(CoreProperties.PROJECT_COVERAGE_EXCLUSIONS_PROPERTY)) {
90       builder.add(WildcardPattern.create(pattern));
91     }
92     resourcePatterns = builder.build();
93     log("Excluded sources for coverage: ", resourcePatterns);
94   }
95
96   private void log(String title, Collection<WildcardPattern> patterns) {
97     if (!patterns.isEmpty()) {
98       LOG.info(title);
99       for (WildcardPattern pattern : patterns) {
100         LOG.info("  " + pattern);
101       }
102     }
103   }
104 }