001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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.jacoco;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.sonar.api.batch.Sensor;
024    import org.sonar.api.batch.SensorContext;
025    import org.sonar.api.measures.CoreMetrics;
026    import org.sonar.api.measures.Measure;
027    import org.sonar.api.resources.JavaFile;
028    import org.sonar.api.resources.Project;
029    
030    import java.util.Collection;
031    
032    /**
033     * Note that this class can't extend {@link org.sonar.api.batch.AbstractCoverageExtension}, because in this case this extension will be
034     * disabled under Sonar 2.3, if JaCoCo is not defined as the default code coverage plugin.
035     *
036     * @author Evgeny Mandrikov
037     */
038    public class JaCoCoItSensor implements Sensor {
039      private JacocoConfiguration configuration;
040    
041      public JaCoCoItSensor(JacocoConfiguration configuration) {
042        this.configuration = configuration;
043      }
044    
045      public boolean shouldExecuteOnProject(Project project) {
046        return StringUtils.isNotBlank(configuration.getItReportPath())
047            && project.getAnalysisType().isDynamic(true);
048      }
049    
050      public void analyse(Project project, SensorContext context) {
051        new ITAnalyzer().analyse(project, context);
052      }
053    
054      class ITAnalyzer extends AbstractAnalyzer {
055        @Override
056        protected String getReportPath(Project project) {
057          return configuration.getItReportPath();
058        }
059    
060        @Override
061        protected void saveMeasures(SensorContext context, JavaFile resource, Collection<Measure> measures) {
062          for (Measure measure : measures) {
063            Measure itMeasure = convertForIT(measure);
064            if (itMeasure != null) {
065              context.saveMeasure(resource, itMeasure);
066            }
067          }
068        }
069    
070        private Measure convertForIT(Measure measure) {
071          Measure itMeasure = null;
072          if (CoreMetrics.LINES_TO_COVER.equals(measure.getMetric())) {
073            itMeasure = new Measure(CoreMetrics.IT_LINES_TO_COVER, measure.getValue());
074    
075          } else if (CoreMetrics.UNCOVERED_LINES.equals(measure.getMetric())) {
076            itMeasure = new Measure(CoreMetrics.IT_UNCOVERED_LINES, measure.getValue());
077    
078          } else if (CoreMetrics.COVERAGE_LINE_HITS_DATA.equals(measure.getMetric())) {
079            itMeasure = new Measure(CoreMetrics.IT_COVERAGE_LINE_HITS_DATA, measure.getData());
080    
081          } else if (CoreMetrics.CONDITIONS_TO_COVER.equals(measure.getMetric())) {
082            itMeasure = new Measure(CoreMetrics.IT_CONDITIONS_TO_COVER, measure.getValue());
083    
084          } else if (CoreMetrics.UNCOVERED_CONDITIONS.equals(measure.getMetric())) {
085            itMeasure = new Measure(CoreMetrics.IT_UNCOVERED_CONDITIONS, measure.getValue());
086    
087          } else if (CoreMetrics.COVERED_CONDITIONS_BY_LINE.equals(measure.getMetric())) {
088            itMeasure = new Measure(CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE, measure.getData());
089    
090          } else if (CoreMetrics.CONDITIONS_BY_LINE.equals(measure.getMetric())) {
091            itMeasure = new Measure(CoreMetrics.IT_CONDITIONS_BY_LINE, measure.getData());
092          }
093          return itMeasure;
094        }
095      }
096    
097      @Override
098      public String toString() {
099        return getClass().getSimpleName();
100      }
101    }