/*
* 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.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 javax.xml.xpath.XPathConstants;
import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
public class CloverCollector extends BaseMeasuresCollector {
protected XmlReportParser parser;
protected Metric cloverMetric;
public List<Collectable> collect() throws MojoExecutionException {
ArrayList<Collectable> result = new ArrayList<Collectable>();
try {
result.addAll( collectProjectMeasures() );
result.addAll( collectFileMeasures() );
} catch (Exception ex) {
throw new MojoExecutionException( "Error during Clover reports parsing", ex );
}
return result;
}
public List<ProjectMeasure> collectProjectMeasures() throws ParseException {
Element projectEl = parser.getChildElement( parser.root, "project" );
Element metricsEl = parser.getChildElement( projectEl, "metrics" );
ProjectMeasure measure = new ProjectMeasure();
measure.setMetric( cloverMetric );
measure.setValue( getCodeCoverageFromMetricsNode( metricsEl ) );
List<ProjectMeasure> measures = new ArrayList<ProjectMeasure>();
measures.add( measure );
return measures;
}
public boolean initialize( MavenProject project ) throws MojoExecutionException {
File report = findCloverXmlReportFile( project );
if ( !report.exists() ) {
return false;
}
parser = new XmlReportParser();
parser.parseDocument( report );
cloverMetric = loadMetric( Metrics.CLOVER_COVERAGE );
return true;
}
private File findCloverXmlReportFile( final MavenProject project ) throws MojoExecutionException {
String xmlOutputDirectory = project.getBuild().getDirectory() + "/site/clover/clover.xml";
File result = new File( xmlOutputDirectory );
return result;
}
public List<FileMeasure> collectFileMeasures() throws MojoExecutionException, ParseException {
List<FileMeasure> measures = new ArrayList<FileMeasure>();
NodeList packages = parser.executeXPathNodeList( "/coverage/project/package" );
for (int i = 0; i < packages.getLength(); i++) {
Element pkElt = (Element) packages.item( i );
String namespace = pkElt.getAttribute( "name" );
NodeList files = parser.executeXPathNodeList( pkElt, "file" );
for (int j = 0; j < files.getLength(); j++) {
Element fileElt = (Element) files.item( j );
String filename = extractFilename( fileElt );
FileMeasure measure = new FileMeasure();
ch.hortis.sonar.model.File fle = getFilesRepository().getFile( namespace, filename );
if ( fle == null ) {
throw new MojoExecutionException( "Unable to find file '" + filename + "' in package '" + namespace + "'" );
}
measure.setFile( fle );
measure.setMetric( cloverMetric );
Element metricsElt = (Element) parser.executeXPath( fileElt, XPathConstants.NODE, "metrics" );
measure.setValue( getCodeCoverageFromMetricsNode( metricsElt ) );
fillParameters( fileElt, measure );
measures.add( measure );
}
}
return measures;
}
private void fillParameters( Element fileElt, FileMeasure measure ) throws ParseException {
List<Element> lines = parser.getChildElements( fileElt, "line" );
for (Element line : lines) {
double hits = 0.0;
if ( "".equals( line.getAttribute( "count" ) ) ) {
hits = parseNumber( line.getAttribute( "truecount" ) ) + parseNumber( line.getAttribute( "falsecount" ) );
} else {
hits = parseNumber( line.getAttribute( "count" ) );
}
if ( hits > 0 ) {
measure.addParameter( "line-hit", parseNumber( line.getAttribute( "num" ) ), hits );
}
}
}
private String extractFilename( Element fileElt ) {
String filename = fileElt.getAttribute( "name" );
filename = StringUtils.replaceChars( filename, '\\', '/' );
filename = StringUtils.substringAfterLast( filename, "/" );
return filename;
}
private Double getCodeCoverageFromMetricsNode( Element metricsNode ) throws ParseException {
double coveredElements = parseNumber( metricsNode.getAttribute( "coveredelements" ) );
double nb = parseNumber( metricsNode.getAttribute( "elements" ) );
if ( nb > 0 ) {
return 100.0 * (coveredElements / nb);
}
return 0.0;
}
}