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

mockit.coverage.reporting.packages.IndexPage 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.packages;

import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import javax.annotation.*;

import mockit.coverage.*;
import mockit.coverage.data.*;
import mockit.coverage.reporting.*;
import mockit.coverage.testRedundancy.*;

public final class IndexPage extends ListWithFilesAndPercentages
{
   @Nullable private final List sourceDirs;
   @Nonnull private final Map> packageToFiles;
   @Nonnull private final Map packageToPackagePercentages;
   @Nonnull private final PackageCoverageReport packageReport;
   private final int totalFileCount;

   public IndexPage(
      @Nonnull File outputFile, @Nullable List sourceDirs, @Nullable Collection sourceFilesNotFound,
      @Nonnull Map> packageToFiles, @Nonnull Map fileToFileData)
      throws IOException
   {
      super(new OutputFile(outputFile), "    ");
      this.sourceDirs = sourceDirs;
      this.packageToFiles = packageToFiles;
      packageToPackagePercentages = new HashMap();
      packageReport = new PackageCoverageReport(output, sourceFilesNotFound, fileToFileData, packageToFiles.values());
      totalFileCount = totalNumberOfSourceFilesWithCoverageData(fileToFileData.values());
   }

   private static int totalNumberOfSourceFilesWithCoverageData(@Nonnull Collection fileData)
   {
      return fileData.size() - Collections.frequency(fileData, null);
   }

   public void generate()
   {
      try {
         writeHeader();

         List packages = new ArrayList(packageToFiles.keySet());
         writeMetricsForEachFile(null, packages);
         writeLineWithCoverageTotals();
         output.println("  ");

         writeListOfRedundantTestsIfAny();
         writeFooter();
      }
      finally {
         output.close();
      }
   }

   private void writeHeader()
   {
      ((OutputFile) output).writeCommonHeader("Code Coverage Report");

      output.println("  ");

      writeTableCaption();
      writeTableFirstRowWithColumnTitles();
   }

   private void writeTableCaption()
   {
      if (sourceDirs == null) {
         output.println("    ");
      }
      else {
         output.write("    ");
      }
   }

   @Nonnull
   private String getCommaSeparatedListOfSourceDirs()
   {
      List dirs = sourceDirs;
      assert dirs != null;
      removeRedundantSourceDirectories(dirs);

      String concatenatedSourceDirs = dirs.toString();
      String prefixToRemove = ".." + File.separatorChar;
      String commaSepDirs = concatenatedSourceDirs.replace(prefixToRemove, "");
      return commaSepDirs.substring(1, commaSepDirs.length() - 1);
   }

   private void removeRedundantSourceDirectories(@Nonnull List dirs)
   {
      for (int i = 0; i < dirs.size(); i++) {
         String dir1 = dirs.get(i).getPath();
         int j = i + 1;

         while (j < dirs.size()) {
            String dir2 = dirs.get(j).getPath();

            if (dir1.startsWith(dir2)) {
               dirs.remove(j);
            }
            else if (dir2.startsWith(dir1)) {
               dirs.remove(i);
               i--;
               break;
            }
            else {
               j++;
            }
         }
      }
   }

   private void writeTableFirstRowWithColumnTitles()
   {
      output.println("    ");
      output.write("      ");
      output.write(
         "      ");

      Metrics.performAction(new Metrics.Action() {
         int tableColumn = 1;

         @Override
         public void perform(@Nonnull Metrics metric)
         {
            writeHeaderCellWithMetricNameAndDescription(metric);
            tableColumn++;
         }

         private void writeHeaderCellWithMetricNameAndDescription(@Nonnull Metrics metric)
         {
            output.write("      ");
         }
      });

      output.println("    ");
   }

   private void writeLineWithCoverageTotals()
   {
      output.println("    ");
      output.println("      ");

      Metrics.performAction(new Metrics.Action() {
         @Override
         public void perform(@Nonnull Metrics metric) { writeLineWithCoverageTotals(metric); }
      });

      output.println("    ");
   }

   private void writeLineWithCoverageTotals(@Nonnull Metrics metric)
   {
      int covered = coveredItems[metric.ordinal()];
      int total = totalItems[metric.ordinal()];
      int percentage = CoveragePercentage.calculate(covered, total);

      printCoveragePercentage(metric, covered, total, percentage);
   }

   @Override @SuppressWarnings("ParameterNameDiffersFromOverriddenParameter")
   protected void writeMetricsForFile(String unused, @Nonnull final String packageName)
   {
      writeRowStart();
      writeTableCellWithPackageName(packageName);
      writeInternalTableForSourceFiles(packageName);

      Metrics.performAction(new Metrics.Action() {
         @Override
         public void perform(@Nonnull Metrics metric) { writeCoveragePercentageForPackage(packageName, metric); }
      });

      writeRowClose();
   }

   private void writeTableCellWithPackageName(@Nonnull String packageName)
   {
      printIndent();
      output.write("  ");
   }

   private void writeInternalTableForSourceFiles(@Nonnull final String packageName)
   {
      printIndent();
      output.println("  ");
   }

   private void recordCoverageInformationForPackage(@Nonnull String packageName, @Nonnull Metrics metric)
   {
      int coveredInPackage = packageReport.coveredItems[metric.ordinal()];
      int totalInPackage = packageReport.totalItems[metric.ordinal()];
      int packagePercentage = CoveragePercentage.calculate(coveredInPackage, totalInPackage);

      setPackageCoveragePercentage(packageName, metric, packagePercentage);

      totalItems[metric.ordinal()] += totalInPackage;
      coveredItems[metric.ordinal()] += coveredInPackage;
   }

   private void setPackageCoveragePercentage(@Nonnull String packageName, @Nonnull Metrics metric, int percentage)
   {
      int[] percentages = packageToPackagePercentages.get(packageName);

      if (percentages == null) {
         percentages = new int[Metrics.values().length];
         packageToPackagePercentages.put(packageName, percentages);
      }

      percentages[metric.ordinal()] = percentage;
   }

   private void writeInitiallyHiddenSourceFileCount(int fileCount)
   {
      output.write("    (");
      output.print(fileCount);
      output.println(" source files)");
   }

   private void writeCoveragePercentageForPackage(@Nonnull String packageName, @Nonnull Metrics metric)
   {
      int coveredInPackage = packageReport.coveredItems[metric.ordinal()];
      int totalInPackage = packageReport.totalItems[metric.ordinal()];
      int filePercentage = packageToPackagePercentages.get(packageName)[metric.ordinal()];

      printCoveragePercentage(metric, coveredInPackage, totalInPackage, filePercentage);
   }

   @Override
   protected void writeClassAttributeForCoveragePercentageCell()
   {
      output.write("class='pt' ");
   }

   private void writeListOfRedundantTestsIfAny()
   {
      List redundantTests = TestCoverage.INSTANCE.getRedundantTests();

      if (!redundantTests.isEmpty()) {
         output.println("  
Redundant tests:"); output.println( "
    "); for (Method testMethod : redundantTests) { String testDescription = testMethod.getDeclaringClass().getSimpleName() + '.' + testMethod.getName(); output.append(""); output.write("
  1. "); output.write(testDescription); output.println("
  2. "); } output.println("
"); } } private void writeFooter() { output.println("

"); output.println(" "); output.write(" Generated on "); output.println(new Date()); output.println("

"); ((OutputFile) output).writeCommonFooter(); } }









All Packages and FilesAll Packages and Files
"); output.write(getCommaSeparatedListOfSourceDirs()); output.println("
Packages: "); output.print(packageToFiles.keySet().size()); output.println("Files: "); output.print(totalFileCount); output.println(""); output.write(metric.toString()); output.println("
Total 
"); output.write(packageName.replace('/', '.')); output.println(""); printIndent(); output.println(" "); List fileNames = packageToFiles.get(packageName); packageReport.writeMetricsForEachFile(packageName, fileNames); Metrics.performAction(new Metrics.Action() { @Override public void perform(@Nonnull Metrics metric) { recordCoverageInformationForPackage(packageName, metric); } }); printIndent(); output.println("
"); printIndent(); writeInitiallyHiddenSourceFileCount(fileNames.size()); output.println("