mockit.coverage.paths.PerFilePathCoverage 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.paths;
import java.io.*;
import java.util.*;
import javax.annotation.*;
import mockit.coverage.*;
import mockit.coverage.data.*;
public final class PerFilePathCoverage implements PerFileCoverage
{
private static final long serialVersionUID = 6075064821486644269L;
@Nonnull
public final Map firstLineToMethodData =
new LinkedHashMap();
// Computed on demand:
private transient int totalPaths;
private transient int coveredPaths;
public PerFilePathCoverage() { initializeCache(); }
private void initializeCache() { totalPaths = coveredPaths = -1; }
private void readObject(@Nonnull ObjectInputStream in) throws IOException, ClassNotFoundException
{
initializeCache();
in.defaultReadObject();
}
public void addMethod(@Nonnull MethodCoverageData methodData)
{
firstLineToMethodData.put(methodData.getFirstLineInBody(), methodData);
}
public int registerExecution(int firstLineInMethodBody, int node)
{
MethodCoverageData methodData = firstLineToMethodData.get(firstLineInMethodBody);
if (methodData != null) {
return methodData.markNodeAsReached(node);
}
return -1;
}
@Override
public int getTotalItems()
{
computeValuesIfNeeded();
return totalPaths;
}
@Override
public int getCoveredItems()
{
computeValuesIfNeeded();
return coveredPaths;
}
@Override
public int getCoveragePercentage()
{
computeValuesIfNeeded();
return CoveragePercentage.calculate(coveredPaths, totalPaths);
}
private void computeValuesIfNeeded()
{
if (totalPaths >= 0) return;
totalPaths = coveredPaths = 0;
for (MethodCoverageData method : firstLineToMethodData.values()) {
totalPaths += method.getTotalPaths();
coveredPaths += method.getCoveredPaths();
}
}
public void reset()
{
for (MethodCoverageData methodData : firstLineToMethodData.values()) {
methodData.reset();
}
initializeCache();
}
public void mergeInformation(@Nonnull PerFilePathCoverage previousCoverage)
{
Map previousInfo = previousCoverage.firstLineToMethodData;
for (Map.Entry firstLineAndInfo : firstLineToMethodData.entrySet()) {
Integer firstLine = firstLineAndInfo.getKey();
MethodCoverageData previousPathInfo = previousInfo.get(firstLine);
if (previousPathInfo != null) {
MethodCoverageData pathInfo = firstLineAndInfo.getValue();
pathInfo.addCountsFromPreviousTestRun(previousPathInfo);
}
}
for (Map.Entry firstLineAndInfo : previousInfo.entrySet()) {
Integer firstLine = firstLineAndInfo.getKey();
if (!firstLineToMethodData.containsKey(firstLine)) {
firstLineToMethodData.put(firstLine, firstLineAndInfo.getValue());
}
}
}
}