*/
package org.sonar.batch.scan.filesystem;
+import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.BatchComponent;
public class ExclusionFilters implements BatchComponent {
private final FileExclusions exclusionSettings;
- private PathPattern[] sourceInclusions;
- private PathPattern[] sourceExclusions;
+ private PathPattern[] mainInclusions;
+ private PathPattern[] mainExclusions;
private PathPattern[] testInclusions;
private PathPattern[] testExclusions;
}
public void prepare(ModuleFileSystem fs) {
- sourceInclusions = computeSourceInclusions(fs);
- sourceExclusions = computeSourceExclusions();
- testInclusions = computeTestInclusions(fs);
- testExclusions = computeTestExclusions();
- log("Included sources: ", sourceInclusions);
- log("Excluded sources: ", sourceExclusions);
+ mainInclusions = prepareMainInclusions(fs);
+ mainExclusions = prepareMainExclusions(fs);
+ testInclusions = prepareTestInclusions(fs);
+ testExclusions = prepareTestExclusions();
+ log("Included sources: ", mainInclusions);
+ log("Excluded sources: ", mainExclusions);
log("Included tests: ", testInclusions);
log("Excluded tests: ", testExclusions);
}
public boolean accept(InputFile inputFile, InputFile.Type type) {
PathPattern[] inclusionPatterns = null;
PathPattern[] exclusionPatterns = null;
- if (InputFile.Type.MAIN==type) {
- inclusionPatterns = sourceInclusions;
- exclusionPatterns = sourceExclusions;
- } else if (InputFile.Type.TEST==type) {
+ if (InputFile.Type.MAIN == type) {
+ inclusionPatterns = mainInclusions;
+ exclusionPatterns = mainExclusions;
+ } else if (InputFile.Type.TEST == type) {
inclusionPatterns = testInclusions;
exclusionPatterns = testExclusions;
}
return matchInclusion;
}
- PathPattern[] computeSourceInclusions(ModuleFileSystem fs) {
+ PathPattern[] prepareMainInclusions(ModuleFileSystem fs) {
if (exclusionSettings.sourceInclusions().length > 0) {
// User defined params
return PathPattern.create(exclusionSettings.sourceInclusions());
return PathPattern.create(sourcePattern.toArray(new String[sourcePattern.size()]));
}
- PathPattern[] computeTestInclusions(ModuleFileSystem fs) {
+ PathPattern[] prepareTestInclusions(ModuleFileSystem fs) {
+ return PathPattern.create(computeTestInclusions(fs));
+ }
+
+ private String[] computeTestInclusions(ModuleFileSystem fs) {
if (exclusionSettings.testInclusions().length > 0) {
// User defined params
- return PathPattern.create(exclusionSettings.testInclusions());
+ return exclusionSettings.testInclusions();
}
// Convert source directories to inclusions
List<String> testPatterns = new ArrayList<String>();
String path = new PathResolver().relativePath(fs.baseDir(), test);
testPatterns.add(path + "/**");
}
- return PathPattern.create(testPatterns.toArray(new String[testPatterns.size()]));
+ return testPatterns.toArray(new String[testPatterns.size()]);
}
- PathPattern[] computeSourceExclusions() {
- return PathPattern.create(exclusionSettings.sourceExclusions());
+ PathPattern[] prepareMainExclusions(ModuleFileSystem fs) {
+ String[] patterns = (String[]) ArrayUtils.addAll(
+ exclusionSettings.sourceExclusions(), computeTestInclusions(fs));
+ return PathPattern.create(patterns);
}
- PathPattern[] computeTestExclusions() {
+ PathPattern[] prepareTestExclusions() {
return PathPattern.create(exclusionSettings.testExclusions());
}
}
}
@Test
- public void should_match_source_inclusion() throws IOException {
+ public void match_main_inclusion() throws IOException {
Settings settings = new Settings();
settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*Dao.java");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
}
@Test
- public void should_include_source_folders_by_default() throws IOException {
+ public void include_main_folders_by_default() throws IOException {
Settings settings = new Settings();
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
when(fs.sourceDirs()).thenReturn(Arrays.asList(new File(basedir, "src/main/java")));
}
@Test
- public void should_include_source_and_test_folders_by_default() throws IOException {
+ public void include_main_and_test_folders_by_default() throws IOException {
Settings settings = new Settings();
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
when(fs.sourceDirs()).thenReturn(Arrays.asList(new File(basedir, "src/main/java")));
}
@Test
- public void should_ignore_source_folders_if_inclusion_defined() throws IOException {
+ public void exclude_test_inclusions_from_main_sources() throws IOException {
+ Settings settings = new Settings();
+ settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*.java");
+ settings.setProperty(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY, "src/test/java/**/*.java");
+ ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
+
+ filter.prepare(fs);
+
+ java.io.File file = temp.newFile();
+
+ DefaultInputFile inputFile = new DefaultInputFile("src/main/java/com/mycompany/Foo.java").setFile(file);
+ assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isTrue();
+ assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isFalse();
+
+ inputFile = new DefaultInputFile("src/test/java/com/mycompany/Foo.java").setFile(file);
+ assertThat(filter.accept(inputFile, InputFile.Type.MAIN)).isFalse();
+ assertThat(filter.accept(inputFile, InputFile.Type.TEST)).isTrue();
+ }
+
+ @Test
+ public void ignore_main_folders_if_inclusion_defined() throws IOException {
Settings settings = new Settings();
settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "src/main/java2/**/*");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
}
@Test
- public void should_include_test_folders_by_default() throws IOException {
+ public void include_test_folders_by_default() throws IOException {
Settings settings = new Settings();
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
when(fs.testDirs()).thenReturn(Arrays.asList(new File(basedir, "src/test/java")));
}
@Test
- public void should_match_at_least_one_source_inclusion() throws IOException {
+ public void match_at_least_one_source_inclusion() throws IOException {
Settings settings = new Settings();
settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*Dao.java,**/*Dto.java");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
}
@Test
- public void should_match_source_exclusions() throws IOException {
+ public void match_source_exclusions() throws IOException {
Settings settings = new Settings();
settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "src/main/java/**/*");
settings.setProperty(CoreProperties.PROJECT_TEST_INCLUSIONS_PROPERTY, "src/test/java/**/*");
}
@Test
- public void should_match_source_exclusion_by_absolute_path() throws IOException {
+ public void match_source_exclusion_by_absolute_path() throws IOException {
java.io.File includedFile = temp.newFile("Foo.java");
java.io.File excludedFile = temp.newFile("Bar.java");
}
@Test
- public void should_trim_pattern() throws IOException {
+ public void trim_pattern() throws IOException {
Settings settings = new Settings();
settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, " **/*Dao.java ");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- assertThat(filter.computeSourceExclusions()[0].toString()).isEqualTo("**/*Dao.java");
+ assertThat(filter.prepareMainExclusions(fs)[0].toString()).isEqualTo("**/*Dao.java");
}
}