diff options
author | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-09-06 11:37:19 +0200 |
---|---|---|
committer | Jean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com> | 2013-09-06 11:37:19 +0200 |
commit | a6cbd7bf8ff033c493158c54fc54a12844b39729 (patch) | |
tree | a2fb4f695440c30f062fd3be597cc1bf0b9da958 /plugins | |
parent | c6999241df2f34d82c8ba476489a469ca6b61c5a (diff) | |
download | sonarqube-a6cbd7bf8ff033c493158c54fc54a12844b39729.tar.gz sonarqube-a6cbd7bf8ff033c493158c54fc54a12844b39729.zip |
SONAR-3644 Break compatibility with plugin configuration
Use relative paths rather than component keys for filter definition
Add utility to compute component key based on underlying file path
Enhance coverage and compliance
Diffstat (limited to 'plugins')
12 files changed, 167 insertions, 91 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesConfiguration.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesConfiguration.java index 045425c5d76..e09370c404f 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesConfiguration.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesConfiguration.java @@ -1,4 +1,3 @@ -// POLOP_IS_GREAT /* * SonarQube, open source software quality management tool. * Copyright (C) 2008-2013 SonarSource @@ -31,8 +30,9 @@ import org.sonar.api.config.PropertyDefinition; import java.util.List; -public class IgnoreIssuesConfiguration { +public final class IgnoreIssuesConfiguration { + private IgnoreIssuesConfiguration() {} static final int LARGE_SIZE = 20; static final int SMALL_SIZE = 10; diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesFilter.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesFilter.java index c60b9b71672..ea342e36a1f 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesFilter.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesFilter.java @@ -40,14 +40,7 @@ public final class IgnoreIssuesFilter implements IssueFilter { } public boolean accept(Issue issue) { - Pattern extraPattern = patternsInitializer.getExtraPattern(issue.componentKey()); - LOG.debug("Extra pattern for resource {}: {}", issue.componentKey(), extraPattern); - if (extraPattern != null && extraPattern.match(issue)) { - logExclusion(issue, extraPattern); - return false; - } - - List<Pattern> patterns = patternsInitializer.getMulticriteriaPatterns(); + List<Pattern> patterns = patternsInitializer.getPatternsForComponent(issue.componentKey()); for (Pattern pattern : patterns) { if (pattern.match(issue)) { logExclusion(issue, pattern); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesPlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesPlugin.java index 29d0048c50a..ea929d517bd 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesPlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesPlugin.java @@ -29,6 +29,8 @@ import java.util.List; public final class IgnoreIssuesPlugin { + private IgnoreIssuesPlugin() {} + public static List<?> getExtensions() { ImmutableList.Builder<Object> extensions = ImmutableList.builder(); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/Pattern.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/Pattern.java index ae3a8ebc91b..40e5855ecfa 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/Pattern.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/Pattern.java @@ -116,7 +116,8 @@ public class Pattern { } public boolean match(Issue violation) { - boolean match = matchResource(violation.componentKey()) && matchRule(violation.ruleKey()); + boolean match = violation.componentKey() != null + && matchRule(violation.ruleKey()); if (checkLines && violation.line() != null) { match = match && matchLine(violation.line()); } @@ -138,7 +139,6 @@ public class Pattern { } boolean matchRule(RuleKey rule) { - System.out.printf("Matching rule {} against pattern {}", rule, rulePattern); if (rule == null) { return false; } @@ -148,7 +148,6 @@ public class Pattern { } boolean matchResource(String resource) { - System.out.printf("Matching resource {} against pattern {}", resource, resourcePattern); return resource != null && resourcePattern.match(resource); } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoder.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoder.java index d40388b9a28..b31ecdc0555 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoder.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoder.java @@ -22,12 +22,9 @@ package org.sonar.plugins.core.issue.ignore.pattern; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.Lists; -import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.sonar.api.utils.SonarException; -import java.io.File; -import java.io.IOException; import java.util.List; public class PatternDecoder { @@ -47,23 +44,6 @@ public class PatternDecoder { return patterns; } - public List<Pattern> decode(File file) { - try { - List<String> lines = FileUtils.readLines(file); - List<Pattern> patterns = Lists.newLinkedList(); - for (String line : lines) { - Pattern pattern = decodeLine(line); - if (pattern != null) { - patterns.add(pattern); - } - } - return patterns; - - } catch (IOException e) { - throw new SonarException("Fail to load the file: " + file.getAbsolutePath(), e); - } - } - /** * Main method that decodes a line which defines a pattern */ @@ -138,7 +118,7 @@ public class PatternDecoder { @VisibleForTesting boolean isBlankOrComment(String line) { - return StringUtils.isBlank(line) || StringUtils.startsWith(line, "#"); + return StringUtils.isBlank(line) ^ StringUtils.startsWith(line, "#"); } @VisibleForTesting diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializer.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializer.java index bfc3bdbe377..e310a265c37 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializer.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializer.java @@ -21,6 +21,7 @@ package org.sonar.plugins.core.issue.ignore.pattern; import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.apache.commons.lang.StringUtils; @@ -42,7 +43,7 @@ public class PatternsInitializer implements BatchExtension { private List<Pattern> multicriteriaPatterns; private List<Pattern> blockPatterns; private List<Pattern> allFilePatterns; - private Map<String, Pattern> extraPatternByResource = Maps.newHashMap(); + private Map<String, List<Pattern>> patternByComponent = Maps.newHashMap(); public PatternsInitializer(Settings settings) { this.settings = settings; @@ -61,8 +62,16 @@ public class PatternsInitializer implements BatchExtension { return allFilePatterns; } - public Pattern getExtraPattern(String resource) { - return extraPatternByResource.get(resource.substring(resource.lastIndexOf(":") + 1)); + public boolean hasFileContentPattern() { + return ! (blockPatterns.isEmpty() && allFilePatterns.isEmpty()); + } + + public boolean hasMulticriteriaPatterns() { + return ! multicriteriaPatterns.isEmpty(); + } + + public boolean hasConfiguredPatterns() { + return hasFileContentPattern() || hasMulticriteriaPatterns(); } @VisibleForTesting @@ -108,11 +117,34 @@ public class PatternsInitializer implements BatchExtension { } public void addPatternToExcludeResource(String resource) { - extraPatternByResource.put(resource, new Pattern(resource, "*").setCheckLines(false)); + addPatternForComponent(resource, new Pattern(resource, "*").setCheckLines(false)); } public void addPatternToExcludeLines(String resource, Set<LineRange> lineRanges) { - extraPatternByResource.put(resource, new Pattern(resource, "*", lineRanges)); + addPatternForComponent(resource, new Pattern(resource, "*", lineRanges)); + } + + public void configurePatternsForComponent(String componentKey, String path) { + for (Pattern pattern: multicriteriaPatterns) { + if (pattern.matchResource(path)) { + addPatternForComponent(componentKey, pattern); + } + } + } + + public List<Pattern> getPatternsForComponent(String componentKey) { + if (patternByComponent.containsKey(componentKey)) { + return patternByComponent.get(componentKey); + } else { + return ImmutableList.of(); + } } + private void addPatternForComponent(String component, Pattern pattern) { + if (!patternByComponent.containsKey(component)) { + List<Pattern> newList = Lists.newArrayList(); + patternByComponent.put(component, newList); + } + patternByComponent.get(component).add(pattern); + } } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScanner.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScanner.java index 55fec5877c9..cc721a15693 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScanner.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScanner.java @@ -26,9 +26,12 @@ import org.sonar.api.batch.SensorContext; import org.sonar.api.resources.Java; import org.sonar.api.resources.JavaFile; import org.sonar.api.resources.Project; +import org.sonar.api.resources.Resource; import org.sonar.api.scan.filesystem.FileQuery; import org.sonar.api.scan.filesystem.ModuleFileSystem; +import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.SonarException; +import org.sonar.core.component.ComponentKeys; import org.sonar.plugins.core.issue.ignore.pattern.PatternsInitializer; import java.io.File; @@ -41,15 +44,17 @@ public final class SourceScanner implements Sensor { private final RegexpScanner regexpScanner; private final PatternsInitializer patternsInitializer; private final ModuleFileSystem fileSystem; + private final PathResolver pathResolver; public SourceScanner(RegexpScanner regexpScanner, PatternsInitializer patternsInitializer, ModuleFileSystem fileSystem) { this.regexpScanner = regexpScanner; this.patternsInitializer = patternsInitializer; this.fileSystem = fileSystem; + this.pathResolver = new PathResolver(); } public boolean shouldExecuteOnProject(Project project) { - return patternsInitializer.getAllFilePatterns().size() > 0 || patternsInitializer.getBlockPatterns().size() > 0; + return patternsInitializer.hasConfiguredPatterns(); } /** @@ -64,17 +69,24 @@ public final class SourceScanner implements Sensor { Charset sourcesEncoding = fileSystem.sourceCharset(); List<File> files; + List<File> dirs; if (isTest) { files = fileSystem.files(FileQuery.onTest().onLanguage(project.getLanguageKey())); + dirs = fileSystem.testDirs(); } else { files = fileSystem.files(FileQuery.onSource().onLanguage(project.getLanguageKey())); + dirs = fileSystem.sourceDirs(); } for (File inputFile : files) { try { - String resource = defineResource(inputFile, fileSystem, project, isTest); - if (resource != null) { - regexpScanner.scan(resource, inputFile, sourcesEncoding); + String componentKey = resolveComponent(inputFile, fileSystem, project, isTest); + if (componentKey != null) { + String relativePath = pathResolver.relativePath(dirs, inputFile).path(); + patternsInitializer.configurePatternsForComponent(componentKey, relativePath); + if (patternsInitializer.hasFileContentPattern()) { + regexpScanner.scan(componentKey, inputFile, sourcesEncoding); + } } } catch (Exception e) { throw new SonarException("Unable to read the source file : '" + inputFile.getAbsolutePath() + "' with the charset : '" @@ -86,7 +98,8 @@ public final class SourceScanner implements Sensor { /* * This method is necessary because Java resources are not treated as every other resource... */ - private String defineResource(File inputFile, ModuleFileSystem fileSystem, Project project, boolean isTest) { + private String resolveComponent(File inputFile, ModuleFileSystem fileSystem, Project project, boolean isTest) { + Resource<?> resource = null; if (Java.KEY.equals(project.getLanguageKey()) && Java.isJavaFile(inputFile)) { List<File> sourceDirs = null; if (isTest) { @@ -94,19 +107,21 @@ public final class SourceScanner implements Sensor { } else { sourceDirs = fileSystem.sourceDirs(); } - JavaFile file = JavaFile.fromIOFile(inputFile, sourceDirs, isTest); - if (file == null) { - return null; - } else { - return file.getKey(); - } + resource = JavaFile.fromIOFile(inputFile, sourceDirs, isTest); + } else { + resource = new org.sonar.api.resources.File(inputFile.getPath()); + } + + if (resource == null) { + return null; + } else { + return ComponentKeys.createKey(project, resource); } - return inputFile.getPath(); } @Override public String toString() { - return "Ignore Issues - Source Scanner"; + return "Issues Exclusions - Source Scanner"; } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesFilterTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesFilterTest.java index 0a37fc7132c..2808d578252 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesFilterTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesFilterTest.java @@ -60,28 +60,28 @@ public class IgnoreIssuesFilterTest { @Test public void shouldBeIgnoredWithStandardPatterns() throws IOException { - when(patternsInitializer.getMulticriteriaPatterns()).thenReturn(createPatterns("org.foo.Bar;*;*\norg.foo.Hello;checkstyle:MagicNumber;[15-200]")); + when(patternsInitializer.getPatternsForComponent(JAVA_FILE)).thenReturn(createPatterns("org.foo.Hello;checkstyle:MagicNumber;[15-200]")); assertThat(filter.accept(create(CHECKSTYLE_RULE, JAVA_FILE, 150))).isFalse(); } @Test public void shouldNotBeIgnoredWithStandardPatterns() throws IOException { - when(patternsInitializer.getMulticriteriaPatterns()).thenReturn(createPatterns("org.foo.Bar;*;*\norg.foo.Hello;checkstyle:MagicNumber;[15-200]")); + when(patternsInitializer.getPatternsForComponent(JAVA_FILE)).thenReturn(createPatterns("org.foo.Hello;checkstyle:MagicNumber;[15-200]")); assertThat(filter.accept(create(CHECKSTYLE_RULE, JAVA_FILE, 5))).isTrue(); } @Test public void shouldBeIgnoredWithExtraPattern() throws IOException { - when(patternsInitializer.getExtraPattern(JAVA_FILE)).thenReturn(createPatterns("org.foo.Hello;*;[15-200]").get(0)); + when(patternsInitializer.getPatternsForComponent(JAVA_FILE)).thenReturn(createPatterns("org.foo.Hello;*;[15-200]")); assertThat(filter.accept(create(CHECKSTYLE_RULE, JAVA_FILE, 150))).isFalse(); } @Test public void shouldNotBeIgnoredWithExtraPattern() throws IOException { - when(patternsInitializer.getExtraPattern(JAVA_FILE)).thenReturn(createPatterns("org.foo.Hello;*;[15-200]").get(0)); + when(patternsInitializer.getPatternsForComponent(JAVA_FILE)).thenReturn(createPatterns("org.foo.Hello;*;[15-200]")); assertThat(filter.accept(create(CHECKSTYLE_RULE, JAVA_FILE, 5))).isTrue(); } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesPluginTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesPluginTest.java index c478e714efe..4c5395a0b06 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesPluginTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/IgnoreIssuesPluginTest.java @@ -27,6 +27,6 @@ import static org.fest.assertions.Assertions.assertThat; public class IgnoreIssuesPluginTest { @Test public void justForCoverage() { - assertThat(new IgnoreIssuesPlugin().getExtensions()).hasSize(3 /* properties */ + 4 /* extensions */); + assertThat(IgnoreIssuesPlugin.getExtensions()).hasSize(3 /* properties */ + 4 /* extensions */); } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoderTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoderTest.java index 3d904b43ec5..c527412a5d6 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoderTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternDecoderTest.java @@ -25,7 +25,6 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.utils.SonarException; -import java.io.File; import java.util.List; import static org.fest.assertions.Assertions.assertThat; @@ -139,14 +138,6 @@ public class PatternDecoderTest { } @Test - public void shouldFailIfUnexistingFile() { - thrown.expect(SonarException.class); - thrown.expectMessage("Fail to load the file"); - - decoder.decode(new File("foo")); - } - - @Test public void shouldFailToReadUncorrectLine1() { thrown.expect(SonarException.class); thrown.expectMessage("Invalid format. The following line has more than 3 fields separated by comma"); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializerTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializerTest.java index ec6a6529255..09ad7fe3482 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializerTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/pattern/PatternsInitializerTest.java @@ -20,17 +20,20 @@ package org.sonar.plugins.core.issue.ignore.pattern; +import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.junit.Before; import org.junit.Test; import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.Settings; import org.sonar.plugins.core.issue.ignore.Constants; -import org.sonar.plugins.core.issue.ignore.IgnoreIssuesPlugin; +import org.sonar.plugins.core.issue.ignore.IgnoreIssuesConfiguration; import java.util.Set; import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class PatternsInitializerTest { @@ -40,13 +43,14 @@ public class PatternsInitializerTest { @Before public void init() { - settings = new Settings(new PropertyDefinitions(new IgnoreIssuesPlugin())); + settings = new Settings(new PropertyDefinitions(IgnoreIssuesConfiguration.getPropertyDefinitions())); patternsInitializer = new PatternsInitializer(settings); } @Test public void testNoConfiguration() { patternsInitializer.initPatterns(); + assertThat(patternsInitializer.hasConfiguredPatterns()).isFalse(); assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0); } @@ -55,7 +59,7 @@ public class PatternsInitializerTest { String file = "foo"; patternsInitializer.addPatternToExcludeResource(file); - Pattern extraPattern = patternsInitializer.getExtraPattern(file); + Pattern extraPattern = patternsInitializer.getPatternsForComponent(file).get(0); assertThat(extraPattern.matchResource(file)).isTrue(); assertThat(extraPattern.isCheckLines()).isFalse(); } @@ -67,7 +71,7 @@ public class PatternsInitializerTest { lineRanges.add(new LineRange(25, 28)); patternsInitializer.addPatternToExcludeLines(file, lineRanges); - Pattern extraPattern = patternsInitializer.getExtraPattern(file); + Pattern extraPattern = patternsInitializer.getPatternsForComponent(file).get(0); assertThat(extraPattern.matchResource(file)).isTrue(); assertThat(extraPattern.getAllLines()).isEqualTo(Sets.newHashSet(25, 26, 27, 28)); } @@ -75,14 +79,17 @@ public class PatternsInitializerTest { @Test public void shouldReturnMulticriteriaPattern() { settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY, "1,2"); - settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "org.foo.Bar"); + settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "org/foo/Bar.java"); settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RULE_KEY, "*"); settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".1." + Constants.RESOURCE_KEY, "*"); - settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.LINE_RANGE_KEY, "org.foo.Hello"); + settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.LINE_RANGE_KEY, "org/foo/Hello.java"); settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.RULE_KEY, "checkstyle:MagicNumber"); settings.setProperty(Constants.PATTERNS_MULTICRITERIA_KEY + ".2." + Constants.LINE_RANGE_KEY, "[15-200]"); patternsInitializer.initPatterns(); + assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue(); + assertThat(patternsInitializer.hasFileContentPattern()).isFalse(); + assertThat(patternsInitializer.hasMulticriteriaPatterns()).isTrue(); assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(2); assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(0); assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0); @@ -97,6 +104,9 @@ public class PatternsInitializerTest { settings.setProperty(Constants.PATTERNS_BLOCK_KEY + ".2." + Constants.END_BLOCK_REGEXP, "// FOO-ON"); patternsInitializer.initPatterns(); + assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue(); + assertThat(patternsInitializer.hasFileContentPattern()).isTrue(); + assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse(); assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0); assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(2); assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(0); @@ -109,8 +119,32 @@ public class PatternsInitializerTest { settings.setProperty(Constants.PATTERNS_ALLFILE_KEY + ".2." + Constants.FILE_REGEXP, "//FOO-IGNORE-ALL"); patternsInitializer.initPatterns(); + assertThat(patternsInitializer.hasConfiguredPatterns()).isTrue(); + assertThat(patternsInitializer.hasFileContentPattern()).isTrue(); + assertThat(patternsInitializer.hasMulticriteriaPatterns()).isFalse(); assertThat(patternsInitializer.getMulticriteriaPatterns().size()).isEqualTo(0); assertThat(patternsInitializer.getBlockPatterns().size()).isEqualTo(0); assertThat(patternsInitializer.getAllFilePatterns().size()).isEqualTo(2); } + + @Test + public void shouldConfigurePatternsForComponents() { + String componentKey = "groupId:artifactId:org.foo.Bar"; + String path = "org/foo/Bar.java"; + + Pattern matching1, matching2, notMatching; + matching1 = mock(Pattern.class); + when(matching1.matchResource(path)).thenReturn(true); + matching2 = mock(Pattern.class); + when(matching2.matchResource(path)).thenReturn(true); + notMatching = mock(Pattern.class); + when(notMatching.matchResource(path)).thenReturn(false); + + patternsInitializer.initPatterns(); + patternsInitializer.getMulticriteriaPatterns().addAll(Lists.newArrayList(matching1, matching2, notMatching)); + patternsInitializer.configurePatternsForComponent(componentKey, path); + + assertThat(patternsInitializer.getPatternsForComponent(componentKey).size()).isEqualTo(2); + assertThat(patternsInitializer.getPatternsForComponent("other").size()).isEqualTo(0); + } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScannerTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScannerTest.java index 954512edf8c..4cd9954ef70 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScannerTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScannerTest.java @@ -20,6 +20,7 @@ package org.sonar.plugins.core.issue.ignore.scanner; +import com.google.common.collect.ImmutableList; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -40,13 +41,12 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import static org.mockito.Mockito.verifyNoMoreInteractions; - import static com.google.common.base.Charsets.UTF_8; import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; public class SourceScannerTest { @@ -58,10 +58,10 @@ public class SourceScannerTest { @Mock private PatternsInitializer patternsInitializer; @Mock - private Project project; - @Mock private ModuleFileSystem fileSystem; + private Project project; + @Rule public ExpectedException thrown = ExpectedException.none(); @@ -69,6 +69,9 @@ public class SourceScannerTest { public void init() { MockitoAnnotations.initMocks(this); + Project realProject = new Project("polop"); + project = Mockito.spy(realProject); + Mockito.doReturn("java").when(project).getLanguageKey(); when(fileSystem.sourceCharset()).thenReturn(UTF_8); scanner = new SourceScanner(regexpScanner, patternsInitializer, fileSystem); @@ -76,20 +79,15 @@ public class SourceScannerTest { @Test public void testToString() throws Exception { - assertThat(scanner.toString()).isEqualTo("Ignore Issues - Source Scanner"); + assertThat(scanner.toString()).isEqualTo("Issues Exclusions - Source Scanner"); } @Test public void shouldExecute() throws IOException { - when(patternsInitializer.getAllFilePatterns()).thenReturn(Arrays.asList(new Pattern(), new Pattern())); + when(patternsInitializer.hasConfiguredPatterns()).thenReturn(true); assertThat(scanner.shouldExecuteOnProject(null)).isTrue(); - when(patternsInitializer.getAllFilePatterns()).thenReturn(Collections.<Pattern>emptyList()); - when(patternsInitializer.getBlockPatterns()).thenReturn(Arrays.asList(new Pattern(), new Pattern())); - assertThat(scanner.shouldExecuteOnProject(null)).isTrue(); - - when(patternsInitializer.getAllFilePatterns()).thenReturn(Collections.<Pattern>emptyList()); - when(patternsInitializer.getBlockPatterns()).thenReturn(Collections.<Pattern>emptyList()); + when(patternsInitializer.hasConfiguredPatterns()).thenReturn(false); assertThat(scanner.shouldExecuteOnProject(null)).isFalse(); } @@ -104,11 +102,34 @@ public class SourceScannerTest { .thenReturn(Arrays.asList(testFile)); when(fileSystem.sourceDirs()).thenReturn(Arrays.asList(new File("src/main/java"))); when(fileSystem.testDirs()).thenReturn(Arrays.asList(new File("src/test/java"))); + when(patternsInitializer.hasFileContentPattern()).thenReturn(true); scanner.analyse(project, null); - verify(regexpScanner).scan("[default].Foo", sourceFile, UTF_8); - verify(regexpScanner).scan("[default].FooTest", testFile, UTF_8); + verify(patternsInitializer).configurePatternsForComponent("polop:[default].Foo", "Foo.java"); + verify(patternsInitializer).configurePatternsForComponent("polop:[default].FooTest", "FooTest.java"); + verify(regexpScanner).scan("polop:[default].Foo", sourceFile, UTF_8); + verify(regexpScanner).scan("polop:[default].FooTest", testFile, UTF_8); + } + + @Test + public void shouldAnalyseFilesOnlyWhenRegexConfigured() throws IOException { + File sourceFile = new File("src/main/java/Foo.java"); + File testFile = new File("src/test/java/FooTest.java"); + + when(project.getLanguageKey()).thenReturn("java"); + when(fileSystem.files(Mockito.isA(FileQuery.class))) + .thenReturn(Arrays.asList(sourceFile)) + .thenReturn(Arrays.asList(testFile)); + when(fileSystem.sourceDirs()).thenReturn(Arrays.asList(new File("src/main/java"))); + when(fileSystem.testDirs()).thenReturn(Arrays.asList(new File("src/test/java"))); + when(patternsInitializer.hasFileContentPattern()).thenReturn(false); + + scanner.analyse(project, null); + + verify(patternsInitializer).configurePatternsForComponent("polop:[default].Foo", "Foo.java"); + verify(patternsInitializer).configurePatternsForComponent("polop:[default].FooTest", "FooTest.java"); + verifyNoMoreInteractions(regexpScanner); } @Test @@ -120,11 +141,16 @@ public class SourceScannerTest { when(fileSystem.files(Mockito.isA(FileQuery.class))) .thenReturn(Arrays.asList(sourceFile)) .thenReturn(Arrays.asList(testFile)); + when(fileSystem.sourceDirs()).thenReturn(ImmutableList.of(new File(""))); + when(fileSystem.testDirs()).thenReturn(ImmutableList.of(new File(""))); + when(patternsInitializer.hasFileContentPattern()).thenReturn(true); scanner.analyse(project, null); - verify(regexpScanner).scan("Foo.php", sourceFile, UTF_8); - verify(regexpScanner).scan("FooTest.php", testFile, UTF_8); + verify(patternsInitializer).configurePatternsForComponent("polop:Foo.php", "Foo.php"); + verify(patternsInitializer).configurePatternsForComponent("polop:FooTest.php", "FooTest.php"); + verify(regexpScanner).scan("polop:Foo.php", sourceFile, UTF_8); + verify(regexpScanner).scan("polop:FooTest.php", testFile, UTF_8); } @Test @@ -137,8 +163,9 @@ public class SourceScannerTest { when(fileSystem.files(Mockito.isA(FileQuery.class))) .thenReturn(Arrays.asList(sourceFile, otherFile)) .thenReturn(empty); - when(fileSystem.sourceDirs()).thenReturn(Arrays.asList(new File("src/main/java"))); - when(fileSystem.testDirs()).thenReturn(Arrays.asList(new File("src/test/java"))); + when(fileSystem.sourceDirs()).thenReturn(ImmutableList.of(new File("src/main/java"), new File(""))); + when(fileSystem.testDirs()).thenReturn(ImmutableList.of(new File("src/test/java"))); + when(patternsInitializer.hasFileContentPattern()).thenReturn(true); scanner.analyse(project, null); @@ -156,6 +183,7 @@ public class SourceScannerTest { .thenReturn(empty); when(fileSystem.sourceDirs()).thenReturn(Arrays.asList(new File("src/main/java"))); when(fileSystem.testDirs()).thenReturn(Arrays.asList(new File("src/test/java"))); + when(patternsInitializer.hasFileContentPattern()).thenReturn(true); scanner.analyse(project, null); @@ -168,7 +196,9 @@ public class SourceScannerTest { when(project.getLanguageKey()).thenReturn("php"); when(fileSystem.files(Mockito.isA(FileQuery.class))).thenReturn(Arrays.asList(sourceFile)); - doThrow(new IOException("BUG")).when(regexpScanner).scan("Foo.php", sourceFile, UTF_8); + List<Pattern> empty = ImmutableList.of(); + when(patternsInitializer.getPatternsForComponent("Foo.php")).thenReturn(empty); + doThrow(new IOException("BUG")).when(regexpScanner).scan("polop:Foo.php", sourceFile, UTF_8); thrown.expect(SonarException.class); thrown.expectMessage("Unable to read the source file"); |