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

mockit.coverage.reporting.sourceFiles.FileCoverageReport Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 1.23
Show newest version
/*
 * Copyright (c) 2006-2013 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.coverage.reporting.sourceFiles;

import java.io.*;
import java.util.*;

import org.jetbrains.annotations.*;

import mockit.coverage.*;
import mockit.coverage.data.*;
import mockit.coverage.dataItems.*;
import mockit.coverage.paths.*;
import mockit.coverage.reporting.*;
import mockit.coverage.reporting.dataCoverage.*;
import mockit.coverage.reporting.lineCoverage.*;
import mockit.coverage.reporting.parsing.*;
import mockit.coverage.reporting.pathCoverage.*;

/**
 * Generates an HTML page containing line-by-line coverage information for a single source file.
 */
public final class FileCoverageReport
{
   @NotNull private final InputFile inputFile;
   @NotNull private final OutputFile output;
   @NotNull private final FileParser fileParser;
   @NotNull private final NeutralOutput neutralOutput;
   @NotNull private final LineCoverageOutput lineCoverage;
   @Nullable private final PathCoverageOutput pathCoverage;
   @Nullable private final DataCoverageOutput dataCoverage;

   public FileCoverageReport(
      @NotNull String outputDir, @NotNull InputFile inputFile, @NotNull FileCoverageData fileData,
      boolean withCallPoints)
      throws IOException
   {
      this.inputFile = inputFile;
      output = new OutputFile(outputDir, inputFile.filePath);
      fileParser = new FileParser();
      neutralOutput = new NeutralOutput(output);
      lineCoverage = new LineCoverageOutput(output, fileData.getLineCoverageData(), withCallPoints);
      pathCoverage = createPathCoverageOutput(fileData);
      dataCoverage = createDataCoverageOutput(fileData);
   }

   @Nullable private PathCoverageOutput createPathCoverageOutput(@NotNull FileCoverageData fileData)
   {
      if (Metrics.PathCoverage.isActive()) {
         Collection methods = fileData.getMethods();
         return methods.isEmpty() ? null : new PathCoverageOutput(output, methods);
      }

      return null;
   }

   @Nullable private DataCoverageOutput createDataCoverageOutput(@NotNull FileCoverageData fileData)
   {
      if (Metrics.DataCoverage.isActive()) {
         PerFileDataCoverage dataCoverageInfo = fileData.dataCoverageInfo;
         return dataCoverageInfo.hasFields() ? new DataCoverageOutput(dataCoverageInfo) : null;
      }

      return null;
   }

   public void generate() throws IOException
   {
      try {
         writeHeader();
         writeFormattedSourceLines();
         writeFooter();
      }
      finally {
         inputFile.close();
         output.close();
      }
   }

   private void writeHeader()
   {
      output.writeCommonHeader(inputFile.getSourceFileName());
      output.println("  ");
      output.println("    ");
   }

   private void writeFormattedSourceLines() throws IOException
   {
      LineParser lineParser = fileParser.lineParser;
      String line;

      while ((line = inputFile.nextLine()) != null) {
         boolean lineWithCodeElements = fileParser.parseCurrentLine(line);

         if (lineWithCodeElements) {
            if (dataCoverage != null) {
               dataCoverage.writeCoverageInfoIfLineStartsANewFieldDeclaration(fileParser);
            }

            if (pathCoverage != null) {
               pathCoverage.writePathCoverageInfoIfLineStartsANewMethodOrConstructor(lineParser.getNumber());
            }
         }

         if (!neutralOutput.writeLineWithoutCoverageInfo(lineParser)) {
            writeOpeningOfNewLine(lineParser.getNumber());

            if (!lineCoverage.writeLineWithCoverageInfo(lineParser)) {
               writeLineWithoutCoverageInfo(lineParser.getInitialElement());
            }

            output.println("    ");
         }
      }
   }

   private void writeOpeningOfNewLine(int lineNumber)
   {
      output.println("    ");
      output.write("      ");
   }

   private void writeLineWithoutCoverageInfo(@NotNull LineElement initialElement)
   {
      output.println("");
      output.write("      ");
   }

   private void writeFooter()
   {
      output.println("  
" + inputFile.getSourceFilePath() + "
"); output.print(lineNumber); output.write(" 
" : "prettyprint'>");
      output.write(initialElement.toString());
      output.println("
"); output.writeCommonFooter(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy