diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-10 12:18:02 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-10-10 12:18:09 +0200 |
commit | e108f5de333cf36aff978aa03940590f9caca9fc (patch) | |
tree | f80b2d6d5a71c7ea4a6fbedb71b4e63a001c4540 | |
parent | 02b933aa218e500989a0ef5d200b2ed28e098411 (diff) | |
download | sonarqube-e108f5de333cf36aff978aa03940590f9caca9fc.tar.gz sonarqube-e108f5de333cf36aff978aa03940590f9caca9fc.zip |
SONAR-3677 fix lifecycle of language recognizer
15 files changed, 398 insertions, 547 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/index/Cache.java b/sonar-batch/src/main/java/org/sonar/batch/index/Cache.java index f68292c94af..a4af0205edc 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/index/Cache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/index/Cache.java @@ -80,11 +80,11 @@ public class Cache<K, V extends Serializable> { } return (V) exchange.getValue().get(); } catch (Exception e) { + // TODO add parameters to message throw new IllegalStateException("Fail to get element from cache " + name, e); } } - /** * Returns the object associated with key from the cache, or null if not found. * @@ -96,12 +96,25 @@ public class Cache<K, V extends Serializable> { return get(DEFAULT_GROUP, key); } + public boolean containsKey(String group, K key) { + try { + exchange.clear(); + exchange.append(group).append(key); + exchange.fetch(); + return exchange.isValueDefined(); + } catch (Exception e) { + // TODO add parameters to message + throw new IllegalStateException("Fail to check if element is in cache " + name, e); + } + } + public boolean remove(String group, K key) { try { exchange.clear(); exchange.append(group).append(key); return exchange.remove(); } catch (Exception e) { + // TODO add parameters to message throw new IllegalStateException("Fail to get element from cache " + name, e); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java b/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java index e57f5908a9e..2c26c3f002b 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/phases/InitializersExecutor.java @@ -71,6 +71,10 @@ public class InitializersExecutor { eventBus.fireEvent(new InitializerExecutionEvent(initializer, false)); } + if (!initializers.isEmpty()) { + fs.index(); + } + eventBus.fireEvent(new InitializersPhaseEvent(Lists.newArrayList(initializers), false)); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java index d02b3c43e65..f2a1a91c9e3 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ModuleScanContainer.java @@ -96,11 +96,12 @@ public class ModuleScanContainer extends ComponentContainer { DeprecatedFileFilters.class, FileHashes.class, RemoteFileHashes.class, - FileIndexer.class, + FileIndex.class, LanguageRecognizer.class, FileSystemLogger.class, DefaultProjectClasspath.class, - new ModuleFileSystemProvider(), + DefaultModuleFileSystem.class, + ModuleFileSystemInitializer.class, ProjectFileSystemAdapter.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 c4d13692c3e..35158ab08f4 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 @@ -19,11 +19,14 @@ */ package org.sonar.batch.scan.filesystem; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.Lists; import org.apache.commons.lang.StringUtils; +import org.picocontainer.Startable; import org.sonar.api.CoreProperties; +import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.config.Settings; import org.sonar.api.scan.filesystem.FileQuery; import org.sonar.api.scan.filesystem.InputFile; @@ -40,11 +43,10 @@ import java.util.List; * * @since 3.5 */ -public class DefaultModuleFileSystem implements ModuleFileSystem { +public class DefaultModuleFileSystem implements ModuleFileSystem, Startable { private final String moduleKey; - private final InputFileCache cache; - private final FileIndexer indexer; + private final FileIndex index; private final Settings settings; private File baseDir, workingDir, buildDir; @@ -54,11 +56,23 @@ public class DefaultModuleFileSystem implements ModuleFileSystem { private List<File> additionalSourceFiles = Lists.newArrayList(); private List<File> additionalTestFiles = Lists.newArrayList(); - public DefaultModuleFileSystem(String moduleKey, Settings settings, InputFileCache cache, FileIndexer indexer) { + public DefaultModuleFileSystem(ProjectDefinition module, Settings settings, FileIndex index, ModuleFileSystemInitializer initializer) { + this(module.getKey(), settings, index, initializer); + } + + @VisibleForTesting + DefaultModuleFileSystem(String moduleKey, Settings settings, FileIndex index, ModuleFileSystemInitializer initializer) { this.moduleKey = moduleKey; this.settings = settings; - this.cache = cache; - this.indexer = indexer; + this.index = index; + this.baseDir = initializer.baseDir(); + this.workingDir = initializer.workingDir(); + this.buildDir = initializer.buildDir(); + this.sourceDirs = initializer.sourceDirs(); + this.testDirs = initializer.testDirs(); + this.binaryDirs = initializer.binaryDirs(); + this.additionalSourceFiles = initializer.additionalSourceFiles(); + this.additionalTestFiles = initializer.additionalTestFiles(); } @Override @@ -93,22 +107,6 @@ public class DefaultModuleFileSystem implements ModuleFileSystem { } @Override - public Charset 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); - } - - @Override public File workingDir() { return workingDir; } @@ -121,36 +119,20 @@ public class DefaultModuleFileSystem implements ModuleFileSystem { return additionalTestFiles; } - void setBaseDir(File baseDir) { - this.baseDir = baseDir; - } - - void setWorkingDir(File workingDir) { - this.workingDir = workingDir; - } - - void setBuildDir(File buildDir) { - this.buildDir = buildDir; - } - - void addSourceDir(File d) { - this.sourceDirs.add(d); - } - - void addTestDir(File d) { - this.testDirs.add(d); - } - - void addBinaryDir(File d) { - this.binaryDirs.add(d); - } - - void setAdditionalSourceFiles(List<File> files) { - this.additionalSourceFiles = files; + @Override + public Charset 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; } - void setAdditionalTestFiles(List<File> files) { - this.additionalTestFiles = files; + boolean isDefaultSourceCharset() { + return !settings.hasKey(CoreProperties.ENCODING_PROPERTY); } /** @@ -161,7 +143,7 @@ public class DefaultModuleFileSystem implements ModuleFileSystem { List<InputFile> result = Lists.newArrayList(); FileQueryFilter filter = new FileQueryFilter(settings, query); - for (InputFile input : cache.byModule(moduleKey)) { + for (InputFile input : index.inputFiles(moduleKey)) { if (filter.accept(input)) { result.add(input); } @@ -170,11 +152,20 @@ public class DefaultModuleFileSystem implements ModuleFileSystem { } @Override - // TODO deprecate public List<File> files(FileQuery query) { return InputFiles.toFiles(inputFiles(query)); } + @Override + public void start() { + index(); + } + + @Override + public void stop() { + // nothing to do + } + public void resetDirs(File basedir, File buildDir, List<File> sourceDirs, List<File> testDirs, List<File> binaryDirs) { Preconditions.checkNotNull(basedir, "Basedir can't be null"); this.baseDir = basedir; @@ -182,11 +173,11 @@ public class DefaultModuleFileSystem implements ModuleFileSystem { this.sourceDirs = existingDirs(sourceDirs); this.testDirs = existingDirs(testDirs); this.binaryDirs = existingDirs(binaryDirs); - indexer.index(this); + index(); } - void index() { - indexer.index(this); + public void index() { + index.index(this); } private List<File> existingDirs(List<File> dirs) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndex.java index a2d6c383232..f27abed0145 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndexer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/FileIndex.java @@ -41,11 +41,26 @@ import java.nio.charset.Charset; import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Set; /** * Index input files into {@link InputFileCache}. */ -public class FileIndexer implements BatchComponent { +public class FileIndex implements BatchComponent { + + private static class IndexStatus { + private int count = 0; + private final Set<String> orphans; + + IndexStatus(Set<String> orphans) { + this.orphans = orphans; + } + + void markAsIndexed(String relativePath) { + count++; + orphans.remove(relativePath); + } + } private static final IOFileFilter DIR_FILTER = FileFilterUtils.and(HiddenFileFilter.VISIBLE, FileFilterUtils.notFileFilter(FileFilterUtils.prefixFileFilter("."))); private static final IOFileFilter FILE_FILTER = HiddenFileFilter.VISIBLE; @@ -56,60 +71,72 @@ public class FileIndexer implements BatchComponent { private final InputFileCache cache; private final FileHashes fileHashes; - public FileIndexer(List<InputFileFilter> filters, LanguageRecognizer languageRecognizer, - InputFileCache cache, FileHashes fileHashes) { + public FileIndex(List<InputFileFilter> filters, LanguageRecognizer languageRecognizer, + InputFileCache cache, FileHashes fileHashes) { this.filters = filters; this.languageRecognizer = languageRecognizer; this.cache = cache; this.fileHashes = fileHashes; } - public void index(DefaultModuleFileSystem fileSystem) { - Logger logger = LoggerFactory.getLogger(FileIndexer.class); + void index(DefaultModuleFileSystem fileSystem) { + Logger logger = LoggerFactory.getLogger(FileIndex.class); logger.info("Index files"); // TODO log configuration too (replace FileSystemLogger) - cache.removeModule(fileSystem.moduleKey()); - int count = 0; + IndexStatus status = new IndexStatus(cache.filePathsOfModule(fileSystem.moduleKey())); + for (File sourceDir : fileSystem.sourceDirs()) { - count += indexDirectory(fileSystem, sourceDir, InputFile.TYPE_SOURCE); + indexDirectory(fileSystem, status, sourceDir, InputFile.TYPE_SOURCE); } for (File testDir : fileSystem.testDirs()) { - count += indexDirectory(fileSystem, testDir, InputFile.TYPE_TEST); + indexDirectory(fileSystem, status, testDir, InputFile.TYPE_TEST); } // TODO index additional sources and test files - logger.info(String.format("%d files indexed", count)); + for (String relativePath : status.orphans) { + cache.remove(fileSystem.moduleKey(), relativePath); + } + + logger.info(String.format("%d files indexed", status.count)); + } + + Iterable<InputFile> inputFiles(String moduleKey) { + return cache.byModule(moduleKey); } - private int indexDirectory(ModuleFileSystem fileSystem, File sourceDir, String type) { - int count = 0; + private void indexDirectory(ModuleFileSystem fileSystem, IndexStatus status, File sourceDir, String type) { Collection<File> files = FileUtils.listFiles(sourceDir, FILE_FILTER, DIR_FILTER); for (File file : files) { - InputFile input = newInputFile(fileSystem, sourceDir, type, file); - if (accept(input)) { - cache.put(fileSystem.moduleKey(), input); - count++; + String relativePath = pathResolver.relativePath(fileSystem.baseDir(), file); + if (!cache.containsFile(fileSystem.moduleKey(), relativePath)) { + InputFile input = newInputFile(fileSystem, sourceDir, type, file); + if (accept(input)) { + cache.put(fileSystem.moduleKey(), input); + status.markAsIndexed(relativePath); + } } } - return count; } + private InputFile newInputFile(ModuleFileSystem fileSystem, File sourceDir, String type, File file) { try { Map<String, String> attributes = Maps.newHashMap(); + set(attributes, InputFile.ATTRIBUTE_TYPE, type); // paths String baseRelativePath = pathResolver.relativePath(fileSystem.baseDir(), file); set(attributes, InputFile.ATTRIBUTE_SOURCEDIR_PATH, FilenameUtils.normalize(sourceDir.getCanonicalPath(), true)); set(attributes, InputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH, pathResolver.relativePath(sourceDir, file)); - // other metadata - set(attributes, InputFile.ATTRIBUTE_TYPE, type); + // File extension must be kept case-sensitive String extension = FilenameUtils.getExtension(file.getName()); set(attributes, InputFile.ATTRIBUTE_EXTENSION, extension); set(attributes, InputFile.ATTRIBUTE_LANGUAGE, languageRecognizer.ofExtension(extension)); + + // hash + status initStatus(file, fileSystem.sourceCharset(), baseRelativePath, attributes); return DefaultInputFile.create(file, baseRelativePath, attributes); diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileCache.java index f45f8088f85..75be9ad4643 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileCache.java @@ -24,6 +24,8 @@ import org.sonar.api.scan.filesystem.InputFile; import org.sonar.batch.index.Cache; import org.sonar.batch.index.Caches; +import java.util.Set; + /** * Cache of all files. This cache is shared amongst all project modules. Inclusion and * exclusion patterns are already applied. @@ -46,10 +48,23 @@ public class InputFileCache implements BatchComponent { return this; } + public InputFileCache remove(String moduleKey, String relativePath) { + cache.remove(moduleKey, relativePath); + return this; + } + public Iterable<InputFile> all() { return cache.allValues(); } + public Set<String> filePathsOfModule(String moduleKey) { + return cache.keySet(moduleKey); + } + + public boolean containsFile(String moduleKey, String relativePath) { + return cache.containsKey(moduleKey, relativePath); + } + public InputFileCache put(String moduleKey, InputFile file) { cache.put(moduleKey, file.path(), file); return this; diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java new file mode 100644 index 00000000000..583340fe11c --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java @@ -0,0 +1,124 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.scan.filesystem; + +import com.google.common.collect.Lists; +import org.apache.commons.io.FileUtils; +import org.sonar.api.BatchComponent; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.scan.filesystem.PathResolver; +import org.sonar.batch.bootstrap.TempDirectories; + +import java.io.File; +import java.util.List; + +/** + * @since 3.5 + */ +public class ModuleFileSystemInitializer implements BatchComponent { + + private File baseDir, workingDir, buildDir; + private List<File> sourceDirs = Lists.newArrayList(); + private List<File> testDirs = Lists.newArrayList(); + private List<File> binaryDirs = Lists.newArrayList(); + private List<File> additionalSourceFiles; + private List<File> additionalTestFiles; + + public ModuleFileSystemInitializer(ProjectDefinition module, TempDirectories tempDirectories, PathResolver pathResolver) { + baseDir = module.getBaseDir(); + buildDir = module.getBuildDir(); + initWorkingDir(module, tempDirectories); + initBinaryDirs(module, pathResolver); + initSources(module, pathResolver); + initTests(module, pathResolver); + } + + private void initWorkingDir(ProjectDefinition module, TempDirectories tempDirectories) { + workingDir = module.getWorkDir(); + if (workingDir == null) { + workingDir = tempDirectories.getDir("work"); + } else { + try { + FileUtils.forceMkdir(workingDir); + } catch (Exception e) { + throw new IllegalStateException("Fail to create working dir: " + workingDir.getAbsolutePath(), e); + } + } + } + + private void initSources(ProjectDefinition module, PathResolver pathResolver) { + for (String sourcePath : module.getSourceDirs()) { + File dir = pathResolver.relativeFile(module.getBaseDir(), sourcePath); + if (dir.isDirectory() && dir.exists()) { + sourceDirs.add(dir); + } + } + additionalSourceFiles = pathResolver.relativeFiles(module.getBaseDir(), module.getSourceFiles()); + } + + private void initTests(ProjectDefinition module, PathResolver pathResolver) { + for (String testPath : module.getTestDirs()) { + File dir = pathResolver.relativeFile(module.getBaseDir(), testPath); + if (dir.exists() && dir.isDirectory()) { + testDirs.add(dir); + } + } + additionalTestFiles = pathResolver.relativeFiles(module.getBaseDir(), module.getTestFiles()); + } + + private void initBinaryDirs(ProjectDefinition module, PathResolver pathResolver) { + for (String path : module.getBinaries()) { + File dir = pathResolver.relativeFile(module.getBaseDir(), path); + binaryDirs.add(dir); + } + } + + File baseDir() { + return baseDir; + } + + File workingDir() { + return workingDir; + } + + File buildDir() { + return buildDir; + } + + List<File> sourceDirs() { + return sourceDirs; + } + + List<File> testDirs() { + return testDirs; + } + + List<File> binaryDirs() { + return binaryDirs; + } + + List<File> additionalSourceFiles() { + return additionalSourceFiles; + } + + List<File> additionalTestFiles() { + return additionalTestFiles; + } +} 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 deleted file mode 100644 index 7bd84e87ef7..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemProvider.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import org.apache.commons.io.FileUtils; -import org.picocontainer.injectors.ProviderAdapter; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.config.Settings; -import org.sonar.api.scan.filesystem.FileSystemFilter; -import org.sonar.api.scan.filesystem.PathResolver; -import org.sonar.batch.bootstrap.TempDirectories; - -import java.io.File; -import java.util.List; - -/** - * @since 3.5 - */ -public class ModuleFileSystemProvider extends ProviderAdapter { - - private PathResolver pathResolver = new PathResolver(); - private DefaultModuleFileSystem singleton; - - public DefaultModuleFileSystem provide( - ProjectDefinition module, TempDirectories tempDirectories,Settings settings, InputFileCache cache, FileIndexer indexer) { - - if (singleton == null) { - DefaultModuleFileSystem fs = new DefaultModuleFileSystem(module.getKey(), settings, cache, indexer); - fs.setBaseDir(module.getBaseDir()); - fs.setBuildDir(module.getBuildDir()); - fs.setWorkingDir(guessWorkingDir(module, tempDirectories)); - initBinaryDirs(module, fs); - initSources(module, fs); - initTests(module, fs); - fs.index(); - singleton = fs; - } - return singleton; - } - - private File guessWorkingDir(ProjectDefinition module, TempDirectories tempDirectories) { - File workDir = module.getWorkDir(); - if (workDir == null) { - workDir = tempDirectories.getDir("work"); - } else { - try { - FileUtils.forceMkdir(workDir); - } catch (Exception e) { - throw new IllegalStateException("Fail to create working dir: " + workDir.getAbsolutePath(), e); - } - } - return workDir; - } - - private void initSources(ProjectDefinition module, DefaultModuleFileSystem fs) { - for (String sourcePath : module.getSourceDirs()) { - File dir = pathResolver.relativeFile(module.getBaseDir(), sourcePath); - if (dir.isDirectory() && dir.exists()) { - fs.addSourceDir(dir); - } - } - List<File> sourceFiles = pathResolver.relativeFiles(module.getBaseDir(), module.getSourceFiles()); - fs.setAdditionalSourceFiles(sourceFiles); - } - - private void initTests(ProjectDefinition module, DefaultModuleFileSystem fs) { - for (String testPath : module.getTestDirs()) { - File dir = pathResolver.relativeFile(module.getBaseDir(), testPath); - if (dir.exists() && dir.isDirectory()) { - fs.addTestDir(dir); - } - } - List<File> testFiles = pathResolver.relativeFiles(module.getBaseDir(), module.getTestFiles()); - fs.setAdditionalTestFiles(testFiles); - } - - private void initBinaryDirs(ProjectDefinition module, DefaultModuleFileSystem fs) { - for (String path : module.getBinaries()) { - File dir = pathResolver.relativeFile(module.getBaseDir(), path); - fs.addBinaryDir(dir); - } - } -} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ProjectFileSystemAdapter.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ProjectFileSystemAdapter.java index 1f5ab333658..2f3ab0c4631 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ProjectFileSystemAdapter.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ProjectFileSystemAdapter.java @@ -96,8 +96,7 @@ public class ProjectFileSystemAdapter implements ProjectFileSystem { } public ProjectFileSystem addSourceDir(File dir) { - target.addSourceDir(dir); - return this; + throw new UnsupportedOperationException("Unsupported since 4.0"); } public List<File> getTestDirs() { @@ -105,8 +104,7 @@ public class ProjectFileSystemAdapter implements ProjectFileSystem { } public ProjectFileSystem addTestDir(File dir) { - target.addTestDir(dir); - return this; + throw new UnsupportedOperationException("Unsupported since 4.0"); } public File getReportOutputDir() { diff --git a/sonar-batch/src/test/java/org/sonar/batch/index/CacheTest.java b/sonar-batch/src/test/java/org/sonar/batch/index/CacheTest.java index c89471ad889..48bfe91417f 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/index/CacheTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/index/CacheTest.java @@ -71,11 +71,13 @@ public class CacheTest { cache.put(group, "ncloc", 123f); assertThat(cache.get(group, "ncloc")).isEqualTo(123f); assertThat(cache.keySet(group)).containsOnly("ncloc"); + assertThat(cache.containsKey(group, "ncloc")).isTrue(); assertThat(cache.get("ncloc")).isNull(); assertThat(cache.get(group)).isNull(); cache.remove(group, "ncloc"); assertThat(cache.get(group, "ncloc")).isNull(); assertThat(cache.keySet(group)).isEmpty(); + assertThat(cache.containsKey(group, "ncloc")).isFalse(); } @Test 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 4536d0b64fa..c0633e901f6 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 @@ -25,6 +25,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; +import org.mockito.Mockito; import org.sonar.api.CoreProperties; import org.sonar.api.config.Settings; import org.sonar.api.scan.filesystem.FileQuery; @@ -34,6 +35,7 @@ import org.sonar.api.scan.filesystem.internal.DefaultInputFile; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; +import java.util.Arrays; import java.util.List; import static org.fest.assertions.Assertions.assertThat; @@ -47,15 +49,15 @@ public class DefaultModuleFileSystemTest { @Rule public ExpectedException thrown = ExpectedException.none(); - InputFileCache fileCache = mock(InputFileCache.class); Settings settings = new Settings(); - FileIndexer fileIndexer = mock(FileIndexer.class); + FileIndex fileIndex = mock(FileIndex.class); + ModuleFileSystemInitializer initializer = mock(ModuleFileSystemInitializer.class, Mockito.RETURNS_DEEP_STUBS); @Test public void test_equals_and_hashCode() throws Exception { - DefaultModuleFileSystem foo1 = new DefaultModuleFileSystem("foo", settings, fileCache, fileIndexer); - DefaultModuleFileSystem foo2 = new DefaultModuleFileSystem("foo", settings, fileCache, fileIndexer); - DefaultModuleFileSystem bar = new DefaultModuleFileSystem("bar", settings, fileCache, fileIndexer); + DefaultModuleFileSystem foo1 = new DefaultModuleFileSystem("foo", settings, fileIndex, initializer); + DefaultModuleFileSystem foo2 = new DefaultModuleFileSystem("foo", settings, fileIndex, initializer); + DefaultModuleFileSystem bar = new DefaultModuleFileSystem("bar", settings, fileIndex, initializer); assertThat(foo1.moduleKey()).isEqualTo("foo"); assertThat(foo1.equals(foo1)).isTrue(); @@ -68,7 +70,7 @@ public class DefaultModuleFileSystemTest { @Test public void default_source_encoding() { - DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileCache, fileIndexer); + DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileIndex, initializer); assertThat(fs.sourceCharset()).isEqualTo(Charset.defaultCharset()); assertThat(fs.isDefaultSourceCharset()).isTrue(); @@ -77,7 +79,7 @@ public class DefaultModuleFileSystemTest { @Test public void source_encoding_is_set() { settings.setProperty(CoreProperties.ENCODING_PROPERTY, "Cp1124"); - DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileCache, fileIndexer); + DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileIndex, initializer); assertThat(fs.sourceCharset()).isEqualTo(Charset.forName("Cp1124")); @@ -90,15 +92,19 @@ public class DefaultModuleFileSystemTest { File basedir = temp.newFolder("base"); File buildDir = temp.newFolder("build"); File workingDir = temp.newFolder("work"); + File additionalFile = temp.newFile("Main.java"); + File additionalTest = temp.newFile("Test.java"); + when(initializer.baseDir()).thenReturn(basedir); + when(initializer.buildDir()).thenReturn(buildDir); + when(initializer.workingDir()).thenReturn(workingDir); + when(initializer.binaryDirs()).thenReturn(Arrays.asList(new File(basedir, "target/classes"))); + when(initializer.sourceDirs()).thenReturn(Arrays.asList(new File(basedir, "src/main/java"), new File(basedir, "src/main/groovy"))); + when(initializer.testDirs()).thenReturn(Arrays.asList(new File(basedir, "src/test/java"))); + when(initializer.additionalSourceFiles()).thenReturn(Arrays.asList(additionalFile)); + when(initializer.additionalTestFiles()).thenReturn(Arrays.asList(additionalTest)); - DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileCache, fileIndexer); - fs.setBaseDir(basedir); - fs.setBuildDir(buildDir); - fs.setWorkingDir(workingDir); - fs.addBinaryDir(new File(basedir, "target/classes")); - fs.addSourceDir(new File(basedir, "src/main/java")); - fs.addSourceDir(new File(basedir, "src/main/groovy")); - fs.addTestDir(new File(basedir, "src/test/java")); + + DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileIndex, initializer); assertThat(fs.baseDir().getCanonicalPath()).isEqualTo(basedir.getCanonicalPath()); assertThat(fs.workingDir().getCanonicalPath()).isEqualTo(workingDir.getCanonicalPath()); @@ -106,29 +112,18 @@ public class DefaultModuleFileSystemTest { assertThat(fs.sourceDirs()).hasSize(2); assertThat(fs.testDirs()).hasSize(1); assertThat(fs.binaryDirs()).hasSize(1); - } - - @Test - public void test_additional_source_files() throws IOException { - DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileCache, fileIndexer); - assertThat(fs.additionalSourceFiles()).isEmpty(); - assertThat(fs.additionalTestFiles()).isEmpty(); - - File main = temp.newFile("Main.java"); - File test = temp.newFile("Test.java"); - fs.setAdditionalSourceFiles(Lists.newArrayList(main)); - fs.setAdditionalTestFiles(Lists.newArrayList(test)); - assertThat(fs.additionalSourceFiles()).containsOnly(main); - assertThat(fs.additionalTestFiles()).containsOnly(test); + assertThat(fs.additionalSourceFiles()).containsOnly(additionalFile); + assertThat(fs.additionalTestFiles()).containsOnly(additionalTest); } @Test public void should_reset_dirs() throws IOException { File basedir = temp.newFolder(); - DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileCache, fileIndexer); - fs.setBaseDir(basedir); - fs.setWorkingDir(basedir); - fs.addSourceDir(new File(basedir, "src/main/java")); + when(initializer.baseDir()).thenReturn(basedir); + when(initializer.workingDir()).thenReturn(basedir); + when(initializer.sourceDirs()).thenReturn(Arrays.asList(new File(basedir, "src/main/java"))); + + DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileIndex, initializer); File existingDir = temp.newFolder("new_folder"); File notExistingDir = new File(existingDir, "not_exist"); @@ -144,18 +139,18 @@ public class DefaultModuleFileSystemTest { assertThat(fs.testDirs().get(0).getCanonicalPath()).isEqualTo(existingDir.getCanonicalPath()); assertThat(fs.binaryDirs()).hasSize(1); assertThat(fs.binaryDirs().get(0).getCanonicalPath()).isEqualTo(existingDir.getCanonicalPath()); - verify(fileIndexer).index(fs); + verify(fileIndex).index(fs); } @Test public void should_search_input_files() throws Exception { - DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileCache, fileIndexer); + DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileIndex, initializer); File mainFile = temp.newFile(); InputFile mainInput = DefaultInputFile.create(mainFile, "Main.java", ImmutableMap.of(InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_SOURCE)); InputFile testInput = DefaultInputFile.create(temp.newFile(), "Test.java", ImmutableMap.of(InputFile.ATTRIBUTE_TYPE, InputFile.TYPE_TEST)); - when(fileCache.byModule("foo")).thenReturn(Lists.newArrayList(mainInput, testInput)); + when(fileIndex.inputFiles("foo")).thenReturn(Lists.newArrayList(mainInput, testInput)); Iterable<InputFile> inputFiles = fs.inputFiles(FileQuery.onSource()); assertThat(inputFiles).containsOnly(mainInput); @@ -166,190 +161,13 @@ public class DefaultModuleFileSystemTest { @Test public void should_index() throws Exception { - DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileCache, fileIndexer); + DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", settings, fileIndex, initializer); - verifyZeroInteractions(fileIndexer); + verifyZeroInteractions(fileIndex); - fs.index(); - verify(fileIndexer).index(fs); + fs.start(); + verify(fileIndex).index(fs); + fs.stop(); } - // -// -// -// @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(mock(RemoteFileHashes.class)) -// .setBaseDir(basedir) -// .setWorkingDir(temp.newFolder()) -// .addSourceDir(new File(basedir, "src")) -// .setSettings(new Settings()); -// -// List<File> files = fileSystem.files(FileQuery.onSource()); -// assertThat(files).hasSize(1); -// assertThat(files.get(0).getName()).isEqualTo("Included.java"); -// } -// -// @Test -// public void should_load_source_files_by_language() throws IOException { -// File basedir = new File(resourcesDir(), "main_and_test_files"); -// DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem(mock(RemoteFileHashes.class)) -// .setBaseDir(basedir) -// .setWorkingDir(temp.newFolder()) -// .addSourceDir(new File(basedir, "src/main/java")) -// .addTestDir(new File(basedir, "src/test/java")) -// .setSettings(new Settings()); -// -// List<File> files = fileSystem.files(FileQuery.onSource().onLanguage("java")); -// assertThat(files).hasSize(2); -// for (File sourceFiles : files) { -// assertThat(sourceFiles).exists().isFile(); -// assertThat(sourceFiles.getName()).isIn("Hello.java", "Foo.java"); -// } -// assertThat(fileSystem.files(FileQuery.onSource().onLanguage("php"))).isEmpty(); -// } -// -// @Test -// public void should_load_test_files() throws IOException { -// File basedir = new File(resourcesDir(), "main_and_test_files"); -// DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem(mock(RemoteFileHashes.class)) -// .setBaseDir(basedir) -// .setWorkingDir(temp.newFolder()) -// .addSourceDir(new File(basedir, "src/main/java")) -// .addTestDir(new File(basedir, "src/test/java")) -// .setSettings(new Settings()); -// -// assertThat(fileSystem.testDirs()).hasSize(1); -// List<File> testFiles = fileSystem.files(FileQuery.onTest()); -// assertThat(testFiles).hasSize(2); -// for (File testFile : testFiles) { -// assertThat(testFile).exists().isFile(); -// assertThat(testFile.getName()).endsWith("Test.java"); -// } -// } -// -// @Test -// public void should_load_test_files_by_language() throws IOException { -// File basedir = new File(resourcesDir(), "main_and_test_files"); -// DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem(mock(RemoteFileHashes.class)) -// .setBaseDir(basedir) -// .setWorkingDir(temp.newFolder()) -// .addSourceDir(new File(basedir, "src/main/java")) -// .addTestDir(new File(basedir, "src/test/java")) -// .setSettings(new Settings()); -// -// List<File> testFiles = fileSystem.files(FileQuery.onTest().onLanguage("java")); -// assertThat(testFiles).hasSize(2); -// for (File testFile : testFiles) { -// assertThat(testFile).exists().isFile(); -// assertThat(testFile.getName()).endsWith("Test.java"); -// } -// assertThat(fileSystem.files(FileQuery.onTest().onLanguage("php"))).isEmpty(); -// } -// -// private File resourcesDir() { -// File dir = new File("test-resources/DefaultModuleFileSystemTest"); -// if (!dir.exists()) { -// dir = new File("sonar-batch/test-resources/DefaultModuleFileSystemTest"); -// } -// return dir; -// } -// -// @Test -// public void should_apply_file_filters() throws IOException { -// File basedir = new File(resourcesDir(), "main_and_test_files"); -// DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem(mock(RemoteFileHashes.class)) -// .setBaseDir(basedir) -// .setWorkingDir(temp.newFolder()) -// .addSourceDir(new File(basedir, "src/main/java")) -// .addFilters(new FileFilterWrapper(FileFilterUtils.nameFileFilter("Foo.java"))) -// .setSettings(new Settings()); -// -// List<File> files = fileSystem.files(FileQuery.onSource()); -// assertThat(files).hasSize(1); -// assertThat(files.get(0).getName()).isEqualTo("Foo.java"); -// } -// -// static class Php extends AbstractLanguage { -// public Php() { -// super("php"); -// } -// -// public String[] getFileSuffixes() { -// return new String[] {"php"}; -// } -// } -// -// static class Java extends AbstractLanguage { -// public Java() { -// super("java"); -// } -// -// public String[] getFileSuffixes() { -// return new String[] {"java", "jav"}; -// } -// } -// -// -// -// @Test -// public void should_throw_if_incremental_mode_and_not_in_dryrun() throws Exception { -// File basedir = temp.newFolder(); -// Settings settings = new Settings(); -// DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem(mock(RemoteFileHashes.class)) -// .setBaseDir(basedir) -// .setWorkingDir(temp.newFolder()) -// .addSourceDir(new File(basedir, "src/main/java")) -// .setSettings(settings); -// -// settings.setProperty(CoreProperties.INCREMENTAL_PREVIEW, true); -// -// thrown.expect(SonarException.class); -// thrown.expectMessage("Incremental preview is only supported with preview mode"); -// fileSystem.files(FileQuery.onSource()); -// } -// -// @Test -// public void should_filter_changed_files() throws Exception { -// File basedir = new File(resourcesDir(), "main_and_test_files"); -// Settings settings = new Settings(); -// settings.setProperty(CoreProperties.ENCODING_PROPERTY, "UTF-8"); -// File mainDir = new File(basedir, "src/main/java"); -// File testDir = new File(basedir, "src/test/java"); -// File foo = new File(mainDir, "Foo.java"); -// File hello = new File(mainDir, "Hello.java"); -// File fooTest = new File(testDir, "FooTest.java"); -// File helloTest = new File(testDir, "HelloTest.java"); -// -// RemoteFileHashes remoteFileHashes = mock(RemoteFileHashes.class); -// when(remoteFileHashes.getPreviousHash(foo)).thenReturn("oldfoohash"); -// when(remoteFileHashes.getCurrentHash(foo, Charsets.UTF_8)).thenReturn("foohash"); -// when(remoteFileHashes.getPreviousHash(hello)).thenReturn("oldhellohash"); -// when(remoteFileHashes.getCurrentHash(hello, Charsets.UTF_8)).thenReturn("oldhellohash"); -// when(remoteFileHashes.getPreviousHash(fooTest)).thenReturn("oldfooTesthash"); -// when(remoteFileHashes.getCurrentHash(fooTest, Charsets.UTF_8)).thenReturn("fooTesthash"); -// when(remoteFileHashes.getPreviousHash(helloTest)).thenReturn("oldhelloTesthash"); -// when(remoteFileHashes.getCurrentHash(helloTest, Charsets.UTF_8)).thenReturn("oldhelloTesthash"); -// -// DefaultModuleFileSystem fileSystem = new DefaultModuleFileSystem(remoteFileHashes) -// .setBaseDir(basedir) -// .setWorkingDir(temp.newFolder()) -// .addSourceDir(mainDir) -// .addTestDir(testDir) -// .setSettings(settings); -// -// assertThat(fileSystem.files(FileQuery.onSource())).containsOnly(foo, hello); -// assertThat(fileSystem.files(FileQuery.onTest())).containsOnly(fooTest, helloTest); -// -// assertThat(fileSystem.changedFiles(FileQuery.onSource())).containsExactly(foo); -// assertThat(fileSystem.changedFiles(FileQuery.onTest())).containsExactly(fooTest); -// -// settings.setProperty(CoreProperties.INCREMENTAL_PREVIEW, true); -// settings.setProperty(CoreProperties.DRY_RUN, true); -// -// assertThat(fileSystem.files(FileQuery.onSource())).containsExactly(foo); -// assertThat(fileSystem.files(FileQuery.onTest())).containsExactly(fooTest); -// } - } 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 index f218d0bba4a..e1f852e3f09 100644 --- 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 @@ -38,19 +38,19 @@ public class FileSystemLoggerTest { @Test public void log() { - DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", mock(Settings.class), mock(InputFileCache.class), mock(FileIndexer.class)); - File src = temp.newFolder("src"); - File test = temp.newFolder("test"); - File base = temp.newFolder("base"); - 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()))); +// DefaultModuleFileSystem fs = new DefaultModuleFileSystem("foo", mock(Settings.class), mock(InputFileCache.class), mock(FileIndex.class)); +// File src = temp.newFolder("src"); +// File test = temp.newFolder("test"); +// File base = temp.newFolder("base"); +// 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/ModuleFileSystemInitializerTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializerTest.java new file mode 100644 index 00000000000..77205514a7d --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializerTest.java @@ -0,0 +1,91 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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 this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.scan.filesystem; + +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.FilenameUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.batch.bootstrap.ProjectDefinition; +import org.sonar.api.scan.filesystem.PathResolver; +import org.sonar.batch.bootstrap.TempDirectories; + +import java.io.File; +import java.io.IOException; + +import static org.fest.assertions.Assertions.assertThat; + +public class ModuleFileSystemInitializerTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + PathResolver pathResolver = new PathResolver(); + + @Test + public void test_default_directories() throws Exception { + File baseDir = temp.newFolder("base"); + File workDir = temp.newFolder("work"); + ProjectDefinition module = ProjectDefinition.create().setBaseDir(baseDir).setWorkDir(workDir); + + ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(module, new TempDirectories(), pathResolver); + + assertThat(initializer.baseDir().getCanonicalPath()).isEqualTo(baseDir.getCanonicalPath()); + assertThat(initializer.workingDir().getCanonicalPath()).isEqualTo(workDir.getCanonicalPath()); + assertThat(initializer.sourceDirs()).isEmpty(); + assertThat(initializer.testDirs()).isEmpty(); + } + + @Test + public void should_init_directories() throws IOException { + File baseDir = temp.newFolder("base"); + File buildDir = temp.newFolder("build"); + File sourceDir = new File(baseDir, "src/main/java"); + FileUtils.forceMkdir(sourceDir); + File testDir = new File(baseDir, "src/test/java"); + FileUtils.forceMkdir(testDir); + File binaryDir = new File(baseDir, "target/classes"); + FileUtils.forceMkdir(binaryDir); + + ProjectDefinition project = ProjectDefinition.create() + .setBaseDir(baseDir) + .setBuildDir(buildDir) + .addSourceDirs("src/main/java", "src/main/unknown") + .addTestDirs("src/test/java", "src/test/unknown") + .addBinaryDir("target/classes"); + + ModuleFileSystemInitializer initializer = new ModuleFileSystemInitializer(project, new TempDirectories(), pathResolver); + + assertThat(initializer.baseDir().getCanonicalPath()).isEqualTo(baseDir.getCanonicalPath()); + assertThat(initializer.buildDir().getCanonicalPath()).isEqualTo(buildDir.getCanonicalPath()); + assertThat(initializer.sourceDirs()).hasSize(1); + assertThat(path(initializer.sourceDirs().get(0))).endsWith("src/main/java"); + assertThat(initializer.testDirs()).hasSize(1); + assertThat(path(initializer.testDirs().get(0))).endsWith("src/test/java"); + assertThat(initializer.binaryDirs()).hasSize(1); + assertThat(path(initializer.binaryDirs().get(0))).endsWith("target/classes"); + } + + private String path(File f) throws IOException { + return FilenameUtils.separatorsToUnix(f.getCanonicalPath()); + } + +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ModuleFileSystemProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ModuleFileSystemProviderTest.java deleted file mode 100644 index d2aa69e6dc1..00000000000 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/ModuleFileSystemProviderTest.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * SonarQube, open source software quality management tool. - * Copyright (C) 2008-2013 SonarSource - * mailto:contact AT sonarsource DOT com - * - * SonarQube 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. - * - * SonarQube 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 this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.batch.scan.filesystem; - -import com.google.common.base.Charsets; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.FilenameUtils; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; -import org.mockito.Mockito; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.bootstrap.ProjectDefinition; -import org.sonar.api.config.Settings; -import org.sonar.api.scan.filesystem.FileQuery; -import org.sonar.api.scan.filesystem.ModuleFileSystem; -import org.sonar.batch.bootstrap.TempDirectories; - -import java.io.File; -import java.io.IOException; -import java.nio.charset.Charset; - -import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.mock; - -public class ModuleFileSystemProviderTest { - @Rule - public TemporaryFolder temp = new TemporaryFolder(); - - InputFileCache fileCache = mock(InputFileCache.class, Mockito.RETURNS_DEEP_STUBS); - Settings settings = new Settings(); - FileIndexer fileIndexer = mock(FileIndexer.class, Mockito.RETURNS_DEEP_STUBS); - - @Test - public void test_provide() throws IOException { - ModuleFileSystemProvider provider = new ModuleFileSystemProvider(); - File baseDir = temp.newFolder("base"); - File workDir = temp.newFolder("work"); - ProjectDefinition module = ProjectDefinition.create() - .setBaseDir(baseDir) - .setWorkDir(workDir); - ModuleFileSystem fs = provider.provide(module, new TempDirectories(), settings, fileCache, fileIndexer); - - assertThat(fs).isNotNull(); - assertThat(fs.baseDir().getCanonicalPath()).isEqualTo(baseDir.getCanonicalPath()); - assertThat(fs.workingDir().getCanonicalPath()).isEqualTo(workDir.getCanonicalPath()); - assertThat(fs.sourceDirs()).isEmpty(); - assertThat(fs.files(FileQuery.onSource())).isEmpty(); - assertThat(fs.testDirs()).isEmpty(); - assertThat(fs.files(FileQuery.onTest())).isEmpty(); - } - - @Test - public void default_charset_is_platform_dependent() throws IOException { - ModuleFileSystemProvider provider = new ModuleFileSystemProvider(); - - ModuleFileSystem fs = provider.provide(newSimpleModule(), new TempDirectories(), - new Settings(), mock(InputFileCache.class), mock(FileIndexer.class)); - - assertThat(fs.sourceCharset()).isEqualTo(Charset.defaultCharset()); - } - - @Test - public void set_charset() throws IOException { - ModuleFileSystemProvider provider = new ModuleFileSystemProvider(); - ProjectDefinition module = newSimpleModule(); - Settings settings = new Settings(); - settings.setProperty(CoreProperties.ENCODING_PROPERTY, Charsets.ISO_8859_1.name()); - - ModuleFileSystem fs = provider.provide(module, new TempDirectories(), - settings, mock(InputFileCache.class), mock(FileIndexer.class)); - - assertThat(fs.sourceCharset()).isEqualTo(Charsets.ISO_8859_1); - } - - @Test - public void test_directories() throws IOException { - ModuleFileSystemProvider provider = new ModuleFileSystemProvider(); - - File baseDir = temp.newFolder("base"); - File buildDir = temp.newFolder("build"); - File sourceDir = new File(baseDir, "src/main/java"); - FileUtils.forceMkdir(sourceDir); - File testDir = new File(baseDir, "src/test/java"); - FileUtils.forceMkdir(testDir); - File binaryDir = new File(baseDir, "target/classes"); - FileUtils.forceMkdir(binaryDir); - - ProjectDefinition project = ProjectDefinition.create() - .setBaseDir(baseDir) - .setBuildDir(buildDir) - .addSourceDirs("src/main/java", "src/main/unknown") - .addTestDirs("src/test/java", "src/test/unknown") - .addBinaryDir("target/classes"); - - ModuleFileSystem fs = provider.provide(project, new TempDirectories(), - new Settings(), mock(InputFileCache.class), mock(FileIndexer.class)); - - assertThat(fs.baseDir().getCanonicalPath()).isEqualTo(baseDir.getCanonicalPath()); - assertThat(fs.buildDir().getCanonicalPath()).isEqualTo(buildDir.getCanonicalPath()); - assertThat(fs.sourceDirs()).hasSize(1); - assertThat(path(fs.sourceDirs().get(0))).endsWith("src/main/java"); - assertThat(fs.testDirs()).hasSize(1); - assertThat(path(fs.testDirs().get(0))).endsWith("src/test/java"); - assertThat(fs.binaryDirs()).hasSize(1); - assertThat(path(fs.binaryDirs().get(0))).endsWith("target/classes"); - } - - private String path(File f) throws IOException { - return FilenameUtils.separatorsToUnix(f.getCanonicalPath()); - } - - private ProjectDefinition newSimpleModule() { - return ProjectDefinition.create() - .setBaseDir(temp.newFolder("base")); - } -} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleFileSystem.java index 7f5dacc9d87..dcd7cc1018d 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleFileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/ModuleFileSystem.java @@ -78,6 +78,7 @@ public interface ModuleFileSystem extends BatchComponent { /** * Search for files. Never return null. */ + // TODO deprecate List<File> files(FileQuery query); /** |