All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ch.hortis.sonar.mvn.mc.SurefireCollector Maven / Gradle / Ivy

/*
 * 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.Metrics;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.project.MavenProject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.FilenameFilter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Surefire reports collector
 */
public class SurefireCollector extends BaseMetricCollector implements MetricsCollector {

  private Log log;
  private File[] reports;

  public void enableLogging( Log log ) {
    this.log = log;
  }

  public boolean initialize( MavenProject project ) throws MojoExecutionException  {
    String baseDir = project.getBuild().getDirectory() + "/surefire-reports";
    reports = findSureFireReportFiles( project, baseDir );
    return (reports!=null && reports.length>0);
  }

  public Collection collectMeasures() throws MojoExecutionException {
    Collection measures;
    try {
      DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
      ReportDataContainer dataContainer = new ReportDataContainer();

      for (File report : reports) {
        log.debug( "Parsing file " + report.getPath() );
        Document document = builder.parse( report );
        XPath xpath = XPathFactory.newInstance().newXPath();

        parseReport( xpath, document, dataContainer );
      }
      measures = dataContainer.asMetrics();

    } catch (Exception ex) {
      throw new MojoExecutionException( "Error during surefire reports parsing", ex );
    }
    return measures;
  }

  private void parseReport( XPath xpath, Node root, ReportDataContainer dataContainer ) throws XPathExpressionException, ParseException {
    Node testSuite = (Node) xpath.evaluate( "//testsuite", root, XPathConstants.NODE );
    Number errors = (Number) xpath.evaluate( "@errors", testSuite, XPathConstants.NUMBER );
    Number skipped = (Number) xpath.evaluate( "@skipped", testSuite, XPathConstants.NUMBER );
    Number tests = (Number) xpath.evaluate( "@tests", testSuite, XPathConstants.NUMBER );
    double time = parseNumber( xpath.evaluate( "@time", testSuite ) );
    Number failures = (Number) xpath.evaluate( "@failures", testSuite, XPathConstants.NUMBER );

    // cumulating the project metrics
    dataContainer.cumulate( errors, skipped, tests, time, failures );
  }

  private File[] findSureFireReportFiles( final MavenProject project, String sureFireOutputDirectory ) {
    File reportsDir = new File( sureFireOutputDirectory );
    if ( !reportsDir.exists() ) {
      return null;
    }
    File[] reports = reportsDir.listFiles( new FilenameFilter() {
      public boolean accept( File dir, String name ) {
        return name.startsWith( "TEST-" ) && name.endsWith( ".xml" );
      }
    } );

    return reports;
  }

  private class ReportDataContainer {
    private double errors;
    private double skipped;
    private double tests;
    private double time;
    private double failures;

    private void cumulate( Number errors, Number skipped, Number tests, double time, Number failures ) {
      this.errors += errors.doubleValue();
      this.skipped += skipped.doubleValue();
      this.failures += failures.doubleValue();
      this.tests += tests.doubleValue();
      this.time += time;
    }

    private Collection asMetrics() {
      Collection metrics = new ArrayList();
      MetricData metric = new MetricData();
      metric.setMetric( Metrics.SUREFIRE_ERRORS );
      metric.setValue( errors );
      metrics.add( metric );

      metric = new MetricData();
      metric.setMetric( Metrics.SUREFIRE_SKIPPED );
      metric.setValue( skipped );
      metrics.add( metric );

      metric = new MetricData();
      metric.setMetric( Metrics.SUREFIRE_FAILURES );
      metric.setValue( failures );
      metrics.add( metric );

      metric = new MetricData();
      metric.setMetric( Metrics.SUREFIRE_TESTS );
      metric.setValue( tests );
      metrics.add( metric );

      metric = new MetricData();
      metric.setMetric( Metrics.SUREFIRE_TIME );
      metric.setValue( scaleValue( time * 1000, 3 ) );
      metrics.add( metric );

      metric = new MetricData();
      metric.setMetric( Metrics.SUREFIRE_SUCCESS_PERCENTAGE );
      metric.setValue( scaleValue( 100d - ( ( errors + failures ) * 100d / tests ) ) );
      metrics.add( metric );

      metric = new MetricData();
      metric.setMetric( Metrics.SUREFIRE_ERRORS_PERCENTAGE );
      metric.setValue( scaleValue( errors * 100d / tests ) );
      metrics.add( metric );

      metric = new MetricData();
      metric.setMetric( Metrics.SUREFIRE_FAILURE_PERCENTAGE );
      metric.setValue( scaleValue( failures * 100d / tests ) );
      metrics.add( metric );
      return metrics;
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy