diff --git a/src/test/java/org/sonar/plugins/scala/sensor/AbstractScalaSensorTest.java b/src/test/java/org/sonar/plugins/scala/sensor/AbstractScalaSensorTest.java index a51f3ba..23c8033 100644 --- a/src/test/java/org/sonar/plugins/scala/sensor/AbstractScalaSensorTest.java +++ b/src/test/java/org/sonar/plugins/scala/sensor/AbstractScalaSensorTest.java @@ -20,27 +20,32 @@ package org.sonar.plugins.scala.sensor; import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; import org.junit.Before; import org.junit.Test; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.fs.FileSystem; +import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; import org.sonar.plugins.scala.language.Scala; +import org.sonar.plugins.scala.util.FileTestUtils; public class AbstractScalaSensorTest { private AbstractScalaSensor abstractScalaSensor; + private Settings settings; + private DefaultFileSystem fileSystem; + @Before public void setUp() { Settings settings = new Settings(); - abstractScalaSensor = new AbstractScalaSensor(new Scala(settings), mock(FileSystem.class)) { + fileSystem = new DefaultFileSystem(); + abstractScalaSensor = new AbstractScalaSensor(new Scala(settings), fileSystem) { public void analyse(Project project, SensorContext context) { // dummy implementation, never called in this test @@ -49,18 +54,23 @@ public void analyse(Project project, SensorContext context) { } @Test - public void shouldOnlyExecuteOnScalaProjects() { - Project scalaProject = mock(Project.class); - when(scalaProject.getLanguage()).thenReturn(new Scala(new Settings())); - Project javaProject = mock(Project.class); - // when(javaProject.getLanguage()).thenReturn(Java.INSTANCE); + public void shouldExecuteOnScalaProjects() { + Project scalaProject = mock(Project.class); + FileTestUtils.addInputFiles(fileSystem, FileTestUtils.getInputFiles( + "/scalaSourceImporter/", "MainFile", "scala", 1), false); + assertTrue(abstractScalaSensor.shouldExecuteOnProject(scalaProject)); + } - // assertTrue(abstractScalaSensor.shouldExecuteOnProject(scalaProject)); - // assertFalse(abstractScalaSensor.shouldExecuteOnProject(javaProject)); + @Test + public void shouldNotExecuteOnJavaProjects() { + Project javaProject = mock(Project.class); + FileTestUtils.addInputFiles(fileSystem, FileTestUtils.getInputFiles( + "/scalaSourceImporter/", "JavaMainFile", "java", 1), false); + assertFalse(abstractScalaSensor.shouldExecuteOnProject(javaProject)); } @Test public void shouldHaveScalaAsLanguage() { - assertThat(abstractScalaSensor.getScala(), equalTo(new Scala(new Settings()))); + assertThat(abstractScalaSensor.getScala(), equalTo(new Scala(settings))); } } \ No newline at end of file diff --git a/src/test/java/org/sonar/plugins/scala/sensor/BaseMetricsSensorTest.java b/src/test/java/org/sonar/plugins/scala/sensor/BaseMetricsSensorTest.java index 850af00..7c9c6bd 100644 --- a/src/test/java/org/sonar/plugins/scala/sensor/BaseMetricsSensorTest.java +++ b/src/test/java/org/sonar/plugins/scala/sensor/BaseMetricsSensorTest.java @@ -56,8 +56,9 @@ public class BaseMetricsSensorTest { public void setUp() { this.settings = new Settings(); this.fileSystem = new DefaultFileSystem(); - this.baseMetricsSensor = new BaseMetricsSensor(mock(Scala.class), fileSystem); - this.project = mock(Project.class); + this.baseMetricsSensor = new BaseMetricsSensor(new Scala(settings), fileSystem); + + this.project = mock(Project.class); this.sensorContext = mock(SensorContext.class); } diff --git a/src/test/java/org/sonar/plugins/scala/util/FileTestUtils.java b/src/test/java/org/sonar/plugins/scala/util/FileTestUtils.java index 052b823..1680924 100644 --- a/src/test/java/org/sonar/plugins/scala/util/FileTestUtils.java +++ b/src/test/java/org/sonar/plugins/scala/util/FileTestUtils.java @@ -48,19 +48,7 @@ private FileTestUtils() { public static String getRelativePath(String path) { return FileTestUtils.class.getResource(path).getFile(); } - - public static void addInputFiles(FileSystem fs, Iterable inputFiles, boolean testFile) { - // if (! (fs instanceof DefaultFileSystem)) return; - if (inputFiles == null) return; - for (InputFile inputFile : inputFiles) { - if (! (inputFile instanceof DefaultInputFile)) continue; - ((DefaultInputFile) inputFile).setType(testFile ? InputFile.Type.TEST : InputFile.Type.MAIN); - ((DefaultFileSystem) fs).add(inputFile); - } - } - - public static List getInputFiles(String path, String fileNameBase, int numberOfFiles) { return getInputFiles(path, fileNameBase, "scala", numberOfFiles); } @@ -70,20 +58,30 @@ public static List getInputFiles(String path, String fileNameBase, St URL resourceURL = FileTestUtils.class.getResource(path + fileNameBase + "1." + fileSuffix); for (int i = 1; resourceURL != null && i <= numberOfFiles; ) { - String relativePath = path + fileNameBase + i + "." + fileSuffix; - if (relativePath.charAt(0) == '/') { + String relativePath = path + fileNameBase + i + "." + fileSuffix; + + if (relativePath.charAt(0) == '/') { relativePath = relativePath.substring(1); - } - DefaultInputFile inputFile = new DefaultInputFile(relativePath); - inputFile.setAbsolutePath(resourceURL.getPath()); - inputFile.setLanguage(fileSuffix); - mainFiles.add(inputFile); + } + + DefaultInputFile inputFile = new DefaultInputFile(relativePath); + inputFile.setLanguage(fileSuffix); + inputFile.setAbsolutePath(resourceURL.getPath()); + + mainFiles.add(inputFile); resourceURL = FileTestUtils.class.getResource(path + fileNameBase + (++i) + "." + fileSuffix); } return mainFiles; } - + + public static void addInputFiles(FileSystem fileSystem, Iterable inputFiles, boolean testFile) { + for (InputFile inputFile : inputFiles) { + ((DefaultInputFile) inputFile).setType(testFile ? InputFile.Type.TEST : InputFile.Type.MAIN); + ((DefaultFileSystem) fileSystem).add(inputFile); + } + } + public static List getContentOfFiles(String path, String fileNameBase, int numberOfFiles) throws IOException { List contentOfFiles = new ArrayList();