-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathJUnitXml.kt
More file actions
51 lines (40 loc) · 1.77 KB
/
JUnitXml.kt
File metadata and controls
51 lines (40 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package ftl.reports.xml
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES
import ftl.reports.xml.model.JUnitTestResult
import ftl.reports.xml.model.JUnitTestSuite
import java.io.File
import java.nio.file.Path
private val xmlModule = JacksonXmlModule().apply { setDefaultUseWrapper(false) }
private val xmlMapper = XmlMapper(xmlModule)
.registerModules(KotlinModule())
.configure(FAIL_ON_UNKNOWN_PROPERTIES, false)
internal val xmlPrettyWriter = xmlMapper.writerWithDefaultPrettyPrinter()
fun JUnitTestResult?.xmlToString(): String {
if (this == null) return ""
val prefix = "<?xml version='1.0' encoding='UTF-8' ?>\n"
return prefix + xmlPrettyWriter.writeValueAsString(this)
}
fun parseOneSuiteXml(path: Path): JUnitTestResult {
return parseOneSuiteXml(path.toFile())
}
fun parseOneSuiteXml(file: File): JUnitTestResult {
if (!file.exists()) throw RuntimeException("$file doesn't exist!")
return JUnitTestResult(mutableListOf(xmlMapper.readValue(file, JUnitTestSuite::class.java)))
}
fun parseOneSuiteXml(data: String): JUnitTestResult {
return JUnitTestResult(mutableListOf(xmlMapper.readValue(data, JUnitTestSuite::class.java)))
}
// --
fun parseAllSuitesXml(path: Path): JUnitTestResult {
return parseAllSuitesXml(path.toFile())
}
fun parseAllSuitesXml(file: File): JUnitTestResult {
if (!file.exists()) throw RuntimeException("$file doesn't exist!")
return xmlMapper.readValue(file, JUnitTestResult::class.java)
}
fun parseAllSuitesXml(data: String): JUnitTestResult {
return xmlMapper.readValue(data, JUnitTestResult::class.java)
}