diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-02-18 13:13:58 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-02-18 13:14:14 +0100 |
commit | 5a1634e38b510c684868cfe96185929622010ece (patch) | |
tree | f1d61092a35761e26af0ded75f7884b2ddc45761 /sonar-batch/src/test/java/org/sonar | |
parent | d5c2f872387c6a2a01d83edf8f259c1a73b4f80e (diff) | |
download | sonarqube-5a1634e38b510c684868cfe96185929622010ece.tar.gz sonarqube-5a1634e38b510c684868cfe96185929622010ece.zip |
Fix backward-compatibility. File system can be changed by sensors...
Diffstat (limited to 'sonar-batch/src/test/java/org/sonar')
3 files changed, 126 insertions, 49 deletions
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<File> 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<File> 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<File> 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<File> 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<File> 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"); + } } |