diff options
Diffstat (limited to 'sonar-plugin-api')
4 files changed, 108 insertions, 173 deletions
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"); - } } |