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