mockit.coverage.XmlFile Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmockit Show documentation
Show all versions of jmockit Show documentation
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.
/*
* 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;
private Writer output;
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 {
output = new FileWriter(outputFile);
try {
output.write("\n");
output.write("\n");
for (Entry fileAndData : coverageData.getFileToFileDataMap().entrySet()) {
String sourceFileName = fileAndData.getKey();
writeOpeningXmlElementForSourceFile(sourceFileName);
PerFileLineCoverage lineInfo = fileAndData.getValue().lineCoverageInfo;
writeXmlElementsForExecutableLines(lineInfo);
output.write("\t\n");
}
output.write(" \n");
}
finally {
output.close();
}
System.out.println("JMockit: Coverage data written to " + outputFile.getCanonicalPath());
}
private void writeOpeningXmlElementForSourceFile(@Nonnull String sourceFileName) throws IOException {
output.write("\t\n");
}
private void writeXmlElementsForExecutableLines(@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);
output.write("\t\t \n");
}
}
}
private void writeNumber(int value) throws IOException { output.write(Integer.toString(value)); }
}