aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-batch/src/test/java/org/sonar/batch/scan
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2014-02-10 16:00:27 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2014-02-10 16:23:07 +0100
commit5eaa8f5ba79468354e84bb3fd9c8b8a581e65faa (patch)
treef46612a5eee00f38c8e44dfbfa85bf49e48ce96e /sonar-batch/src/test/java/org/sonar/batch/scan
parentbdee87ee4aa439e188604afd46d5c5b374599dbe (diff)
downloadsonarqube-5eaa8f5ba79468354e84bb3fd9c8b8a581e65faa.tar.gz
sonarqube-5eaa8f5ba79468354e84bb3fd9c8b8a581e65faa.zip
SONAR-926 improve computation of number of lines
Diffstat (limited to 'sonar-batch/src/test/java/org/sonar/batch/scan')
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileHashDigestTest.java82
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileHashesTest.java58
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileIndexTest.java71
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java114
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java212
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageRecognizerTest.java245
-rw-r--r--sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashesLoaderTest.java (renamed from sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/RemoteFileHashesTest.java)22
7 files changed, 336 insertions, 468 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileHashDigestTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileHashDigestTest.java
deleted file mode 100644
index c3345c26af3..00000000000
--- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileHashDigestTest.java
+++ /dev/null
@@ -1,82 +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.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.rules.TemporaryFolder;
-
-import java.io.File;
-
-import static org.fest.assertions.Assertions.assertThat;
-
-public class FileHashDigestTest {
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Test
- public void should_compute_hash() throws Exception {
- File tempFile = temp.newFile();
- FileUtils.write(tempFile, "foo\r\nbar", Charsets.UTF_8, true);
-
- assertThat(FileHashDigest.INSTANCE.hash(tempFile, Charsets.UTF_8)).isEqualTo("daef8a22a3f12580beadf086a9e11519");
- }
-
- @Test
- public void should_normalize_line_ends() throws Exception {
- File file1 = temp.newFile();
- FileUtils.write(file1, "foobar\nfofo", Charsets.UTF_8);
- String hash1 = FileHashDigest.INSTANCE.hash(file1, Charsets.UTF_8);
-
- File file2 = temp.newFile();
- FileUtils.write(file2, "foobar\r\nfofo", Charsets.UTF_8);
- String hash2 = FileHashDigest.INSTANCE.hash(file2, Charsets.UTF_8);
-
- File file3 = temp.newFile();
- FileUtils.write(file3, "foobar\rfofo", Charsets.UTF_8);
- String hash3 = FileHashDigest.INSTANCE.hash(file3, Charsets.UTF_8);
-
- File file4 = temp.newFile();
- FileUtils.write(file4, "foobar\nfofo\n", Charsets.UTF_8);
- String hash4 = FileHashDigest.INSTANCE.hash(file4, Charsets.UTF_8);
-
- assertThat(hash1).isEqualTo(hash2);
- assertThat(hash1).isEqualTo(hash3);
- assertThat(hash1).isNotEqualTo(hash4);
- }
-
- @Test
- public void should_throw_if_file_does_not_exist() throws Exception {
- File tempFolder = temp.newFolder();
- File file = new File(tempFolder, "doesNotExist.txt");
-
- thrown.expect(IllegalStateException.class);
- thrown.expectMessage("Fail to compute hash of file " + file.getAbsolutePath() + " with charset UTF-8");
-
- FileHashDigest.INSTANCE.hash(file, Charsets.UTF_8);
- }
-}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileHashesTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileHashesTest.java
deleted file mode 100644
index 98ae99d7006..00000000000
--- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileHashesTest.java
+++ /dev/null
@@ -1,58 +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.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-
-import java.io.File;
-import java.nio.charset.Charset;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.*;
-
-public class FileHashesTest {
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- RemoteFileHashes remoteFileHashes = mock(RemoteFileHashes.class);
-
- @Test
- public void hash() throws Exception {
- File file = temp.newFile();
- FileUtils.write(file, "fooo");
-
- FileHashes hashes = new FileHashes(remoteFileHashes);
- assertThat(hashes.hash(file, Charset.forName("UTF-8"))).isEqualTo("efc4470c96a94b1ff400175ef8368444");
- verifyZeroInteractions(remoteFileHashes);
- }
-
- @Test
- public void remote_hash() throws Exception {
- String path = "src/main/java/Foo.java";
- when(remoteFileHashes.remoteHash(path)).thenReturn("ABCDE");
-
- FileHashes hashes = new FileHashes(remoteFileHashes);
- assertThat(hashes.remoteHash(path)).isEqualTo("ABCDE");
- }
-}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileIndexTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileIndexTest.java
deleted file mode 100644
index b0c6ed5c763..00000000000
--- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileIndexTest.java
+++ /dev/null
@@ -1,71 +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.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.api.resources.Project;
-import org.sonar.api.scan.filesystem.InputDir;
-import org.sonar.api.scan.filesystem.InputFile;
-import org.sonar.api.scan.filesystem.PathResolver;
-import org.sonar.api.scan.filesystem.internal.DefaultInputDir;
-
-import java.io.File;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.anyString;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-public class FileIndexTest {
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Test
- public void should_return_inputDir() throws Exception {
- FileIndex index = new FileIndex(null, null, null, null, null, new PathResolver(), new Project("myProject"));
- File baseDir = temp.newFolder();
- DefaultModuleFileSystem fileSystem = mock(DefaultModuleFileSystem.class);
- when(fileSystem.baseDir()).thenReturn(baseDir);
- File ioFile = new File(baseDir, "src/main/java/com/foo");
- InputDir inputDir = index.inputDir(fileSystem, ioFile);
-
- assertThat(inputDir.name()).isEqualTo("src/main/java/com/foo");
- assertThat(inputDir.file()).isEqualTo(ioFile);
- assertThat(inputDir.attribute(DefaultInputDir.ATTRIBUTE_COMPONENT_KEY)).isEqualTo("myProject:src/main/java/com/foo");
- }
-
- @Test
- public void should_not_index_aggregator() throws Exception {
- Project project = new Project("myProject");
- new Project("moduleA").setParent(project);
- InputFileCache fileCache = mock(InputFileCache.class);
- FileIndex index = new FileIndex(null, null, null, fileCache, null, new PathResolver(), project);
-
- index.index(null);
-
- verify(fileCache, never()).put(anyString(), any(InputFile.class));
- }
-}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java
new file mode 100644
index 00000000000..b0a4703d773
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/FileMetadataTest.java
@@ -0,0 +1,114 @@
+/*
+ * 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.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+
+import java.io.File;
+
+import static org.fest.assertions.Assertions.assertThat;
+
+public class FileMetadataTest {
+
+ private static final String EXPECTED_HASH_WITHOUT_LATEST_EOL = "c80cc50d65ace6c4eb63f189d274dbeb";
+ private static final String EXPECTED_HASH_WITH_LATEST_EOL = "bf77e51d219e7d7d643faac86f1b5d15";
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Test
+ public void windows_without_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\r\nbar\r\nbaz", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(3L);
+ assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_WITHOUT_LATEST_EOL);
+ }
+
+ @Test
+ public void windows_with_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\r\nbar\r\nbaz\r\n", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(3L);
+ assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_WITH_LATEST_EOL);
+ }
+
+ @Test
+ public void unix_without_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\nbar\nbaz", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(3L);
+ assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_WITHOUT_LATEST_EOL);
+ }
+
+ @Test
+ public void unix_with_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\nbar\nbaz\n", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(3L);
+ assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_WITH_LATEST_EOL);
+ }
+
+ @Test
+ public void mix_of_newlines_with_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\nbar\r\nbaz\n", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(3L);
+ assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_WITH_LATEST_EOL);
+ }
+
+ @Test
+ public void mix_of_newlines_without_latest_eol() throws Exception {
+ File tempFile = temp.newFile();
+ FileUtils.write(tempFile, "foo\nbar\r\nbaz", Charsets.UTF_8, true);
+
+ FileMetadata.Metadata metadata = FileMetadata.INSTANCE.read(tempFile, Charsets.UTF_8);
+ assertThat(metadata.lines).isEqualTo(3L);
+ assertThat(metadata.hash).isEqualTo(EXPECTED_HASH_WITHOUT_LATEST_EOL);
+ }
+
+ @Test
+ public void should_throw_if_file_does_not_exist() throws Exception {
+ File tempFolder = temp.newFolder();
+ File file = new File(tempFolder, "doesNotExist.txt");
+
+ thrown.expect(IllegalStateException.class);
+ thrown.expectMessage("Fail to read file '" + file.getAbsolutePath() + "' with encoding 'UTF-8'");
+
+ FileMetadata.INSTANCE.read(file, Charsets.UTF_8);
+ }
+}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java
new file mode 100644
index 00000000000..4f455152512
--- /dev/null
+++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionTest.java
@@ -0,0 +1,212 @@
+/*
+ * 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.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.CoreProperties;
+import org.sonar.api.config.Settings;
+import org.sonar.api.resources.Language;
+import org.sonar.api.resources.Languages;
+import org.sonar.api.scan.filesystem.InputFile;
+import org.sonar.api.scan.filesystem.internal.InputFileBuilder;
+import org.sonar.api.utils.MessageException;
+
+import java.io.File;
+import java.io.IOException;
+
+import static junit.framework.Assert.fail;
+import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Mockito.spy;
+
+public class LanguageDetectionTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void test_sanitizeExtension() throws Exception {
+ assertThat(LanguageDetection.sanitizeExtension(".cbl")).isEqualTo("cbl");
+ assertThat(LanguageDetection.sanitizeExtension(".CBL")).isEqualTo("cbl");
+ assertThat(LanguageDetection.sanitizeExtension("CBL")).isEqualTo("cbl");
+ assertThat(LanguageDetection.sanitizeExtension("cbl")).isEqualTo("cbl");
+ }
+
+ @Test
+ public void search_by_file_extension() throws Exception {
+ Languages languages = new Languages(new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob"));
+ LanguageDetection detection = new LanguageDetection(new Settings(), languages);
+
+ assertThat(detection.language(newInputFile("Foo.java"))).isEqualTo("java");
+ assertThat(detection.language(newInputFile("src/Foo.java"))).isEqualTo("java");
+ assertThat(detection.language(newInputFile("Foo.JAVA"))).isEqualTo("java");
+ assertThat(detection.language(newInputFile("Foo.jav"))).isEqualTo("java");
+ assertThat(detection.language(newInputFile("Foo.Jav"))).isEqualTo("java");
+
+ assertThat(detection.language(newInputFile("abc.cbl"))).isEqualTo("cobol");
+ assertThat(detection.language(newInputFile("abc.CBL"))).isEqualTo("cobol");
+
+ assertThat(detection.language(newInputFile("abc.php"))).isNull();
+ assertThat(detection.language(newInputFile("abc"))).isNull();
+ }
+
+ @Test
+ public void should_not_fail_if_no_language() throws Exception {
+ LanguageDetection detection = spy(new LanguageDetection(new Settings(), new Languages()));
+ assertThat(detection.language(newInputFile("Foo.java"))).isNull();
+ }
+
+ @Test
+ public void plugin_can_declare_a_file_extension_twice_for_case_sensitivity() throws Exception {
+ Languages languages = new Languages(new MockLanguage("abap", "abap", "ABAP"));
+
+ LanguageDetection detection = new LanguageDetection(new Settings(), languages);
+ assertThat(detection.language(newInputFile("abc.abap"))).isEqualTo("abap");
+ }
+
+ @Test
+ public void language_with_no_extension() throws Exception {
+ // abap does not declare any file extensions.
+ // When analyzing an ABAP project, then all source files must be parsed.
+ Languages languages = new Languages(new MockLanguage("java", "java"), new MockLanguage("abap"));
+
+ // No side-effect on non-ABAP projects
+ LanguageDetection detection = new LanguageDetection(new Settings(), languages);
+ assertThat(detection.language(newInputFile("abc"))).isNull();
+ assertThat(detection.language(newInputFile("abc.abap"))).isNull();
+ assertThat(detection.language(newInputFile("abc.java"))).isEqualTo("java");
+
+ Settings settings = new Settings();
+ settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "abap");
+ detection = new LanguageDetection(settings, languages);
+ assertThat(detection.language(newInputFile("abc"))).isEqualTo("abap");
+ assertThat(detection.language(newInputFile("abc.txt"))).isEqualTo("abap");
+ assertThat(detection.language(newInputFile("abc.java"))).isEqualTo("abap");
+ }
+
+ @Test
+ public void force_language_using_deprecated_property() throws Exception {
+ Languages languages = new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php"));
+
+ Settings settings = new Settings();
+ settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "java");
+ LanguageDetection detection = new LanguageDetection(settings, languages);
+ assertThat(detection.language(newInputFile("abc"))).isNull();
+ assertThat(detection.language(newInputFile("abc.php"))).isNull();
+ assertThat(detection.language(newInputFile("abc.java"))).isEqualTo("java");
+ assertThat(detection.language(newInputFile("src/abc.java"))).isEqualTo("java");
+ }
+
+ @Test
+ public void fail_if_invalid_language() throws Exception {
+ thrown.expect(MessageException.class);
+ thrown.expectMessage("No language is installed with key 'unknown'. Please update property 'sonar.language'");
+
+ Languages languages = new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php"));
+ Settings settings = new Settings();
+ settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "unknown");
+ new LanguageDetection(settings, languages);
+ }
+
+ @Test
+ public void fail_if_conflicting_language_suffix() throws Exception {
+ Languages languages = new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"));
+ LanguageDetection detection = new LanguageDetection(new Settings(), languages);
+ try {
+ detection.language(newInputFile("abc.xhtml"));
+ fail();
+ } catch (MessageException e) {
+ assertThat(e.getMessage())
+ .contains("Language of file 'abc.xhtml' can not be decided as the file matches patterns of both ")
+ .contains("sonar.lang.patterns.web : **/*.xhtml")
+ .contains("sonar.lang.patterns.xml : **/*.xhtml");
+ }
+ }
+
+ @Test
+ public void solve_conflict_using_filepattern() throws Exception {
+ Languages languages = new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"));
+
+ Settings settings = new Settings();
+ settings.setProperty("sonar.lang.patterns.xml", "xml/**");
+ settings.setProperty("sonar.lang.patterns.web", "web/**");
+ LanguageDetection detection = new LanguageDetection(settings, languages);
+ assertThat(detection.language(newInputFile("xml/abc.xhtml"))).isEqualTo("xml");
+ assertThat(detection.language(newInputFile("web/abc.xhtml"))).isEqualTo("web");
+ }
+
+ @Test
+ public void fail_if_conflicting_filepattern() throws Exception {
+ Languages languages = new Languages(new MockLanguage("abap", "abap"), new MockLanguage("cobol", "cobol"));
+ Settings settings = new Settings();
+ settings.setProperty("sonar.lang.patterns.abap", "*.abap,*.txt");
+ settings.setProperty("sonar.lang.patterns.cobol", "*.cobol,*.txt");
+
+ LanguageDetection detection = new LanguageDetection(settings, languages);
+
+ assertThat(detection.language(newInputFile("abc.abap"))).isEqualTo("abap");
+ assertThat(detection.language(newInputFile("abc.cobol"))).isEqualTo("cobol");
+ try {
+ detection.language(newInputFile("abc.txt"));
+ fail();
+ } catch (MessageException e) {
+ assertThat(e.getMessage())
+ .contains("Language of file 'abc.txt' can not be decided as the file matches patterns of both ")
+ .contains("sonar.lang.patterns.abap : *.abap,*.txt")
+ .contains("sonar.lang.patterns.cobol : *.cobol,*.txt");
+ }
+ }
+
+ private InputFile newInputFile(String path) throws IOException {
+ File basedir = temp.newFolder();
+ return new InputFileBuilder(new File(basedir, path), Charsets.UTF_8, path).build();
+ }
+
+ static class MockLanguage implements Language {
+ private final String key;
+ private final String[] extensions;
+
+ MockLanguage(String key, String... extensions) {
+ this.key = key;
+ this.extensions = extensions;
+ }
+
+ @Override
+ public String getKey() {
+ return key;
+ }
+
+ @Override
+ public String getName() {
+ return key;
+ }
+
+ @Override
+ public String[] getFileSuffixes() {
+ return extensions;
+ }
+ }
+}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageRecognizerTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageRecognizerTest.java
deleted file mode 100644
index 70984dce07c..00000000000
--- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageRecognizerTest.java
+++ /dev/null
@@ -1,245 +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.hamcrest.BaseMatcher;
-import org.hamcrest.Description;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.api.CoreProperties;
-import org.sonar.api.config.Settings;
-import org.sonar.api.resources.Language;
-import org.sonar.api.resources.Languages;
-import org.sonar.api.scan.filesystem.InputFile;
-import org.sonar.api.scan.filesystem.internal.InputFileBuilder;
-import org.sonar.api.utils.SonarException;
-
-import java.io.File;
-import java.io.IOException;
-
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Mockito.spy;
-
-public class LanguageRecognizerTest {
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- @Test
- public void test_sanitizeExtension() throws Exception {
- assertThat(LanguageRecognizer.sanitizeExtension(".cbl")).isEqualTo("cbl");
- assertThat(LanguageRecognizer.sanitizeExtension(".CBL")).isEqualTo("cbl");
- assertThat(LanguageRecognizer.sanitizeExtension("CBL")).isEqualTo("cbl");
- assertThat(LanguageRecognizer.sanitizeExtension("cbl")).isEqualTo("cbl");
- }
-
- @Test
- public void search_by_file_extension() throws Exception {
- Languages languages = new Languages(new MockLanguage("java", "java", "jav"), new MockLanguage("cobol", "cbl", "cob"));
- LanguageRecognizer recognizer = new LanguageRecognizer(new Settings(), languages);
-
- recognizer.start();
- assertThat(recognizer.of(newInputFile("Foo.java"))).isEqualTo("java");
- assertThat(recognizer.of(newInputFile("src/Foo.java"))).isEqualTo("java");
- assertThat(recognizer.of(newInputFile("Foo.JAVA"))).isEqualTo("java");
- assertThat(recognizer.of(newInputFile("Foo.jav"))).isEqualTo("java");
- assertThat(recognizer.of(newInputFile("Foo.Jav"))).isEqualTo("java");
-
- assertThat(recognizer.of(newInputFile("abc.cbl"))).isEqualTo("cobol");
- assertThat(recognizer.of(newInputFile("abc.CBL"))).isEqualTo("cobol");
-
- assertThat(recognizer.of(newInputFile("abc.php"))).isNull();
- assertThat(recognizer.of(newInputFile("abc"))).isNull();
- recognizer.stop();
- }
-
- @Test
- public void should_not_fail_if_no_language() throws Exception {
- LanguageRecognizer recognizer = spy(new LanguageRecognizer(new Settings(), new Languages()));
- recognizer.start();
- assertThat(recognizer.of(newInputFile("Foo.java"))).isNull();
- }
-
- @Test
- public void plugin_can_declare_a_file_extension_twice_for_case_sensitivity() throws Exception {
- Languages languages = new Languages(new MockLanguage("abap", "abap", "ABAP"));
-
- LanguageRecognizer recognizer = new LanguageRecognizer(new Settings(), languages);
- recognizer.start();
- assertThat(recognizer.of(newInputFile("abc.abap"))).isEqualTo("abap");
- }
-
- @Test
- public void language_with_no_extension() throws Exception {
- // abap does not declare any file extensions.
- // When analyzing an ABAP project, then all source files must be parsed.
- Languages languages = new Languages(new MockLanguage("java", "java"), new MockLanguage("abap"));
-
- // No side-effect on non-ABAP projects
- LanguageRecognizer recognizer = new LanguageRecognizer(new Settings(), languages);
- recognizer.start();
- assertThat(recognizer.of(newInputFile("abc"))).isNull();
- assertThat(recognizer.of(newInputFile("abc.abap"))).isNull();
- assertThat(recognizer.of(newInputFile("abc.java"))).isEqualTo("java");
- recognizer.stop();
-
- Settings settings = new Settings();
- settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "abap");
- recognizer = new LanguageRecognizer(settings, languages);
- recognizer.start();
- assertThat(recognizer.of(newInputFile("abc"))).isEqualTo("abap");
- assertThat(recognizer.of(newInputFile("abc.txt"))).isEqualTo("abap");
- assertThat(recognizer.of(newInputFile("abc.java"))).isEqualTo("abap");
- recognizer.stop();
- }
-
- @Test
- public void force_language_using_deprecated_property() throws Exception {
- Languages languages = new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php"));
-
- Settings settings = new Settings();
- settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "java");
- LanguageRecognizer recognizer = new LanguageRecognizer(settings, languages);
- recognizer.start();
- assertThat(recognizer.of(newInputFile("abc"))).isNull();
- assertThat(recognizer.of(newInputFile("abc.php"))).isNull();
- assertThat(recognizer.of(newInputFile("abc.java"))).isEqualTo("java");
- assertThat(recognizer.of(newInputFile("src/abc.java"))).isEqualTo("java");
- recognizer.stop();
- }
-
- @Test
- public void fail_if_invalid_language() throws Exception {
- Languages languages = new Languages(new MockLanguage("java", "java"), new MockLanguage("php", "php"));
-
- Settings settings = new Settings();
- settings.setProperty(CoreProperties.PROJECT_LANGUAGE_PROPERTY, "unknow");
- LanguageRecognizer recognizer = new LanguageRecognizer(settings, languages);
- recognizer.start();
- thrown.expect(SonarException.class);
- thrown.expectMessage("No language is installed with key 'unknow'. Please update property 'sonar.language'");
- recognizer.of(newInputFile("abc"));
- recognizer.stop();
- }
-
- @Test
- public void fail_if_conflicting_language_suffix() throws Exception {
- Languages languages = new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"));
-
- Settings settings = new Settings();
- LanguageRecognizer recognizer = new LanguageRecognizer(settings, languages);
- recognizer.start();
- thrown.expect(SonarException.class);
- thrown.expectMessage(new BaseMatcher<String>() {
- @Override
- public void describeTo(Description arg0) {
- }
-
- @Override
- public boolean matches(Object arg0) {
- // Need custom matcher because order of language in the exception is not deterministic (hashmap)
- return arg0.toString().contains("Language of file 'abc.xhtml' can not be decided as the file matches patterns of both ")
- && arg0.toString().contains("sonar.lang.patterns.web : **/*.xhtml")
- && arg0.toString().contains("sonar.lang.patterns.xml : **/*.xhtml");
- }
- });
- recognizer.of(newInputFile("abc.xhtml"));
- recognizer.stop();
- }
-
- @Test
- public void solve_conflict_using_filepattern() throws Exception {
- Languages languages = new Languages(new MockLanguage("xml", "xhtml"), new MockLanguage("web", "xhtml"));
-
- Settings settings = new Settings();
- settings.setProperty("sonar.lang.patterns.xml", "xml/**");
- settings.setProperty("sonar.lang.patterns.web", "web/**");
- LanguageRecognizer recognizer = new LanguageRecognizer(settings, languages);
- recognizer.start();
- assertThat(recognizer.of(newInputFile("xml/abc.xhtml"))).isEqualTo("xml");
- assertThat(recognizer.of(newInputFile("web/abc.xhtml"))).isEqualTo("web");
- recognizer.stop();
- }
-
- @Test
- public void fail_if_conflicting_filepattern() throws Exception {
- Languages languages = new Languages(new MockLanguage("abap", "abap"), new MockLanguage("cobol", "cobol"));
-
- Settings settings = new Settings();
- settings.setProperty("sonar.lang.patterns.abap", "*.abap,*.txt");
- settings.setProperty("sonar.lang.patterns.cobol", "*.cobol,*.txt");
- LanguageRecognizer recognizer = new LanguageRecognizer(settings, languages);
- recognizer.start();
- assertThat(recognizer.of(newInputFile("abc.abap"))).isEqualTo("abap");
- assertThat(recognizer.of(newInputFile("abc.cobol"))).isEqualTo("cobol");
- thrown.expect(SonarException.class);
- thrown.expectMessage(new BaseMatcher<String>() {
- @Override
- public void describeTo(Description arg0) {
- }
-
- @Override
- public boolean matches(Object arg0) {
- // Need custom matcher because order of language in the exception is not deterministic (hashmap)
- return arg0.toString().contains("Language of file 'abc.txt' can not be decided as the file matches patterns of both ")
- && arg0.toString().contains("sonar.lang.patterns.abap : *.abap,*.txt")
- && arg0.toString().contains("sonar.lang.patterns.cobol : *.cobol,*.txt");
- }
- });
- recognizer.of(newInputFile("abc.txt"));
- recognizer.stop();
- }
-
- private InputFile newInputFile(String path) throws IOException {
- File basedir = temp.newFolder();
- return new InputFileBuilder(new File(basedir, path), Charsets.UTF_8, path).build();
- }
-
- static class MockLanguage implements Language {
- private final String key;
- private final String[] extensions;
-
- MockLanguage(String key, String... extensions) {
- this.key = key;
- this.extensions = extensions;
- }
-
- @Override
- public String getKey() {
- return key;
- }
-
- @Override
- public String getName() {
- return key;
- }
-
- @Override
- public String[] getFileSuffixes() {
- return extensions;
- }
- }
-}
diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/RemoteFileHashesTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashesLoaderTest.java
index 1212f999fc3..6b4e518a354 100644
--- a/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/RemoteFileHashesTest.java
+++ b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashesLoaderTest.java
@@ -32,12 +32,13 @@ import org.sonar.core.source.db.SnapshotDataDto;
import java.util.Arrays;
import java.util.Date;
+import java.util.Map;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public class RemoteFileHashesTest {
+public class PreviousFileHashesLoaderTest {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@@ -48,15 +49,14 @@ public class RemoteFileHashesTest {
PastSnapshotFinder pastSnapshotFinder = mock(PastSnapshotFinder.class);
Snapshot snapshot = mock(Snapshot.class);
SnapshotDataDao snapshotDataDao = mock(SnapshotDataDao.class);
- RemoteFileHashes hashes = new RemoteFileHashes(snapshot, snapshotDataDao, pastSnapshotFinder);
+ PreviousFileHashLoader loader = new PreviousFileHashLoader(snapshot, snapshotDataDao, pastSnapshotFinder);
@Test
- public void should_return_null_if_no_remote_snapshot() throws Exception {
+ public void should_return_null_if_no_previous_snapshot() throws Exception {
when(pastSnapshotFinder.findPreviousAnalysis(snapshot)).thenReturn(new PastSnapshot("foo"));
- hashes.start();
- assertThat(hashes.remoteHash("src/main/java/foo/Bar.java")).isNull();
- hashes.stop();
+ Map<String, String> hashByRelativePath = loader.hashByRelativePath();
+ assertThat(hashByRelativePath.get("src/main/java/foo/Bar.java")).isNull();
}
@Test
@@ -65,9 +65,8 @@ public class RemoteFileHashesTest {
PastSnapshot pastSnapshot = new PastSnapshot("foo", new Date(), previousSnapshot);
when(pastSnapshotFinder.findPreviousAnalysis(snapshot)).thenReturn(pastSnapshot);
- hashes.start();
- assertThat(hashes.remoteHash("src/main/java/foo/Bar.java")).isNull();
- hashes.stop();
+ Map<String, String> hashByRelativePath = loader.hashByRelativePath();
+ assertThat(hashByRelativePath.get("src/main/java/foo/Bar.java")).isNull();
}
@Test
@@ -82,8 +81,7 @@ public class RemoteFileHashesTest {
when(snapshotDataDao.selectSnapshotData(123, Arrays.asList(SnapshotDataTypes.FILE_HASHES)))
.thenReturn(Arrays.asList(snapshotDataDto));
- hashes.start();
- assertThat(hashes.remoteHash("src/main/java/foo/Bar.java")).isEqualTo("abcd1234");
- hashes.stop();
+ Map<String, String> hashByRelativePath = loader.hashByRelativePath();
+ assertThat(hashByRelativePath.get("src/main/java/foo/Bar.java")).isEqualTo("abcd1234");
}
}