mockit.coverage.reporting.packages.IndexPage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit-coverage Show documentation
Show all versions of jmockit-coverage Show documentation
JMockit Coverage is a code coverage tool with several metrics (line, path, data) capable of generating HTML
reports. It is designed with ease of use in mind, avoiding the need for complex configuration.
Instead, smart (but overridable) defaults are employed, such as the selection of which classes to consider for
coverage, and where to find sources files for report generation.
/*
* Copyright (c) 2006-2014 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.coverage.reporting.packages;
import java.io.*;
import java.util.*;
import org.jetbrains.annotations.*;
import mockit.coverage.*;
import mockit.coverage.data.*;
import mockit.coverage.reporting.OutputFile;
public final class IndexPage extends ListWithFilesAndPercentages
{
@Nullable private final List sourceDirs;
@NotNull private final Map> packageToFiles;
@NotNull private final Map packageToPackagePercentages;
@NotNull private final PackageCoverageReport packageReport;
private final int totalFileCount;
public IndexPage(
@NotNull File outputFile, @Nullable List sourceDirs, @Nullable Collection sourceFilesNotFound,
@NotNull Map> packageToFiles, @NotNull Map fileToFileData)
throws IOException
{
super(new OutputFile(outputFile), " ");
this.sourceDirs = sourceDirs;
this.packageToFiles = packageToFiles;
packageToPackagePercentages = new HashMap();
packageReport = new PackageCoverageReport(output, sourceFilesNotFound, fileToFileData, packageToFiles.values());
totalFileCount = totalNumberOfSourceFilesWithCoverageData(fileToFileData.values());
}
private static int totalNumberOfSourceFilesWithCoverageData(@NotNull Collection fileData)
{
return fileData.size() - Collections.frequency(fileData, null);
}
public void generate()
{
try {
writeHeader();
List packages = new ArrayList(packageToFiles.keySet());
writeMetricsForEachFile(null, packages);
writeLineWithCoverageTotals();
writeFooter();
}
finally {
output.close();
}
}
private void writeHeader()
{
((OutputFile) output).writeCommonHeader(null);
output.println(" JMockit Coverage Report
");
output.println(" ");
writeTableCaption();
writeTableFirstRowWithColumnTitles();
}
private void writeTableCaption()
{
if (sourceDirs == null) {
output.println(" All Packages and Files ");
}
else {
output.write(" All Packages and Files");
output.write(getCommaSeparatedListOfSourceDirs(sourceDirs.toString()));
output.println(" ");
}
}
@NotNull
private static String getCommaSeparatedListOfSourceDirs(@NotNull String concatenatedSourceDirs)
{
String prefixToRemove = ".." + File.separatorChar;
String commaSepDirs = concatenatedSourceDirs.replace(prefixToRemove, "");
return commaSepDirs.substring(1, commaSepDirs.length() - 1);
}
private void writeTableFirstRowWithColumnTitles()
{
output.println(" ");
output.write(" Packages: ");
output.print(packageToFiles.keySet().size());
output.println(" ");
output.write(
" Files: ");
output.print(totalFileCount);
output.println(" ");
Metrics.performAction(new Metrics.Action() {
int tableColumn = 1;
@Override
public void perform(@NotNull Metrics metric)
{
writeHeaderCellWithMetricNameAndDescription(metric);
tableColumn++;
}
private void writeHeaderCellWithMetricNameAndDescription(@NotNull Metrics metric)
{
output.write(" ");
output.write(metric.toString());
output.println(" ");
}
});
output.println(" ");
}
private void writeLineWithCoverageTotals()
{
output.println(" ");
output.println(" Total ");
Metrics.performAction(new Metrics.Action() {
@Override
public void perform(@NotNull Metrics metric) { writeLineWithCoverageTotals(metric); }
});
output.println(" ");
}
private void writeLineWithCoverageTotals(@NotNull Metrics metric)
{
int covered = coveredItems[metric.ordinal()];
int total = totalItems[metric.ordinal()];
int percentage = CoveragePercentage.calculate(covered, total);
printCoveragePercentage(metric, covered, total, percentage);
}
private void writeFooter()
{
output.println("
");
output.println(" ");
output.println(" ");
output.write(" Generated on ");
output.println(new Date());
output.println("
");
((OutputFile) output).writeCommonFooter();
}
@Override @SuppressWarnings("ParameterNameDiffersFromOverriddenParameter")
protected void writeMetricsForFile(String unused, @NotNull final String packageName)
{
writeRowStart();
writeTableCellWithPackageName(packageName);
writeInternalTableForSourceFiles(packageName);
Metrics.performAction(new Metrics.Action() {
@Override
public void perform(@NotNull Metrics metric) { writeCoveragePercentageForPackage(packageName, metric); }
});
writeRowClose();
}
private void writeTableCellWithPackageName(@NotNull String packageName)
{
printIndent();
output.write(" ");
output.write(packageName.replace('/', '.'));
output.println(" ");
}
private void writeInternalTableForSourceFiles(@NotNull final String packageName)
{
printIndent();
output.println(" ");
printIndent();
output.println(" ");
List fileNames = packageToFiles.get(packageName);
packageReport.writeMetricsForEachFile(packageName, fileNames);
Metrics.performAction(new Metrics.Action() {
@Override
public void perform(@NotNull Metrics metric) { recordCoverageInformationForPackage(packageName, metric); }
});
printIndent();
output.println("
");
printIndent();
writeInitiallyHiddenSourceFileCount(fileNames.size());
output.println(" ");
}
private void recordCoverageInformationForPackage(@NotNull String packageName, @NotNull Metrics metric)
{
int coveredInPackage = packageReport.coveredItems[metric.ordinal()];
int totalInPackage = packageReport.totalItems[metric.ordinal()];
int packagePercentage = CoveragePercentage.calculate(coveredInPackage, totalInPackage);
setPackageCoveragePercentage(packageName, metric, packagePercentage);
totalItems[metric.ordinal()] += totalInPackage;
coveredItems[metric.ordinal()] += coveredInPackage;
}
private void setPackageCoveragePercentage(@NotNull String packageName, @NotNull Metrics metric, int percentage)
{
int[] percentages = packageToPackagePercentages.get(packageName);
if (percentages == null) {
percentages = new int[Metrics.values().length];
packageToPackagePercentages.put(packageName, percentages);
}
percentages[metric.ordinal()] = percentage;
}
private void writeInitiallyHiddenSourceFileCount(int fileCount)
{
output.write(" (");
output.print(fileCount);
output.println(" source files)");
}
private void writeCoveragePercentageForPackage(@NotNull String packageName, @NotNull Metrics metric)
{
int coveredInPackage = packageReport.coveredItems[metric.ordinal()];
int totalInPackage = packageReport.totalItems[metric.ordinal()];
int filePercentage = packageToPackagePercentages.get(packageName)[metric.ordinal()];
printCoveragePercentage(metric, coveredInPackage, totalInPackage, filePercentage);
}
@Override
protected void writeClassAttributeForCoveragePercentageCell()
{
output.write("class='pt' ");
}
}