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;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
025    import org.sonar.api.batch.AbstractCoverageExtension;
026    import org.sonar.api.batch.DependsUpon;
027    import org.sonar.api.batch.Sensor;
028    import org.sonar.api.batch.SensorContext;
029    import org.sonar.api.resources.Java;
030    import org.sonar.api.resources.JavaFile;
031    import org.sonar.api.resources.Project;
032    import org.sonar.api.resources.Resource;
033    import org.sonar.plugins.surefire.api.AbstractSurefireParser;
034    import org.sonar.plugins.surefire.api.SurefireUtils;
035    
036    import java.io.File;
037    
038    public 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    }