aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorDavid Gageot <david@gageot.net>2012-11-06 15:33:57 +0100
committerDavid Gageot <david@gageot.net>2012-11-06 15:46:10 +0100
commit0ca3e750948ff947c6ab5918cb7f36c9ad0095bb (patch)
tree181d7c36a4284efef7bb3a649cdf17cf6e120d92 /sonar-plugin-api
parentc69fa0d61d84df1a411f58811125510d93c07c08 (diff)
downloadsonarqube-0ca3e750948ff947c6ab5918cb7f36c9ad0095bb.tar.gz
sonarqube-0ca3e750948ff947c6ab5918cb7f36c9ad0095bb.zip
SONAR-3885 Inclusions
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/resources/DefaultProjectFileSystem.java45
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/resources/DefaultProjectFileSystemTest.java154
2 files changed, 98 insertions, 101 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 5bef696d85b..6fc144a846d 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,7 +26,13 @@ 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.*;
+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.lang.CharEncoding;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.CoreProperties;
@@ -36,10 +42,15 @@ 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.*;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Set;
/**
* An implementation of {@link ProjectFileSystem}.
@@ -211,14 +222,15 @@ public class DefaultProjectFileSystem implements ProjectFileSystem {
return !testFiles(lang.getKey()).isEmpty();
}
- List<InputFile> getFiles(List<File> directories, List<File> initialFiles, String[] patterns, String... langs) {
+ List<InputFile> getFiles(List<File> directories, List<File> initialFiles, String[] exclusions, String[] inclusions, boolean toto, String... langs) {
List<InputFile> result = Lists.newArrayList();
if (directories == null) {
return result;
}
IOFileFilter suffixFilter = getFileSuffixFilter(langs);
- WildcardPattern[] exclusionPatterns = WildcardPattern.create(patterns);
+ WildcardPattern[] exclusionPatterns = WildcardPattern.create(exclusions);
+ WildcardPattern[] inclusionPatterns = WildcardPattern.create(inclusions);
IOFileFilter initialFilesFilter = TrueFileFilter.INSTANCE;
if (initialFiles != null && !initialFiles.isEmpty()) {
@@ -227,7 +239,7 @@ public class DefaultProjectFileSystem implements ProjectFileSystem {
for (File dir : directories) {
if (dir.exists()) {
- IOFileFilter exclusionFilter = new ExclusionFilter(dir, exclusionPatterns);
+ IOFileFilter exclusionFilter = new ExclusionFilter(dir, exclusionPatterns, inclusionPatterns);
IOFileFilter visibleFileFilter = HiddenFileFilter.VISIBLE;
List<IOFileFilter> fileFilters = Lists.newArrayList(visibleFileFilter, suffixFilter, exclusionFilter, initialFilesFilter);
fileFilters.addAll(this.filters);
@@ -255,12 +267,14 @@ public class DefaultProjectFileSystem implements ProjectFileSystem {
}
private static class ExclusionFilter implements IOFileFilter {
- File sourceDir;
- WildcardPattern[] patterns;
+ final File sourceDir;
+ final WildcardPattern[] exclusions;
+ final WildcardPattern[] inclusions;
- ExclusionFilter(File sourceDir, WildcardPattern[] patterns) {
+ ExclusionFilter(File sourceDir, WildcardPattern[] exclusions, WildcardPattern[] inclusions) {
this.sourceDir = sourceDir;
- this.patterns = patterns;
+ this.exclusions = exclusions;
+ this.inclusions = inclusions;
}
public boolean accept(File file) {
@@ -268,8 +282,13 @@ public class DefaultProjectFileSystem implements ProjectFileSystem {
if (relativePath == null) {
return false;
}
- for (WildcardPattern pattern : patterns) {
- if (pattern.match(relativePath)) {
+ for (WildcardPattern exclusion : exclusions) {
+ if (exclusion.match(relativePath)) {
+ return false;
+ }
+ }
+ for (WildcardPattern inclusion : inclusions) {
+ if (!inclusion.match(relativePath)) {
return false;
}
}
@@ -391,14 +410,14 @@ public class DefaultProjectFileSystem implements ProjectFileSystem {
* @since 2.6
*/
public List<InputFile> mainFiles(String... langs) {
- return getFiles(getSourceDirs(), getInitialSourceFiles(), project.getExclusionPatterns(), langs);
+ return getFiles(getSourceDirs(), getInitialSourceFiles(), project.getExclusionPatterns(), project.getInclusionPatterns(), true, langs);
}
/**
* @since 2.6
*/
public List<InputFile> testFiles(String... langs) {
- return getFiles(getTestDirs(), getInitialTestFiles(), project.getTestExclusionPatterns(), langs);
+ return getFiles(getTestDirs(), getInitialTestFiles(), project.getTestExclusionPatterns(), project.getTestInclusionPatterns(), true, langs);
}
protected List<File> getInitialSourceFiles() {
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 da456d5b0b4..865cdfdf4aa 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,10 +23,6 @@ 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;
@@ -38,15 +34,12 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.Matchers.*;
-import static org.junit.Assert.assertThat;
+import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class DefaultProjectFileSystemTest {
-
- private Project project = null;
+ Project project;
@Before
public void before() {
@@ -58,39 +51,39 @@ public class DefaultProjectFileSystemTest {
*/
@Test
public void shouldReturnOnlyExistingSourceAndTestDirectories() {
- // in this example : "src/main/java" is a file, "src/test/java" doesn't exists
+ // in this example : "src/main/java" is a file, "src/test/java" doesn't exist
project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "nonexistent-dirs/pom.xml");
- DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
- assertThat(fs.getSourceDirs().size(), is(0));
- assertThat(fs.getTestDirs().size(), is(0));
+
+ ProjectFileSystem fs = project.getFileSystem();
+
+ assertThat(fs.getSourceDirs()).isEmpty();
+ assertThat(fs.getTestDirs()).isEmpty();
}
@Test
public void getJavaSourceFiles() {
- final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
+ ProjectFileSystem fs = project.getFileSystem();
- assertThat(fs.getJavaSourceFiles().size(), is(2));
- assertThat(fs.getJavaSourceFiles(), hasItem(named("Bar.java")));
- assertThat(fs.getJavaSourceFiles(), hasItem(named("Whizz.java")));
+ assertThat(fs.getJavaSourceFiles()).onProperty("name").containsOnly("Whizz.java", "Bar.java");
}
@Test
public void hasJavaSourceFiles() {
- final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
- assertThat(fs.hasJavaSourceFiles(), is(true));
+ ProjectFileSystem fs = project.getFileSystem();
+ assertThat(fs.hasJavaSourceFiles()).isTrue();
PropertiesConfiguration conf = new PropertiesConfiguration();
conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*.java");
project.setConfiguration(conf);
- assertThat(fs.hasJavaSourceFiles(), is(false));
+
+ assertThat(fs.hasJavaSourceFiles()).isFalse();
}
@Test
public void getTestFiles() {
- final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
+ ProjectFileSystem fs = project.getFileSystem();
- assertThat(fs.getTestFiles(Java.INSTANCE).size(), is(1));
- assertThat(fs.getTestFiles(Java.INSTANCE), hasItem(named("BarTest.java")));
+ assertThat(fs.getTestFiles(Java.INSTANCE)).onProperty("name").containsOnly("BarTest.java");
}
@Test
@@ -99,10 +92,9 @@ public class DefaultProjectFileSystemTest {
conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/B*.java");
project.setConfiguration(conf);
- final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
+ ProjectFileSystem fs = project.getFileSystem();
- assertThat(fs.getJavaSourceFiles().size(), is(1));
- assertThat(fs.getJavaSourceFiles(), hasItem(named("Whizz.java")));
+ assertThat(fs.getJavaSourceFiles()).onProperty("name").containsOnly("Whizz.java");
}
/**
@@ -114,11 +106,20 @@ public class DefaultProjectFileSystemTest {
conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*.aj");
project.setConfiguration(conf);
- final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
+ 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();
- 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")));
+ assertThat(fs.getSourceFiles(Java.INSTANCE)).onProperty("name").containsOnly("Whizz.java");
}
@Test
@@ -127,25 +128,26 @@ public class DefaultProjectFileSystemTest {
conf.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/B*.java");
project.setConfiguration(conf);
- final DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
+ ProjectFileSystem fs = project.getFileSystem();
- assertThat(fs.getTestFiles(Java.INSTANCE).size(), is(1));
- assertThat(fs.getTestFiles(Java.INSTANCE), hasItem(named("BarTest.java")));
+ assertThat(fs.getTestFiles(Java.INSTANCE)).onProperty("name").containsOnly("BarTest.java");
}
@Test
public void createSonarWorkingDirectory() {
- DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
+ ProjectFileSystem fs = project.getFileSystem();
java.io.File dir = fs.getSonarWorkingDirectory();
- assertThat(dir.exists(), is(true));
- assertThat(dir.listFiles().length, is(0));
+
+ assertThat(dir).exists();
+ assertThat(dir.listFiles()).isEmpty();
}
@Test
public void getJapaneseCharSet() {
project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "japanese-project/pom.xml");
- DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
- assertThat(fs.getSourceCharset().name(), is("Shift_JIS"));
+ ProjectFileSystem fs = project.getFileSystem();
+
+ assertThat(fs.getSourceCharset().name()).isEqualTo("Shift_JIS");
}
@Test
@@ -165,9 +167,10 @@ public class DefaultProjectFileSystemTest {
}
project = MavenTestUtils.loadProjectFromPom(DefaultProjectFileSystemTest.class, "sample-with-different-suffixes/pom.xml");
- ProjectFileSystem fs = newDefaultProjectFileSystem(project);
+ ProjectFileSystem fs = project.getFileSystem();
List<File> files = fs.getSourceFiles(new NoSuffixLanguage());
- assertThat(files.size(), is(2));
+
+ assertThat(files).hasSize(2);
}
/**
@@ -178,10 +181,10 @@ public class DefaultProjectFileSystemTest {
MavenProject pom = mock(MavenProject.class);
when(pom.getBasedir()).thenReturn(new File("/project"));
Project project = new Project("foo").setPom(pom);
- DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project, null);
+ ProjectFileSystem 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");
}
/**
@@ -193,18 +196,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 = newDefaultProjectFileSystem(project);
+ ProjectFileSystem fs = project.getFileSystem();
List<File> files = fs.getSourceFiles();
- assertThat(files.size(), is(1));
- assertThat(files.get(0).getName(), is("foo.sql"));
+ assertThat(files).hasSize(1);
+ assertThat(files.get(0).getName()).isEqualTo("foo.sql");
}
}
@Test
public void shouldUseExtendedFilters() {
- DefaultProjectFileSystem fsWithoutFilter = newDefaultProjectFileSystem(project);
- assertThat(fsWithoutFilter.getSourceFiles().size(), is(2));
- assertThat(fsWithoutFilter.getSourceFiles(), hasItem(named("Bar.java")));
+ ProjectFileSystem fsWithoutFilter = project.getFileSystem();
+ assertThat(fsWithoutFilter.getSourceFiles()).hasSize(2);
+ assertThat(fsWithoutFilter.getSourceFiles()).onProperty("name").contains("Bar.java");
FileFilter filter = new FileFilter() {
public boolean accept(File file) {
@@ -212,21 +215,21 @@ public class DefaultProjectFileSystemTest {
}
};
DefaultProjectFileSystem fsWithFilter = new DefaultProjectFileSystem(project, new Languages(Java.INSTANCE), filter);
- assertThat(fsWithFilter.getSourceFiles().size(), is(1));
- assertThat(fsWithFilter.getSourceFiles(), not(hasItem(named("Bar.java"))));
+ assertThat(fsWithFilter.getSourceFiles()).hasSize(1);
+ assertThat(fsWithFilter.getSourceFiles()).onProperty("name").excludes("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")), 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));
+ 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();
}
/**
@@ -236,33 +239,8 @@ 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]);
- 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());
- }
- };
+ 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");
}
-} \ No newline at end of file
+}