]> source.dussan.org Git - sonarqube.git/blob
eeda60396a926e582ec89132dd22c26c2052eb0d
[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.sonar.api.config.Settings;
27 import org.sonar.api.measures.Measure;
28 import org.sonar.api.measures.Metric;
29 import org.sonar.api.resources.Resource;
30 import org.sonar.api.utils.WildcardPattern;
31 import org.sonar.core.measure.MeasurementFilter;
32
33 import java.util.Collection;
34 import java.util.Iterator;
35
36 public class CoverageMeasurementFilter implements MeasurementFilter {
37
38   private final Settings settings;
39   private final ImmutableSet<Metric> coverageMetrics;
40   private Collection<WildcardPattern> resourcePatterns;
41
42   public CoverageMeasurementFilter(Settings settings,
43                                    CoverageDecorator coverageDecorator,
44                                    LineCoverageDecorator lineCoverageDecorator,
45                                    BranchCoverageDecorator branchCoverageDecorator) {
46     this.settings = settings;
47     this.coverageMetrics = ImmutableSet.<Metric>builder()
48       .addAll(coverageDecorator.generatedMetrics())
49       .addAll(coverageDecorator.usedMetrics())
50       .addAll(lineCoverageDecorator.generatedMetrics())
51       .addAll(lineCoverageDecorator.dependsUponMetrics())
52       .addAll(branchCoverageDecorator.generatedMetrics())
53       .addAll(branchCoverageDecorator.dependsUponMetrics())
54       .build();
55
56     initPatterns();
57   }
58
59   @Override
60   public boolean accept(Resource resource, Measure measure) {
61     if (isCoverageMetric(measure.getMetric())) {
62       return !hasMatchingPattern(resource);
63     } else {
64       return true;
65     }
66   }
67
68   private boolean isCoverageMetric(Metric metric) {
69     return this.coverageMetrics.contains(metric);
70   }
71
72   private boolean hasMatchingPattern(Resource resource) {
73     boolean found = false;
74     Iterator<WildcardPattern> iterator = resourcePatterns.iterator();
75     while (!found && iterator.hasNext()) {
76       found = resource.matchFilePattern(iterator.next().toString());
77     }
78     return found;
79   }
80
81   @VisibleForTesting
82   final void initPatterns() {
83     Builder<WildcardPattern> builder = ImmutableList.builder();
84     for (String pattern : settings.getStringArray("sonar.coverage.exclusions")) {
85       builder.add(WildcardPattern.create(pattern));
86     }
87     resourcePatterns = builder.build();
88   }
89 }