From 8f0e9552a27ca3e63f1da665d0eb934de33cf05b Mon Sep 17 00:00:00 2001 From: Michael Klein Date: Thu, 13 Nov 2014 15:29:56 +0100 Subject: [PATCH] Replace deprecated functions and add test for SurefireUtils class --- pom.xml | 12 ++-- .../scala/surefire/ScalaSurefireParser.java | 8 --- .../scala/surefire/SurefireSensor.java | 15 ++-- .../plugins/scala/surefire/SurefireUtils.java | 54 +++++++++----- .../scala/surefire/SurefireUtilsTest.java | 71 +++++++++++++++++++ .../pom.xml | 21 ++++++ .../surefire-reports/emptyReport.txt | 0 .../surefire-reports/emptyReport.xml | 0 .../targetdir/test-reports/emptyReport.txt | 0 .../targetdir/test-reports/emptyReport.xml | 0 10 files changed, 146 insertions(+), 35 deletions(-) create mode 100644 src/test/java/org/sonar/plugins/scala/surefire/SurefireUtilsTest.java create mode 100644 src/test/resources/org/sonar/plugins/scala/surefire/SurefireUtilsTest/shouldGetReportsFromPluginConfiguration/pom.xml create mode 100644 src/test/resources/surefire/targetdir/surefire-reports/emptyReport.txt create mode 100644 src/test/resources/surefire/targetdir/surefire-reports/emptyReport.xml create mode 100644 src/test/resources/surefire/targetdir/test-reports/emptyReport.txt create mode 100644 src/test/resources/surefire/targetdir/test-reports/emptyReport.xml diff --git a/pom.xml b/pom.xml index d9d1074..cd37a6d 100644 --- a/pom.xml +++ b/pom.xml @@ -83,6 +83,12 @@ + + org.apache.maven + maven-project + 2.2.1 + provided + @@ -108,12 +114,6 @@ 1.9.0 test - - org.apache.maven - maven-project - 2.2.1 - test - diff --git a/src/main/java/org/sonar/plugins/scala/surefire/ScalaSurefireParser.java b/src/main/java/org/sonar/plugins/scala/surefire/ScalaSurefireParser.java index 1a21046..6843505 100644 --- a/src/main/java/org/sonar/plugins/scala/surefire/ScalaSurefireParser.java +++ b/src/main/java/org/sonar/plugins/scala/surefire/ScalaSurefireParser.java @@ -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; -//} -//} } diff --git a/src/main/java/org/sonar/plugins/scala/surefire/SurefireSensor.java b/src/main/java/org/sonar/plugins/scala/surefire/SurefireSensor.java index 1208a92..7fdb25c 100644 --- a/src/main/java/org/sonar/plugins/scala/surefire/SurefireSensor.java +++ b/src/main/java/org/sonar/plugins/scala/surefire/SurefireSensor.java @@ -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; @@ -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() { @@ -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 { @@ -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); } diff --git a/src/main/java/org/sonar/plugins/scala/surefire/SurefireUtils.java b/src/main/java/org/sonar/plugins/scala/surefire/SurefireUtils.java index 60a5547..8115ee6 100644 --- a/src/main/java/org/sonar/plugins/scala/surefire/SurefireUtils.java +++ b/src/main/java/org/sonar/plugins/scala/surefire/SurefireUtils.java @@ -20,10 +20,15 @@ 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 @@ -31,42 +36,57 @@ 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() { - } - -} +} \ No newline at end of file diff --git a/src/test/java/org/sonar/plugins/scala/surefire/SurefireUtilsTest.java b/src/test/java/org/sonar/plugins/scala/surefire/SurefireUtilsTest.java new file mode 100644 index 0000000..905ca13 --- /dev/null +++ b/src/test/java/org/sonar/plugins/scala/surefire/SurefireUtilsTest.java @@ -0,0 +1,71 @@ +/* + * Sonar Scala Plugin + * Copyright (C) 2011 - 2014 All contributors + * dev@sonar.codehaus.org + * + * 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(); + } + + + +} diff --git a/src/test/resources/org/sonar/plugins/scala/surefire/SurefireUtilsTest/shouldGetReportsFromPluginConfiguration/pom.xml b/src/test/resources/org/sonar/plugins/scala/surefire/SurefireUtilsTest/shouldGetReportsFromPluginConfiguration/pom.xml new file mode 100644 index 0000000..4d4c297 --- /dev/null +++ b/src/test/resources/org/sonar/plugins/scala/surefire/SurefireUtilsTest/shouldGetReportsFromPluginConfiguration/pom.xml @@ -0,0 +1,21 @@ + + 4.0.0 + fake.group + fake.artifactId + jar + 1.0-SNAPSHOT + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.4 + + src/test/resources/surefire/targetdir/test-reports + + + + + \ No newline at end of file diff --git a/src/test/resources/surefire/targetdir/surefire-reports/emptyReport.txt b/src/test/resources/surefire/targetdir/surefire-reports/emptyReport.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/surefire/targetdir/surefire-reports/emptyReport.xml b/src/test/resources/surefire/targetdir/surefire-reports/emptyReport.xml new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/surefire/targetdir/test-reports/emptyReport.txt b/src/test/resources/surefire/targetdir/test-reports/emptyReport.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/surefire/targetdir/test-reports/emptyReport.xml b/src/test/resources/surefire/targetdir/test-reports/emptyReport.xml new file mode 100644 index 0000000..e69de29