Skip to content

Commit

Permalink
Set title for an XML report
Browse files Browse the repository at this point in the history
  • Loading branch information
zuevmaxim committed Feb 6, 2024
1 parent 0bbd4b2 commit 2913ce6
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,20 @@ private String getAttribute(String attributeName) {
return value;
}

public void write(FileOutputStream fOut, ProjectData project) throws IOException {
public void write(FileOutputStream fOut, ProjectData project, String title) throws IOException {
XMLOutputFactory factory = XMLOutputFactory.newInstance();
try {
myOut = factory.createXMLStreamWriter(new BufferedOutputStream(fOut));
myFiles.clear();

myOut.writeStartDocument();
newLine();
myOut.writeStartElement(REPORT_TAG);
String reportName = title != null ? title : IJ_REPORT_NAME;
myOut.writeAttribute(NAME_TAG, reportName);
newLine();
writeProject(project);
myOut.writeEndDocument();
} catch (XMLStreamException e) {
throw wrapIOException(e);
} finally {
Expand All @@ -272,12 +280,6 @@ private void newLine() throws XMLStreamException {
}

private void writeProject(ProjectData project) throws XMLStreamException {
myOut.writeStartDocument();
newLine();
myOut.writeStartElement(REPORT_TAG);
myOut.writeAttribute(NAME_TAG, IJ_REPORT_NAME);
newLine();

final HashMap<String, List<ClassData>> packages = mapClassesToPackages(project, true);

final Counter counter = new Counter();
Expand All @@ -290,7 +292,6 @@ private void writeProject(ProjectData project) throws XMLStreamException {
writeCounter(counter, INSTRUCTION_MASK | LINE_MASK | BRANCH_MASK | METHOD_MASK | CLASS_MASK);
myOut.writeEndElement();
newLine();
myOut.writeEndDocument();
}

private Counter writePackage(ProjectData project, String packageName, List<ClassData> classes) throws XMLStreamException {
Expand Down
2 changes: 1 addition & 1 deletion reporter/resources/xml/xmlTest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" ?>
<report name="Intellij Coverage Report">
<report name="TITLE">
<package name="MyClass">
<class name="MyClass" sourcefilename="F.java">
<method name="foo" desc="(I)V">
Expand Down
12 changes: 7 additions & 5 deletions reporter/src/com/intellij/rt/coverage/report/Reporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
*/
public class Reporter {
private final ReportLoadStrategy myLoad;
private final String myTitle;

public Reporter(ReportLoadStrategy loadStrategy) {
public Reporter(ReportLoadStrategy loadStrategy, String title) {
myLoad = loadStrategy;
myTitle = title;
}

public void createXMLReport(File xmlFile) throws IOException {
Expand All @@ -44,19 +46,19 @@ public void createXMLReport(File xmlFile) throws IOException {
try {
xmlFile.getParentFile().mkdirs();
out = new FileOutputStream(xmlFile);
report.write(out, myLoad.getProjectData());
report.write(out, myLoad.getProjectData(), myTitle);
} finally {
CoverageIOUtil.close(out);
}
}

public void createHTMLReport(File htmlDir, String title, String charset) throws IOException {
public void createHTMLReport(File htmlDir, String charset) throws IOException {
htmlDir.mkdirs();
final HTMLReportBuilder builder = ReportBuilderFactory.createHTMLReportBuilderForKover();
builder.setReportDir(htmlDir);
if (builder instanceof HTMLReportBuilderImpl) {
if (title != null) {
((HTMLReportBuilderImpl) builder).setReportTitle(title);
if (myTitle != null) {
((HTMLReportBuilderImpl) builder).setReportTitle(myTitle);
}

if (charset != null) {
Expand Down
12 changes: 6 additions & 6 deletions reporter/src/com/intellij/rt/coverage/report/api/ReportApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ private ReportApi() {
// no-op
}

public static void xmlReport(File xmlReportFile, List<File> reports, List<File> outputRoots, List<File> sourceRoots, Filters filters) throws IOException {
Reporter reporter = createReporter(reports, outputRoots, sourceRoots, filters);
public static void xmlReport(File xmlReportFile, String title, List<File> reports, List<File> outputRoots, List<File> sourceRoots, Filters filters) throws IOException {
Reporter reporter = createReporter(title, reports, outputRoots, sourceRoots, filters);
reporter.createXMLReport(xmlReportFile);
}

Expand All @@ -46,11 +46,11 @@ public static void htmlReport(
List<File> sourceRoots,
Filters filters
) throws IOException {
Reporter reporter = createReporter(reports, outputRoots, sourceRoots, filters);
reporter.createHTMLReport(htmlReportDir, title, charset);
Reporter reporter = createReporter(title, reports, outputRoots, sourceRoots, filters);
reporter.createHTMLReport(htmlReportDir, charset);
}

private static Reporter createReporter(List<File> reports, List<File> outputRoots, List<File> sourceRoots, Filters filters) {
private static Reporter createReporter(String title, List<File> reports, List<File> outputRoots, List<File> sourceRoots, Filters filters) {
List<BinaryReport> binaryReports = new ArrayList<BinaryReport>();
for (File report : reports) {
binaryReports.add(new BinaryReport(report, null));
Expand All @@ -59,7 +59,7 @@ private static Reporter createReporter(List<File> reports, List<File> outputRoot
ReportLoadStrategy loadStrategy =
new ReportLoadStrategy.RawReportLoadStrategy(binaryReports, outputRoots, sourceRoots, filters);

return new Reporter(loadStrategy);
return new Reporter(loadStrategy, title);
}

public static void setFreemarkerRetry(int repeatMaxCount, long repeatCooldownMs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private File runTestAndConvertToHTML(String patterns, String className) throws T
BinaryReport report = TestUtils.runTest(patterns, className);
File htmlDir = createHtmlDir(report.getDataFile());
TestUtils.clearLogFile(new File("."));
TestUtils.createRawReporter(report, patterns).createHTMLReport(htmlDir, DEFAULT_TITLE, DEFAULT_CHARSET);
TestUtils.createRawReporter(report, patterns, DEFAULT_TITLE).createHTMLReport(htmlDir, DEFAULT_CHARSET);
TestUtils.checkLogFile(new File("."));
return htmlDir;
}
Expand Down
10 changes: 6 additions & 4 deletions reporter/test/com/intellij/rt/coverage/report/TestUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,22 @@ object TestUtils {
return File(expectedPath)
}

@JvmOverloads
@JvmStatic
fun createRawReporter(report: BinaryReport?, patterns: String): Reporter {
fun createRawReporter(report: BinaryReport?, patterns: String, title: String? = null): Reporter {
val reports = if (report == null) emptyList() else listOf(report)
val filters = getFilters(patterns)
return Reporter(RawReportLoadStrategy(reports, outputRoots, sourceRoots, filters))
return Reporter(RawReportLoadStrategy(reports, outputRoots, sourceRoots, filters), title)
}

@JvmOverloads
@JvmStatic
fun createReporter(report: BinaryReport, patterns: String): Reporter {
fun createReporter(report: BinaryReport, patterns: String, title: String? = null): Reporter {
val smapFile = File(report.dataFile.absolutePath + ".sm")
val aggregatedReport = BinaryReport(report.dataFile, smapFile)
runAggregator(aggregatedReport, patterns)
val reports = listOf(aggregatedReport)
return Reporter(AggregatedReportLoadStrategy(reports, outputRoots, sourceRoots))
return Reporter(AggregatedReportLoadStrategy(reports, outputRoots, sourceRoots), title)
}

@JvmStatic
Expand Down
6 changes: 3 additions & 3 deletions reporter/test/com/intellij/rt/coverage/report/XMLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public void apiTest() throws Throwable {

TestUtils.clearLogFile(new File("."));
Filters filters = TestUtils.createFilters(Pattern.compile("testData.simple.*"));
ReportApi.xmlReport(xmlFile, singletonList(report.getDataFile()), singletonList(new File(TestUtils.JAVA_OUTPUT)), singletonList(new File("test")), filters);
ReportApi.xmlReport(xmlFile, null, singletonList(report.getDataFile()), singletonList(new File(TestUtils.JAVA_OUTPUT)), singletonList(new File("test")), filters);

TestUtils.checkLogFile(new File("."));
XMLTest.verifyXMLWithExpected(xmlFile, "xml/simple.xml");
Expand Down Expand Up @@ -137,7 +137,7 @@ public void basicTest() throws Throwable {

File file = createXMLFile();
TestUtils.clearLogFile(new File("."));
new XMLCoverageReport().write(new FileOutputStream(file), project);
new XMLCoverageReport().write(new FileOutputStream(file), project, "TITLE");
TestUtils.checkLogFile(new File("."));
verifyXMLWithExpected(file, "xml/xmlTest.xml");
}
Expand Down Expand Up @@ -172,7 +172,7 @@ public void sameFileNameTest() throws Throwable {

File file = createXMLFile();
TestUtils.clearLogFile(new File("."));
new XMLCoverageReport().write(new FileOutputStream(file), project);
new XMLCoverageReport().write(new FileOutputStream(file), project, null);
TestUtils.checkLogFile(new File("."));
verifyXMLWithExpected(file, "xml/sameSource.xml");
}
Expand Down

0 comments on commit 2913ce6

Please sign in to comment.