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     */
020    package org.sonar.plugins.surefire.api;
021    
022    import org.sonar.api.CoreProperties;
023    import org.sonar.api.batch.maven.MavenPlugin;
024    import org.sonar.api.batch.maven.MavenSurefireUtils;
025    import org.sonar.api.resources.Project;
026    
027    import java.io.File;
028    
029    /**
030     * @since 2.4
031     */
032    public final class SurefireUtils {
033    
034      public static File getReportsDirectory(Project project) {
035        File dir = getReportsDirectoryFromProperty(project);
036        if (dir == null) {
037          dir = getReportsDirectoryFromPluginConfiguration(project);
038        }
039        if (dir == null) {
040          dir = getReportsDirectoryFromDefaultConfiguration(project);
041        }
042        return dir;
043      }
044    
045      private static File getReportsDirectoryFromProperty(Project project) {
046        String path = (String) project.getProperty(CoreProperties.SUREFIRE_REPORTS_PATH_PROPERTY);
047        if (path != null) {
048          return project.getFileSystem().resolvePath(path);
049        }
050        return null;
051      }
052    
053      private static File getReportsDirectoryFromPluginConfiguration(Project project) {
054        MavenPlugin plugin = MavenPlugin.getPlugin(project.getPom(), MavenSurefireUtils.GROUP_ID, MavenSurefireUtils.ARTIFACT_ID);
055        if (plugin != null) {
056          String path = plugin.getParameter("reportsDirectory");
057          if (path != null) {
058            return project.getFileSystem().resolvePath(path);
059          }
060        }
061        return null;
062      }
063    
064      private static File getReportsDirectoryFromDefaultConfiguration(Project project) {
065        return new File(project.getFileSystem().getBuildDir(), "surefire-reports");
066      }
067    
068      private SurefireUtils() {
069      }
070    
071    }