diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2014-02-11 22:47:32 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2014-02-11 22:47:32 +0100 |
commit | 2dbbfd0e46a23e9012f981ba868bdbbd5988dd72 (patch) | |
tree | 89d0a5d7ba83709d89e87e945788ccb6886010a8 /sonar-batch/src/test/java | |
parent | 0944275d564461066c49d633f69549428f8bf65d (diff) | |
download | sonarqube-2dbbfd0e46a23e9012f981ba868bdbbd5988dd72.tar.gz sonarqube-2dbbfd0e46a23e9012f981ba868bdbbd5988dd72.zip |
SONAR-926 add missing tests
Diffstat (limited to 'sonar-batch/src/test/java')
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactoryTest.java | 50 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderTest.java | 172 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java | 39 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashLoaderTest.java (renamed from sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashesLoaderTest.java) | 2 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/StatusDetectionFactoryTest.java | 34 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/StatusDetectionTest.java | 40 |
6 files changed, 336 insertions, 1 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactoryTest.java new file mode 100644 index 00000000000..5eba1db2260 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactoryTest.java @@ -0,0 +1,50 @@ +/* + * 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.junit.Test; +import org.mockito.Mockito; +import org.sonar.api.resources.Project; +import org.sonar.api.scan.filesystem.PathResolver; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class InputFileBuilderFactoryTest { + @Test + public void create_builder() throws Exception { + PathResolver pathResolver = new PathResolver(); + Project project = new Project("struts"); + LanguageDetectionFactory langDetectionFactory = mock(LanguageDetectionFactory.class, Mockito.RETURNS_MOCKS); + StatusDetectionFactory statusDetectionFactory = mock(StatusDetectionFactory.class, Mockito.RETURNS_MOCKS); + DefaultModuleFileSystem fs = mock(DefaultModuleFileSystem.class); + + InputFileBuilderFactory factory = new InputFileBuilderFactory( + project, pathResolver, langDetectionFactory, + statusDetectionFactory); + InputFileBuilder builder = factory.create(fs); + + assertThat(builder.langDetection()).isNotNull(); + assertThat(builder.statusDetection()).isNotNull(); + assertThat(builder.pathResolver()).isSameAs(pathResolver); + assertThat(builder.fs()).isSameAs(fs); + assertThat(builder.moduleKey()).isEqualTo("struts"); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderTest.java new file mode 100644 index 00000000000..4e19c94bd1b --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderTest.java @@ -0,0 +1,172 @@ +/* + * 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.Charsets; +import org.apache.commons.io.FileUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.sonar.api.scan.filesystem.InputFile; +import org.sonar.api.scan.filesystem.PathResolver; +import org.sonar.api.scan.filesystem.internal.DefaultInputFile; +import org.sonar.api.utils.PathUtils; + +import java.io.File; +import java.util.Arrays; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class InputFileBuilderTest { + + @Rule + public TemporaryFolder temp = new TemporaryFolder(); + + LanguageDetection langDetection = mock(LanguageDetection.class); + StatusDetection statusDetection = mock(StatusDetection.class); + DefaultModuleFileSystem fs = mock(DefaultModuleFileSystem.class); + + @Test + public void create_input_file() throws Exception { + // file system + File basedir = temp.newFolder(); + File srcFile = new File(basedir, "src/main/java/foo/Bar.java"); + FileUtils.touch(srcFile); + FileUtils.write(srcFile, "single line"); + when(fs.baseDir()).thenReturn(basedir); + when(fs.sourceCharset()).thenReturn(Charsets.UTF_8); + + // lang + when(langDetection.language(any(InputFile.class))).thenReturn("java"); + + // status + when(statusDetection.status("src/main/java/foo/Bar.java", "6c1d64c0b3555892fe7273e954f6fb5a")).thenReturn(InputFile.STATUS_ADDED); + + InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(), + langDetection, statusDetection, fs); + DefaultInputFile inputFile = builder.create(srcFile, InputFile.TYPE_MAIN); + + assertThat(inputFile.name()).isEqualTo("Bar.java"); + assertThat(inputFile.file()).isEqualTo(srcFile.getAbsoluteFile()); + assertThat(inputFile.absolutePath()).isEqualTo(PathUtils.sanitize(srcFile.getAbsolutePath())); + assertThat(inputFile.language()).isEqualTo("java"); + assertThat(inputFile.attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_KEY)).isEqualTo("struts:src/main/java/foo/Bar.java"); + assertThat(inputFile.path()).isEqualTo("src/main/java/foo/Bar.java"); + + assertThat(inputFile.attribute(InputFile.ATTRIBUTE_LINE_COUNT)).isEqualTo("1"); + assertThat(inputFile.attribute(DefaultInputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH)).isNull(); + assertThat(inputFile.attribute(DefaultInputFile.ATTRIBUTE_SOURCEDIR_PATH)).isNull(); + assertThat(inputFile.attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_DEPRECATED_KEY)).isNull(); + } + + @Test + public void return_null_if_file_outside_basedir() throws Exception { + // file system + File basedir = temp.newFolder(); + File otherDir = temp.newFolder(); + File srcFile = new File(otherDir, "src/main/java/foo/Bar.java"); + FileUtils.touch(srcFile); + when(fs.baseDir()).thenReturn(basedir); + + InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(), + langDetection, statusDetection, fs); + DefaultInputFile inputFile = builder.create(srcFile, InputFile.TYPE_MAIN); + + assertThat(inputFile).isNull(); + } + + @Test + public void return_null_if_language_not_detected() throws Exception { + // file system + File basedir = temp.newFolder(); + File srcFile = new File(basedir, "src/main/java/foo/Bar.java"); + FileUtils.touch(srcFile); + FileUtils.write(srcFile, "single line"); + when(fs.baseDir()).thenReturn(basedir); + when(fs.sourceCharset()).thenReturn(Charsets.UTF_8); + + // lang + when(langDetection.language(any(InputFile.class))).thenReturn(null); + + InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(), + langDetection, statusDetection, fs); + DefaultInputFile inputFile = builder.create(srcFile, InputFile.TYPE_MAIN); + + assertThat(inputFile).isNull(); + } + + @Test + public void fill_deprecated_data_of_java_file() throws Exception { + // file system + File basedir = temp.newFolder(); + File srcFile = new File(basedir, "src/main/java/foo/Bar.java"); + FileUtils.touch(srcFile); + FileUtils.write(srcFile, "single line"); + when(fs.baseDir()).thenReturn(basedir); + when(fs.sourceCharset()).thenReturn(Charsets.UTF_8); + File sourceDir = new File(basedir, "src/main/java"); + when(fs.sourceDirs()).thenReturn(Arrays.asList(sourceDir)); + + // lang + when(langDetection.language(any(InputFile.class))).thenReturn("java"); + + // status + when(statusDetection.status("src/main/java/foo/Bar.java", "6c1d64c0b3555892fe7273e954f6fb5a")).thenReturn(InputFile.STATUS_ADDED); + + InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(), + langDetection, statusDetection, fs); + DefaultInputFile inputFile = builder.create(srcFile, InputFile.TYPE_MAIN); + + assertThat(inputFile.attribute(DefaultInputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH)).isEqualTo("foo/Bar.java"); + assertThat(inputFile.attribute(DefaultInputFile.ATTRIBUTE_SOURCEDIR_PATH)).isEqualTo(PathUtils.sanitize(sourceDir.getAbsolutePath())); + assertThat(inputFile.attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_DEPRECATED_KEY)).isEqualTo("struts:foo.Bar"); + } + + @Test + public void fill_deprecated_data_of_non_java_file() throws Exception { + // file system + File basedir = temp.newFolder(); + File srcFile = new File(basedir, "src/foo/Bar.php"); + FileUtils.touch(srcFile); + FileUtils.write(srcFile, "single line"); + when(fs.baseDir()).thenReturn(basedir); + when(fs.sourceCharset()).thenReturn(Charsets.UTF_8); + File sourceDir = new File(basedir, "src"); + when(fs.sourceDirs()).thenReturn(Arrays.asList(sourceDir)); + + // lang + when(langDetection.language(any(InputFile.class))).thenReturn("php"); + + // status + when(statusDetection.status("src/Bar.php", "6c1d64c0b3555892fe7273e954f6fb5a")).thenReturn(InputFile.STATUS_ADDED); + + InputFileBuilder builder = new InputFileBuilder("struts", new PathResolver(), + langDetection, statusDetection, fs); + DefaultInputFile inputFile = builder.create(srcFile, InputFile.TYPE_MAIN); + + assertThat(inputFile.attribute(DefaultInputFile.ATTRIBUTE_SOURCE_RELATIVE_PATH)).isEqualTo("foo/Bar.php"); + assertThat(inputFile.attribute(DefaultInputFile.ATTRIBUTE_SOURCEDIR_PATH)).isEqualTo(PathUtils.sanitize(sourceDir.getAbsolutePath())); + assertThat(inputFile.attribute(DefaultInputFile.ATTRIBUTE_COMPONENT_DEPRECATED_KEY)).isEqualTo("struts:foo/Bar.php"); + + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java new file mode 100644 index 00000000000..0df875cfe42 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java @@ -0,0 +1,39 @@ +/* + * 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.junit.Test; +import org.sonar.api.config.Settings; +import org.sonar.api.resources.Java; +import org.sonar.api.resources.Languages; + +import static org.fest.assertions.Assertions.assertThat; + +public class LanguageDetectionFactoryTest { + @Test + public void testCreate() throws Exception { + Languages languages = new Languages(Java.INSTANCE); + LanguageDetectionFactory factory = new LanguageDetectionFactory(new Settings(), languages); + LanguageDetection languageDetection = factory.create(); + assertThat(languageDetection).isNotNull(); + assertThat(languageDetection.patternsByLanguage()).hasSize(1); + assertThat(languageDetection.patternsByLanguage().containsKey("java")).isTrue(); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashLoaderTest.java index 6b4e518a354..e886299bfbc 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashesLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashLoaderTest.java @@ -38,7 +38,7 @@ import static org.fest.assertions.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class PreviousFileHashesLoaderTest { +public class PreviousFileHashLoaderTest { @Rule public TemporaryFolder temp = new TemporaryFolder(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/StatusDetectionFactoryTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/StatusDetectionFactoryTest.java new file mode 100644 index 00000000000..460d527848c --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/StatusDetectionFactoryTest.java @@ -0,0 +1,34 @@ +/* + * 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.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class StatusDetectionFactoryTest { + @Test + public void testCreate() throws Exception { + StatusDetectionFactory factory = new StatusDetectionFactory(mock(PreviousFileHashLoader.class)); + StatusDetection detection = factory.create(); + assertThat(detection).isNotNull(); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/StatusDetectionTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/StatusDetectionTest.java new file mode 100644 index 00000000000..79404404f8f --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/StatusDetectionTest.java @@ -0,0 +1,40 @@ +/* + * 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.ImmutableMap; +import org.junit.Test; +import org.sonar.api.scan.filesystem.InputFile; + +import static org.fest.assertions.Assertions.assertThat; + +public class StatusDetectionTest { + @Test + public void detect_status() throws Exception { + StatusDetection statusDetection = new StatusDetection(ImmutableMap.<String, String>of( + "src/Foo.java", "ABCDE", + "src/Bar.java", "FGHIJ" + )); + + assertThat(statusDetection.status("src/Foo.java", "ABCDE")).isEqualTo(InputFile.STATUS_SAME); + assertThat(statusDetection.status("src/Foo.java", "XXXXX")).isEqualTo(InputFile.STATUS_CHANGED); + assertThat(statusDetection.status("src/Other.java", "QWERT")).isEqualTo(InputFile.STATUS_ADDED); + } +} |