Skip to content

Commit

Permalink
Adapt unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miklein committed Nov 3, 2014
1 parent 4d74ca5 commit 70a60c1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
38 changes: 18 additions & 20 deletions src/test/java/org/sonar/plugins/scala/util/FileTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputFile> 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<InputFile> getInputFiles(String path, String fileNameBase, int numberOfFiles) {
return getInputFiles(path, fileNameBase, "scala", numberOfFiles);
}
Expand All @@ -70,20 +58,30 @@ public static List<InputFile> 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<InputFile> inputFiles, boolean testFile) {
for (InputFile inputFile : inputFiles) {
((DefaultInputFile) inputFile).setType(testFile ? InputFile.Type.TEST : InputFile.Type.MAIN);
((DefaultFileSystem) fileSystem).add(inputFile);
}
}

public static List<String> getContentOfFiles(String path, String fileNameBase, int numberOfFiles) throws IOException {
List<String> contentOfFiles = new ArrayList<String>();

Expand Down

0 comments on commit 70a60c1

Please sign in to comment.