mockit.coverage.reporting.pathCoverage.PathCoverageOutput 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.pathCoverage;
import java.io.*;
import java.util.*;
import javax.annotation.*;
import mockit.coverage.*;
import mockit.coverage.paths.*;
public final class PathCoverageOutput
{
@Nonnull private final PrintWriter output;
@Nonnull private final PathCoverageFormatter pathFormatter;
@Nonnull private final Iterator nextMethod;
// Helper fields:
@Nullable private MethodCoverageData currentMethod;
public PathCoverageOutput(@Nonnull PrintWriter output, @Nonnull Collection methods)
{
this.output = output;
pathFormatter = new PathCoverageFormatter(output);
nextMethod = methods.iterator();
moveToNextMethod();
}
private void moveToNextMethod()
{
currentMethod = nextMethod.hasNext() ? nextMethod.next() : null;
}
public void writePathCoverageInfoIfLineStartsANewMethodOrConstructor(int lineNumber)
{
if (currentMethod != null && lineNumber == currentMethod.getFirstLineInBody()) {
writePathCoverageInformationForMethod(currentMethod);
moveToNextMethod();
}
}
private void writePathCoverageInformationForMethod(@Nonnull MethodCoverageData methodData)
{
List paths = methodData.getPaths();
if (paths.size() > 1) {
writeHeaderForAllPaths(methodData);
pathFormatter.writeInformationForEachPath(paths);
writeFooterForAllPaths();
}
}
private void writeHeaderForAllPaths(@Nonnull MethodCoverageData methodData)
{
int coveredPaths = methodData.getCoveredPaths();
int totalPaths = methodData.getTotalPaths();
output.println(" ");
output.write(" ");
output.print(methodData.getExecutionCount());
output.println(" ");
output.println(" ");
output.write(" Path coverage: ");
output.print(coveredPaths);
output.print('/');
output.print(totalPaths);
output.println("");
}
private void writeFooterForAllPaths()
{
output.println(" ");
output.println(" ");
}
}