import com.google.common.collect.Maps;
import org.apache.commons.io.FilenameUtils;
+import org.slf4j.LoggerFactory;
import org.sonar.api.resources.Java;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.scan.filesystem.InputFile;
this.fs = fs;
}
+ String moduleKey() {
+ return moduleKey;
+ }
+
+ PathResolver pathResolver() {
+ return pathResolver;
+ }
+
+ LanguageDetection langDetection() {
+ return langDetection;
+ }
+
+ StatusDetection statusDetection() {
+ return statusDetection;
+ }
+
+ DefaultModuleFileSystem fs() {
+ return fs;
+ }
+
@CheckForNull
DefaultInputFile create(File file, String type) {
String relativePath = pathResolver.relativePath(fs.baseDir(), file);
if (relativePath == null) {
- throw MessageException.of(String.format("File '%s' is ignored. It is not in module basedir '%s'.", file.getAbsolutePath(), fs.baseDir()));
+ LoggerFactory.getLogger(getClass()).warn(
+ "File '%s' is ignored. It is not in module basedir '%s'.", file.getAbsolutePath(), fs.baseDir());
+ return null;
}
DefaultInputFile inputFile = DefaultInputFile.create(file, fs.sourceCharset(), relativePath, Maps.<String, String>newHashMap());
inputFile.setType(type);
import org.sonar.api.utils.MessageException;
import javax.annotation.CheckForNull;
-import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Lower-case extension -> languages
*/
- private final Map<String, PathPattern[]> patternByLanguage = Maps.newLinkedHashMap();
+ private final Map<String, PathPattern[]> patternsByLanguage = Maps.newLinkedHashMap();
private final List<String> languagesToConsider = Lists.newArrayList();
private final String forcedLanguage;
String[] filePatterns = settings.getStringArray(getFileLangPatternPropKey(language.getKey()));
PathPattern[] pathPatterns = PathPattern.create(filePatterns);
if (pathPatterns.length > 0) {
- patternByLanguage.put(language.getKey(), pathPatterns);
+ patternsByLanguage.put(language.getKey(), pathPatterns);
} else {
// If no custom language pattern is defined then fallback to suffixes declared by language
String[] patterns = Arrays.copyOf(language.getFileSuffixes(), language.getFileSuffixes().length);
patterns[i] = "**/*." + extension;
}
PathPattern[] defaultLanguagePatterns = PathPattern.create(patterns);
- patternByLanguage.put(language.getKey(), defaultLanguagePatterns);
+ patternsByLanguage.put(language.getKey(), defaultLanguagePatterns);
LOG.debug("Declared extensions of language " + language + " were converted to " + getDetails(language.getKey()));
}
}
forcedLanguage = StringUtils.defaultIfBlank(settings.getString(CoreProperties.PROJECT_LANGUAGE_PROPERTY), null);
// First try with lang patterns
if (forcedLanguage != null) {
- if (!patternByLanguage.containsKey(forcedLanguage)) {
+ if (!patternsByLanguage.containsKey(forcedLanguage)) {
throw MessageException.of("No language is installed with key '" + forcedLanguage + "'. Please update property '" + CoreProperties.PROJECT_LANGUAGE_PROPERTY + "'");
}
languagesToConsider.add(forcedLanguage);
} else {
- languagesToConsider.addAll(patternByLanguage.keySet());
+ languagesToConsider.addAll(patternsByLanguage.keySet());
}
}
+ Map<String, PathPattern[]> patternsByLanguage() {
+ return patternsByLanguage;
+ }
+
@CheckForNull
String language(InputFile inputFile) {
String detectedLanguage = null;
for (String languageKey : languagesToConsider) {
- PathPattern[] patterns = patternByLanguage.get(languageKey);
+ PathPattern[] patterns = patternsByLanguage.get(languageKey);
if (patterns != null) {
for (PathPattern pathPattern : patterns) {
if (pathPattern.match(inputFile, false)) {
// Check if deprecated sonar.language is used and we are on a language without declared extensions
if (forcedLanguage != null) {
// Languages without declared suffixes match everything
- if (patternByLanguage.get(forcedLanguage).length == 0) {
+ if (patternsByLanguage.get(forcedLanguage).length == 0) {
return forcedLanguage;
}
}
}
private String getDetails(String detectedLanguage) {
- return getFileLangPatternPropKey(detectedLanguage) + " : " + Joiner.on(",").join(patternByLanguage.get(detectedLanguage));
+ return getFileLangPatternPropKey(detectedLanguage) + " : " + Joiner.on(",").join(patternsByLanguage.get(detectedLanguage));
}
static String sanitizeExtension(String suffix) {
--- /dev/null
+/*
+ * 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");
+ }
+}
--- /dev/null
+/*
+ * 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");
+
+ }
+}
--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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.ExpectedException;
+import org.junit.rules.TemporaryFolder;
+import org.sonar.api.database.model.Snapshot;
+import org.sonar.batch.components.PastSnapshot;
+import org.sonar.batch.components.PastSnapshotFinder;
+import org.sonar.core.source.SnapshotDataTypes;
+import org.sonar.core.source.db.SnapshotDataDao;
+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 PreviousFileHashLoaderTest {
+
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ PastSnapshotFinder pastSnapshotFinder = mock(PastSnapshotFinder.class);
+ Snapshot snapshot = mock(Snapshot.class);
+ SnapshotDataDao snapshotDataDao = mock(SnapshotDataDao.class);
+ PreviousFileHashLoader loader = new PreviousFileHashLoader(snapshot, snapshotDataDao, pastSnapshotFinder);
+
+ @Test
+ public void should_return_null_if_no_previous_snapshot() throws Exception {
+ when(pastSnapshotFinder.findPreviousAnalysis(snapshot)).thenReturn(new PastSnapshot("foo"));
+
+ Map<String, String> hashByRelativePath = loader.hashByRelativePath();
+ assertThat(hashByRelativePath.get("src/main/java/foo/Bar.java")).isNull();
+ }
+
+ @Test
+ public void should_return_null_if_no_remote_hashes() throws Exception {
+ Snapshot previousSnapshot = mock(Snapshot.class);
+ PastSnapshot pastSnapshot = new PastSnapshot("foo", new Date(), previousSnapshot);
+ when(pastSnapshotFinder.findPreviousAnalysis(snapshot)).thenReturn(pastSnapshot);
+
+ Map<String, String> hashByRelativePath = loader.hashByRelativePath();
+ assertThat(hashByRelativePath.get("src/main/java/foo/Bar.java")).isNull();
+ }
+
+ @Test
+ public void should_return_remote_hash() throws Exception {
+ Snapshot previousSnapshot = mock(Snapshot.class);
+ when(previousSnapshot.getId()).thenReturn(123);
+ PastSnapshot pastSnapshot = new PastSnapshot("foo", new Date(), previousSnapshot);
+ when(pastSnapshotFinder.findPreviousAnalysis(snapshot)).thenReturn(pastSnapshot);
+
+ SnapshotDataDto snapshotDataDto = new SnapshotDataDto();
+ snapshotDataDto.setData("src/main/java/foo/Bar.java=abcd1234");
+ when(snapshotDataDao.selectSnapshotData(123, Arrays.asList(SnapshotDataTypes.FILE_HASHES)))
+ .thenReturn(Arrays.asList(snapshotDataDto));
+
+ Map<String, String> hashByRelativePath = loader.hashByRelativePath();
+ assertThat(hashByRelativePath.get("src/main/java/foo/Bar.java")).isEqualTo("abcd1234");
+ }
+}
+++ /dev/null
-/*
- * 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.ExpectedException;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.api.database.model.Snapshot;
-import org.sonar.batch.components.PastSnapshot;
-import org.sonar.batch.components.PastSnapshotFinder;
-import org.sonar.core.source.SnapshotDataTypes;
-import org.sonar.core.source.db.SnapshotDataDao;
-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 PreviousFileHashesLoaderTest {
-
- @Rule
- public TemporaryFolder temp = new TemporaryFolder();
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- PastSnapshotFinder pastSnapshotFinder = mock(PastSnapshotFinder.class);
- Snapshot snapshot = mock(Snapshot.class);
- SnapshotDataDao snapshotDataDao = mock(SnapshotDataDao.class);
- PreviousFileHashLoader loader = new PreviousFileHashLoader(snapshot, snapshotDataDao, pastSnapshotFinder);
-
- @Test
- public void should_return_null_if_no_previous_snapshot() throws Exception {
- when(pastSnapshotFinder.findPreviousAnalysis(snapshot)).thenReturn(new PastSnapshot("foo"));
-
- Map<String, String> hashByRelativePath = loader.hashByRelativePath();
- assertThat(hashByRelativePath.get("src/main/java/foo/Bar.java")).isNull();
- }
-
- @Test
- public void should_return_null_if_no_remote_hashes() throws Exception {
- Snapshot previousSnapshot = mock(Snapshot.class);
- PastSnapshot pastSnapshot = new PastSnapshot("foo", new Date(), previousSnapshot);
- when(pastSnapshotFinder.findPreviousAnalysis(snapshot)).thenReturn(pastSnapshot);
-
- Map<String, String> hashByRelativePath = loader.hashByRelativePath();
- assertThat(hashByRelativePath.get("src/main/java/foo/Bar.java")).isNull();
- }
-
- @Test
- public void should_return_remote_hash() throws Exception {
- Snapshot previousSnapshot = mock(Snapshot.class);
- when(previousSnapshot.getId()).thenReturn(123);
- PastSnapshot pastSnapshot = new PastSnapshot("foo", new Date(), previousSnapshot);
- when(pastSnapshotFinder.findPreviousAnalysis(snapshot)).thenReturn(pastSnapshot);
-
- SnapshotDataDto snapshotDataDto = new SnapshotDataDto();
- snapshotDataDto.setData("src/main/java/foo/Bar.java=abcd1234");
- when(snapshotDataDao.selectSnapshotData(123, Arrays.asList(SnapshotDataTypes.FILE_HASHES)))
- .thenReturn(Arrays.asList(snapshotDataDto));
-
- Map<String, String> hashByRelativePath = loader.hashByRelativePath();
- assertThat(hashByRelativePath.get("src/main/java/foo/Bar.java")).isEqualTo("abcd1234");
- }
-}
--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+}
private final String path;
private final Map<String, String> attributes;
private final String encoding;
- private long lines = 0L;
private DefaultInputFile(File file, Charset encoding, String path, Map<String, String> attributes) {
this.encoding = encoding.name();
}
public DefaultInputFile setLines(long l) {
- this.lines = l;
+ attributes.put(ATTRIBUTE_LINE_COUNT, String.valueOf(l));
return this;
}