mockit.coverage.reporting.OutputFile 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.regex.*;
import javax.annotation.*;
public final class OutputFile extends PrintWriter
{
private static final Pattern PATH_SEPARATOR = Pattern.compile("/");
@Nonnull private final String relPathToOutDir;
private final boolean withPrettyPrint;
public OutputFile(@Nonnull File file) throws IOException
{
super(file, "UTF-8");
relPathToOutDir = "";
withPrettyPrint = false;
}
public OutputFile(@Nonnull String outputDir, @Nonnull String sourceFilePath) throws IOException
{
super(getOutputFileCreatingDirIfNeeded(outputDir, sourceFilePath));
relPathToOutDir = getRelativeSubPathToOutputDir(sourceFilePath);
withPrettyPrint = true;
}
@Nonnull
private static File getOutputFileCreatingDirIfNeeded(@Nonnull String outputDir, @Nonnull String sourceFilePath)
{
File outputFile = getOutputFile(outputDir, sourceFilePath);
File parentDir = outputFile.getParentFile();
if (!parentDir.exists()) {
boolean outputDirCreated = parentDir.mkdirs();
assert outputDirCreated : "Failed to create output dir: " + outputDir;
}
return outputFile;
}
@Nonnull
static File getOutputFile(@Nonnull String outputDir, @Nonnull String sourceFilePath)
{
int p = sourceFilePath.lastIndexOf('.');
String outputFileName = sourceFilePath.substring(0, p) + ".html";
File outputFile = new File(outputDir, outputFileName);
return outputFile;
}
@Nonnull
private static String getRelativeSubPathToOutputDir(@Nonnull String filePath)
{
StringBuilder cssRelPath = new StringBuilder();
int n = PATH_SEPARATOR.split(filePath).length;
for (int i = 1; i < n; i++) {
cssRelPath.append("../");
}
return cssRelPath.toString();
}
public void writeCommonHeader(@Nonnull String pageTitle)
{
println("");
println("");
println("");
println(" " + pageTitle + " ");
println(" ");
println(" ");
println(" ");
println(" ");
if (withPrettyPrint) {
println(" ");
}
println("");
println(withPrettyPrint ? "" : "");
}
public void writeCommonFooter()
{
println("");
println("");
}
}