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.surefire;
021
022import org.apache.commons.lang.StringUtils;
023import org.slf4j.Logger;
024import org.slf4j.LoggerFactory;
025import org.sonar.api.batch.AbstractCoverageExtension;
026import org.sonar.api.batch.DependsUpon;
027import org.sonar.api.batch.Sensor;
028import org.sonar.api.batch.SensorContext;
029import org.sonar.api.resources.Java;
030import org.sonar.api.resources.JavaFile;
031import org.sonar.api.resources.Project;
032import org.sonar.api.resources.Resource;
033import org.sonar.plugins.surefire.api.AbstractSurefireParser;
034import org.sonar.plugins.surefire.api.SurefireUtils;
035
036import java.io.File;
037
038public class SurefireSensor implements Sensor {
039
040  private static Logger logger = LoggerFactory.getLogger(SurefireSensor.class);
041
042  @DependsUpon
043  public Class dependsUponCoverageSensors() {
044    return AbstractCoverageExtension.class;
045  }
046
047  public boolean shouldExecuteOnProject(Project project) {
048    return project.getAnalysisType().isDynamic(true) && Java.KEY.equals(project.getLanguageKey());
049  }
050
051  public void analyse(Project project, SensorContext context) {
052    File dir = SurefireUtils.getReportsDirectory(project);
053    collect(project, context, dir);
054  }
055
056  protected void collect(Project project, SensorContext context, File reportsDir) {
057    logger.info("parsing {}", reportsDir);
058    new AbstractSurefireParser() {
059      @Override
060      protected Resource<?> getUnitTestResource(String classKey) {
061        if (!StringUtils.contains(classKey, "$")) {
062          // temporary hack waiting for http://jira.codehaus.org/browse/SONAR-1865
063          return new JavaFile(classKey, true);
064        }
065        return null;
066      }
067    }.collect(project, context, reportsDir);
068  }
069
070  @Override
071  public String toString() {
072    return getClass().getSimpleName();
073  }
074}