From 2913ce6f3fbee7c6131ec180d72e9ab3d978ebc9 Mon Sep 17 00:00:00 2001 From: Maksim Zuev Date: Tue, 6 Feb 2024 10:24:59 +0100 Subject: [PATCH] Set title for an XML report https://github.com/Kotlin/kotlinx-kover/issues/527 --- .../rt/coverage/report/XMLCoverageReport.java | 17 +++++++++-------- reporter/resources/xml/xmlTest.xml | 2 +- .../intellij/rt/coverage/report/Reporter.java | 12 +++++++----- .../rt/coverage/report/api/ReportApi.java | 12 ++++++------ .../intellij/rt/coverage/report/HTMLTest.java | 2 +- .../intellij/rt/coverage/report/TestUtils.kt | 10 ++++++---- .../intellij/rt/coverage/report/XMLTest.java | 6 +++--- 7 files changed, 33 insertions(+), 28 deletions(-) diff --git a/java6-utils/src/com/intellij/rt/coverage/report/XMLCoverageReport.java b/java6-utils/src/com/intellij/rt/coverage/report/XMLCoverageReport.java index 8bf63065..7f04e92c 100644 --- a/java6-utils/src/com/intellij/rt/coverage/report/XMLCoverageReport.java +++ b/java6-utils/src/com/intellij/rt/coverage/report/XMLCoverageReport.java @@ -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 { @@ -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> packages = mapClassesToPackages(project, true); final Counter counter = new Counter(); @@ -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 classes) throws XMLStreamException { diff --git a/reporter/resources/xml/xmlTest.xml b/reporter/resources/xml/xmlTest.xml index c9d3342d..4026a881 100644 --- a/reporter/resources/xml/xmlTest.xml +++ b/reporter/resources/xml/xmlTest.xml @@ -1,5 +1,5 @@ - + diff --git a/reporter/src/com/intellij/rt/coverage/report/Reporter.java b/reporter/src/com/intellij/rt/coverage/report/Reporter.java index 42e109e9..ea754984 100644 --- a/reporter/src/com/intellij/rt/coverage/report/Reporter.java +++ b/reporter/src/com/intellij/rt/coverage/report/Reporter.java @@ -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 { @@ -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) { diff --git a/reporter/src/com/intellij/rt/coverage/report/api/ReportApi.java b/reporter/src/com/intellij/rt/coverage/report/api/ReportApi.java index 3a49675e..6e751bdf 100644 --- a/reporter/src/com/intellij/rt/coverage/report/api/ReportApi.java +++ b/reporter/src/com/intellij/rt/coverage/report/api/ReportApi.java @@ -32,8 +32,8 @@ private ReportApi() { // no-op } - public static void xmlReport(File xmlReportFile, List reports, List outputRoots, List sourceRoots, Filters filters) throws IOException { - Reporter reporter = createReporter(reports, outputRoots, sourceRoots, filters); + public static void xmlReport(File xmlReportFile, String title, List reports, List outputRoots, List sourceRoots, Filters filters) throws IOException { + Reporter reporter = createReporter(title, reports, outputRoots, sourceRoots, filters); reporter.createXMLReport(xmlReportFile); } @@ -46,11 +46,11 @@ public static void htmlReport( List 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 reports, List outputRoots, List sourceRoots, Filters filters) { + private static Reporter createReporter(String title, List reports, List outputRoots, List sourceRoots, Filters filters) { List binaryReports = new ArrayList(); for (File report : reports) { binaryReports.add(new BinaryReport(report, null)); @@ -59,7 +59,7 @@ private static Reporter createReporter(List reports, List 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) { diff --git a/reporter/test/com/intellij/rt/coverage/report/HTMLTest.java b/reporter/test/com/intellij/rt/coverage/report/HTMLTest.java index 1678803f..5c78e9bf 100644 --- a/reporter/test/com/intellij/rt/coverage/report/HTMLTest.java +++ b/reporter/test/com/intellij/rt/coverage/report/HTMLTest.java @@ -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; } diff --git a/reporter/test/com/intellij/rt/coverage/report/TestUtils.kt b/reporter/test/com/intellij/rt/coverage/report/TestUtils.kt index 082817dc..679ed8fa 100644 --- a/reporter/test/com/intellij/rt/coverage/report/TestUtils.kt +++ b/reporter/test/com/intellij/rt/coverage/report/TestUtils.kt @@ -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 diff --git a/reporter/test/com/intellij/rt/coverage/report/XMLTest.java b/reporter/test/com/intellij/rt/coverage/report/XMLTest.java index 8e6defa4..6e2162b3 100644 --- a/reporter/test/com/intellij/rt/coverage/report/XMLTest.java +++ b/reporter/test/com/intellij/rt/coverage/report/XMLTest.java @@ -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"); @@ -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"); } @@ -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"); }