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 is a Java toolkit for automated developer testing. It contains mocking/faking APIs and a code coverage tool, supporting both JUnit and TestNG. The mocking APIs allow all kinds of Java code, without testability restrictions, to be tested in isolation from selected dependencies.

There is a newer version: 1.49
Show newest version
/*
 * Copyright (c) 2006 JMockit developers
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.coverage.reporting.sourceFiles;

import java.io.*;
import javax.annotation.*;

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

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

   public FileCoverageReport(
      @Nonnull String outputDir, @Nonnull InputFile inputFile, @Nonnull 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);
      dataCoverage = createDataCoverageOutput(fileData);
   }

   @Nullable
   private static DataCoverageOutput createDataCoverageOutput(@Nonnull FileCoverageData fileData) {
      PerFileDataCoverage dataCoverageInfo = fileData.dataCoverageInfo;
      return dataCoverageInfo.hasFields() ? new DataCoverageOutput(dataCoverageInfo) : 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 (!neutralOutput.writeLineWithoutCoverageInfo(lineParser)) {
            writeOpeningOfNewLine(lineParser.getNumber());

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

            output.println("    ");
         }
      }
   }

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

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

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy