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.jacoco;
021
022import org.apache.commons.lang.StringUtils;
023import org.sonar.api.batch.Sensor;
024import org.sonar.api.batch.SensorContext;
025import org.sonar.api.measures.CoreMetrics;
026import org.sonar.api.measures.Measure;
027import org.sonar.api.resources.JavaFile;
028import org.sonar.api.resources.Project;
029
030import 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 */
038public 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 String getExcludes(Project project) {
062      return configuration.getExcludes();
063    }
064
065    @Override
066    protected void saveMeasures(SensorContext context, JavaFile resource, Collection<Measure> measures) {
067      for (Measure measure : measures) {
068        Measure itMeasure = convertForIT(measure);
069        if (itMeasure != null) {
070          context.saveMeasure(resource, itMeasure);
071        }
072      }
073    }
074
075    private Measure convertForIT(Measure measure) {
076      Measure itMeasure = null;
077      if (CoreMetrics.LINES_TO_COVER.equals(measure.getMetric())) {
078        itMeasure = new Measure(CoreMetrics.IT_LINES_TO_COVER, measure.getValue());
079
080      } else if (CoreMetrics.UNCOVERED_LINES.equals(measure.getMetric())) {
081        itMeasure = new Measure(CoreMetrics.IT_UNCOVERED_LINES, measure.getValue());
082
083      } else if (CoreMetrics.COVERAGE_LINE_HITS_DATA.equals(measure.getMetric())) {
084        itMeasure = new Measure(CoreMetrics.IT_COVERAGE_LINE_HITS_DATA, measure.getData());
085
086      } else if (CoreMetrics.CONDITIONS_TO_COVER.equals(measure.getMetric())) {
087        itMeasure = new Measure(CoreMetrics.IT_CONDITIONS_TO_COVER, measure.getValue());
088
089      } else if (CoreMetrics.UNCOVERED_CONDITIONS.equals(measure.getMetric())) {
090        itMeasure = new Measure(CoreMetrics.IT_UNCOVERED_CONDITIONS, measure.getValue());
091
092      } else if (CoreMetrics.COVERED_CONDITIONS_BY_LINE.equals(measure.getMetric())) {
093        itMeasure = new Measure(CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE, measure.getData());
094
095      } else if (CoreMetrics.CONDITIONS_BY_LINE.equals(measure.getMetric())) {
096        itMeasure = new Measure(CoreMetrics.IT_CONDITIONS_BY_LINE, measure.getData());
097      }
098      return itMeasure;
099    }
100  }
101
102  @Override
103  public String toString() {
104    return getClass().getSimpleName();
105  }
106}