diff options
author | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2012-12-03 11:08:19 +0100 |
---|---|---|
committer | Fabrice Bellingard <fabrice.bellingard@sonarsource.com> | 2012-12-03 14:07:13 +0100 |
commit | 7c48d7d02be54043f6ec236abf422fe265fc5a69 (patch) | |
tree | d4bdb03f667676bdbae6fb6f6198a532823621fc | |
parent | f629d8a518be51db00d1abc84ff043676ed51582 (diff) | |
download | sonarqube-7c48d7d02be54043f6ec236abf422fe265fc5a69.tar.gz sonarqube-7c48d7d02be54043f6ec236abf422fe265fc5a69.zip |
Revert SONAR-3885 code
This reverts commit ef9e63fa87b0a4cc943b969f972217163241869e.
This reverts commit 0ca3e750948ff947c6ab5918cb7f36c9ad0095bb.
This reverts commit b988f880e5658cb6cc733391de782ff539cd527b.
This reverts commit ab832caa3878603fc087749732def51e1f4ffacc.
This reverts commit 5d308f5d402522b2a387e30292187d71a3724ba8.
This reverts commit 123ef89aa488f906363d5fba90951be7d430756a.
This reverts commit 14de16c62104cff7d8cb0a272b1f3abb29458f6e.
11 files changed, 172 insertions, 314 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ExcludedResourceFilter.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ExcludedResourceFilter.java index 8a71fb83425..46516ff4d8a 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ExcludedResourceFilter.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ExcludedResourceFilter.java @@ -35,18 +35,13 @@ public class ExcludedResourceFilter implements ResourceFilter { } public boolean isIgnored(Resource resource) { - boolean isTest = ResourceUtils.isUnitTestClass(resource); - - String[] exclusions = isTest ? project.getTestExclusionPatterns() : project.getExclusionPatterns(); - for (String exclusion : exclusions) { - if (resource.matchFilePattern(exclusion)) { - return true; - } + String[] patterns = ResourceUtils.isUnitTestClass(resource) ? project.getTestExclusionPatterns() : project.getExclusionPatterns(); + if (patterns == null) { + return false; } - String[] inclusions = isTest ? project.getTestInclusionPatterns() : project.getInclusionPatterns(); - for (String inclusion : inclusions) { - if (!resource.matchFilePattern(inclusion)) { + for (String pattern : patterns) { + if (resource.matchFilePattern(pattern)) { return true; } } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ProjectFileSystemLogger.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ProjectFileSystemLogger.java index d28b0ba002a..3364e99b8f6 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ProjectFileSystemLogger.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/batch/ProjectFileSystemLogger.java @@ -33,9 +33,7 @@ public class ProjectFileSystemLogger extends Initializer { @Override public void execute(Project project) { - logExclusionPatterns("Included sources: {}", project.getInclusionPatterns()); logExclusionPatterns("Excluded sources: {}", project.getExclusionPatterns()); - logExclusionPatterns("Included tests: {}", project.getTestExclusionPatterns()); logExclusionPatterns("Excluded tests: {}", project.getTestExclusionPatterns()); ProjectFileSystem projectFileSystem = project.getFileSystem(); diff --git a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties index 092e3188251..5932b982884 100644 --- a/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties +++ b/plugins/sonar-core-plugin/src/main/resources/org/sonar/l10n/core.properties @@ -659,10 +659,6 @@ category.exclusions.help=<h2>Wildcards</h2>\ <td>?</td>\ <td>Match a single character</td>\ </tr>\ - <tr class="even">\ - <td>!</td>\ - <td>Negates a pattern</td>\ - </tr>\ </table>\ <p>Examples:</p>\ <table class="data">\ @@ -720,17 +716,6 @@ category.exclusions.help=<h2>Wildcards</h2>\ </ul>\ </td>\ </tr>\ - <tr class="even">\ - <td>!**/*Test.java</td>\ - <td>matches all <code>.java</code> files whose name doesn't end with <code>Test</code></td>\ - <td>\ - <ul>\ - <li>Foo.java</li>\ - <li>foo/Bar.java</li>\ - <li>foo/bar/Baz.java</li>\ - </ul>\ - </td>\ - </tr>\ </table> property.error.notBoolean=Valid options are "true" and "false" diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/batch/ExcludedResourceFilterTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/batch/ExcludedResourceFilterTest.java index bdd1bb7643f..1b9612ae750 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/batch/ExcludedResourceFilterTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/batch/ExcludedResourceFilterTest.java @@ -26,76 +26,55 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Resource; -import static org.fest.assertions.Assertions.assertThat; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class ExcludedResourceFilterTest { - Resource resource = mock(Resource.class); @Test public void doNotFailIfNoPatterns() { - Project project = new Project("foo").setConfiguration(configWithExclusions()); - + PropertiesConfiguration conf = new PropertiesConfiguration(); + Project project = new Project("foo").setConfiguration(conf); ExcludedResourceFilter filter = new ExcludedResourceFilter(project); - - assertThat(filter.isIgnored(resource)).isFalse(); + assertThat(filter.isIgnored(mock(Resource.class)), is(false)); } @Test public void noPatternsMatch() { - Project project = new Project("foo").setConfiguration(configWithExclusions("**/foo/*.java", "**/bar/*")); - + PropertiesConfiguration conf = new PropertiesConfiguration(); + conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, new String[]{"**/foo/*.java", "**/bar/*"}); + Project project = new Project("foo").setConfiguration(conf); ExcludedResourceFilter filter = new ExcludedResourceFilter(project); - - assertThat(filter.isIgnored(resource)).isFalse(); + assertThat(filter.isIgnored(mock(Resource.class)), is(false)); } @Test public void ignoreResourceIfMatchesPattern() { - when(resource.matchFilePattern("**/bar/*")).thenReturn(true); - - Project project = new Project("foo").setConfiguration(configWithExclusions("**/foo/*.java", "**/bar/*")); - ExcludedResourceFilter filter = new ExcludedResourceFilter(project); - - assertThat(filter.isIgnored(resource)).isTrue(); - } - - @Test - public void should_support_inclusions() { - Project project = new Project("foo").setConfiguration(configWithExclusions("!**/bar/*")); + PropertiesConfiguration conf = new PropertiesConfiguration(); + conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, new String[]{"**/foo/*.java", "**/bar/*"}); + Project project = new Project("foo").setConfiguration(conf); ExcludedResourceFilter filter = new ExcludedResourceFilter(project); + Resource resource = mock(Resource.class); when(resource.matchFilePattern("**/bar/*")).thenReturn(true); - assertThat(filter.isIgnored(resource)).isFalse(); - when(resource.matchFilePattern("**/bar/*")).thenReturn(false); - assertThat(filter.isIgnored(resource)).isTrue(); + assertThat(filter.isIgnored(resource), is(true)); } @Test - public void should_support_test_inclusions() { - when(resource.getQualifier()).thenReturn(Qualifiers.UNIT_TEST_FILE); - - Project project = new Project("foo").setConfiguration(configWithTestExclusions("!**/bar/*")); + public void ignoreTestIfMatchesPattern() { + PropertiesConfiguration conf = new PropertiesConfiguration(); + conf.setProperty(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY, new String[]{"**/foo/*.java", "**/bar/*"}); + Project project = new Project("foo").setConfiguration(conf); ExcludedResourceFilter filter = new ExcludedResourceFilter(project); - when(resource.matchFilePattern("**/bar/*")).thenReturn(true); - assertThat(filter.isIgnored(resource)).isFalse(); - - when(resource.matchFilePattern("**/bar/*")).thenReturn(false); - assertThat(filter.isIgnored(resource)).isTrue(); - } - - @Test - public void ignoreTestIfMatchesPattern() { + Resource resource = mock(Resource.class); when(resource.getQualifier()).thenReturn(Qualifiers.UNIT_TEST_FILE); when(resource.matchFilePattern("**/bar/*")).thenReturn(true); - Project project = new Project("foo").setConfiguration(configWithTestExclusions("**/foo/*.java", "**/bar/*")); - ExcludedResourceFilter filter = new ExcludedResourceFilter(project); - - assertThat(filter.isIgnored(resource)).isTrue(); + assertThat(filter.isIgnored(resource), is(true)); } /** @@ -103,26 +82,17 @@ public class ExcludedResourceFilterTest { */ @Test public void doNotExcludeUnitTestFiles() { - when(resource.getQualifier()).thenReturn(Qualifiers.UNIT_TEST_FILE); - when(resource.matchFilePattern("**/bar/*")).thenReturn(true); - - Project project = new Project("foo").setConfiguration(configWithExclusions("**/foo/*.java", "**/bar/*")); + PropertiesConfiguration conf = new PropertiesConfiguration(); + conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, new String[]{"**/foo/*.java", "**/bar/*"}); + Project project = new Project("foo").setConfiguration(conf); ExcludedResourceFilter filter = new ExcludedResourceFilter(project); - assertThat(filter.isIgnored(resource)).isFalse(); - } + Resource unitTest = mock(Resource.class); + when(unitTest.getQualifier()).thenReturn(Qualifiers.UNIT_TEST_FILE); - static PropertiesConfiguration configWithExclusions(String... exclusions) { - return config(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, exclusions); - } - - static PropertiesConfiguration configWithTestExclusions(String... exclusions) { - return config(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY, exclusions); - } + // match exclusion pattern + when(unitTest.matchFilePattern("**/bar/*")).thenReturn(true); - static PropertiesConfiguration config(String property, String... exclusions) { - PropertiesConfiguration config = new PropertiesConfiguration(); - config.setProperty(property, exclusions); - return config; + assertThat(filter.isIgnored(unitTest), is(false)); } } diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java index 24ff073ddf9..ad24fa026ae 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsConfiguration.java @@ -30,11 +30,15 @@ import org.sonar.api.config.Settings; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Project; import org.sonar.api.utils.SonarException; +import org.sonar.plugins.findbugs.xml.ClassFilter; +import org.sonar.plugins.findbugs.xml.FindBugsFilter; +import org.sonar.plugins.findbugs.xml.Match; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.StringWriter; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -87,16 +91,21 @@ public class FindbugsConfiguration implements BatchExtension { @VisibleForTesting File saveIncludeConfigXml() throws IOException { - String xml = exporter.exportProfileAndInclusions(profile, project.getInclusionPatterns()); - - return project.getFileSystem().writeToWorkingDirectory(xml, "findbugs-include.xml"); + StringWriter conf = new StringWriter(); + exporter.exportProfile(profile, conf); + return project.getFileSystem().writeToWorkingDirectory(conf.toString(), "findbugs-include.xml"); } @VisibleForTesting File saveExcludeConfigXml() throws IOException { - String xml = exporter.exportExclusions(project.getExclusionPatterns()); - - return project.getFileSystem().writeToWorkingDirectory(xml, "findbugs-exclude.xml"); + FindBugsFilter findBugsFilter = new FindBugsFilter(); + if (project.getExclusionPatterns() != null) { + for (String exclusion : project.getExclusionPatterns()) { + ClassFilter classFilter = new ClassFilter(FindbugsAntConverter.antToJavaRegexpConvertor(exclusion)); + findBugsFilter.addMatch(new Match(classFilter)); + } + } + return project.getFileSystem().writeToWorkingDirectory(findBugsFilter.toXml(), "findbugs-exclude.xml"); } @VisibleForTesting diff --git a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsProfileExporter.java b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsProfileExporter.java index b261f2f203a..d735e5ebfb4 100644 --- a/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsProfileExporter.java +++ b/plugins/sonar-findbugs-plugin/src/main/java/org/sonar/plugins/findbugs/FindbugsProfileExporter.java @@ -19,25 +19,25 @@ */ package org.sonar.plugins.findbugs; +import java.io.IOException; +import java.io.Writer; +import java.util.List; + import org.sonar.api.profiles.ProfileExporter; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Java; import org.sonar.api.rules.ActiveRule; import org.sonar.api.utils.SonarException; import org.sonar.plugins.findbugs.xml.Bug; -import org.sonar.plugins.findbugs.xml.ClassFilter; import org.sonar.plugins.findbugs.xml.FindBugsFilter; import org.sonar.plugins.findbugs.xml.Match; -import java.io.IOException; -import java.io.Writer; -import java.util.List; +import com.thoughtworks.xstream.XStream; public class FindbugsProfileExporter extends ProfileExporter { public FindbugsProfileExporter() { super(FindbugsConstants.REPOSITORY_KEY, FindbugsConstants.PLUGIN_NAME); - setSupportedLanguages(Java.KEY); setMimeType("application/xml"); } @@ -45,44 +45,16 @@ public class FindbugsProfileExporter extends ProfileExporter { @Override public void exportProfile(RulesProfile profile, Writer writer) { try { - String xml = exportProfileAndInclusions(profile, null); - writer.write(xml); + FindBugsFilter filter = buildFindbugsFilter(profile.getActiveRulesByRepository(FindbugsConstants.REPOSITORY_KEY)); + XStream xstream = FindBugsFilter.createXStream(); + writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!-- Generated by Sonar -->\n".concat(xstream.toXML(filter))); } catch (IOException e) { - throw new SonarException("Fail to export Findbugs profile: " + profile.getName(), e); + throw new SonarException("Fail to export the Findbugs profile : " + profile, e); } } - public String exportProfileAndInclusions(RulesProfile profile, String[] inclusions) { + protected static FindBugsFilter buildFindbugsFilter(List<ActiveRule> activeRules) { FindBugsFilter root = new FindBugsFilter(); - addRules(root, profile.getActiveRulesByRepository(FindbugsConstants.REPOSITORY_KEY)); - addPatterns(root, inclusions); - return toXml(root); - } - - public String exportExclusions(String[] exclusions) { - FindBugsFilter root = new FindBugsFilter(); - addPatterns(root, exclusions); - return toXml(root); - } - - private static String toXml(FindBugsFilter root) { - return new StringBuilder() - .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") - .append("<!-- Generated by Sonar -->\n") - .append(root.toXml()) - .toString(); - } - - private static void addPatterns(FindBugsFilter root, String[] patterns) { - if (patterns != null) { - for (String pattern : patterns) { - ClassFilter classFilter = new ClassFilter(FindbugsAntConverter.antToJavaRegexpConvertor(pattern)); - root.addMatch(new Match(classFilter)); - } - } - } - - protected static void addRules(FindBugsFilter root, List<ActiveRule> activeRules) { for (ActiveRule activeRule : activeRules) { if (FindbugsConstants.REPOSITORY_KEY.equals(activeRule.getRepositoryKey())) { Match child = new Match(); @@ -90,5 +62,6 @@ public class FindbugsProfileExporter extends ProfileExporter { root.addMatch(child); } } + return root; } } diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileExporterTest.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileExporterTest.java index 9cb7eb742c0..27f3545dfd4 100644 --- a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileExporterTest.java +++ b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileExporterTest.java @@ -64,19 +64,15 @@ public class FindbugsProfileExporterTest extends FindbugsTests { @Test public void shouldBuildOnlyOneModuleWhenNoActiveRules() { - FindBugsFilter filter = new FindBugsFilter(); - FindbugsProfileExporter.addRules(filter, Collections.<ActiveRule> emptyList()); - - assertThat(filter.getMatchs()).isEmpty(); + FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Collections.<ActiveRule> emptyList()); + assertThat(filter.getMatchs()).hasSize(0); } @Test public void shouldBuildTwoModulesEvenIfSameTwoRulesActivated() { ActiveRule activeRule1 = anActiveRule(DLS_DEAD_LOCAL_STORE); ActiveRule activeRule2 = anActiveRule(SS_SHOULD_BE_STATIC); - - FindBugsFilter filter = new FindBugsFilter(); - FindbugsProfileExporter.addRules(filter, Arrays.asList(activeRule1, activeRule2)); + FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule1, activeRule2)); List<Match> matches = filter.getMatchs(); assertThat(matches).hasSize(2); @@ -90,17 +86,14 @@ public class FindbugsProfileExporterTest extends FindbugsTests { ActiveRule activeRule1 = anActiveRuleFromAnotherPlugin(); ActiveRule activeRule2 = anActiveRuleFromAnotherPlugin(); - FindBugsFilter filter = new FindBugsFilter(); - FindbugsProfileExporter.addRules(filter, Arrays.asList(activeRule1, activeRule2)); + FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule1, activeRule2)); assertThat(filter.getMatchs()).hasSize(0); } @Test public void shouldBuildModuleWithProperties() { ActiveRule activeRule = anActiveRule(DLS_DEAD_LOCAL_STORE); - - FindBugsFilter filter = new FindBugsFilter(); - FindbugsProfileExporter.addRules(filter, Arrays.asList(activeRule)); + FindBugsFilter filter = FindbugsProfileExporter.buildFindbugsFilter(Arrays.asList(activeRule)); assertThat(filter.getMatchs()).hasSize(1); assertThat(filter.getMatchs().get(0).getBug().getPattern()).isEqualTo("DLS_DEAD_LOCAL_STORE"); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java index 6fc144a846d..5bef696d85b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java @@ -26,13 +26,7 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; -import org.apache.commons.io.filefilter.AndFileFilter; -import org.apache.commons.io.filefilter.DelegateFileFilter; -import org.apache.commons.io.filefilter.FileFilterUtils; -import org.apache.commons.io.filefilter.HiddenFileFilter; -import org.apache.commons.io.filefilter.IOFileFilter; -import org.apache.commons.io.filefilter.SuffixFileFilter; -import org.apache.commons.io.filefilter.TrueFileFilter; +import org.apache.commons.io.filefilter.*; import org.apache.commons.lang.CharEncoding; import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; @@ -42,15 +36,10 @@ import org.sonar.api.utils.SonarException; import org.sonar.api.utils.WildcardPattern; import javax.annotation.Nullable; - import java.io.File; import java.io.IOException; import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Set; +import java.util.*; /** * An implementation of {@link ProjectFileSystem}. @@ -222,15 +211,14 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { return !testFiles(lang.getKey()).isEmpty(); } - List<InputFile> getFiles(List<File> directories, List<File> initialFiles, String[] exclusions, String[] inclusions, boolean toto, String... langs) { + List<InputFile> getFiles(List<File> directories, List<File> initialFiles, String[] patterns, String... langs) { List<InputFile> result = Lists.newArrayList(); if (directories == null) { return result; } IOFileFilter suffixFilter = getFileSuffixFilter(langs); - WildcardPattern[] exclusionPatterns = WildcardPattern.create(exclusions); - WildcardPattern[] inclusionPatterns = WildcardPattern.create(inclusions); + WildcardPattern[] exclusionPatterns = WildcardPattern.create(patterns); IOFileFilter initialFilesFilter = TrueFileFilter.INSTANCE; if (initialFiles != null && !initialFiles.isEmpty()) { @@ -239,7 +227,7 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { for (File dir : directories) { if (dir.exists()) { - IOFileFilter exclusionFilter = new ExclusionFilter(dir, exclusionPatterns, inclusionPatterns); + IOFileFilter exclusionFilter = new ExclusionFilter(dir, exclusionPatterns); IOFileFilter visibleFileFilter = HiddenFileFilter.VISIBLE; List<IOFileFilter> fileFilters = Lists.newArrayList(visibleFileFilter, suffixFilter, exclusionFilter, initialFilesFilter); fileFilters.addAll(this.filters); @@ -267,14 +255,12 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { } private static class ExclusionFilter implements IOFileFilter { - final File sourceDir; - final WildcardPattern[] exclusions; - final WildcardPattern[] inclusions; + File sourceDir; + WildcardPattern[] patterns; - ExclusionFilter(File sourceDir, WildcardPattern[] exclusions, WildcardPattern[] inclusions) { + ExclusionFilter(File sourceDir, WildcardPattern[] patterns) { this.sourceDir = sourceDir; - this.exclusions = exclusions; - this.inclusions = inclusions; + this.patterns = patterns; } public boolean accept(File file) { @@ -282,13 +268,8 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { if (relativePath == null) { return false; } - for (WildcardPattern exclusion : exclusions) { - if (exclusion.match(relativePath)) { - return false; - } - } - for (WildcardPattern inclusion : inclusions) { - if (!inclusion.match(relativePath)) { + for (WildcardPattern pattern : patterns) { + if (pattern.match(relativePath)) { return false; } } @@ -410,14 +391,14 @@ public class DefaultProjectFileSystem implements ProjectFileSystem { * @since 2.6 */ public List<InputFile> mainFiles(String... langs) { - return getFiles(getSourceDirs(), getInitialSourceFiles(), project.getExclusionPatterns(), project.getInclusionPatterns(), true, langs); + return getFiles(getSourceDirs(), getInitialSourceFiles(), project.getExclusionPatterns(), langs); } /** * @since 2.6 */ public List<InputFile> testFiles(String... langs) { - return getFiles(getTestDirs(), getInitialTestFiles(), project.getTestExclusionPatterns(), project.getTestInclusionPatterns(), true, langs); + return getFiles(getTestDirs(), getInitialTestFiles(), project.getTestExclusionPatterns(), langs); } protected List<File> getInitialSourceFiles() { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java index 812eeab7d6e..a7e8c7bd326 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/resources/Project.java @@ -362,7 +362,9 @@ public class Project extends Resource { * @since 3.3 also applies exclusions in general settings page and global exclusions. */ public String[] getExclusionPatterns() { - return removeNegativePatterns(exclusionPatterns()); + return trimExclusions(ImmutableList.<String> builder() + .add(configuration.getStringArray(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY)) + .add(configuration.getStringArray(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY)).build()); } /** @@ -372,34 +374,6 @@ public class Project extends Resource { * @since 3.3 */ public String[] getTestExclusionPatterns() { - return removeNegativePatterns(testExclusionPatterns()); - } - - /** - * Patterns of resource inclusion as defined in project settings page. - * - * @since 3.4 - */ - public String[] getInclusionPatterns() { - return onlyNegativePatterns(exclusionPatterns()); - } - - /** - * Patterns of test inclusion as defined in project settings page. - * - * @since 3.4 - */ - public String[] getTestInclusionPatterns() { - return onlyNegativePatterns(testExclusionPatterns()); - } - - private List<String> exclusionPatterns() { - return trimExclusions(ImmutableList.<String> builder() - .add(configuration.getStringArray(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY)) - .add(configuration.getStringArray(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY)).build()); - } - - private List<String> testExclusionPatterns() { String[] globalTestExclusions = configuration.getStringArray(CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY); if (globalTestExclusions.length == 0) { globalTestExclusions = new String[] {CoreProperties.GLOBAL_TEST_EXCLUSIONS_DEFAULT}; @@ -410,33 +384,13 @@ public class Project extends Resource { .add(globalTestExclusions).build()); } - private static String[] removeNegativePatterns(List<String> patterns) { - List<String> filtered = Lists.newArrayList(); - for (String pattern : patterns) { - if (!pattern.startsWith("!")) { - filtered.add(pattern); - } - } - return filtered.toArray(new String[filtered.size()]); - } - - private static String[] onlyNegativePatterns(List<String> patterns) { - List<String> filtered = Lists.newArrayList(); - for (String pattern : patterns) { - if (pattern.startsWith("!")) { - filtered.add(pattern.substring(1)); - } - } - return filtered.toArray(new String[filtered.size()]); - } - // http://jira.codehaus.org/browse/SONAR-2261 - exclusion must be trimmed - private static List<String> trimExclusions(List<String> patterns) { + private static String[] trimExclusions(List<String> exclusions) { List<String> trimmed = Lists.newArrayList(); - for (String pattern : patterns) { - trimmed.add(StringUtils.trim(pattern)); + for (String exclusion : exclusions) { + trimmed.add(StringUtils.trim(exclusion)); } - return trimmed; + return trimmed.toArray(new String[trimmed.size()]); } /** diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java index 865cdfdf4aa..da456d5b0b4 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java @@ -23,6 +23,10 @@ import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.SystemUtils; import org.apache.maven.project.MavenProject; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.Matchers; +import org.hamcrest.TypeSafeMatcher; import org.junit.Before; import org.junit.Test; import org.sonar.api.CoreProperties; @@ -34,12 +38,15 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import static org.fest.assertions.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class DefaultProjectFileSystemTest { - Project project; + + private Project project = null; @Before public void before() { @@ -51,39 +58,39 @@ public class DefaultProjectFileSystemTest { */ @Test public void shouldReturnOnlyExistingSourceAndTestDirectories() { - // in this example : "src/main/java" is a file, "src/test/java" doesn't exist + // in this example : "src/main/java" is a file, "src/test/java" doesn't exists project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "nonexistent-dirs/pom.xml"); - - ProjectFileSystem fs = project.getFileSystem(); - - assertThat(fs.getSourceDirs()).isEmpty(); - assertThat(fs.getTestDirs()).isEmpty(); + DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); + assertThat(fs.getSourceDirs().size(), is(0)); + assertThat(fs.getTestDirs().size(), is(0)); } @Test public void getJavaSourceFiles() { - ProjectFileSystem fs = project.getFileSystem(); + final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); - assertThat(fs.getJavaSourceFiles()).onProperty("name").containsOnly("Whizz.java", "Bar.java"); + assertThat(fs.getJavaSourceFiles().size(), is(2)); + assertThat(fs.getJavaSourceFiles(), hasItem(named("Bar.java"))); + assertThat(fs.getJavaSourceFiles(), hasItem(named("Whizz.java"))); } @Test public void hasJavaSourceFiles() { - ProjectFileSystem fs = project.getFileSystem(); - assertThat(fs.hasJavaSourceFiles()).isTrue(); + final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); + assertThat(fs.hasJavaSourceFiles(), is(true)); PropertiesConfiguration conf = new PropertiesConfiguration(); conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*.java"); project.setConfiguration(conf); - - assertThat(fs.hasJavaSourceFiles()).isFalse(); + assertThat(fs.hasJavaSourceFiles(), is(false)); } @Test public void getTestFiles() { - ProjectFileSystem fs = project.getFileSystem(); + final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); - assertThat(fs.getTestFiles(Java.INSTANCE)).onProperty("name").containsOnly("BarTest.java"); + assertThat(fs.getTestFiles(Java.INSTANCE).size(), is(1)); + assertThat(fs.getTestFiles(Java.INSTANCE), hasItem(named("BarTest.java"))); } @Test @@ -92,9 +99,10 @@ public class DefaultProjectFileSystemTest { conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/B*.java"); project.setConfiguration(conf); - ProjectFileSystem fs = project.getFileSystem(); + final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); - assertThat(fs.getJavaSourceFiles()).onProperty("name").containsOnly("Whizz.java"); + assertThat(fs.getJavaSourceFiles().size(), is(1)); + assertThat(fs.getJavaSourceFiles(), hasItem(named("Whizz.java"))); } /** @@ -106,20 +114,11 @@ public class DefaultProjectFileSystemTest { conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*.aj"); project.setConfiguration(conf); - ProjectFileSystem fs = project.getFileSystem(); - - assertThat(fs.getSourceFiles(Java.INSTANCE)).onProperty("name").containsOnly("Whizz.java", "Bar.java"); - } - - @Test - public void should_apply_inclusions() { - PropertiesConfiguration conf = new PropertiesConfiguration(); - conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "!**/W*.*"); - project.setConfiguration(conf); - - ProjectFileSystem fs = project.getFileSystem(); + final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); - assertThat(fs.getSourceFiles(Java.INSTANCE)).onProperty("name").containsOnly("Whizz.java"); + assertThat(fs.getSourceFiles(Java.INSTANCE).size(), is(2)); + assertThat(fs.getSourceFiles(Java.INSTANCE), hasItem(named("Whizz.java"))); + assertThat(fs.getSourceFiles(Java.INSTANCE), hasItem(named("Bar.java"))); } @Test @@ -128,26 +127,25 @@ public class DefaultProjectFileSystemTest { conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/B*.java"); project.setConfiguration(conf); - ProjectFileSystem fs = project.getFileSystem(); + final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); - assertThat(fs.getTestFiles(Java.INSTANCE)).onProperty("name").containsOnly("BarTest.java"); + assertThat(fs.getTestFiles(Java.INSTANCE).size(), is(1)); + assertThat(fs.getTestFiles(Java.INSTANCE), hasItem(named("BarTest.java"))); } @Test public void createSonarWorkingDirectory() { - ProjectFileSystem fs = project.getFileSystem(); + DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); java.io.File dir = fs.getSonarWorkingDirectory(); - - assertThat(dir).exists(); - assertThat(dir.listFiles()).isEmpty(); + assertThat(dir.exists(), is(true)); + assertThat(dir.listFiles().length, is(0)); } @Test public void getJapaneseCharSet() { project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "japanese-project/pom.xml"); - ProjectFileSystem fs = project.getFileSystem(); - - assertThat(fs.getSourceCharset().name()).isEqualTo("Shift_JIS"); + DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project); + assertThat(fs.getSourceCharset().name(), is("Shift_JIS")); } @Test @@ -167,10 +165,9 @@ public class DefaultProjectFileSystemTest { } project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "sample-with-different-suffixes/pom.xml"); - ProjectFileSystem fs = project.getFileSystem(); + ProjectFileSystem fs = newDefaultProjectFileSystem(project); List<File> files = fs.getSourceFiles(new NoSuffixLanguage()); - - assertThat(files).hasSize(2); + assertThat(files.size(), is(2)); } /** @@ -181,10 +178,10 @@ public class DefaultProjectFileSystemTest { MavenProject pom = mock(MavenProject.class); when(pom.getBasedir()).thenReturn(new File("/project")); Project project = new Project("foo").setPom(pom); - ProjectFileSystem fs = new DefaultProjectFileSystem(project, null); + DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project, null); - assertThat(fs.resolvePath(".").getAbsolutePath()).endsWith("project"); - assertThat(fs.resolvePath("../project").getAbsolutePath()).endsWith("project"); + assertThat(fs.resolvePath(".").getAbsolutePath(), endsWith("project")); + assertThat(fs.resolvePath("../project").getAbsolutePath(), endsWith("project")); } /** @@ -196,18 +193,18 @@ public class DefaultProjectFileSystemTest { // hidden files/directories can not be stored in svn windows // On Mac/Linux it's easy, just prefix the filename by '.' project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "hidden-files/pom.xml"); - ProjectFileSystem fs = project.getFileSystem(); + ProjectFileSystem fs = newDefaultProjectFileSystem(project); List<File> files = fs.getSourceFiles(); - assertThat(files).hasSize(1); - assertThat(files.get(0).getName()).isEqualTo("foo.sql"); + assertThat(files.size(), is(1)); + assertThat(files.get(0).getName(), is("foo.sql")); } } @Test public void shouldUseExtendedFilters() { - ProjectFileSystem fsWithoutFilter = project.getFileSystem(); - assertThat(fsWithoutFilter.getSourceFiles()).hasSize(2); - assertThat(fsWithoutFilter.getSourceFiles()).onProperty("name").contains("Bar.java"); + DefaultProjectFileSystem fsWithoutFilter = newDefaultProjectFileSystem(project); + assertThat(fsWithoutFilter.getSourceFiles().size(), is(2)); + assertThat(fsWithoutFilter.getSourceFiles(), hasItem(named("Bar.java"))); FileFilter filter = new FileFilter() { public boolean accept(File file) { @@ -215,21 +212,21 @@ public class DefaultProjectFileSystemTest { } }; DefaultProjectFileSystem fsWithFilter = new DefaultProjectFileSystem(project, new Languages(Java.INSTANCE), filter); - assertThat(fsWithFilter.getSourceFiles()).hasSize(1); - assertThat(fsWithFilter.getSourceFiles()).onProperty("name").excludes("Bar.java"); + assertThat(fsWithFilter.getSourceFiles().size(), is(1)); + assertThat(fsWithFilter.getSourceFiles(), not(hasItem(named("Bar.java")))); } @Test public void testSelectiveFileFilter() { DefaultProjectFileSystem.FileSelectionFilter filter = new DefaultProjectFileSystem.FileSelectionFilter( - Arrays.asList(new File("foo/Bar.java"), new File("hello/Bar.java"), new File("hello/World.java"))); - assertThat(filter.accept(new File("foo/Bar.java"))).isTrue(); - assertThat(filter.accept(new File("hello/Bar.java"))).isTrue(); - assertThat(filter.accept(new File("hello/World.java"))).isTrue(); - - assertThat(filter.accept(new File("foo/Unknown.java"))).isFalse(); - assertThat(filter.accept(new File("foo/bar/Bar.java"))).isFalse(); - assertThat(filter.accept(new File("foo/World.java"))).isFalse(); + Arrays.asList(new File("foo/Bar.java"), new File("hello/Bar.java"), new File("hello/World.java"))); + assertThat(filter.accept(new File("foo/Bar.java")), Matchers.is(true)); + assertThat(filter.accept(new File("hello/Bar.java")), Matchers.is(true)); + assertThat(filter.accept(new File("hello/World.java")), Matchers.is(true)); + + assertThat(filter.accept(new File("foo/Unknown.java")), Matchers.is(false)); + assertThat(filter.accept(new File("foo/bar/Bar.java")), Matchers.is(false)); + assertThat(filter.accept(new File("foo/World.java")), Matchers.is(false)); } /** @@ -239,8 +236,33 @@ public class DefaultProjectFileSystemTest { public void shouldExcludeDirectoriesStartingWithDot() { List<File> dirs = Arrays.asList(new File("test-resources/org/sonar/api/resources/DefaultProjectFileSystemTest/shouldExcludeDirectoriesStartingWithDot/src")); - List<InputFile> files = new DefaultProjectFileSystem(new Project("foo"), null).getFiles(dirs, Collections.<File> emptyList(), new String[0], new String[0], true); - assertThat(files).hasSize(1); - assertThat(files.get(0).getRelativePath()).isEqualTo("org/sonar/Included.java"); + List<InputFile> files = new DefaultProjectFileSystem(new Project("foo"), null).getFiles(dirs, Collections.<File>emptyList(), new String[0]); + assertThat(files.size(), is(1)); + assertThat(files.get(0).getRelativePath(), is("org/sonar/Included.java")); + } + + private DefaultProjectFileSystem newDefaultProjectFileSystem(Project project) { + return (DefaultProjectFileSystem) project.getFileSystem(); + } + + private static Matcher<java.io.File> named(final String name) { + return new TypeSafeMatcher<java.io.File>() { + java.io.File fileTested; + + @Override + public boolean matchesSafely(java.io.File item) { + fileTested = item; + return name.equals(item.getName()); + } + + public void describeTo(Description description) { + description.appendText(" that file "); + description.appendValue(fileTested); + description.appendText(" is named"); + description.appendText(name); + description.appendText(" not "); + description.appendValue(fileTested.getName()); + } + }; } -} +}
\ No newline at end of file diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java index 2ff1a1a6ec0..81c3f209886 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/resources/ProjectTest.java @@ -103,26 +103,4 @@ public class ProjectTest { assertThat(project.getExclusionPatterns()).containsOnly("**/*Foo.java", "**/*Bar.java"); } - - @Test - public void should_support_inclusion_patterns() { - conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "!**/*foo.java, **/*bar.java"); - conf.setProperty(CoreProperties.GLOBAL_EXCLUSIONS_PROPERTY, "!**/*quix.java"); - - Project project = new Project("key").setConfiguration(conf); - - assertThat(project.getExclusionPatterns()).containsOnly("**/*bar.java"); - assertThat(project.getInclusionPatterns()).containsOnly("**/*foo.java", "**/*quix.java"); - } - - @Test - public void should_support_test_inclusion_patterns() { - conf.setProperty(CoreProperties.PROJECT_TEST_EXCLUSIONS_PROPERTY, "!**/*Test.java, **/*FunctionalTest.java"); - conf.setProperty(CoreProperties.GLOBAL_TEST_EXCLUSIONS_PROPERTY, "!**/*IntegrationTest.java"); - - Project project = new Project("key").setConfiguration(conf); - - assertThat(project.getTestExclusionPatterns()).containsOnly("**/*FunctionalTest.java"); - assertThat(project.getTestInclusionPatterns()).containsOnly("**/*Test.java", "**/*IntegrationTest.java"); - } } |