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

mockit.coverage.XmlFile 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;

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

import mockit.coverage.data.*;
import mockit.coverage.lines.*;

/**
 * Generates a XML file containing the coverage data gathered by the test run.
 * The XML schema used is the one defined by
 * the SonarQube project:
 * 
 * <coverage version="1">
 *    <file path="com/example/MyClass.java">
 *       <lineToCover lineNumber="5" covered="false"/>
 *       <lineToCover lineNumber="8" covered="true" branchesToCover="2" coveredBranches="1"/>
 *    </file>
 * </coverage>
 * 
*/ final class XmlFile { @Nonnull private final String srcDir; @Nonnull private final File outputFile; @Nonnull private final CoverageData coverageData; XmlFile(@Nonnull String outputDir, @Nonnull CoverageData coverageData) { //noinspection DynamicRegexReplaceableByCompiledPattern String firstSrcDir = Configuration.getProperty("srcDirs", "").split("\\s*,\\s*")[0]; srcDir = firstSrcDir.isEmpty() ? "" : firstSrcDir + '/'; String parentDir = Configuration.getOrChooseOutputDirectory(outputDir); outputFile = new File(parentDir, "coverage.xml"); this.coverageData = coverageData; } void generate() throws IOException { try (Writer out = new FileWriter(outputFile)) { out.write("\n"); out.write("\n"); for (Entry fileAndData : coverageData.getFileToFileData().entrySet()) { String sourceFileName = fileAndData.getKey(); writeOpeningXmlElementForSourceFile(out, sourceFileName); PerFileLineCoverage lineInfo = fileAndData.getValue().lineCoverageInfo; writeXmlElementsForExecutableLines(out, lineInfo); out.write("\t\n"); } out.write("\n"); } System.out.println("JMockit: Coverage data written to " + outputFile.getCanonicalPath()); } private void writeOpeningXmlElementForSourceFile(@Nonnull Writer out, @Nonnull String sourceFileName) throws IOException { out.write("\t\n"); } private static void writeXmlElementsForExecutableLines(@Nonnull Writer out, @Nonnull PerFileLineCoverage lineInfo) throws IOException { int lineCount = lineInfo.getLineCount(); for (int lineNum = 1; lineNum <= lineCount; lineNum++) { if (lineInfo.hasLineData(lineNum)) { LineCoverageData lineData = lineInfo.getLineData(lineNum); out.write("\t\t\n"); } } } private static void writeNumber(@Nonnull Writer out, @Nonnegative int value) throws IOException { out.write(Integer.toString(value)); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy