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.api;
021
022import org.sonar.api.CoreProperties;
023import org.sonar.api.batch.maven.MavenPlugin;
024import org.sonar.api.batch.maven.MavenUtils;
025import org.sonar.api.resources.Project;
026import org.sonar.api.utils.Logs;
027
028import java.io.File;
029
030/**
031 * @since 2.4
032 */
033public final class CoberturaUtils {
034
035  public static final String COBERTURA_GROUP_ID = MavenUtils.GROUP_ID_CODEHAUS_MOJO;
036  public static final String COBERTURA_ARTIFACT_ID = "cobertura-maven-plugin";
037
038  /**
039   * @deprecated in 2.8, because assumes that Sonar executed from Maven. Not used any more in sonar-cobertura-plugin.
040   *             See http://jira.codehaus.org/browse/SONAR-2321
041   */
042  @Deprecated
043  public static File getReport(Project project) {
044    File report = getReportFromProperty(project);
045    if (report == null) {
046      report = getReportFromPluginConfiguration(project);
047    }
048    if (report == null) {
049      report = getReportFromDefaultPath(project);
050    }
051
052    if (report == null || !report.exists() || !report.isFile()) {
053      Logs.INFO.warn("Cobertura report not found at {}", report);
054      report = null;
055    }
056    return report;
057  }
058
059  private static File getReportFromProperty(Project project) {
060    String path = (String) project.getProperty(CoreProperties.COBERTURA_REPORT_PATH_PROPERTY);
061    if (path != null) {
062      return project.getFileSystem().resolvePath(path);
063    }
064    return null;
065  }
066
067  private static File getReportFromPluginConfiguration(Project project) {
068    MavenPlugin mavenPlugin = MavenPlugin.getPlugin(project.getPom(), COBERTURA_GROUP_ID, COBERTURA_ARTIFACT_ID);
069    if (mavenPlugin != null) {
070      String path = mavenPlugin.getParameter("outputDirectory");
071      if (path != null) {
072        return new File(project.getFileSystem().resolvePath(path), "coverage.xml");
073      }
074    }
075    return null;
076  }
077
078  private static File getReportFromDefaultPath(Project project) {
079    return new File(project.getFileSystem().getReportOutputDir(), "cobertura/coverage.xml");
080  }
081
082  private CoberturaUtils() {
083  }
084
085}