]> source.dussan.org Git - sonarqube.git/commitdiff
Fix SourceScannerTest in order to use existing files
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 11 Oct 2013 12:24:20 +0000 (14:24 +0200)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 11 Oct 2013 12:25:15 +0000 (14:25 +0200)
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScanner.java
plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScannerTest.java

index 0f3f06e185b0bbb197207a69275eabf46c2d2e0c..986271f1bd525549ccaa92ce788d610eee98de48 100644 (file)
@@ -106,7 +106,7 @@ public final class SourceScanner implements Sensor {
    * This method is necessary because Java resources are not treated as every other resource...
    */
   private String resolveComponent(File inputFile, List<File> sourceDirs, Project project, boolean isTest) {
-    Resource resource = null;
+    Resource resource;
 
     if (Java.KEY.equals(project.getLanguageKey()) && Java.isJavaFile(inputFile)) {
 
index 10e62084d0ce82349f9952861d0b19213ad12b90..02a169b7f6700ebd44fe25af175f8249b5a61834 100644 (file)
@@ -25,6 +25,7 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
+import org.junit.rules.TemporaryFolder;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
@@ -71,6 +72,9 @@ public class SourceScannerTest {
   @Rule
   public ExpectedException thrown = ExpectedException.none();
 
+  @Rule
+  public TemporaryFolder temp = new TemporaryFolder();
+
   @Before
   public void init() {
     MockitoAnnotations.initMocks(this);
@@ -154,16 +158,17 @@ public class SourceScannerTest {
   }
 
   @Test
-  public void shouldAnalyseOtherProject() throws IOException {
-    File sourceFile = new File("Foo.php");
-    File testFile = new File("FooTest.php");
+  public void shouldAnalyseOtherProject() throws Exception {
+    File rootDir = temp.newFolder();
+    File sourceFile = new File(rootDir, "Foo.php");
+    File testFile = new File(rootDir, "FooTest.php");
 
     when(project.getLanguageKey()).thenReturn("php");
     when(fileSystem.files(Mockito.isA(FileQuery.class)))
       .thenReturn(Arrays.asList(sourceFile))
       .thenReturn(Arrays.asList(testFile));
-    when(fileSystem.sourceDirs()).thenReturn(ImmutableList.of(new File("")));
-    when(fileSystem.testDirs()).thenReturn(ImmutableList.of(new File("")));
+    when(fileSystem.sourceDirs()).thenReturn(ImmutableList.of(rootDir));
+    when(fileSystem.testDirs()).thenReturn(ImmutableList.of(rootDir));
     when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(true);
 
     scanner.analyse(project, null);
@@ -178,16 +183,17 @@ public class SourceScannerTest {
 
   @Test
   public void shouldAnalyseJavaProjectWithNonJavaFile() throws IOException {
-    File sourceFile = new File("src/main/java/Foo.java");
-    File otherFile = new File("other.js");
+    File rootDir = temp.newFolder();
+    File sourceFile = new File(rootDir, "src/main/java/Foo.java");
+    File otherFile = new File(rootDir, "other.js");
 
     when(project.getLanguageKey()).thenReturn("java");
     List<File> empty = Collections.emptyList();
     when(fileSystem.files(Mockito.isA(FileQuery.class)))
       .thenReturn(Arrays.asList(sourceFile, otherFile))
       .thenReturn(empty);
-    when(fileSystem.sourceDirs()).thenReturn(ImmutableList.of(new File("src/main/java"), new File("")));
-    when(fileSystem.testDirs()).thenReturn(ImmutableList.of(new File("src/test/java")));
+    when(fileSystem.sourceDirs()).thenReturn(ImmutableList.of(new File(rootDir, "src/main/java"), rootDir));
+    when(fileSystem.testDirs()).thenReturn(ImmutableList.of(new File(rootDir, "src/test/java")));
     when(exclusionPatternInitializer.hasFileContentPattern()).thenReturn(true);
 
     scanner.analyse(project, null);