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.

The 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.packages;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import mockit.coverage.CoveragePercentage;
import mockit.coverage.data.FileCoverageData;
import mockit.coverage.reporting.OutputFile;
import mockit.coverage.testRedundancy.TestCoverage;

import org.checkerframework.checker.index.qual.NonNegative;

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;
    @NonNegative
    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());
    }

    @NonNegative
    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);
            writeLineWithCoverageTotal();
            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 static void removeRedundantSourceDirectories(@NonNull List dirs) {
        for (int i = 0; i < dirs.size(); i++) {
            i = removeRedundantSourceDirectory(dirs, i);
        }
    }

    private static int removeRedundantSourceDirectory(@NonNull List dirs, @NonNegative int dirIndex) {
        String dir1 = dirs.get(dirIndex).getPath();
        int j = dirIndex + 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(dirIndex);
                dirIndex--;
                break;
            } else {
                j++;
            }
        }

        return dirIndex;
    }

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

    private void writeHeaderCellWithMetricNameAndDescription() {
        output.println("      ");
    }

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

        int covered = coveredItems;
        int total = totalItems;
        int percentage = CoveragePercentage.calculate(covered, total);
        printCoveragePercentage(covered, total, percentage);

        output.println("    ");
    }

    @Override
    @SuppressWarnings("ParameterNameDiffersFromOverriddenParameter")
    protected void writeMetricsForFile(String unused, @NonNull String packageName) {
        writeRowStart();
        writeTableCellWithPackageName(packageName);
        writeInternalTableForSourceFiles(packageName);
        writeCoveragePercentageForPackage(packageName);
        writeRowClose();
    }

    private void writeTableCellWithPackageName(@NonNull String packageName) {
        printIndent();
        output.write("   filesInPackage = packageToFiles.get(packageName);

        if (filesInPackage.size() > 1) {
            output.write(" class='click' onclick='shFls(this)'");
        }

        output.write('>');
        output.write(packageName.replace('/', '.'));
        output.println("");
    }

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

    private void recordCoverageInformationForPackage(@NonNull String packageName) {
        int coveredInPackage = packageReport.coveredItems;
        int totalInPackage = packageReport.totalItems;
        int packagePercentage = CoveragePercentage.calculate(coveredInPackage, totalInPackage);

        totalItems += totalInPackage;
        coveredItems += coveredInPackage;
        packageToPackagePercentages.put(packageName, packagePercentage);
    }

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

    private void writeCoveragePercentageForPackage(@NonNull String packageName) {
        int filePercentage = packageToPackagePercentages.get(packageName);
        printCoveragePercentage(packageReport.coveredItems, packageReport.totalItems, filePercentage);
    }

    private void writeListOfRedundantTestsIfAny() {
        TestCoverage testCoverage = TestCoverage.INSTANCE;

        if (testCoverage == null) {
            return;
        }

        List redundantTests = testCoverage.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.size()); output.println("Files: "); output.print(totalFileCount); output.println("
Cvrg
Total 
"); printIndent(); output.println(" "); List fileNames = packageToFiles.get(packageName); packageReport.writeMetricsForEachFile(packageName, fileNames); recordCoverageInformationForPackage(packageName); printIndent(); output.println("
"); printIndent(); writeInitiallyHiddenSourceFileCount(fileNames.size()); printIndent(); output.println("