3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program 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 * This program 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.scanner.scan.filesystem;
22 import java.util.Collection;
23 import java.util.function.Function;
24 import java.util.stream.Collectors;
25 import java.util.stream.Stream;
26 import javax.annotation.concurrent.Immutable;
27 import org.sonar.api.CoreProperties;
28 import org.sonar.api.utils.WildcardPattern;
29 import org.sonar.api.utils.log.Logger;
30 import org.sonar.api.utils.log.Loggers;
31 import org.sonar.api.impl.fs.DefaultInputFile;
33 import static java.util.stream.Collectors.toList;
36 public abstract class AbstractCoverageAndDuplicationExclusions {
37 private static final Logger LOG = Loggers.get(AbstractCoverageAndDuplicationExclusions.class);
38 private final Function<DefaultInputFile, String> pathExtractor;
39 private final String[] coverageExclusionConfig;
40 private final String[] duplicationExclusionConfig;
42 private final Collection<WildcardPattern> coverageExclusionPatterns;
43 private final Collection<WildcardPattern> duplicationExclusionPatterns;
45 public AbstractCoverageAndDuplicationExclusions(Function<String, String[]> configProvider, Function<DefaultInputFile, String> pathExtractor) {
46 this.pathExtractor = pathExtractor;
47 coverageExclusionConfig = configProvider.apply(CoreProperties.PROJECT_COVERAGE_EXCLUSIONS_PROPERTY);
48 coverageExclusionPatterns = Stream.of(coverageExclusionConfig).map(WildcardPattern::create).collect(toList());
49 duplicationExclusionConfig = configProvider.apply(CoreProperties.CPD_EXCLUSIONS);
50 duplicationExclusionPatterns = Stream.of(duplicationExclusionConfig).map(WildcardPattern::create).collect(toList());
53 public String[] getCoverageExclusionConfig() {
54 return coverageExclusionConfig;
57 public String[] getDuplicationExclusionConfig() {
58 return duplicationExclusionConfig;
61 void log(String indent) {
62 if (!coverageExclusionPatterns.isEmpty()) {
63 log("Excluded sources for coverage:", coverageExclusionPatterns, indent);
65 if (!duplicationExclusionPatterns.isEmpty()) {
66 log("Excluded sources for duplication:", duplicationExclusionPatterns, indent);
70 public boolean isExcludedForCoverage(DefaultInputFile file) {
71 return isExcluded(file, coverageExclusionPatterns);
74 public boolean isExcludedForDuplication(DefaultInputFile file) {
75 return isExcluded(file, duplicationExclusionPatterns);
78 private boolean isExcluded(DefaultInputFile file, Collection<WildcardPattern> patterns) {
79 if (patterns.isEmpty()) {
82 final String path = pathExtractor.apply(file);
85 .anyMatch(p -> p.match(path));
88 private static void log(String title, Collection<WildcardPattern> patterns, String ident) {
89 if (!patterns.isEmpty()) {
90 LOG.info("{}{} {}", ident, title, patterns.stream().map(WildcardPattern::toString).collect(Collectors.joining(", ")));