Package ch.hortis.sonar.mvn.mc

Source Code of ch.hortis.sonar.mvn.mc.CoberturaCollector

/*
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.mvn.mc;

import ch.hortis.sonar.model.Collectable;
import ch.hortis.sonar.model.File;
import ch.hortis.sonar.model.FileMeasure;
import ch.hortis.sonar.model.Metric;
import ch.hortis.sonar.model.Metrics;
import ch.hortis.sonar.model.ProjectMeasure;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

public class CoberturaCollector extends BaseMeasuresCollector {
  private XmlReportParser parser;
  private Metric lineCoverageMetric;
  private Metric branchCoverageMetric;

  public boolean initialize(MavenProject project) {
    java.io.File reportFile = findFileFromBuildDirectory(project, "site/cobertura/coverage.xml");
    boolean ok = false;
    if (reportFile != null) {
      ok = true;
      parser = new XmlReportParser();
      parser.parse(reportFile);
      lineCoverageMetric = loadMetric(Metrics.COBERTURA_LINE_COVERAGE);
      branchCoverageMetric = loadMetric(Metrics.COBERTURA_BRANCH_COVERAGE);
    }
    return ok;
  }

  public List<Collectable> collect() throws MojoExecutionException {
    ArrayList<Collectable> result = new ArrayList<Collectable>();
    result.addAll(collectProjectMeasures());
    result.addAll(collectFileMeasures());
    return result;
  }


  public List<ProjectMeasure> collectProjectMeasures() throws MojoExecutionException {
    List<ProjectMeasure> measures = new ArrayList<ProjectMeasure>();
    try {
      double lineRate = parseNumber(parser.executeXPath("/coverage/@line-rate"));
      measures.add(new ProjectMeasure(lineCoverageMetric, convertPercentage(lineRate)));
      double branchRate = parseNumber(parser.executeXPath("/coverage/@branch-rate"));
      measures.add(new ProjectMeasure(branchCoverageMetric, convertPercentage(branchRate)));
    } catch (Exception ex) {
      throw new MojoExecutionException("Error during Cobertura reports parsing", ex);
    }
    return measures;
  }

  public List<FileMeasure> collectFileMeasures() throws MojoExecutionException {
    List<FileMeasure> measures = new ArrayList<FileMeasure>();
    NodeList classes = parser.executeXPathNodeList("//classes/class");
    for (int i = 0; i < classes.getLength(); i++) {
      Element elt = (Element) classes.item(i);
      FileMeasure lineMeasure = toFileMeasure(elt, lineCoverageMetric, "line-rate");
      if (lineMeasure != null) {
        measures.add(lineMeasure);
      }
      FileMeasure branchMeasure = toFileMeasure(elt, branchCoverageMetric, "branch-rate");
      if (branchMeasure != null) {
        measures.add(branchMeasure);
      }
    }
    return measures;
  }

  protected FileMeasure toFileMeasure(Element elt, Metric metric, String valueAttributeName) throws MojoExecutionException {

    String name = elt.getAttribute("filename");
    name = StringUtils.replaceChars(name, '\\', '/');
    String filename = StringUtils.substringAfterLast(name, "/");
    String namespace = StringUtils.substringBeforeLast(name, "/" + filename);
    namespace = StringUtils.replaceChars(namespace, '/', '.');
    File file = getFilesRepository().getFile(namespace, filename);
    if (file == null) {
      // cobertura 2.0 is quite bugged and generates some time coverage measures for unknown files (in Tuscany project for example)
      // so we simply return and do not create the file measure
      return null;
    }

    FileMeasure measure = new FileMeasure();
    measure.setMetric(null);
    measure.setFile(file);
    measure.setMetric(metric);
    measure.setValue(convertPercentage(Double.parseDouble(elt.getAttribute(valueAttributeName))));

    NodeList lines = parser.executeXPathNodeList(elt, "methods/method/lines/line");
    for (int i = 0; i < lines.getLength(); i++) {
      Element line = (Element) lines.item(i);
      double hits = Double.parseDouble(line.getAttribute("hits"));
      if (hits > 0) {
        measure.addParameter("line-hit", Double.parseDouble(line.getAttribute("number")), hits);
      }
    }
    return measure;
  }


  protected double convertPercentage(Number percentage) {
    return scaleValue(percentage.doubleValue() * 100.0);
  }
}
TOP

Related Classes of ch.hortis.sonar.mvn.mc.CoberturaCollector

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.