You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CloverSensor.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2009 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * Sonar is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.plugins.clover;
  21. import org.slf4j.LoggerFactory;
  22. import org.sonar.api.Plugins;
  23. import org.sonar.api.batch.AbstractCoverageExtension;
  24. import org.sonar.api.batch.Sensor;
  25. import org.sonar.api.batch.SensorContext;
  26. import org.sonar.api.batch.maven.DependsUponMavenPlugin;
  27. import org.sonar.api.batch.maven.MavenPlugin;
  28. import org.sonar.api.batch.maven.MavenPluginHandler;
  29. import org.sonar.api.resources.Project;
  30. import java.io.File;
  31. public class CloverSensor extends AbstractCoverageExtension implements Sensor, DependsUponMavenPlugin {
  32. private CloverMavenPluginHandler handler;
  33. public CloverSensor(Plugins plugins, CloverMavenPluginHandler handler) {
  34. super(plugins);
  35. this.handler = handler;
  36. }
  37. public MavenPluginHandler getMavenPluginHandler(Project project) {
  38. if (project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC)) {
  39. return handler;
  40. }
  41. return null;
  42. }
  43. public void analyse(Project project, SensorContext context) {
  44. File report = getReport(project);
  45. if (reportExists(report)) {
  46. new XmlReportParser(context).collect(report);
  47. } else {
  48. LoggerFactory.getLogger(getClass()).info("Clover XML report not found");
  49. }
  50. }
  51. @Override
  52. public boolean shouldExecuteOnProject(Project project) {
  53. return super.shouldExecuteOnProject(project) &&
  54. project.getFileSystem().hasJavaSourceFiles();
  55. }
  56. protected File getReport(Project pom) {
  57. File report = getReportFromProperty(pom);
  58. if (report == null) {
  59. report = getReportFromPluginConfiguration(pom);
  60. }
  61. if (report == null) {
  62. report = getReportFromDefaultPath(pom);
  63. }
  64. return report;
  65. }
  66. private File getReportFromProperty(Project pom) {
  67. String path = (String) pom.getProperty(CloverConstants.REPORT_PATH_PROPERTY);
  68. if (path != null) {
  69. return pom.getFileSystem().resolvePath(path);
  70. }
  71. return null;
  72. }
  73. private File getReportFromPluginConfiguration(Project pom) {
  74. MavenPlugin plugin = MavenPlugin.getPlugin(pom.getPom(), CloverConstants.MAVEN_GROUP_ID, CloverConstants.MAVEN_ARTIFACT_ID);
  75. if (plugin != null) {
  76. String path = plugin.getParameter("outputDirectory");
  77. if (path != null) {
  78. return new File(pom.getFileSystem().resolvePath(path), "clover.xml");
  79. }
  80. }
  81. return null;
  82. }
  83. private File getReportFromDefaultPath(Project pom) {
  84. return new File(pom.getFileSystem().getReportOutputDir(), "clover/clover.xml");
  85. }
  86. private boolean reportExists(File report) {
  87. return report != null && report.exists() && report.isFile();
  88. }
  89. }