From: Simon Brandhof Date: Fri, 11 Oct 2013 12:24:20 +0000 (+0200) Subject: Fix SourceScannerTest in order to use existing files X-Git-Tag: 4.0~135 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=401abbe560aa06d5e3a4668f14cd0c646eea40af;p=sonarqube.git Fix SourceScannerTest in order to use existing files --- diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScanner.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScanner.java index 0f3f06e185b..986271f1bd5 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScanner.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScanner.java @@ -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 sourceDirs, Project project, boolean isTest) { - Resource resource = null; + Resource resource; if (Java.KEY.equals(project.getLanguageKey()) && Java.isJavaFile(inputFile)) { diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScannerTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScannerTest.java index 10e62084d0c..02a169b7f67 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScannerTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/ignore/scanner/SourceScannerTest.java @@ -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 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);