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 Show documentation
Show all versions of jmockit Show documentation
JMockit is a Java toolkit for automated developer testing.
It contains APIs for the creation of the objects to be tested, for mocking dependencies, and for faking external
APIs; JUnit (4 & 5) and TestNG test runners are supported.
It also contains an advanced code coverage tool.
/*
* Copyright (c) 2006 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 HashMap();
// 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)
{
int firstLineInBody = methodData.getFirstLineInBody();
firstLineToMethodData.put(firstLineInBody, 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 mergeInformation(@Nonnull PerFilePathCoverage previousCoverage)
{
Map previousInfo = previousCoverage.firstLineToMethodData;
addExecutionCountsFromPreviousTestRun(previousInfo);
addPathInfoFromPreviousTestRunForMethodsNotExecutedInCurrentTestRun(previousInfo);
}
private void addExecutionCountsFromPreviousTestRun(@Nonnull Map previousInfo)
{
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);
}
}
}
private void addPathInfoFromPreviousTestRunForMethodsNotExecutedInCurrentTestRun(
@Nonnull Map previousInfo)
{
for (Map.Entry firstLineAndInfo : previousInfo.entrySet()) {
Integer firstLine = firstLineAndInfo.getKey();
if (!firstLineToMethodData.containsKey(firstLine)) {
MethodCoverageData pathInfo = firstLineAndInfo.getValue();
firstLineToMethodData.put(firstLine, pathInfo);
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy