Skip to content

Commit 5495e8c

Browse files
committed
chore: review xml report
1 parent 77679c7 commit 5495e8c

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/main/java/org/fugerit/java/junit5/tag/check/facade/TagReportFacade.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import lombok.extern.slf4j.Slf4j;
5+
import org.fugerit.java.core.io.helper.HelperIOException;
6+
import org.fugerit.java.core.xml.dom.DOMIO;
57
import org.fugerit.java.doc.base.config.DocConfig;
68
import org.fugerit.java.junit5.tag.check.model.ExecutedTest;
79
import org.fugerit.java.junit5.tag.check.model.ReportModel;
810
import org.fugerit.java.junit5.tag.check.model.TestStats;
11+
import org.w3c.dom.Document;
12+
import org.w3c.dom.Element;
913

1014
import java.io.File;
1115
import java.io.FileWriter;
@@ -43,7 +47,7 @@ private void generateReport(Map<ExecutedTest, Set<String>> testTagMap)
4347
case DocConfig.TYPE_JSON:
4448
generateJsonReport(model);
4549
break;
46-
case "xml":
50+
case DocConfig.TYPE_XML:
4751
generateXmlReport(testTagMap);
4852
break;
4953
case "html":
@@ -288,26 +292,30 @@ private void generateJsonReport(ReportModel report)
288292

289293
private void generateXmlReport(Map<ExecutedTest, Set<String>> testTagMap)
290294
throws IOException {
291-
try (FileWriter writer = new FileWriter(outputFile)) {
292-
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
293-
writer.write("<executedTestTagReport>\n");
294-
295-
for (Map.Entry<ExecutedTest, Set<String>> entry : testTagMap.entrySet()) {
296-
ExecutedTest test = entry.getKey();
297-
writer.write(" <test class=\"" + escapeXml(test.getClassName()) + "\" ");
298-
writer.write("method=\"" + escapeXml(test.getMethodName()) + "\" ");
299-
writer.write("time=\"" + test.getTime() + "\" ");
300-
writer.write("skipped=\"" + test.isSkipped() + "\" ");
301-
writer.write("failed=\"" + test.isFailed() + "\" ");
302-
writer.write("error=\"" + test.isError() + "\">\n");
303-
for (String tag : entry.getValue()) {
304-
writer.write(" <tag>" + escapeXml(tag) + "</tag>\n");
295+
HelperIOException.apply( () -> {
296+
try (FileWriter writer = new FileWriter(outputFile)) {
297+
Document document = DOMIO.newSafeDocumentBuilderFactory().newDocumentBuilder().newDocument();
298+
Element root = document.createElement("executedTestTagReport");
299+
for (Map.Entry<ExecutedTest, Set<String>> entry : testTagMap.entrySet()) {
300+
ExecutedTest test = entry.getKey();
301+
Element current = document.createElement("test");
302+
current.setAttribute("class", test.getClassName());
303+
current.setAttribute("method", test.getMethodName());
304+
current.setAttribute("time", test.getTime().toString());
305+
current.setAttribute("skipped", String.valueOf( test.isSkipped() ) );
306+
current.setAttribute("failed", String.valueOf( test.isFailed() ) );
307+
current.setAttribute("error", String.valueOf( test.isError() ) );
308+
for (String tag : entry.getValue()) {
309+
Element tagElement = document.createElement( "tag" );
310+
tagElement.appendChild( document.createTextNode(tag) );
311+
current.appendChild(tagElement);
312+
}
313+
root.appendChild(current);
305314
}
306-
writer.write(" </test>\n");
315+
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
316+
DOMIO.writeDOMIndent( root, writer );
307317
}
308-
309-
writer.write("</executedTestTagReport>\n");
310-
}
318+
} );
311319
}
312320

313321
private String getStatusIcon(ExecutedTest test) {

src/test/java/org/fugerit/java/junit5/tag/check/ExecutedTestTagReporterMojoTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ void testExecuteGeneratesXmlReport() throws Exception {
136136
// Then: XML file should be created
137137
assertTrue(outputFile.exists());
138138
String content = new String(Files.readAllBytes(outputFile.toPath()));
139+
log.info( "xml content : {}", content );
139140
assertTrue(content.contains("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
140141
assertTrue(content.contains("<executedTestTagReport>"));
141142
}

0 commit comments

Comments
 (0)