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 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;
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("");
}
}