From 44bef0b215e0234e4fbbad5ec293bcc9bc0c858a Mon Sep 17 00:00:00 2001 From: Cyril Date: Fri, 21 Feb 2014 15:17:32 +0100 Subject: [PATCH] Fix issue #50 with SurefireAPI. Set a sensible default for the JUnit report path, avoiding one more parameter in sonar-project.properties --- build-and-deploy.sh | 2 +- pom.xml | 2 +- sample/sonar-project.properties | 4 +- .../objectivec/tests/SurefireSensor.java | 41 +++++++++++++++++-- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/build-and-deploy.sh b/build-and-deploy.sh index 5e8398be..c21fbfff 100755 --- a/build-and-deploy.sh +++ b/build-and-deploy.sh @@ -16,7 +16,7 @@ if [ "$?" != 0 ]; then fi # Deploy new verion of plugin in Sonar dir -cp target/sonar-objective-c-plugin-0.3.1.jar $SONARQUBE_HOME/extensions/plugins +cp target/sonar-objective-c-plugin-0.3.2-SNAPSHOT.jar $SONARQUBE_HOME/extensions/plugins # Stop/start Sonar $SONARQUBE_HOME/bin/macosx-universal-64/sonar.sh stop diff --git a/pom.xml b/pom.xml index ad473763..cad8a9fc 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.codehaus.sonar-plugin.objectivec sonar-objective-c-plugin - 0.3.1 + 0.3.2-SNAPSHOT sonar-plugin diff --git a/sample/sonar-project.properties b/sample/sonar-project.properties index 4163e5e0..68adcb9e 100644 --- a/sample/sonar-project.properties +++ b/sample/sonar-project.properties @@ -34,8 +34,8 @@ sonar.sourceEncoding=UTF-8 # JUnit report generated by run-sonar.sh is stored in sonar-reports/TEST-report.xml # Change it only if you generate the file on your own -# The XML files have to be prefixed by TEST- otherwise it is not processed -sonar.junit.reportsPath=sonar-reports/ +# The XML files have to be prefixed by TEST- otherwise they are not processed +# sonar.junit.reportsPath=sonar-reports/ # Cobertura report generated by run-sonar.sh is stored in sonar-reports/coverage.xml # Change it only if you generate the file on your own diff --git a/src/main/java/org/sonar/plugins/objectivec/tests/SurefireSensor.java b/src/main/java/org/sonar/plugins/objectivec/tests/SurefireSensor.java index 765c61fc..24b8b29b 100644 --- a/src/main/java/org/sonar/plugins/objectivec/tests/SurefireSensor.java +++ b/src/main/java/org/sonar/plugins/objectivec/tests/SurefireSensor.java @@ -26,18 +26,29 @@ import org.sonar.api.batch.DependsUpon; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; +import org.sonar.api.config.Settings; import org.sonar.api.resources.Project; import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Resource; import org.sonar.plugins.objectivec.core.ObjectiveC; import org.sonar.plugins.surefire.api.AbstractSurefireParser; -import org.sonar.plugins.surefire.api.SurefireUtils; import java.io.File; public class SurefireSensor implements Sensor { private static final Logger LOG = LoggerFactory.getLogger(SurefireSensor.class); + public static final String REPORT_PATH_KEY = "sonar.junit.reportsPath"; + public static final String DEFAULT_REPORT_PATH = "sonar-reports/"; + private final Settings conf; + + public SurefireSensor() { + this(null); + } + + public SurefireSensor(final Settings config) { + conf = config; + } @DependsUpon public Class dependsUponCoverageSensors() { @@ -49,8 +60,24 @@ public boolean shouldExecuteOnProject(Project project) { } public void analyse(Project project, SensorContext context) { - File dir = SurefireUtils.getReportsDirectory(project); - collect(project, context, dir); + + /* + GitHub Issue #50 + Formerly we used SurefireUtils.getReportsDirectory(project). It seems that is this one: + http://grepcode.com/file/repo1.maven.org/maven2/org.codehaus.sonar.plugins/sonar-surefire-plugin/3.3.2/org/sonar/plugins/surefire/api/SurefireUtils.java?av=f#34 + However it turns out that the Java plugin contains its own version of SurefireUtils + that is very different (and does not contain a matching method). + That seems to be this one: http://svn.codehaus.org/sonar-plugins/tags/sonar-groovy-plugin-0.5/src/main/java/org/sonar/plugins/groovy/surefire/SurefireSensor.java + + The result is as follows: + + 1. At runtime getReportsDirectory(project) fails if you have the Java plugin installed + 2. At build time the new getReportsDirectory(project,settings) because I guess something in the build chain doesn't know about the Java plugin version + + So the implementation here reaches into the project properties and pulls the path out by itself. + */ + + collect(project, context, new File(reportPath())); } protected void collect(Project project, SensorContext context, File reportsDir) { @@ -73,4 +100,12 @@ public String toString() { return "Objective-C SurefireSensor"; } + private String reportPath() { + String reportPath = conf.getString(REPORT_PATH_KEY); + if (reportPath == null) { + reportPath = DEFAULT_REPORT_PATH; + } + return reportPath; + } + } \ No newline at end of file