From 5a1634e38b510c684868cfe96185929622010ece Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 18 Feb 2013 13:13:58 +0100 Subject: [PATCH] Fix backward-compatibility. File system can be changed by sensors... --- .../java/org/sonar/batch/phases/Phases.java | 10 +- .../org/sonar/batch/scan/ScanContainer.java | 2 + .../filesystem/DefaultModuleFileSystem.java | 131 ++++++++---------- .../DeprecatedFileSystemAdapter.java | 6 +- .../scan/filesystem/ExclusionFilter.java | 5 + .../scan/filesystem/FileSystemLogger.java | 81 +++++++++++ .../scan/filesystem/InclusionFilter.java | 5 + .../filesystem/ModuleFileSystemProvider.java | 94 +++---------- .../scan/filesystem/WhiteListFileFilter.java | 12 ++ .../DefaultModuleFileSystemTest.java | 105 +++++++------- .../scan/filesystem/FileSystemLoggerTest.java | 57 ++++++++ .../filesystem/WhiteListFileFilterTest.java | 13 ++ 12 files changed, 322 insertions(+), 199 deletions(-) create mode 100644 sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileSystemLogger.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileSystemLoggerTest.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java b/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java index 9c2a358b0ce..249606d3ee9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/Phases.java @@ -25,6 +25,7 @@ import org.sonar.api.resources.Project; import org.sonar.batch.events.EventBus; import org.sonar.batch.index.DefaultIndex; import org.sonar.batch.index.PersistenceManager; +import org.sonar.batch.scan.filesystem.FileSystemLogger; import org.sonar.batch.scan.maven.MavenPhaseExecutor; import org.sonar.batch.scan.maven.MavenPluginsConfigurator; import org.sonar.core.component.ScanGraphStore; @@ -52,13 +53,14 @@ public final class Phases { private DefaultIndex index; private ProjectInitializer pi; private ScanGraphStore graphStorage; + private FileSystemLogger fsLogger; public Phases(DecoratorsExecutor decoratorsExecutor, MavenPhaseExecutor mavenPhaseExecutor, MavenPluginsConfigurator mavenPluginsConfigurator, InitializersExecutor initializersExecutor, PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor, PersistenceManager persistenceManager, SensorContext sensorContext, DefaultIndex index, EventBus eventBus, UpdateStatusJob updateStatusJob, ProjectInitializer pi, - ScanGraphStore graphStorage) { + ScanGraphStore graphStorage, FileSystemLogger fsLogger) { this.decoratorsExecutor = decoratorsExecutor; this.mavenPhaseExecutor = mavenPhaseExecutor; this.mavenPluginsConfigurator = mavenPluginsConfigurator; @@ -72,15 +74,16 @@ public final class Phases { this.updateStatusJob = updateStatusJob; this.pi = pi; this.graphStorage = graphStorage; + this.fsLogger = fsLogger; } public Phases(DecoratorsExecutor decoratorsExecutor, MavenPhaseExecutor mavenPhaseExecutor, MavenPluginsConfigurator mavenPluginsConfigurator, InitializersExecutor initializersExecutor, PostJobsExecutor postJobsExecutor, SensorsExecutor sensorsExecutor, PersistenceManager persistenceManager, SensorContext sensorContext, DefaultIndex index, - EventBus eventBus, ProjectInitializer pi, ScanGraphStore graphStorage) { + EventBus eventBus, ProjectInitializer pi, ScanGraphStore graphStorage, FileSystemLogger fsLogger) { this(decoratorsExecutor, mavenPhaseExecutor, mavenPluginsConfigurator, initializersExecutor, postJobsExecutor, - sensorsExecutor, persistenceManager, sensorContext, index, eventBus, null, pi, graphStorage); + sensorsExecutor, persistenceManager, sensorContext, index, eventBus, null, pi, graphStorage, fsLogger); } /** @@ -92,6 +95,7 @@ public final class Phases { mavenPluginsConfigurator.execute(project); mavenPhaseExecutor.execute(project); initializersExecutor.execute(); + fsLogger.log(); persistenceManager.setDelayedMode(true); sensorsExecutor.execute(sensorContext); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ScanContainer.java index 1dd44465f01..dcb25cbdeaa 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ScanContainer.java @@ -49,6 +49,7 @@ import org.sonar.batch.phases.Phases; import org.sonar.batch.phases.PhasesTimeProfiler; import org.sonar.batch.scan.filesystem.DeprecatedFileSystemAdapter; import org.sonar.batch.scan.filesystem.ExclusionFilters; +import org.sonar.batch.scan.filesystem.FileSystemLogger; import org.sonar.batch.scan.filesystem.LanguageFilters; import org.sonar.batch.scan.filesystem.ModuleFileSystemProvider; import org.sonar.core.qualitymodel.DefaultModelFinder; @@ -102,6 +103,7 @@ public class ScanContainer extends Container { container.addSingleton(DefaultProjectClasspath.class); container.addPicoAdapter(new ModuleFileSystemProvider()); container.addSingleton(DeprecatedFileSystemAdapter.class); + container.addSingleton(FileSystemLogger.class); // the Snapshot component will be removed when asynchronous measures are improved (required for AsynchronousMeasureSensor) diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java index bb2449c6176..73ba970889d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystem.java @@ -26,6 +26,9 @@ import org.apache.commons.io.FileUtils; 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.lang.StringUtils; +import org.sonar.api.CoreProperties; +import org.sonar.api.config.Settings; import org.sonar.api.scan.filesystem.FileQuery; import org.sonar.api.scan.filesystem.FileSystemFilter; import org.sonar.api.scan.filesystem.FileType; @@ -47,23 +50,16 @@ public class DefaultModuleFileSystem implements ModuleFileSystem { private static final IOFileFilter DIR_FILTER = FileFilterUtils.and(HiddenFileFilter.VISIBLE, FileFilterUtils.notFileFilter(FileFilterUtils.prefixFileFilter("."))); - private final Charset sourceCharset; + private Settings settings; private File baseDir, workingDir, buildDir; - private List sourceDirs, testDirs, binaryDirs; - private final PathResolver pathResolver = new PathResolver(); - private final List fsFilters; - private final LanguageFilters languageFilters; - - private DefaultModuleFileSystem(Builder builder) { - sourceCharset = builder.sourceCharset; - baseDir = builder.baseDir; - buildDir = builder.buildDir; - workingDir = builder.workingDir; - sourceDirs = ImmutableList.copyOf(builder.sourceDirs); - testDirs = ImmutableList.copyOf(builder.testDirs); - binaryDirs = ImmutableList.copyOf(builder.binaryDirs); - fsFilters = ImmutableList.copyOf(builder.fsFilters); - languageFilters = builder.languageFilters; + private List sourceDirs = Lists.newArrayList(); + private List testDirs = Lists.newArrayList(); + private List binaryDirs = Lists.newArrayList(); + private PathResolver pathResolver = new PathResolver(); + private List fsFilters = Lists.newArrayList(); + private LanguageFilters languageFilters; + + DefaultModuleFileSystem() { } public File baseDir() { @@ -87,14 +83,25 @@ public class DefaultModuleFileSystem implements ModuleFileSystem { } public Charset sourceCharset() { - return sourceCharset; + final Charset charset; + String encoding = settings.getString(CoreProperties.ENCODING_PROPERTY); + if (StringUtils.isNotEmpty(encoding)) { + charset = Charset.forName(StringUtils.trim(encoding)); + } else { + charset = Charset.defaultCharset(); + } + return charset; + } + + boolean isDefaultSourceCharset() { + return settings.hasKey(CoreProperties.ENCODING_PROPERTY); } public File workingDir() { return workingDir; } - List fsFilters() { + List filters() { return fsFilters; } @@ -162,9 +169,6 @@ public class DefaultModuleFileSystem implements ModuleFileSystem { return true; } - /** - * Breaks immutability but it's required to allow Maven Plugins to be executed and to change project structure. - */ public void resetDirs(File basedir, File buildDir, List sourceDirs, List testDirs, List binaryDirs) { Preconditions.checkNotNull(basedir, "Basedir can't be null"); this.baseDir = basedir; @@ -184,63 +188,50 @@ public class DefaultModuleFileSystem implements ModuleFileSystem { return builder.build(); } - static final class Builder { - private Charset sourceCharset; - private File baseDir, workingDir, buildDir; - private List sourceDirs = Lists.newArrayList(), testDirs = Lists.newArrayList(), binaryDirs = Lists.newArrayList(); - private List fsFilters = Lists.newArrayList(); - private LanguageFilters languageFilters; - - Builder sourceCharset(Charset c) { - this.sourceCharset = c; - return this; - } - - Builder baseDir(File d) { - this.baseDir = d; - return this; - } + DefaultModuleFileSystem setSettings(Settings settings) { + this.settings = settings; + return this; + } - Builder buildDir(File d) { - this.buildDir = d; - return this; - } + DefaultModuleFileSystem setBaseDir(File baseDir) { + this.baseDir = baseDir; + return this; + } - Builder workingDir(File d) { - this.workingDir = d; - return this; - } + DefaultModuleFileSystem setWorkingDir(File workingDir) { + this.workingDir = workingDir; + return this; + } - Builder addSourceDir(File d) { - sourceDirs.add(d); - return this; - } + DefaultModuleFileSystem setBuildDir(File buildDir) { + this.buildDir = buildDir; + return this; + } - Builder addTestDir(File d) { - testDirs.add(d); - return this; + DefaultModuleFileSystem addFilters(FileSystemFilter... f) { + for (FileSystemFilter filter : f) { + this.fsFilters.add(filter); } + return this; + } - Builder addBinaryDir(File d) { - binaryDirs.add(d); - return this; - } + DefaultModuleFileSystem setLanguageFilters(LanguageFilters languageFilters) { + this.languageFilters = languageFilters; + return this; + } - Builder addFsFilter(FileSystemFilter f) { - fsFilters.add(f); - return this; - } + DefaultModuleFileSystem addSourceDir(File d) { + this.sourceDirs.add(d); + return this; + } - Builder languageFilters(LanguageFilters l) { - languageFilters = l; - return this; - } + DefaultModuleFileSystem addTestDir(File d) { + this.testDirs.add(d); + return this; + } - DefaultModuleFileSystem build() { - Preconditions.checkNotNull(baseDir, "Module base directory is not set"); - Preconditions.checkNotNull(workingDir, "Module working directory is not set"); - Preconditions.checkNotNull(sourceCharset, "Module source charset is not set"); - return new DefaultModuleFileSystem(this); - } + DefaultModuleFileSystem addBinaryDir(File d) { + this.binaryDirs.add(d); + return this; } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileSystemAdapter.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileSystemAdapter.java index 3da0c77c7f9..cdbdae58748 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileSystemAdapter.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DeprecatedFileSystemAdapter.java @@ -103,7 +103,8 @@ public class DeprecatedFileSystemAdapter implements ProjectFileSystem { } public ProjectFileSystem addSourceDir(File dir) { - throw new UnsupportedOperationException("File system is immutable"); + target.addSourceDir(dir); + return this; } public List getTestDirs() { @@ -111,7 +112,8 @@ public class DeprecatedFileSystemAdapter implements ProjectFileSystem { } public ProjectFileSystem addTestDir(File dir) { - throw new UnsupportedOperationException("File system is immutable"); + target.addTestDir(dir); + return this; } public File getReportOutputDir() { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilter.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilter.java index d9e3f3067bc..fc84514c9a1 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilter.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ExclusionFilter.java @@ -33,4 +33,9 @@ class ExclusionFilter implements FileSystemFilter { public boolean accept(File file, FileSystemFilter.Context context) { return !pattern.match(context); } + + @Override + public String toString() { + return "Excludes: " + pattern; + } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileSystemLogger.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileSystemLogger.java new file mode 100644 index 00000000000..7b403b4dfe9 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileSystemLogger.java @@ -0,0 +1,81 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch.scan.filesystem; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Joiner; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.sonar.api.scan.filesystem.FileSystemFilter; + +import java.io.File; +import java.nio.charset.Charset; +import java.util.List; +import java.util.Locale; + +public class FileSystemLogger { + + private final DefaultModuleFileSystem fs; + + public FileSystemLogger(DefaultModuleFileSystem fs) { + this.fs = fs; + } + + public void log() { + doLog(LoggerFactory.getLogger(getClass())); + } + + @VisibleForTesting + void doLog(Logger logger) { + logDir(logger, "Base dir: ", fs.baseDir()); + logDir(logger, "Working dir: ", fs.workingDir()); + logDirs(logger, "Source dirs: ", fs.sourceDirs()); + logDirs(logger, "Test dirs: ", fs.testDirs()); + logDirs(logger, "Binary dirs: ", fs.binaryDirs()); + logFilters(logger, fs.filters()); + logEncoding(logger, fs.sourceCharset()); + } + + private void logEncoding(Logger logger, Charset charset) { + if (!fs.isDefaultSourceCharset()) { + logger.info("Source encoding: " + charset.displayName() + ", default locale: " + Locale.getDefault()); + } else { + logger.warn("Source encoding is platform dependent (" + charset.displayName() + "), default locale: " + Locale.getDefault()); + } + } + + private void logFilters(Logger logger, List filters) { + for (FileSystemFilter filter : filters) { + logger.info(filter.toString()); + } + } + + private void logDirs(Logger logger, String label, List dirs) { + if (!dirs.isEmpty()) { + logger.info(label + Joiner.on(", ").join(dirs)); + } + } + + private void logDir(Logger logger, String label, File dir) { + if (dir != null) { + logger.info(label + dir.getAbsolutePath()); + } + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InclusionFilter.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InclusionFilter.java index 0abb90d4461..ec2e66cfc53 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InclusionFilter.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InclusionFilter.java @@ -33,4 +33,9 @@ class InclusionFilter implements FileSystemFilter { public boolean accept(File file, FileSystemFilter.Context context) { return pattern.match(context); } + + @Override + public String toString() { + return "Includes: " + pattern; + } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemProvider.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemProvider.java index 9407a85c13a..40fc52aef51 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemProvider.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemProvider.java @@ -19,15 +19,9 @@ */ package org.sonar.batch.scan.filesystem; -import com.google.common.base.Joiner; import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Lists; import org.apache.commons.io.FileUtils; -import org.apache.commons.lang.StringUtils; import org.picocontainer.injectors.ProviderAdapter; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.Settings; import org.sonar.api.scan.filesystem.FileSystemFilter; @@ -36,39 +30,29 @@ import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.batch.bootstrap.TempDirectories; import java.io.File; -import java.nio.charset.Charset; import java.util.List; -import java.util.Locale; /** * @since 3.5 */ public class ModuleFileSystemProvider extends ProviderAdapter { - private static final Logger LOG = LoggerFactory.getLogger(ModuleFileSystemProvider.class); private DefaultModuleFileSystem singleton; public DefaultModuleFileSystem provide(ProjectDefinition module, PathResolver pathResolver, TempDirectories tempDirectories, LanguageFilters languageFilters, Settings settings, FileSystemFilter[] pluginFileFilters) { if (singleton == null) { - DefaultModuleFileSystem.Builder builder = new DefaultModuleFileSystem.Builder(); - - // dependencies - builder.languageFilters(languageFilters); - - // files and directories - // TODO should the basedir always exist ? If yes, then we check also check that it's a dir but not a file - builder.baseDir(module.getBaseDir()); - builder.buildDir(module.getBuildDir()); - builder.sourceCharset(guessCharset(settings)); - builder.workingDir(guessWorkingDir(module, tempDirectories)); - initBinaryDirs(module, pathResolver, builder); - initSources(module, pathResolver, builder); - initTests(module, pathResolver, builder); - - // file filters - initCustomFilters(builder, pluginFileFilters); - singleton = builder.build(); + DefaultModuleFileSystem fs = new DefaultModuleFileSystem(); + fs.setLanguageFilters(languageFilters); + fs.setBaseDir(module.getBaseDir()); + fs.setBuildDir(module.getBuildDir()); + fs.setSettings(settings); + fs.setWorkingDir(guessWorkingDir(module, tempDirectories)); + fs.addFilters(pluginFileFilters); + initBinaryDirs(module, pathResolver, fs); + initSources(module, pathResolver, fs); + initTests(module, pathResolver, fs); + singleton = fs; } return singleton; } @@ -77,9 +61,7 @@ public class ModuleFileSystemProvider extends ProviderAdapter { File workDir = module.getWorkDir(); if (workDir == null) { workDir = tempDirectories.getDir("work"); - LOG.warn("Working dir is not set. Using: " + workDir.getAbsolutePath()); } else { - LOG.info("Working dir: " + workDir.getAbsolutePath()); try { FileUtils.forceMkdir(workDir); } catch (Exception e) { @@ -89,75 +71,37 @@ public class ModuleFileSystemProvider extends ProviderAdapter { return workDir; } - private Charset guessCharset(Settings settings) { - final Charset charset; - String encoding = settings.getString(CoreProperties.ENCODING_PROPERTY); - if (StringUtils.isNotEmpty(encoding)) { - charset = Charset.forName(StringUtils.trim(encoding)); - LOG.info("Source encoding: " + charset.displayName() + ", default locale: " + Locale.getDefault()); - } else { - charset = Charset.defaultCharset(); - LOG.warn("Source encoding is platform dependent (" + charset.displayName() + "), default locale: " + Locale.getDefault()); - } - return charset; - } - - private void initSources(ProjectDefinition module, PathResolver pathResolver, DefaultModuleFileSystem.Builder builder) { - List paths = Lists.newArrayList(); + private void initSources(ProjectDefinition module, PathResolver pathResolver, DefaultModuleFileSystem fs) { for (String sourcePath : module.getSourceDirs()) { File dir = pathResolver.relativeFile(module.getBaseDir(), sourcePath); if (dir.isDirectory() && dir.exists()) { - paths.add(dir.getAbsolutePath()); - builder.addSourceDir(dir); + fs.addSourceDir(dir); } } - if (!paths.isEmpty()) { - LOG.info("Source dirs: " + Joiner.on(", ").join(paths)); - } List sourceFiles = pathResolver.relativeFiles(module.getBaseDir(), module.getSourceFiles()); if (!sourceFiles.isEmpty()) { - LOG.info("Source files: "); - for (File sourceFile : sourceFiles) { - LOG.info(" " + sourceFile.getAbsolutePath()); - } - builder.addFsFilter(new WhiteListFileFilter(FileType.SOURCE, ImmutableSet.copyOf(sourceFiles))); + fs.addFilters(new WhiteListFileFilter(FileType.SOURCE, ImmutableSet.copyOf(sourceFiles))); } } - private void initTests(ProjectDefinition module, PathResolver pathResolver, DefaultModuleFileSystem.Builder builder) { - List paths = Lists.newArrayList(); + private void initTests(ProjectDefinition module, PathResolver pathResolver, DefaultModuleFileSystem fs) { for (String testPath : module.getTestDirs()) { File dir = pathResolver.relativeFile(module.getBaseDir(), testPath); if (dir.exists() && dir.isDirectory()) { - paths.add(dir.getAbsolutePath()); - builder.addTestDir(dir); + fs.addTestDir(dir); } } - if (!paths.isEmpty()) { - LOG.info("Test dirs: " + Joiner.on(", ").join(paths)); - } - List testFiles = pathResolver.relativeFiles(module.getBaseDir(), module.getTestFiles()); if (!testFiles.isEmpty()) { - LOG.info("Test files:"); - for (File testFile : testFiles) { - LOG.info(" " + testFile.getAbsolutePath()); - } - builder.addFsFilter(new WhiteListFileFilter(FileType.TEST, ImmutableSet.copyOf(testFiles))); + fs.addFilters(new WhiteListFileFilter(FileType.TEST, ImmutableSet.copyOf(testFiles))); } } - private void initCustomFilters(DefaultModuleFileSystem.Builder builder, FileSystemFilter[] pluginFileFilters) { - for (FileSystemFilter pluginFileFilter : pluginFileFilters) { - builder.addFsFilter(pluginFileFilter); - } - } - private void initBinaryDirs(ProjectDefinition module, PathResolver pathResolver, DefaultModuleFileSystem.Builder builder) { + private void initBinaryDirs(ProjectDefinition module, PathResolver pathResolver, DefaultModuleFileSystem fs) { for (String path : module.getBinaries()) { File dir = pathResolver.relativeFile(module.getBaseDir(), path); - LOG.info("Binary dir: " + dir.getAbsolutePath()); - builder.addBinaryDir(dir); + fs.addBinaryDir(dir); } } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/WhiteListFileFilter.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/WhiteListFileFilter.java index 55450db7f09..c7e0cf228f0 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/WhiteListFileFilter.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/WhiteListFileFilter.java @@ -19,6 +19,10 @@ */ package org.sonar.batch.scan.filesystem; +import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.SystemUtils; import org.sonar.api.scan.filesystem.FileSystemFilter; import org.sonar.api.scan.filesystem.FileType; @@ -33,6 +37,8 @@ class WhiteListFileFilter implements FileSystemFilter { private final Set files; WhiteListFileFilter(FileType fileType, Set files) { + Preconditions.checkNotNull(fileType); + Preconditions.checkNotNull(files); this.fileType = fileType; this.files = files; } @@ -40,4 +46,10 @@ class WhiteListFileFilter implements FileSystemFilter { public boolean accept(File file, Context context) { return !context.type().equals(fileType) || files.contains(file); } + + @Override + public String toString() { + return StringUtils.capitalize(fileType.name().toLowerCase()) + " files: " + SystemUtils.LINE_SEPARATOR + + Joiner.on(SystemUtils.LINE_SEPARATOR).join(files); + } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java index 64629f4ee8d..afc4009f67b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/DefaultModuleFileSystemTest.java @@ -19,19 +19,20 @@ */ package org.sonar.batch.scan.filesystem; -import com.google.common.base.Charsets; import org.apache.commons.io.filefilter.FileFilterUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; +import org.sonar.api.CoreProperties; +import org.sonar.api.config.Settings; import org.sonar.api.resources.AbstractLanguage; import org.sonar.api.resources.Languages; import org.sonar.api.scan.filesystem.FileQuery; import org.sonar.api.scan.filesystem.FileSystemFilter; -import org.sonar.api.scan.filesystem.PathResolver; import java.io.File; import java.io.IOException; +import java.nio.charset.Charset; import java.util.Arrays; import java.util.List; @@ -43,24 +44,21 @@ public class DefaultModuleFileSystemTest { public TemporaryFolder temp = new TemporaryFolder(); @Test - public void test_builder() throws IOException { + public void test_new_file_system() throws IOException { File basedir = temp.newFolder("base"); File workingDir = temp.newFolder("work"); - PathResolver pathResolver = mock(PathResolver.class); LanguageFilters languageFilters = mock(LanguageFilters.class); FileSystemFilter fileFilter = mock(FileSystemFilter.class); - DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem.Builder() - .baseDir(basedir) - .workingDir(workingDir) + DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem() + .setBaseDir(basedir) + .setWorkingDir(workingDir) .addBinaryDir(new File(basedir, "target/classes")) .addSourceDir(new File(basedir, "src/main/java")) .addSourceDir(new File(basedir, "src/main/groovy")) .addTestDir(new File(basedir, "src/test/java")) - .addFsFilter(fileFilter) - .sourceCharset(Charsets.UTF_8) - .languageFilters(languageFilters) - .build(); + .addFilters(fileFilter) + .setLanguageFilters(languageFilters); assertThat(fileSystem).isNotNull(); assertThat(fileSystem.baseDir().getCanonicalPath()).isEqualTo(basedir.getCanonicalPath()); @@ -68,20 +66,39 @@ public class DefaultModuleFileSystemTest { assertThat(fileSystem.sourceDirs()).hasSize(2); assertThat(fileSystem.testDirs()).hasSize(1); assertThat(fileSystem.binaryDirs()).hasSize(1); - assertThat(fileSystem.sourceCharset().name()).isEqualTo("UTF-8"); - assertThat(fileSystem.fsFilters()).containsOnly(fileFilter); + assertThat(fileSystem.filters()).containsOnly(fileFilter); assertThat(fileSystem.languageFilters()).isSameAs(languageFilters); } + @Test + public void default_source_encoding() { + File basedir = temp.newFolder("base"); + DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem() + .setBaseDir(basedir) + .setSettings(new Settings()); + + assertThat(fileSystem.sourceCharset()).isEqualTo(Charset.defaultCharset()); + } + + @Test + public void source_encoding_is_set() { + File basedir = temp.newFolder("base"); + Settings settings = new Settings(); + settings.setProperty(CoreProperties.ENCODING_PROPERTY, "UTF-8"); + DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem() + .setBaseDir(basedir) + .setSettings(settings); + + assertThat(fileSystem.sourceCharset()).isEqualTo(Charset.forName("UTF-8")); + } + @Test public void should_exclude_dirs_starting_with_dot() throws IOException { File basedir = new File(resourcesDir(), "exclude_dir_starting_with_dot"); - DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem.Builder() - .baseDir(basedir) - .sourceCharset(Charsets.UTF_8) - .workingDir(temp.newFolder()) - .addSourceDir(new File(basedir, "src")) - .build(); + DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem() + .setBaseDir(basedir) + .setWorkingDir(temp.newFolder()) + .addSourceDir(new File(basedir, "src")); List files = fileSystem.files(FileQuery.onSource()); assertThat(files).hasSize(1); @@ -91,14 +108,12 @@ public class DefaultModuleFileSystemTest { @Test public void should_load_source_files_by_language() throws IOException { File basedir = new File(resourcesDir(), "main_and_test_files"); - DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem.Builder() - .baseDir(basedir) - .sourceCharset(Charsets.UTF_8) - .workingDir(temp.newFolder()) + DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem() + .setBaseDir(basedir) + .setWorkingDir(temp.newFolder()) .addSourceDir(new File(basedir, "src/main/java")) .addTestDir(new File(basedir, "src/test/java")) - .languageFilters(new LanguageFilters(new Languages(new Java(), new Php()))) - .build(); + .setLanguageFilters(new LanguageFilters(new Languages(new Java(), new Php()))); List files = fileSystem.files(FileQuery.onSource().onLanguage("java")); assertThat(files).hasSize(2); @@ -112,13 +127,11 @@ public class DefaultModuleFileSystemTest { @Test public void should_load_test_files() throws IOException { File basedir = new File(resourcesDir(), "main_and_test_files"); - DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem.Builder() - .baseDir(basedir) - .sourceCharset(Charsets.UTF_8) - .workingDir(temp.newFolder()) + DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem() + .setBaseDir(basedir) + .setWorkingDir(temp.newFolder()) .addSourceDir(new File(basedir, "src/main/java")) - .addTestDir(new File(basedir, "src/test/java")) - .build(); + .addTestDir(new File(basedir, "src/test/java")); assertThat(fileSystem.testDirs()).hasSize(1); List testFiles = fileSystem.files(FileQuery.onTest()); @@ -132,14 +145,12 @@ public class DefaultModuleFileSystemTest { @Test public void should_load_test_files_by_language() throws IOException { File basedir = new File(resourcesDir(), "main_and_test_files"); - DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem.Builder() - .baseDir(basedir) - .sourceCharset(Charsets.UTF_8) - .workingDir(temp.newFolder()) + DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem() + .setBaseDir(basedir) + .setWorkingDir(temp.newFolder()) .addSourceDir(new File(basedir, "src/main/java")) .addTestDir(new File(basedir, "src/test/java")) - .languageFilters(new LanguageFilters(new Languages(new Java(), new Php()))) - .build(); + .setLanguageFilters(new LanguageFilters(new Languages(new Java(), new Php()))); List testFiles = fileSystem.files(FileQuery.onTest().onLanguage("java")); assertThat(testFiles).hasSize(2); @@ -161,13 +172,11 @@ public class DefaultModuleFileSystemTest { @Test public void should_apply_file_filters() throws IOException { File basedir = new File(resourcesDir(), "main_and_test_files"); - DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem.Builder() - .baseDir(basedir) - .sourceCharset(Charsets.UTF_8) - .workingDir(temp.newFolder()) + DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem() + .setBaseDir(basedir) + .setWorkingDir(temp.newFolder()) .addSourceDir(new File(basedir, "src/main/java")) - .addFsFilter(new FileFilterWrapper(FileFilterUtils.nameFileFilter("Foo.java"))) - .build(); + .addFilters(new FileFilterWrapper(FileFilterUtils.nameFileFilter("Foo.java"))); List files = fileSystem.files(FileQuery.onSource()); assertThat(files).hasSize(1); @@ -197,13 +206,11 @@ public class DefaultModuleFileSystemTest { @Test public void test_reset_dirs() throws IOException { File basedir = temp.newFolder(); - DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem.Builder() - .baseDir(basedir) - .sourceCharset(Charsets.UTF_8) - .workingDir(basedir) + DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem() + .setBaseDir(basedir) + .setWorkingDir(basedir) .addSourceDir(new File(basedir, "src/main/java")) - .addFsFilter(new FileFilterWrapper(FileFilterUtils.nameFileFilter("Foo.java"))) - .build(); + .addFilters(new FileFilterWrapper(FileFilterUtils.nameFileFilter("Foo.java"))); File existingDir = temp.newFolder("new_folder"); File notExistingDir = new File(existingDir, "not_exist"); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileSystemLoggerTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileSystemLoggerTest.java new file mode 100644 index 00000000000..93b3dc64bb4 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileSystemLoggerTest.java @@ -0,0 +1,57 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.batch.scan.filesystem; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.slf4j.Logger; +import org.sonar.api.config.Settings; + +import java.io.File; + +import static org.mockito.AdditionalMatchers.and; +import static org.mockito.Matchers.contains; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class FileSystemLoggerTest { + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + @Test + public void log() { + DefaultModuleFileSystem fs = new DefaultModuleFileSystem(); + File src = temp.newFolder("src"); + File test = temp.newFolder("test"); + File base = temp.newFolder("base"); + fs.setSettings(new Settings()); + fs.setBaseDir(base); + fs.addSourceDir(src); + fs.addTestDir(test); + + Logger slf4j = mock(Logger.class); + new FileSystemLogger(fs).doLog(slf4j); + + verify(slf4j).info(and(contains("Base dir:"), contains(base.getAbsolutePath()))); + verify(slf4j).info(and(contains("Source dirs:"), contains(src.getAbsolutePath()))); + verify(slf4j).info(and(contains("Test dirs:"), contains(test.getAbsolutePath()))); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/WhiteListFileFilterTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/WhiteListFileFilterTest.java index 3c145b1d24f..98f8df0609b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/WhiteListFileFilterTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/WhiteListFileFilterTest.java @@ -53,4 +53,17 @@ public class WhiteListFileFilterTest { assertThat(filter.accept(new File("Foo.java"), context)).isTrue(); assertThat(filter.accept(new File("Other.java"), context)).isTrue(); } + + @Test + public void test_toString() throws IOException { + WhiteListFileFilter filter = new WhiteListFileFilter(FileType.SOURCE, Sets.newHashSet( + new File("Foo.java"), + new File("Bar.java") + )); + + assertThat(filter.toString()) + .contains("Source files:") + .contains("Foo.java") + .contains("Bar.java"); + } } -- 2.39.5