diff options
author | Godin <mandrikov@gmail.com> | 2010-10-20 10:59:39 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-10-20 10:59:39 +0000 |
commit | ab6d196d40e7879b96c3e944a379dc9b05fa934e (patch) | |
tree | 05414141202c3aa85de71e3ce9f570b00810bef3 /plugins/sonar-surefire-plugin | |
parent | 2ca1298c7baa4db9e3cc4882078a84edc099d7fa (diff) | |
download | sonarqube-ab6d196d40e7879b96c3e944a379dc9b05fa934e.tar.gz sonarqube-ab6d196d40e7879b96c3e944a379dc9b05fa934e.zip |
SONAR-1784: Add SurefireUtils
Diffstat (limited to 'plugins/sonar-surefire-plugin')
11 files changed, 98 insertions, 88 deletions
diff --git a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/SurefireSensor.java b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/SurefireSensor.java index 81cdd4279ce..70dfe4e38b3 100644 --- a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/SurefireSensor.java +++ b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/SurefireSensor.java @@ -21,18 +21,16 @@ package org.sonar.plugins.surefire; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.sonar.api.CoreProperties; import org.sonar.api.batch.AbstractCoverageExtension; import org.sonar.api.batch.DependsUpon; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; -import org.sonar.api.batch.maven.MavenPlugin; -import org.sonar.api.batch.maven.MavenSurefireUtils; import org.sonar.api.resources.Java; import org.sonar.api.resources.JavaFile; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.plugins.surefire.api.AbstractSurefireParser; +import org.sonar.plugins.surefire.api.SurefireUtils; import java.io.File; @@ -50,50 +48,16 @@ public class SurefireSensor implements Sensor { } public void analyse(Project project, SensorContext context) { - File dir = getReportsDirectory(project); + File dir = SurefireUtils.getReportsDirectory(project); collect(project, context, dir); } - protected File getReportsDirectory(Project project) { - File dir = getReportsDirectoryFromProperty(project); - if (dir == null) { - dir = getReportsDirectoryFromPluginConfiguration(project); - } - if (dir == null) { - dir = getReportsDirectoryFromDefaultConfiguration(project); - } - return dir; - } - - private File getReportsDirectoryFromProperty(Project project) { - String path = (String) project.getProperty(CoreProperties.SUREFIRE_REPORTS_PATH_PROPERTY); - if (path != null) { - return project.getFileSystem().resolvePath(path); - } - return null; - } - - private File getReportsDirectoryFromPluginConfiguration(Project project) { - MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), MavenSurefireUtils.GROUP_ID, MavenSurefireUtils.ARTIFACT_ID); - if (plugin != null) { - String path = plugin.getParameter("reportsDirectory"); - if (path != null) { - return project.getFileSystem().resolvePath(path); - } - } - return null; - } - - private File getReportsDirectoryFromDefaultConfiguration(Project project) { - return new File(project.getFileSystem().getBuildDir(), "surefire-reports"); - } - protected void collect(Project project, SensorContext context, File reportsDir) { logger.info("parsing {}", reportsDir); new AbstractSurefireParser() { @Override - protected Resource<?> getUnitTestResource(TestSuiteReport fileReport) { - return new JavaFile(fileReport.getClassKey(), true); + protected Resource<?> getUnitTestResource(String classKey) { + return new JavaFile(classKey, true); } }.collect(project, context, reportsDir); } diff --git a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java index bc2e18d7d46..a144431af8c 100644 --- a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java +++ b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/AbstractSurefireParser.java @@ -110,15 +110,15 @@ public abstract class AbstractSurefireParser { } } testCaseDetails.append("</tests-details>"); - context.saveMeasure(getUnitTestResource(fileReport), new Measure(CoreMetrics.TEST_DATA, testCaseDetails.toString())); + context.saveMeasure(getUnitTestResource(fileReport.getClassKey()), new Measure(CoreMetrics.TEST_DATA, testCaseDetails.toString())); } private void saveClassMeasure(SensorContext context, TestSuiteReport fileReport, Metric metric, double value) { if ( !Double.isNaN(value)) { - context.saveMeasure(getUnitTestResource(fileReport), metric, value); + context.saveMeasure(getUnitTestResource(fileReport.getClassKey()), metric, value); } } - protected abstract Resource<?> getUnitTestResource(TestSuiteReport fileReport); + protected abstract Resource<?> getUnitTestResource(String classKey); } diff --git a/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/SurefireUtils.java b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/SurefireUtils.java new file mode 100644 index 00000000000..8e8787bd19b --- /dev/null +++ b/plugins/sonar-surefire-plugin/src/main/java/org/sonar/plugins/surefire/api/SurefireUtils.java @@ -0,0 +1,52 @@ +package org.sonar.plugins.surefire.api; + +import org.sonar.api.CoreProperties; +import org.sonar.api.batch.maven.MavenPlugin; +import org.sonar.api.batch.maven.MavenSurefireUtils; +import org.sonar.api.resources.Project; + +import java.io.File; + +/** + * @since 2.4 + */ +public final class SurefireUtils { + + public static File getReportsDirectory(Project project) { + File dir = getReportsDirectoryFromProperty(project); + if (dir == null) { + dir = getReportsDirectoryFromPluginConfiguration(project); + } + if (dir == null) { + dir = getReportsDirectoryFromDefaultConfiguration(project); + } + return dir; + } + + private static File getReportsDirectoryFromProperty(Project project) { + String path = (String) project.getProperty(CoreProperties.SUREFIRE_REPORTS_PATH_PROPERTY); + if (path != null) { + return project.getFileSystem().resolvePath(path); + } + return null; + } + + private static File getReportsDirectoryFromPluginConfiguration(Project project) { + MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), MavenSurefireUtils.GROUP_ID, MavenSurefireUtils.ARTIFACT_ID); + if (plugin != null) { + String path = plugin.getParameter("reportsDirectory"); + if (path != null) { + return project.getFileSystem().resolvePath(path); + } + } + return null; + } + + private static File getReportsDirectoryFromDefaultConfiguration(Project project) { + return new File(project.getFileSystem().getBuildDir(), "surefire-reports"); + } + + private SurefireUtils() { + } + +} diff --git a/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/SurefireSensorTest.java b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/SurefireSensorTest.java index 1e70ea71ed1..e21df1d9648 100644 --- a/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/SurefireSensorTest.java +++ b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/SurefireSensorTest.java @@ -19,6 +19,19 @@ */ package org.sonar.plugins.surefire; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyDouble; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + import org.apache.commons.lang.ObjectUtils; import org.custommonkey.xmlunit.DetailedDiff; import org.custommonkey.xmlunit.Diff; @@ -42,15 +55,6 @@ import java.io.FileReader; import java.io.StringReader; import java.net.URISyntaxException; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.mockito.Matchers.anyDouble; -import static org.mockito.Matchers.anyObject; -import static org.mockito.Matchers.argThat; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.*; - public class SurefireSensorTest { @Test @@ -70,20 +74,6 @@ public class SurefireSensorTest { } @Test - public void shouldGetReportsFromProperty() { - Project project = MavenTestUtils.loadProjectFromPom(getClass(), "shouldGetReportsFromProperty/pom.xml"); - assertThat(new SurefireSensor().getReportsDirectory(project).exists(), is(true)); - assertThat(new SurefireSensor().getReportsDirectory(project).isDirectory(), is(true)); - } - - @Test - public void shouldGetReportsFromPluginConfiguration() { - Project project = MavenTestUtils.loadProjectFromPom(getClass(), "shouldGetReportsFromPluginConfiguration/pom.xml"); - assertThat(new SurefireSensor().getReportsDirectory(project).exists(), is(true)); - assertThat(new SurefireSensor().getReportsDirectory(project).isDirectory(), is(true)); - } - - @Test public void shouldNotFailIfReportsNotFound() { Project project = MavenTestUtils.loadProjectFromPom(getClass(), "shouldNotFailIfReportsNotFound/pom.xml"); new SurefireSensor().collect(project, mock(SensorContext.class), new File("unknown")); diff --git a/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/SurefireUtilsTest.java b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/SurefireUtilsTest.java new file mode 100644 index 00000000000..a53c531544b --- /dev/null +++ b/plugins/sonar-surefire-plugin/src/test/java/org/sonar/plugins/surefire/api/SurefireUtilsTest.java @@ -0,0 +1,26 @@ +package org.sonar.plugins.surefire.api; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.sonar.api.resources.Project; +import org.sonar.api.test.MavenTestUtils; + +public class SurefireUtilsTest { + + @Test + public void shouldGetReportsFromProperty() { + Project project = MavenTestUtils.loadProjectFromPom(getClass(), "shouldGetReportsFromProperty/pom.xml"); + assertThat(SurefireUtils.getReportsDirectory(project).exists(), is(true)); + assertThat(SurefireUtils.getReportsDirectory(project).isDirectory(), is(true)); + } + + @Test + public void shouldGetReportsFromPluginConfiguration() { + Project project = MavenTestUtils.loadProjectFromPom(getClass(), "shouldGetReportsFromPluginConfiguration/pom.xml"); + assertThat(SurefireUtils.getReportsDirectory(project).exists(), is(true)); + assertThat(SurefireUtils.getReportsDirectory(project).isDirectory(), is(true)); + } + +} diff --git a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromPluginConfiguration/pom.xml b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromPluginConfiguration/pom.xml deleted file mode 100644 index 4e3df754a65..00000000000 --- a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromPluginConfiguration/pom.xml +++ /dev/null @@ -1,21 +0,0 @@ -<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>build/junit</reportsDirectory> - </configuration> - </plugin> - </plugins> - </build> -</project>
\ No newline at end of file diff --git a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromProperty/target/surefire/hack.txt b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromProperty/target/surefire/hack.txt deleted file mode 100644 index b1fdc56ae6f..00000000000 --- a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromProperty/target/surefire/hack.txt +++ /dev/null @@ -1 +0,0 @@ -hack for maven in order the directory target/surefire to be included in unit tests classpath
\ No newline at end of file diff --git a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromOverriddenPath/output/hack.txt b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/SurefireUtilsTest/shouldGetReportsFromPluginConfiguration/build/junit/hack.txt index b1fdc56ae6f..b1fdc56ae6f 100644 --- a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromOverriddenPath/output/hack.txt +++ b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/SurefireUtilsTest/shouldGetReportsFromPluginConfiguration/build/junit/hack.txt diff --git a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromOverriddenPath/pom.xml b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/SurefireUtilsTest/shouldGetReportsFromPluginConfiguration/pom.xml index 4e3df754a65..4e3df754a65 100644 --- a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromOverriddenPath/pom.xml +++ b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/SurefireUtilsTest/shouldGetReportsFromPluginConfiguration/pom.xml diff --git a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromProperty/pom.xml b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/SurefireUtilsTest/shouldGetReportsFromProperty/pom.xml index 948c4a831e3..948c4a831e3 100644 --- a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromProperty/pom.xml +++ b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/SurefireUtilsTest/shouldGetReportsFromProperty/pom.xml diff --git a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromPluginConfiguration/build/junit/hack.txt b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/SurefireUtilsTest/shouldGetReportsFromProperty/target/surefire/hack.txt index b1fdc56ae6f..b1fdc56ae6f 100644 --- a/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/SurefireSensorTest/shouldGetReportsFromPluginConfiguration/build/junit/hack.txt +++ b/plugins/sonar-surefire-plugin/src/test/resources/org/sonar/plugins/surefire/api/SurefireUtilsTest/shouldGetReportsFromProperty/target/surefire/hack.txt |