2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 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.sensors;
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;
36 import java.util.Collection;
37 import java.util.Iterator;
39 public class CoverageMeasurementFilter implements MeasurementFilter {
41 private static final Logger LOG = LoggerFactory.getLogger(CoverageMeasurementFilter.class);
43 private final Settings settings;
44 private final ImmutableSet<Metric> coverageMetrics;
45 private Collection<WildcardPattern> resourcePatterns;
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())
65 public boolean accept(Resource resource, Measure measure) {
66 if (isCoverageMetric(measure.getMetric())) {
67 return !hasMatchingPattern(resource);
73 private boolean isCoverageMetric(Metric metric) {
74 return this.coverageMetrics.contains(metric);
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());
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));
92 resourcePatterns = builder.build();
93 log("Excluded sources for coverage: ", resourcePatterns);
96 private void log(String title, Collection<WildcardPattern> patterns) {
97 if (!patterns.isEmpty()) {
99 for (WildcardPattern pattern : patterns) {
100 LOG.info(" " + pattern);