All Downloads are FREE. Search and download functionalities are using the official Maven repository.

mockit.coverage.paths.MethodCoverageData Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.49
Show newest version
/*
 * 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.paths.Node.*;

public final class MethodCoverageData implements Serializable
{
   private static final long serialVersionUID = -5073393714435522417L;

   @Nonnull private List nodes;
   private int firstLine;
   private int lastLine;

   // Helper fields used during node building and path execution:
   @Nonnull private final transient ThreadLocal> nodesReached;
   @Nonnull private final transient ThreadLocal previousNodeIndex;

   @Nonnull public List paths;
   @Nonnull private List nonShadowedPaths;

   public MethodCoverageData()
   {
      nodes = Collections.emptyList();
      paths = Collections.emptyList();
      nonShadowedPaths = Collections.emptyList();
      nodesReached = new ThreadLocal>();
      previousNodeIndex = new ThreadLocal();
      clearNodes();
   }

   public void buildPaths(int lastExecutableLine, @Nonnull NodeBuilder nodeBuilder)
   {
      firstLine = nodeBuilder.firstLine;
      lastLine = lastExecutableLine;

      nodes = nodeBuilder.nodes;
      paths = PathBuilder.buildPaths(nodes);
      buildListOfNonShadowedPaths();
   }

   private void buildListOfNonShadowedPaths()
   {
      nonShadowedPaths = new ArrayList(paths.size());

      for (Path path : paths) {
         if (!path.isShadowed()) {
            nonShadowedPaths.add(path);
         }
      }
   }

   public int getFirstLineInBody() { return firstLine; }
   public int getLastLineInBody() { return lastLine; }

   public int markNodeAsReached(int nodeIndex)
   {
      if (nodeIndex == 0) {
         clearNodes();
      }

      if (nodeIndex < nodes.size()) {
         Node node = nodes.get(nodeIndex);
         List currentNodesReached = nodesReached.get();

         if (!node.wasReached() && (nodeIndex == 0 || nodeIndex > previousNodeIndex.get())) {
            node.setReached(Boolean.TRUE);
            currentNodesReached.add(node);
            previousNodeIndex.set(nodeIndex);
         }

         if (node instanceof Exit) {
            Exit exitNode = (Exit) node;

            for (Path path : exitNode.paths) {
               int previousExecutionCount = path.countExecutionIfAllNodesWereReached(currentNodesReached);

               if (previousExecutionCount >= 0) {
                  return previousExecutionCount;
               }
            }
         }
      }

      return -1;
   }

   private void clearNodes()
   {
      for (Node node : nodes) {
         node.setReached(null);
      }

      nodesReached.set(new ArrayList());
      previousNodeIndex.set(0);
   }

   @Nonnull public List getPaths() { return nonShadowedPaths; }

   public int getExecutionCount()
   {
      int totalCount = 0;

      for (Path path : nonShadowedPaths) {
         totalCount += path.getExecutionCount();
      }

      return totalCount;
   }

   public int getTotalPaths() { return nonShadowedPaths.size(); }

   public int getCoveredPaths()
   {
      int coveredCount = 0;

      for (Path path : nonShadowedPaths) {
         if (path.getExecutionCount() > 0) {
            coveredCount++;
         }
      }

      return coveredCount;
   }

   public void addCountsFromPreviousTestRun(MethodCoverageData previousData)
   {
      for (int i = 0; i < paths.size(); i++) {
         Path path = paths.get(i);
         Path previousPath = previousData.paths.get(i);
         path.addCountFromPreviousTestRun(previousPath);
      }
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy