Skip to content

Commit

Permalink
Replace deprecated functions and add test for SurefireUtils class
Browse files Browse the repository at this point in the history
  • Loading branch information
miklein committed Nov 13, 2014
1 parent 7b9ed3f commit 8f0e955
Show file tree
Hide file tree
Showing 10 changed files with 146 additions and 35 deletions.
12 changes: 6 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>

<!-- unit tests -->
<dependency>
Expand All @@ -108,12 +114,6 @@
<version>1.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-project</artifactId>
<version>2.2.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,5 @@ private InputFile getUnitTestResource(String classKey){

return fileSystem.inputFile(filePredicates. matchesPathPattern("**/*" + filename));
}

// for(InputFile inputFile : inputFiles){
////System.out.println("inputFile: " + inputFile.absolutePath());
////System.out.println("filename: " + filename);
//if (inputFile.absolutePath().endsWith(filename)){
// return inputFile;
//}
//}

}
15 changes: 11 additions & 4 deletions src/main/java/org/sonar/plugins/scala/surefire/SurefireSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;

import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.CoverageExtension;
Expand All @@ -37,6 +38,7 @@ public class SurefireSensor implements Sensor {
private static final Logger LOG = LoggerFactory.getLogger(SurefireSensor.class);
private final Settings settings;
private final FileSystem fileSystem;
private MavenProject pom;

@DependsUpon
public Class<?> dependsUponCoverageSensors() {
Expand All @@ -48,9 +50,14 @@ public SurefireSensor (Settings settings, FileSystem fileSystem){
this.fileSystem = fileSystem;
}

public boolean shouldExecuteOnProject(Project project) {

if(project.getAnalysisType().isDynamic(true) && Scala.INSTANCE.getKey().equals(project.getLanguageKey())){
public SurefireSensor (Settings settings, FileSystem fileSystem, MavenProject pom){
this.settings = settings;
this.fileSystem = fileSystem;
this.pom = pom;
}

public boolean shouldExecuteOnProject(Project project) {
if( fileSystem.hasFiles(fileSystem.predicates().hasLanguage(Scala.KEY))){
LOG.info("SurefireSensor will be executed");
return true;
} else {
Expand All @@ -60,7 +67,7 @@ public boolean shouldExecuteOnProject(Project project) {
}

public void analyse(Project project, SensorContext context) {
File dir = SurefireUtils.getReportsDirectory(settings, project);
File dir = SurefireUtils.getReportsDirectory(fileSystem, settings, pom);
collect(project, context, dir);
}

Expand Down
54 changes: 37 additions & 17 deletions src/main/java/org/sonar/plugins/scala/surefire/SurefireUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,73 @@
package org.sonar.plugins.scala.surefire;

import java.io.File;
import java.io.IOException;

import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.maven.MavenPlugin;
import org.sonar.api.batch.maven.MavenSurefireUtils;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;

/**
* @since 2.4
*/
public final class SurefireUtils {

public static final String SUREFIRE_REPORTS_PATH_PROPERTY = "sonar.junit.reportsPath";
private static final Logger LOG = LoggerFactory.getLogger(SurefireUtils.class);

public static File getReportsDirectory(Settings settings, Project project) {
File dir = getReportsDirectoryFromProperty(settings, project);
private SurefireUtils() {
// to prevent instantiation
}

public static File getReportsDirectory(FileSystem fileSystem, Settings settings, MavenProject pom) {
File dir = getReportsDirectoryFromProperty(fileSystem, settings);
if (dir == null) {
dir = getReportsDirectoryFromPluginConfiguration(project);
dir = getReportsDirectoryFromPluginConfiguration(pom);
}
if (dir == null) {
dir = getReportsDirectoryFromDefaultConfiguration(project);
dir = getReportsDirectoryFromDefaultConfiguration(fileSystem);
}
return dir;
}

private static File getReportsDirectoryFromProperty(Settings settings, Project project) {
private static File getReportsDirectoryFromProperty(FileSystem fileSystem, Settings settings) {
String path = settings.getString(SUREFIRE_REPORTS_PATH_PROPERTY);
if (path != null) {
return project.getFileSystem().resolvePath(path);
File reportsDir = null;
try {
File canonicalBase = fileSystem.baseDir().getCanonicalFile();
reportsDir = new File(canonicalBase, path);
} catch (IOException e) {
LOG.warn("Reports path could not be created", e);
}
return reportsDir;
}
return null;
}

private static File getReportsDirectoryFromPluginConfiguration(Project project) {
MavenPlugin plugin = null; //MavenPlugin.getPlugin(project.getPom(), MavenSurefireUtils.GROUP_ID, MavenSurefireUtils.ARTIFACT_ID);
private static File getReportsDirectoryFromPluginConfiguration(MavenProject pom) {
MavenPlugin plugin = MavenPlugin.getPlugin(pom, MavenSurefireUtils.GROUP_ID, MavenSurefireUtils.ARTIFACT_ID);
if (plugin != null) {
String path = plugin.getParameter("reportsDirectory");
if (path != null) {
return project.getFileSystem().resolvePath(path);
return new File(path);
}
}
return null;
}

private static File getReportsDirectoryFromDefaultConfiguration(Project project) {
return new File(project.getFileSystem().getBuildDir(), "surefire-reports");
private static File getReportsDirectoryFromDefaultConfiguration(FileSystem fileSystem) {
File reportsDir = null;
try {
File canonicalBase = fileSystem.baseDir().getCanonicalFile();
reportsDir = new File(canonicalBase, "target/surefire-reports");
} catch (IOException e) {
LOG.warn("Reports path could not be created", e);
}
return reportsDir;
}

private SurefireUtils() {
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Sonar Scala Plugin
* Copyright (C) 2011 - 2014 All contributors
* [email protected]
*
* This program 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.
*
* This program 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 02
*/
package org.sonar.plugins.scala.surefire;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.io.File;

import org.apache.maven.project.MavenProject;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.fs.internal.DefaultFileSystem;
import org.sonar.api.config.Settings;
import org.sonar.api.test.MavenTestUtils;

public class SurefireUtilsTest {

private DefaultFileSystem fileSystem;
private Settings settings;

@Before
public void setUp() {
this.fileSystem = new DefaultFileSystem();
this.settings = mock(Settings.class);
}

@Test
public void shouldGetReportsFromProperty() {
fileSystem.setBaseDir(new File("src/test/resources/surefire"));
when(settings.getString("sonar.junit.reportsPath")).thenReturn("targetdir/surefire-reports");
assertThat(SurefireUtils.getReportsDirectory(fileSystem, settings, null).exists()).isTrue();
assertThat(SurefireUtils.getReportsDirectory(fileSystem, settings, null).isDirectory()).isTrue();
}

@Test
public void shouldGetReportsFromPluginConfiguration() {
MavenProject pom = MavenTestUtils.loadPom(getClass(), "shouldGetReportsFromPluginConfiguration/pom.xml");
assertThat(SurefireUtils.getReportsDirectory(fileSystem, settings, pom).exists()).isTrue();
assertThat(SurefireUtils.getReportsDirectory(fileSystem, settings, pom).isDirectory()).isTrue();
}

@Test
public void shouldGetReportsFromDefaultConfiguration() {
fileSystem.setBaseDir(new File("src/test/resources/surefire"));
File reportsDir = SurefireUtils.getReportsDirectory(fileSystem, settings, null);
assertThat(reportsDir.getPath().endsWith("src/test/resources/surefire/target/surefire-reports")).isTrue();
assertThat(reportsDir.getPath().endsWith("src/test/resources/surefire/target/foo")).isFalse();
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fake.group</groupId>
<artifactId>fake.artifactId</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.4</version>
<configuration>
<reportsDirectory>src/test/resources/surefire/targetdir/test-reports</reportsDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 8f0e955

Please sign in to comment.