mockit.coverage.reporting.CoverageReport 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-2015 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.coverage.reporting;
import java.io.*;
import java.util.*;
import java.util.Map.*;
import javax.annotation.*;
import mockit.coverage.data.*;
import mockit.coverage.reporting.packages.*;
import mockit.coverage.reporting.sourceFiles.*;
class CoverageReport
{
@Nonnull private final String outputDir;
private boolean outputDirCreated;
@Nullable private final List sourceDirs;
@Nonnull private final Map fileToFileData;
@Nonnull private final Map> packageToFiles;
private final boolean withCallPoints;
@Nullable private final Collection sourceFilesNotFound;
protected CoverageReport(
@Nonnull String outputDir, boolean outputDirCreated, @Nullable String[] srcDirs,
@Nonnull CoverageData coverageData, boolean withCallPoints)
{
this.outputDir = getOrChooseOutputDirectory(outputDir);
this.outputDirCreated = outputDirCreated;
sourceDirs = srcDirs == null ? null : new SourceFiles().buildListOfSourceDirectories(srcDirs);
fileToFileData = coverageData.getFileToFileDataMap();
packageToFiles = new HashMap>();
this.withCallPoints = withCallPoints;
sourceFilesNotFound = srcDirs == null ? null : new ArrayList();
}
@Nonnull
private static String getOrChooseOutputDirectory(@Nonnull String outputDir)
{
if (!outputDir.isEmpty()) {
return outputDir;
}
String mavenBaseDir = System.getProperty("basedir");
return mavenBaseDir == null ? "coverage-report" : "target/coverage-report";
}
public final void generate() throws IOException
{
createReportOutputDirIfNotExists();
File outputFile = createOutputFileForIndexPage();
if (outputFile == null) {
return;
}
boolean withSourceFilePages = sourceDirs != null;
if (withSourceFilePages && sourceDirs.size() > 1) {
System.out.println("JMockit: Coverage source dirs: " + sourceDirs);
}
generateFileCoverageReportsWhileBuildingPackageLists();
new IndexPage(outputFile, sourceDirs, sourceFilesNotFound, packageToFiles, fileToFileData).generate();
new StaticFiles(outputDir).copyToOutputDir(withSourceFilePages);
System.out.println("JMockit: Coverage report written to " + outputFile.getParentFile().getCanonicalPath());
}
private void createReportOutputDirIfNotExists()
{
if (!outputDirCreated) {
File outDir = new File(outputDir);
outputDirCreated = outDir.mkdirs();
}
}
@Nullable
private File createOutputFileForIndexPage() throws IOException
{
File outputFile = new File(outputDir, "index.html");
if (outputFile.exists() && !outputFile.canWrite()) {
System.out.println("JMockit: " + outputFile.getCanonicalPath() + " is read-only; report generation canceled");
return null;
}
return outputFile;
}
private void generateFileCoverageReportsWhileBuildingPackageLists() throws IOException
{
Set> files = fileToFileData.entrySet();
for (Entry fileAndFileData : files) {
generateFileCoverageReport(fileAndFileData.getKey(), fileAndFileData.getValue());
}
}
private void generateFileCoverageReport(@Nonnull String sourceFile, @Nonnull FileCoverageData fileData)
throws IOException
{
if (sourceDirs == null) {
addFileToPackageFileList(sourceFile);
}
else {
InputFile inputFile = InputFile.createIfFileExists(sourceDirs, sourceFile);
if (inputFile != null) {
new FileCoverageReport(outputDir, inputFile, fileData, withCallPoints).generate();
}
else {
deleteOutdatedHTMLFileIfExists(sourceFile);
if (sourceFilesNotFound != null) {
sourceFilesNotFound.add(sourceFile);
}
}
addFileToPackageFileList(sourceFile);
}
}
private void addFileToPackageFileList(@Nonnull String file)
{
int p = file.lastIndexOf('/');
String filePackage = p < 0 ? "" : file.substring(0, p);
List filesInPackage = packageToFiles.get(filePackage);
if (filesInPackage == null) {
filesInPackage = new ArrayList();
packageToFiles.put(filePackage, filesInPackage);
}
filesInPackage.add(file.substring(p + 1));
}
private void deleteOutdatedHTMLFileIfExists(@Nonnull String filePath)
{
if (!outputDirCreated) {
File outputFile = OutputFile.getOutputFile(outputDir, filePath);
//noinspection ResultOfMethodCallIgnored
outputFile.delete();
}
}
}