public class ExclusionFilters implements BatchComponent {
private final FileExclusions exclusionSettings;
+ private PathPattern[] sourceInclusions;
+ private PathPattern[] sourceExclusions;
+ private PathPattern[] testInclusions;
+ private PathPattern[] testExclusions;
+
public ExclusionFilters(FileExclusions exclusions) {
this.exclusionSettings = exclusions;
}
- public void logConfiguration(ModuleFileSystem fs) {
- log("Included sources: ", sourceInclusions(fs));
- log("Excluded sources: ", sourceExclusions());
- log("Included tests: ", testInclusions(fs));
- log("Excluded tests: ", testExclusions());
+ public void prepare(ModuleFileSystem fs) {
+ sourceInclusions = computeSourceInclusions(fs);
+ sourceExclusions = computeSourceExclusions();
+ testInclusions = computeTestInclusions(fs);
+ testExclusions = computeTestExclusions();
+ log("Included sources: ", sourceInclusions);
+ log("Excluded sources: ", sourceExclusions);
+ log("Included tests: ", testInclusions);
+ log("Excluded tests: ", testExclusions);
}
private void log(String title, PathPattern[] patterns) {
}
}
- public boolean accept(InputFile inputFile, ModuleFileSystem fs) {
- String type = inputFile.attribute(InputFile.ATTRIBUTE_TYPE);
+ public boolean accept(File ioFile, String relativePathFromBasedir, String type) {
PathPattern[] inclusionPatterns = null;
PathPattern[] exclusionPatterns = null;
if (InputFile.TYPE_MAIN.equals(type)) {
- inclusionPatterns = sourceInclusions(fs);
- exclusionPatterns = sourceExclusions();
+ inclusionPatterns = sourceInclusions;
+ exclusionPatterns = sourceExclusions;
} else if (InputFile.TYPE_TEST.equals(type)) {
- inclusionPatterns = testInclusions(fs);
- exclusionPatterns = testExclusions();
+ inclusionPatterns = testInclusions;
+ exclusionPatterns = testExclusions;
}
boolean matchInclusion = false;
if (inclusionPatterns != null && inclusionPatterns.length > 0) {
for (PathPattern pattern : inclusionPatterns) {
- matchInclusion |= pattern.match(inputFile);
+ matchInclusion |= pattern.match(ioFile, relativePathFromBasedir);
}
if (!matchInclusion) {
return false;
}
if (exclusionPatterns != null && exclusionPatterns.length > 0) {
for (PathPattern pattern : exclusionPatterns) {
- if (pattern.match(inputFile)) {
+ if (pattern.match(ioFile, relativePathFromBasedir)) {
return false;
}
}
return matchInclusion;
}
- PathPattern[] sourceInclusions(ModuleFileSystem fs) {
+ PathPattern[] computeSourceInclusions(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[] testInclusions(ModuleFileSystem fs) {
+ PathPattern[] computeTestInclusions(ModuleFileSystem fs) {
if (exclusionSettings.testInclusions().length > 0) {
// User defined params
return PathPattern.create(exclusionSettings.testInclusions());
return PathPattern.create(testPatterns.toArray(new String[testPatterns.size()]));
}
- PathPattern[] sourceExclusions() {
+ PathPattern[] computeSourceExclusions() {
return PathPattern.create(exclusionSettings.sourceExclusions());
}
- PathPattern[] testExclusions() {
+ PathPattern[] computeTestExclusions() {
return PathPattern.create(exclusionSettings.testExclusions());
}
}
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+
import java.io.File;
import java.nio.charset.Charset;
-import java.util.*;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
/**
* Index input files into {@link InputFileCache}.
return;
}
logger.info("Index files");
- exclusionFilters.logConfiguration(fileSystem);
+ exclusionFilters.prepare(fileSystem);
// TODO log configuration too (replace FileSystemLogger)
Progress progress = new Progress(fileCache.fileRelativePaths(fileSystem.moduleKey()));
indexFiles(fileSystem, progress, fileSystem.testFiles(), InputFile.TYPE_TEST);
} else if (fileSystem.baseDir() != null) {
// index from basedir
- indexDirectory(fileSystem, progress, fileSystem.baseDir(), InputFile.TYPE_MAIN);
- indexDirectory(fileSystem, progress, fileSystem.baseDir(), InputFile.TYPE_TEST);
+ indexDirectory(fileSystem, progress, fileSystem.baseDir());
}
// Remove files that have been removed since previous indexation
"File '%s' is not declared in module basedir %s", sourceFile.getAbsoluteFile(), fileSystem.baseDir()
));
} else {
- indexFile(fileSystem, progress, sourceFile, type);
+ if (exclusionFilters.accept(sourceFile, path, type)) {
+ indexFile(fileSystem, progress, sourceFile, path, type);
+ }
}
}
}
return DefaultInputDir.create(ioFile, path, attributes);
}
- private void indexDirectory(DefaultModuleFileSystem fileSystem, Progress status, File dirToIndex, String type) {
+ private void indexDirectory(DefaultModuleFileSystem fileSystem, Progress status, File dirToIndex) {
Collection<File> files = FileUtils.listFiles(dirToIndex, FILE_FILTER, DIR_FILTER);
- for (File file : files) {
- indexFile(fileSystem, status, file, type);
+ for (File sourceFile : files) {
+ String path = pathResolver.relativePath(fileSystem.baseDir(), sourceFile);
+ if (path == null) {
+ LoggerFactory.getLogger(getClass()).warn(String.format(
+ "File '%s' is not declared in module basedir %s", sourceFile.getAbsoluteFile(), fileSystem.baseDir()
+ ));
+ } else {
+ if (exclusionFilters.accept(sourceFile, path, InputFile.TYPE_MAIN)) {
+ indexFile(fileSystem, status, sourceFile, path, InputFile.TYPE_MAIN);
+ }
+ if (exclusionFilters.accept(sourceFile, path, InputFile.TYPE_TEST)) {
+ indexFile(fileSystem, status, sourceFile, path, InputFile.TYPE_TEST);
+ }
+ }
}
}
- private void indexFile(DefaultModuleFileSystem fileSystem, Progress status, File file, String type) {
- String path = computeFilePath(fileSystem, file);
- if (path == null) {
- LoggerFactory.getLogger(getClass()).warn(String.format("File '%s' is not in basedir '%s'", file.getAbsolutePath(), fileSystem.baseDir()));
- } else {
- InputFile input = newInputFile(fileSystem, type, file, path);
- if (input != null && accept(input, fileSystem)) {
- fileCache.put(fileSystem.moduleKey(), input);
- status.markAsIndexed(path);
- }
+ private void indexFile(DefaultModuleFileSystem fileSystem, Progress status, File file, String path, String type) {
+ InputFile input = newInputFile(fileSystem, type, file, path);
+ if (input != null && accept(input)) {
+ fileCache.put(fileSystem.moduleKey(), input);
+ status.markAsIndexed(path);
}
}
}
}
- private boolean accept(InputFile inputFile, ModuleFileSystem fs) {
- if (!exclusionFilters.accept(inputFile, fs)) {
- return false;
- }
- // Other InputFileFilter extensions
+ private boolean accept(InputFile inputFile) {
+ // InputFileFilter extensions
for (InputFileFilter filter : filters) {
if (!filter.accept(inputFile)) {
return false;
*/
package org.sonar.batch.scan.filesystem;
-import com.google.common.base.Charsets;
-import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.scan.filesystem.FileExclusions;
import org.sonar.api.scan.filesystem.InputFile;
import org.sonar.api.scan.filesystem.ModuleFileSystem;
-import org.sonar.api.scan.filesystem.internal.DefaultInputFile;
import java.io.File;
import java.io.IOException;
Settings settings = new Settings();
settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*Dao.java");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
+ filter.prepare(fs);
java.io.File file = temp.newFile();
- InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN
- ));
- assertThat(filter.accept(inputFile, fs)).isFalse();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/Foo.java", InputFile.TYPE_MAIN)).isFalse();
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/FooDao.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN));
- assertThat(filter.accept(inputFile, fs)).isTrue();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/FooDao.java", InputFile.TYPE_MAIN)).isTrue();
// test are excluded by default if no sonar.tests nor sonar.test.inclusions
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST));
- assertThat(filter.accept(inputFile, fs)).isFalse();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/Foo.java", InputFile.TYPE_TEST)).isFalse();
}
@Test
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
when(fs.sourceDirs()).thenReturn(Arrays.asList(new File(basedir, "src/main/java")));
- filter.logConfiguration(fs);
+ filter.prepare(fs);
java.io.File file = temp.newFile();
- InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN
- ));
- assertThat(filter.accept(inputFile, fs)).isTrue();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/Foo.java", InputFile.TYPE_MAIN)).isTrue();
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java2/com/mycompany/FooDao.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN));
- assertThat(filter.accept(inputFile, fs)).isFalse();
+ assertThat(filter.accept(file, "src/main/java2/com/mycompany/FooDao.java", InputFile.TYPE_MAIN)).isFalse();
// source inclusions do not apply to tests
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST));
- assertThat(filter.accept(inputFile, fs)).isFalse();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/Foo.java", InputFile.TYPE_TEST)).isFalse();
}
@Test
when(fs.sourceDirs()).thenReturn(Arrays.asList(new File(basedir, "src/main/java")));
when(fs.testDirs()).thenReturn(Arrays.asList(new File(basedir, "src/test/java")));
- filter.logConfiguration(fs);
+ filter.prepare(fs);
java.io.File file = temp.newFile();
- InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN
- ));
- assertThat(filter.accept(inputFile, fs)).isTrue();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/Foo.java", InputFile.TYPE_MAIN)).isTrue();
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST));
- assertThat(filter.accept(inputFile, fs)).isFalse();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/Foo.java", InputFile.TYPE_TEST)).isFalse();
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/test/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST));
- assertThat(filter.accept(inputFile, fs)).isTrue();
+ assertThat(filter.accept(file, "src/test/java/com/mycompany/Foo.java", InputFile.TYPE_TEST)).isTrue();
}
@Test
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
when(fs.sourceDirs()).thenReturn(Arrays.asList(new File(basedir, "src/main/java")));
- filter.logConfiguration(fs);
+ filter.prepare(fs);
java.io.File file = temp.newFile();
- InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN
- ));
- assertThat(filter.accept(inputFile, fs)).isFalse();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/Foo.java", InputFile.TYPE_MAIN)).isFalse();
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java2/com/mycompany/FooDao.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN));
- assertThat(filter.accept(inputFile, fs)).isTrue();
+ assertThat(filter.accept(file, "src/main/java2/com/mycompany/FooDao.java", InputFile.TYPE_MAIN)).isTrue();
// source inclusions do not apply to tests
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST));
- assertThat(filter.accept(inputFile, fs)).isFalse();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/Foo.java", InputFile.TYPE_TEST)).isFalse();
}
@Test
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
when(fs.testDirs()).thenReturn(Arrays.asList(new File(basedir, "src/test/java")));
- filter.logConfiguration(fs);
+ filter.prepare(fs);
java.io.File file = temp.newFile();
- InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/test/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN
- ));
// test inclusions do not apply to main code
- assertThat(filter.accept(inputFile, fs)).isFalse();
+ assertThat(filter.accept(file, "src/test/java/com/mycompany/Foo.java", InputFile.TYPE_MAIN)).isFalse();
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/test2/java/com/mycompany/FooTest.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST));
- assertThat(filter.accept(inputFile, fs)).isFalse();
+ assertThat(filter.accept(file, "src/test2/java/com/mycompany/FooTest.java", InputFile.TYPE_TEST)).isFalse();
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/test/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST));
- assertThat(filter.accept(inputFile, fs)).isTrue();
+ assertThat(filter.accept(file, "src/test/java/com/mycompany/Foo.java", InputFile.TYPE_TEST)).isTrue();
}
@Test
settings.setProperty(CoreProperties.PROJECT_INCLUSIONS_PROPERTY, "**/*Dao.java,**/*Dto.java");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
+ filter.prepare(fs);
+
java.io.File file = temp.newFile();
- InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN
- ));
- assertThat(filter.accept(inputFile, fs)).isFalse();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/Foo.java", InputFile.TYPE_MAIN)).isFalse();
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/FooDto.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN
- ));
- assertThat(filter.accept(inputFile, fs)).isTrue();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/FooDto.java", InputFile.TYPE_MAIN)).isTrue();
}
@Test
settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "**/*Dao.java");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
+ filter.prepare(fs);
+
java.io.File file = temp.newFile();
- InputFile inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/FooDao.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN
- ));
- assertThat(filter.accept(inputFile, fs)).isFalse();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/FooDao.java", InputFile.TYPE_MAIN)).isFalse();
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/main/java/com/mycompany/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN));
- assertThat(filter.accept(inputFile, fs)).isTrue();
+ assertThat(filter.accept(file, "src/main/java/com/mycompany/Foo.java", InputFile.TYPE_MAIN)).isTrue();
// source exclusions do not apply to tests
- inputFile = DefaultInputFile.create(file, Charsets.UTF_8, "src/test/java/com/mycompany/FooDao.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST));
- assertThat(filter.accept(inputFile, fs)).isTrue();
+ assertThat(filter.accept(file, "src/test/java/com/mycompany/FooDao.java", InputFile.TYPE_TEST)).isTrue();
}
@Test
settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, "file:" + excludedFile.getCanonicalPath());
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- InputFile includedInput = DefaultInputFile.create(includedFile, Charsets.UTF_8, "src/main/java/org/bar/Foo.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN
- ));
- assertThat(filter.accept(includedInput, fs)).isTrue();
+ filter.prepare(fs);
+
+ assertThat(filter.accept(includedFile, "src/main/java/org/bar/Foo.java", InputFile.TYPE_MAIN)).isTrue();
- InputFile excludedInput = DefaultInputFile.create(excludedFile, Charsets.UTF_8, "src/main/java/org/bar/Bar.java", ImmutableMap.of(
- InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_MAIN
- ));
- assertThat(filter.accept(excludedInput, fs)).isFalse();
+ assertThat(filter.accept(excludedFile, "src/main/java/org/bar/Bar.java", InputFile.TYPE_MAIN)).isFalse();
}
@Test
settings.setProperty(CoreProperties.PROJECT_EXCLUSIONS_PROPERTY, " **/*Dao.java ");
ExclusionFilters filter = new ExclusionFilters(new FileExclusions(settings));
- assertThat(filter.sourceExclusions()[0].toString()).isEqualTo("**/*Dao.java");
+ assertThat(filter.computeSourceExclusions()[0].toString()).isEqualTo("**/*Dao.java");
}
}