Skip to content

Commit

Permalink
First pass at generating cobertura-style output
Browse files Browse the repository at this point in the history
Adding a "sourceDir" property in the mojo to allow us to generate Cobertura reports with proper source pointers.

Adding the ability to define multiple source directories to be listed in Cobertura report output.
  • Loading branch information
Stanton Sievers committed Sep 18, 2013
1 parent dac9291 commit 75a15fc
Show file tree
Hide file tree
Showing 15 changed files with 130 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void instrumentAndGenerateReports() throws IOException {
final TestRunCoverageStatistics totalStats = new TestRunCoverageStatistics(baseUri.relativize(URI.create(TOTAL_REPORT_NAME)), "Total coverage report");
totalStats.setSortBy(config.getSortBy());
totalStats.setOrder(config.getOrder());
totalStats.setSourceDirs(config.getSourceDirs());

maybePreloadSources(totalStats);
runTests(tests, actualThreadCount, outputStrategy, totalStats);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@ public String getExtension() {
}
},
CSV,
PDF;
PDF,
COBERTURA {
@Override
public String getSuffix() {
return "coverage";
}

@Override
public String getExtension() {
return "xml";
}
};

public String getSuffix() {
return "report";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
Expand All @@ -25,7 +26,7 @@ public interface Config {
SortBy DEFAULT_SORT_BY = SortBy.COVERAGE;
Order DEFAULT_ORDER = Order.DESC;

Set<ReportFormat> DEFAULT_REPORT_FORMATS = ImmutableSet.of(ReportFormat.HTML, ReportFormat.RAW);
Set<ReportFormat> DEFAULT_REPORT_FORMATS = ImmutableSet.of(ReportFormat.HTML, ReportFormat.RAW, ReportFormat.COBERTURA);

String DEFAULT_SOURCES_TO_PRELOAD_ENCODING = "UTF-8";

Expand Down Expand Up @@ -132,4 +133,8 @@ public interface Config {

Map<String, String> getWebDriverCapabilities();

void setSourceDir(List<String> sourceDirs);

List<String> getSourceDirs();

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.net.URI;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -60,6 +61,8 @@ public class InstanceFieldPerPropertyConfig implements Config {
private InstrumentingBrowser browser = null;
private Map<String, String> webDriverCapabilities = Maps.newHashMap();

private List<String> sourceDirs;

@Override
public void setBaseDir(final String baseDir) {
this.baseDir = baseDir;
Expand Down Expand Up @@ -361,4 +364,14 @@ public Map<String, String> getWebDriverCapabilities() {
return webDriverCapabilities;
}

@Override
public void setSourceDir(List<String> sourceDirs) {
this.sourceDirs = sourceDirs;
}

@Override
public List<String> getSourceDirs() {
return sourceDirs;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public int getCoverage() {
return MiscUtil.toCoverage(getStatements(), getExecuted());
}

public double getCoverageRate() {
return MiscUtil.toCoverageRate(getStatements(), getExecuted());
}

public boolean getHasStatements() {
return getStatements() > 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public final class TestRunCoverageStatistics implements Iterable<ScriptCoverageS

private final Map<URI, ScriptCoverageStatistics> fileStatsMap = Maps.newTreeMap();

private List<String> sourceDirs;

public TestRunCoverageStatistics(final URI test) {
this(test, String.format("Coverage report for \"%s\"", test));
}
Expand Down Expand Up @@ -103,6 +105,10 @@ public int getTotalCoverage() {
return MiscUtil.toCoverage(getTotalStatements(), getTotalExecuted());
}

public double getTotalCoverageRate() {
return MiscUtil.toCoverageRate(getTotalStatements(), getTotalExecuted());
}

public boolean getHasStatements() {
return getTotalStatements() > 0;
}
Expand Down Expand Up @@ -136,4 +142,12 @@ public Order getOrder() {
return order;
}

public void setSourceDirs(List<String> sourceDirs) {
this.sourceDirs = sourceDirs;
}

public List<String> getSourceDirs(){
return sourceDirs;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.timurstrekalov.saga.core.reporter;

import java.io.File;
import java.io.IOException;

import com.github.timurstrekalov.saga.core.ReportFormat;
import com.github.timurstrekalov.saga.core.model.TestRunCoverageStatistics;

public class CoberturaReporter extends AbstractStringTemplateBasedReporter {

public CoberturaReporter() {
super(ReportFormat.COBERTURA);
}

@Override
protected void writeReportThreadSafe(final File outputFile, final TestRunCoverageStatistics runStats) throws IOException {
stringTemplateGroup.getInstanceOf("runStatsCobertura")
.add("stats", runStats)
.add("name", config.getProperty("app.name"))
.add("version", config.getProperty("app.version"))
.add("url", config.getProperty("app.url"))
.write(outputFile, listener);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public final class ReporterFactory {
.put(ReportFormat.RAW, RawReporter.class)
.put(ReportFormat.CSV, CsvReporter.class)
.put(ReportFormat.PDF, PdfReporter.class)
.put(ReportFormat.COBERTURA, CoberturaReporter.class)
.build();

private ReporterFactory() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ private MiscUtil() {
}

public static int toCoverage(final int totalStatements, final int totalExecuted) {
return (int) ((double) totalExecuted / totalStatements * 100);
return (int) toCoverageRate(totalStatements, totalExecuted) * 100;
}

public static double toCoverageRate(final int totalStatements, final int totalExecuted) {
return (double) totalExecuted / totalStatements;
}

public static <T> int sum(final Iterable<T> objects, final Function<T, Integer> transformer) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
classStatsCobertura(fileStats) ::= <<
<class name="$fileStats.fileName$" filename="$fileStats.relativeName$" line-rate="$fileStats.coverageRate$" branch-rate="0.0" complexity="0">
<methods />
<lines>
$fileStats.executableLineCoverageRecords:lineCoverageDataCobertura(); separator="\n"$
</lines>
</class>
>>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lineCoverageDataCobertura(lineStats) ::= <<
<line number="$lineStats.lineNr$" hits="$lineStats.timesExecuted$" branch="false"/>
>>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
packageStatsCobertura(fileStats) ::= <<
<package name="$fileStats.relativeName$" line-rate="0.0" branch-rate="0.0" complexity="0">
<classes>
</classes>
</package>

>>
18 changes: 18 additions & 0 deletions saga-core/src/main/resources/stringTemplates/runStatsCobertura.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
runStatsCobertura(stats, name, version, url) ::= <<
<?xml version="1.0" ?>
<!DOCTYPE coverage SYSTEM 'http://cobertura.sourceforge.net/xml/coverage-04.dtd'>
<!-- Generated using $name$ ($url$) version $version$ -->
<coverage branch-rate="0.0" branches-covered="0" branches-valid="0" complexity="0" line-rate="$stats.totalCoverageRate$" lines-valid="$stats.totalStatements$" lines-covered="$stats.totalExecuted$" timestamp="1376875707" version="1.9">
<sources>
$stats.sourceDirs:sourceDirsCobertura(); separator="\n"$
</sources>
<packages>
<package name="default" line-rate="0.0" branch-rate="0.0" complexity="0">
<classes>
$stats.fileStatsWithSeparateFileOnly:classStatsCobertura(); separator="\n"$
</classes>
</package>
</packages>
</coverage>
>>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sourceDirsCobertura(sourceDir) ::= <<
<source>$sourceDir$</source>
>>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.timurstrekalov.saga.maven;

import java.io.File;
import java.util.List;
import java.util.Map;

import com.github.timurstrekalov.saga.core.CoverageGenerator;
Expand Down Expand Up @@ -130,9 +131,10 @@ public class SagaMojo extends AbstractMojo {
* <li>RAW</li>
* <li>CSV</li>
* <li>PDF</li>
* <li>COBERTURA</li>
* </ul>
*/
@Parameter(defaultValue = "HTML, RAW")
@Parameter(defaultValue = "HTML, RAW, COBERTURA")
private String reportFormats;

/**
Expand Down Expand Up @@ -174,6 +176,12 @@ public class SagaMojo extends AbstractMojo {
@Parameter
private Map<String, String> webDriverCapabilities;

/**
* Absolute path to the sources. This is used when generating COBERTURA style reports.
*/
@Parameter
private List<String> sourceDirs;

@Override
public void execute() throws MojoExecutionException {
if (skipTests) {
Expand Down Expand Up @@ -202,6 +210,7 @@ public void execute() throws MojoExecutionException {
config.setOrder(order);
config.setWebDriverCapabilities(webDriverCapabilities);
config.setWebDriverClassName(webDriverClassName);
config.setSourceDir(sourceDirs);

gen.instrumentAndGenerateReports();
} catch (final IllegalArgumentException e) {
Expand Down

0 comments on commit 75a15fc

Please sign in to comment.