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 Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for automated developer testing.
It contains mocking/faking APIs and a code coverage tool, supporting both JUnit and TestNG.
The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested
in isolation from selected dependencies.
/*
* Copyright (c) 2006 JMockit developers
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.coverage.reporting.packages;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.annotation.*;
import mockit.coverage.*;
import mockit.coverage.data.*;
import mockit.coverage.reporting.*;
import mockit.coverage.testRedundancy.*;
public final class IndexPage extends ListWithFilesAndPercentages
{
@Nullable private final List sourceDirs;
@Nonnull private final Map> packageToFiles;
@Nonnull private final Map packageToPackagePercentages;
@Nonnull private final PackageCoverageReport packageReport;
@Nonnegative private final int totalFileCount;
public IndexPage(
@Nonnull File outputFile, @Nullable List sourceDirs, @Nullable Collection sourceFilesNotFound,
@Nonnull Map> packageToFiles, @Nonnull 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());
}
@Nonnegative
private static int totalNumberOfSourceFilesWithCoverageData(@Nonnull Collection fileData) {
return fileData.size() - Collections.frequency(fileData, null);
}
public void generate() {
try {
writeHeader();
List packages = new ArrayList<>(packageToFiles.keySet());
writeMetricsForEachFile(null, packages);
writeLineWithCoverageTotal();
output.println(" ");
writeListOfRedundantTestsIfAny();
writeFooter();
}
finally {
output.close();
}
}
private void writeHeader() {
((OutputFile) output).writeCommonHeader("Code 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());
output.println(" ");
}
}
@Nonnull
private String getCommaSeparatedListOfSourceDirs() {
List dirs = sourceDirs;
assert dirs != null;
removeRedundantSourceDirectories(dirs);
String concatenatedSourceDirs = dirs.toString();
String prefixToRemove = ".." + File.separatorChar;
String commaSepDirs = concatenatedSourceDirs.replace(prefixToRemove, "");
return commaSepDirs.substring(1, commaSepDirs.length() - 1);
}
private static void removeRedundantSourceDirectories(@Nonnull List dirs) {
for (int i = 0; i < dirs.size(); i++) {
i = removeRedundantSourceDirectory(dirs, i);
}
}
private static int removeRedundantSourceDirectory(@Nonnull List dirs, @Nonnegative int i) {
String dir1 = dirs.get(i).getPath();
int j = i + 1;
while (j < dirs.size()) {
String dir2 = dirs.get(j).getPath();
if (dir1.startsWith(dir2)) {
dirs.remove(j);
}
else if (dir2.startsWith(dir1)) {
dirs.remove(i);
i--;
break;
}
else {
j++;
}
}
return i;
}
private void writeTableFirstRowWithColumnTitles() {
output.println(" ");
output.write(" Packages: ");
output.print(packageToFiles.keySet().size());
output.println(" ");
output.write(
" Files: ");
output.print(totalFileCount);
output.println(" ");
writeHeaderCellWithMetricNameAndDescription();
output.println(" ");
}
private void writeHeaderCellWithMetricNameAndDescription() {
output.write(
" Cvrg ");
}
private void writeLineWithCoverageTotal() {
output.println(" ");
output.println(" Total ");
int covered = coveredItems;
int total = totalItems;
int percentage = CoveragePercentage.calculate(covered, total);
printCoveragePercentage(covered, total, percentage);
output.println(" ");
}
@Override @SuppressWarnings("ParameterNameDiffersFromOverriddenParameter")
protected void writeMetricsForFile(String unused, @Nonnull String packageName) {
writeRowStart();
writeTableCellWithPackageName(packageName);
writeInternalTableForSourceFiles(packageName);
writeCoveragePercentageForPackage(packageName);
writeRowClose();
}
private void writeTableCellWithPackageName(@Nonnull String packageName) {
printIndent();
output.write(" ");
output.write(packageName.replace('/', '.'));
output.println(" ");
}
private void writeInternalTableForSourceFiles(@Nonnull String packageName) {
printIndent();
output.println(" ");
printIndent();
output.println(" ");
List fileNames = packageToFiles.get(packageName);
packageReport.writeMetricsForEachFile(packageName, fileNames);
recordCoverageInformationForPackage(packageName);
printIndent();
output.println("
");
printIndent();
writeInitiallyHiddenSourceFileCount(fileNames.size());
output.println(" ");
}
private void recordCoverageInformationForPackage(@Nonnull String packageName) {
int coveredInPackage = packageReport.coveredItems;
int totalInPackage = packageReport.totalItems;
int packagePercentage = CoveragePercentage.calculate(coveredInPackage, totalInPackage);
totalItems += totalInPackage;
coveredItems += coveredInPackage;
packageToPackagePercentages.put(packageName, packagePercentage);
}
private void writeInitiallyHiddenSourceFileCount(@Nonnegative int fileCount) {
output.write(" (");
output.print(fileCount);
output.println(" source files)");
}
private void writeCoveragePercentageForPackage(@Nonnull String packageName) {
int filePercentage = packageToPackagePercentages.get(packageName);
printCoveragePercentage(packageReport.coveredItems, packageReport.totalItems, filePercentage);
}
@Override
protected void writeClassAttributeForCoveragePercentageCell() {
output.write("class='pt' ");
}
private void writeListOfRedundantTestsIfAny() {
TestCoverage testCoverage = TestCoverage.INSTANCE;
if (testCoverage == null) {
return;
}
List redundantTests = testCoverage.getRedundantTests();
if (!redundantTests.isEmpty()) {
output.println("
Redundant tests:");
output.println(
" ");
for (Method testMethod : redundantTests) {
String testDescription = testMethod.getDeclaringClass().getSimpleName() + '.' + testMethod.getName();
output.append("");
output.write(" - ");
output.write(testDescription);
output.println("
");
}
output.println("
");
}
}
private void writeFooter() {
output.println(" ");
output.println(" ");
output.write(" Generated on ");
output.println(new Date());
output.println("
");
((OutputFile) output).writeCommonFooter();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy