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

mockit.coverage.reporting.OutputFile 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-2014 Rogério Liesenfeld
 * This file is subject to the terms of the MIT license (see LICENSE.txt).
 */
package mockit.coverage.reporting;

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

import org.jetbrains.annotations.*;

public final class OutputFile extends PrintWriter
{
   private static final Pattern PATH_SEPARATOR = Pattern.compile("/");

   @NotNull private final String relPathToOutDir;
   private final boolean withPrettyPrint;

   public OutputFile(@NotNull File file) throws IOException
   {
      super(new FileWriter(file));
      relPathToOutDir = "";
      withPrettyPrint = false;
   }

   public OutputFile(@NotNull String outputDir, @NotNull String sourceFilePath) throws IOException
   {
      super(new FileWriter(getOutputFileCreatingDirIfNeeded(outputDir, sourceFilePath)));
      relPathToOutDir = getRelativeSubPathToOutputDir(sourceFilePath);
      withPrettyPrint = true;
   }

   @NotNull
   private static File getOutputFileCreatingDirIfNeeded(@NotNull String outputDir, @NotNull String sourceFilePath)
   {
      File outputFile = getOutputFile(outputDir, sourceFilePath);
      File parentDir = outputFile.getParentFile();

      if (!parentDir.exists()) {
         boolean outputDirCreated = parentDir.mkdirs();
         assert outputDirCreated : "Failed to create output dir: " + outputDir;
      }

      return outputFile;
   }

   @NotNull
   static File getOutputFile(@NotNull String outputDir, @NotNull String sourceFilePath)
   {
      int p = sourceFilePath.lastIndexOf('.');
      String outputFileName = sourceFilePath.substring(0, p) + ".html";
      File outputFile = new File(outputDir, outputFileName);
      return outputFile;
   }

   @NotNull private static String getRelativeSubPathToOutputDir(@NotNull String filePath)
   {
      StringBuilder cssRelPath = new StringBuilder();
      int n = PATH_SEPARATOR.split(filePath).length;

      for (int i = 1; i < n; i++) {
         cssRelPath.append("../");
      }

      return cssRelPath.toString();
   }

   public void writeCommonHeader(@Nullable String subTitle)
   {
      String titleSuffix = subTitle == null ? "" : " - " + subTitle;

      println("");
      println("");
      println("");
      println("  JMockit Coverage Report" + titleSuffix + "");
      println("  ");
      println("  ");
      println("  ");
      println("  ");

      if (withPrettyPrint) {
         println("  ");
      }

      println("");
      println(withPrettyPrint ? "" : "");
   }

   public void writeCommonFooter()
   {
      println("");
      println("");
   }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy