001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.plugins.cobertura;
021
022import org.apache.commons.configuration.Configuration;
023import org.sonar.api.CoreProperties;
024import org.sonar.api.batch.CoverageExtension;
025import org.sonar.api.batch.Initializer;
026import org.sonar.api.batch.maven.DependsUponMavenPlugin;
027import org.sonar.api.batch.maven.MavenPlugin;
028import org.sonar.api.batch.maven.MavenPluginHandler;
029import org.sonar.api.resources.Java;
030import org.sonar.api.resources.Project;
031import org.sonar.plugins.cobertura.api.CoberturaUtils;
032
033/**
034 * Provides {@link CoberturaMavenPluginHandler} and configures correct path to report.
035 * Enabled only in Maven environment.
036 */
037public class CoberturaMavenInitializer extends Initializer implements CoverageExtension, DependsUponMavenPlugin {
038
039  private CoberturaMavenPluginHandler handler;
040
041  public CoberturaMavenInitializer(CoberturaMavenPluginHandler handler) {
042    this.handler = handler;
043  }
044
045  public MavenPluginHandler getMavenPluginHandler(Project project) {
046    if (project.getAnalysisType().equals(Project.AnalysisType.DYNAMIC)) {
047      return handler;
048    }
049    return null;
050  }
051
052  @Override
053  public boolean shouldExecuteOnProject(Project project) {
054    return project.getAnalysisType().isDynamic(true) &&
055        !project.getFileSystem().mainFiles(Java.KEY).isEmpty();
056  }
057
058  @Override
059  public void execute(Project project) {
060    Configuration conf = project.getConfiguration();
061    if (!conf.containsKey(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY)) {
062      String report = getReportPathFromPluginConfiguration(project);
063      if (report == null) {
064        report = getDefaultReportPath(project);
065      }
066      conf.setProperty(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY, report);
067    }
068  }
069
070  private static String getDefaultReportPath(Project project) {
071    return project.getFileSystem().getReportOutputDir() + "/cobertura/coverage.xml";
072  }
073
074  private static String getReportPathFromPluginConfiguration(Project project) {
075    MavenPlugin mavenPlugin = MavenPlugin.getPlugin(
076        project.getPom(),
077        CoberturaUtils.COBERTURA_GROUP_ID,
078        CoberturaUtils.COBERTURA_ARTIFACT_ID);
079    if (mavenPlugin != null) {
080      String path = mavenPlugin.getParameter("outputDirectory");
081      if (path != null) {
082        return path + "/coverage.xml";
083      }
084    }
085    return null;
086  }
087
088}