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

mockit.coverage.reporting.pathCoverage.PathCoverageOutput 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.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("    ");
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy