]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-926 add missing tests
authorSimon Brandhof <simon.brandhof@gmail.com>
Tue, 11 Feb 2014 21:47:32 +0000 (22:47 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Tue, 11 Feb 2014 21:47:32 +0000 (22:47 +0100)
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputFileBuilder.java
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/LanguageDetection.java
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderFactoryTest.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/InputFileBuilderTest.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/LanguageDetectionFactoryTest.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashLoaderTest.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashesLoaderTest.java [deleted file]
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/StatusDetectionFactoryTest.java [new file with mode: 0644]
sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/StatusDetectionTest.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/scan/filesystem/internal/DefaultInputFile.java

index 44409fb4a59ad063f87d4c833f7ddd26e63a3106..17f24253f070d24f7b56c53a84b168d272323d31 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.batch.scan.filesystem;
 
 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;
@@ -49,11 +50,33 @@ class InputFileBuilder {
     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);
index a8a7019fd3a360e721dac27c9884fd4ff7444fc9..f4f876905a775bcae0af6fd8fc3da129f433f008 100644 (file)
@@ -33,7 +33,6 @@ import org.sonar.api.scan.filesystem.InputFile;
 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;
@@ -48,7 +47,7 @@ class LanguageDetection {
   /**
    * 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;
 
@@ -57,7 +56,7 @@ class LanguageDetection {
       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);
@@ -67,7 +66,7 @@ class LanguageDetection {
           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()));
       }
     }
@@ -75,20 +74,24 @@ class LanguageDetection {
     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)) {
@@ -112,7 +115,7 @@ class LanguageDetection {
     // 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;
       }
     }
@@ -124,7 +127,7 @@ class LanguageDetection {
   }
 
   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) {
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 (file)
index 0000000..5eba1db
--- /dev/null
@@ -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 (file)
index 0000000..4e19c94
--- /dev/null
@@ -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 (file)
index 0000000..0df875c
--- /dev/null
@@ -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/PreviousFileHashLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/filesystem/PreviousFileHashLoaderTest.java
new file mode 100644 (file)
index 0000000..e886299
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+ * 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");
+  }
+}
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/PreviousFileHashesLoaderTest.java
deleted file mode 100644 (file)
index 6b4e518..0000000
+++ /dev/null
@@ -1,87 +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.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");
-  }
-}
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 (file)
index 0000000..460d527
--- /dev/null
@@ -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 (file)
index 0000000..7940440
--- /dev/null
@@ -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);
+  }
+}
index 8509d14e329d561ff0138c9d9334b37749afa742..9f75219c86d6a65b7c51a33becb6db54223400b4 100644 (file)
@@ -66,7 +66,6 @@ public class DefaultInputFile implements InputFile {
   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();
@@ -149,7 +148,7 @@ public class DefaultInputFile implements InputFile {
   }
 
   public DefaultInputFile setLines(long l) {
-    this.lines = l;
+    attributes.put(ATTRIBUTE_LINE_COUNT, String.valueOf(l));
     return this;
   }