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 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.
/*
* Copyright (c) 2006 Rogério Liesenfeld
* 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;
@Nonnull private Writer output;
XmlFile(@Nonnull String outputDir, @Nonnull CoverageData coverageData)
{
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)); }
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy